简体   繁体   中英

performing git describe in libgit2

I want to work this like "git describe" is working in terminal.

How can I get current tag of my repo? Now my program is printing

09B8A518

Everytime I try that, this number is different so I dont think it is commit ID or so.

When I perform "git describe" in terminal, output is "v0.1.2"

Is there a way to do that?

By the way, how can I convert "git_describe_result *out;" to string?

    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();

使用git_describe_formatgit_describe_result转换为字符串。

Now this works for me

            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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM