|
| 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 | +/** utility functions that parse options and help with log output */ |
| 18 | +static void print_time(const git_time *intime, const char *prefix); |
| 19 | +static void print_commit_info(git_commit *commit); |
| 20 | + |
| 21 | +int lg2_log_git_merge_base_commits(git_repository *repo, int argc, char *argv[]) |
| 22 | +{ |
| 23 | + git_oid merge_base_oid; |
| 24 | + char* commit_id_1 = NULL; |
| 25 | + char* commit_id_2 = NULL; |
| 26 | + git_commit *commit_1 = NULL; |
| 27 | + git_commit *commit_2 = NULL; |
| 28 | + git_oid oid_1; |
| 29 | + git_oid oid_2; |
| 30 | + int error; |
| 31 | + |
| 32 | + if (argc != 3) { |
| 33 | + fprintf(stderr, "You have to specify exactly two commit ids. Usage: git log-git-merge-base <commitId1> <commitId2>\n"); |
| 34 | + goto cleanup; |
| 35 | + } else { |
| 36 | + commit_id_1 = argv[1]; |
| 37 | + commit_id_2 = argv[2]; |
| 38 | + } |
| 39 | + |
| 40 | + // Lookup commit_id_1 commit by hash |
| 41 | + error = git_oid_fromstr(&oid_1, commit_id_1); |
| 42 | + if (error < 0) { |
| 43 | + fprintf(stderr, "Failed to convert commitId1 commit hash.\n"); |
| 44 | + goto cleanup; |
| 45 | + } |
| 46 | + |
| 47 | + error = git_commit_lookup(&commit_1, repo, &oid_1); |
| 48 | + if (error < 0) { |
| 49 | + fprintf(stderr, "Failed to lookup commitId1 commit.\n"); |
| 50 | + goto cleanup; |
| 51 | + } |
| 52 | + |
| 53 | + // Lookup commit_id_2 commit by hash |
| 54 | + error = git_oid_fromstr(&oid_2, commit_id_2); |
| 55 | + if (error < 0) { |
| 56 | + fprintf(stderr, "Failed to convert commitId2 commit hash.\n"); |
| 57 | + goto cleanup; |
| 58 | + } |
| 59 | + |
| 60 | + error = git_commit_lookup(&commit_2, repo, &oid_2); |
| 61 | + if (error < 0) { |
| 62 | + fprintf(stderr, "Failed to lookup commitId2 commit.\n"); |
| 63 | + goto cleanup; |
| 64 | + } |
| 65 | + |
| 66 | + // Find the merge base between commitId1 and commitId2 |
| 67 | + error = git_merge_base(&merge_base_oid, repo, &oid_1, &oid_2); |
| 68 | + if (error < 0) { |
| 69 | + fprintf(stderr, "Failed to find merge base.\n"); |
| 70 | + goto cleanup; |
| 71 | + } |
| 72 | + |
| 73 | + // Setup revwalker |
| 74 | + git_revwalk *walker; |
| 75 | + git_revwalk_new(&walker, repo); |
| 76 | + git_revwalk_sorting(walker, GIT_SORT_TIME | GIT_SORT_REVERSE); |
| 77 | + git_revwalk_push(walker, &oid_2); |
| 78 | + git_revwalk_hide(walker, &merge_base_oid); |
| 79 | + |
| 80 | + // Log commit list |
| 81 | + git_oid oid; |
| 82 | + git_commit *commit; |
| 83 | + |
| 84 | + while (!git_revwalk_next(&oid, walker)) { |
| 85 | + git_commit_lookup(&commit, repo, &oid); |
| 86 | + print_commit_info(commit); |
| 87 | + git_commit_free(commit); |
| 88 | + } |
| 89 | + |
| 90 | +cleanup: |
| 91 | + git_commit_free(commit_1); |
| 92 | + git_commit_free(commit_2); |
| 93 | + if (walker) git_revwalk_free(walker); |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
0 commit comments