Skip to content

Commit dfe52cc

Browse files
committed
Add log-git-merge-base
1 parent d821437 commit dfe52cc

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

libgit2patchedfiles/examples/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ extern int lg2_index_pack(git_repository *repo, int argc, char **argv);
7171
extern int lg2_init(git_repository *repo, int argc, char **argv);
7272
extern int lg2_log(git_repository *repo, int argc, char **argv);
7373
extern int lg2_log_last_commit_of_branch(git_repository *repo, int argc, char **argv);
74+
extern int lg2_log_git_merge_base(git_repository *repo, int argc, char **argv);
7475
extern int lg2_ls_files(git_repository *repo, int argc, char **argv);
7576
extern int lg2_ls_remote(git_repository *repo, int argc, char **argv);
7677
extern int lg2_merge(git_repository *repo, int argc, char **argv);

libgit2patchedfiles/examples/lg2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct {
2727
{ "init", lg2_init, 0 },
2828
{ "log", lg2_log, 1 },
2929
{ "log-last-commit-of-branch", lg2_log_last_commit_of_branch, 1 },
30+
{ "log-git-merge-base", lg2_log_git_merge_base, 1 },
3031
{ "ls-files", lg2_ls_files, 1 },
3132
{ "ls-remote", lg2_ls_remote, 1 },
3233
{ "merge", lg2_merge, 1 },
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* libgit2 "log" example - shows how to walk history and get commit info
3+
*
4+
* Written by the libgit2 contributors
5+
*
6+
* To the extent possible under law, the author(s) have dedicated all copyright
7+
* and related and neighboring rights to this software to the public domain
8+
* worldwide. This software is distributed without any warranty.
9+
*
10+
* You should have received a copy of the CC0 Public Domain Dedication along
11+
* with this software. If not, see
12+
* <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
*/
14+
15+
#include "common.h"
16+
17+
int lg2_log_git_merge_base(git_repository *repo, int argc, char *argv[])
18+
{
19+
git_oid merge_base_oid;
20+
char* commit_id_1 = NULL;
21+
char* commit_id_2 = NULL;
22+
git_commit *commit_1 = NULL;
23+
git_commit *commit_2 = NULL;
24+
git_oid oid_1;
25+
git_oid oid_2;
26+
int error;
27+
28+
if (argc != 3) {
29+
fprintf(stderr, "You have to specify exactly two commit ids. Usage: git log-git-merge-base <commitId1> <commitId2>\n");
30+
goto cleanup;
31+
} else {
32+
commit_id_1 = argv[1];
33+
commit_id_2 = argv[2];
34+
}
35+
36+
// Lookup commit_id_1 commit by hash
37+
error = git_oid_fromstr(&oid_1, commit_id_1);
38+
if (error < 0) {
39+
fprintf(stderr, "Failed to convert commitId1 commit hash.\n");
40+
goto cleanup;
41+
}
42+
43+
error = git_commit_lookup(&commit_1, repo, &oid_1);
44+
if (error < 0) {
45+
fprintf(stderr, "Failed to lookup commitId1 commit.\n");
46+
goto cleanup;
47+
}
48+
49+
// Lookup commit_id_2 commit by hash
50+
error = git_oid_fromstr(&oid_2, commit_id_2);
51+
if (error < 0) {
52+
fprintf(stderr, "Failed to convert commitId2 commit hash.\n");
53+
goto cleanup;
54+
}
55+
56+
error = git_commit_lookup(&commit_2, repo, &oid_2);
57+
if (error < 0) {
58+
fprintf(stderr, "Failed to lookup commitId2 commit.\n");
59+
goto cleanup;
60+
}
61+
62+
// Find the merge base between commitId1 and commitId2
63+
error = git_merge_base(&merge_base_oid, repo, &oid_1, &oid_2);
64+
if (error < 0) {
65+
fprintf(stderr, "Failed to find merge base.\n");
66+
goto cleanup;
67+
}
68+
69+
// Convert the merge base oid to a hex string and print it
70+
char merge_base_str[40 + 1];
71+
git_oid_tostr(merge_base_str, sizeof(merge_base_str), &merge_base_oid);
72+
printf("Merge base commit hash: %s\n", merge_base_str);
73+
74+
cleanup:
75+
git_commit_free(commit_1);
76+
git_commit_free(commit_2);
77+
78+
return 0;
79+
}

0 commit comments

Comments
 (0)