2017-04-18 47 views
1

我想這樣工作就像「git describe」在終端工作。在libgit2中執行git描述

如何獲取我的回購的當前標籤?現在我的計劃是打印

09B8A518

每次我嘗試,這個數字是不同的,所以我不認爲這是提交ID左右。

當我執行「git的描述」在終端,輸出爲「v0.1.2」

有沒有辦法做到這一點?

順便說一句,我怎樣才能轉換「git_describe_result *出」;「字符串?

string path = C://my/local/repo; 
    string result; 
    git_libgit2_init(); 

    const char * REPO_PATH = path.c_str(); 
    git_repository * repo = nullptr; 
    git_repository_open(&repo, REPO_PATH); 

    git_describe_result *out; 
    git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT; 
    opts.version = GIT_DESCRIBE_FORMAT_OPTIONS_VERSION; // GIT_DESCRIBE_OPTIONS_VERSION; 

    git_describe_workdir(&out, repo, &opts); 
    cout << out << endl; 

    git_describe_result_free(out); 
    git_repository_free(repo); 
    git_libgit2_shutdown(); 
+0

要打印的指針這對我的作品。這裏真正的問題是如何將'git_describe_result'格式化爲一個字符串。 –

回答

0

git_describe_result轉換爲git_describe_format的字符串。

0

現在

  string path = C://my/local/repo; 
      string result; 
      git_libgit2_init(); 

      const char * REPO_PATH = path.c_str(); 
      git_repository * repo = nullptr; 
      git_repository_open(&repo, REPO_PATH); 
      git_describe_result *out; 
      git_describe_options opts = GIT_DESCRIBE_OPTIONS_INIT; 
// --------------------------------------- 
      // I added this 
      opts.describe_strategy = GIT_DESCRIBE_ALL; 
      opts.max_candidates_tags = 0; 
      opts.only_follow_first_parent = 1; 
// --------------------------------------- 
      git_describe_workdir(&out, repo, &opts); 

    // -------------------------------------- 

    // and also this 
       git_buf out1 = { 0 }; 
       const git_describe_format_options opts1=GIT_DESCRIBE_FORMAT_OPTIONS_INIT; 
       git_describe_format(&out1, out, &opts1); 
       result = out1.ptr; 
       cout << result << endl; 

    // --------------------------------------- 
      git_describe_result_free(out); 
      git_repository_free(repo); 
      git_libgit2_shutdown();