Search code examples
cgitlibgit2objective-git

libgit2: Finding all commits between two tags?


I was wondering how to find all commits between two tags in Git using the C-API? I know this can easily be done with the CLI tool:

git log [first commit]..[second commit ] --pretty=oneline

However, I can't seem to figure out how to do it with the C API. The issue I'm stuck with is when there ends up being a cycle in the graph.

Here is some Objective-C code I was using with Objective-Git, however since most people don't know Objective-C, I'm happy for an answer in the C API or some other Git API. I'd imagine that I'll have to keep a Dictionary of previously traversed commits or something?

- (void)traverseCommits: (GTCommit *)start withGoal: (GTCommit *)goal withHistory: (NSMutableDictionary *)history{
    NSLog(@"%@", start.shortSHA);
    if ([start.SHA isEqualToString:goal.SHA]) {
        return;
    }
    for (GTCommit *c in start.parents) {
        //[history setObject:c forKey:c.SHA];
        if(![c.SHA isEqualToString:goal.SHA])
            [self traverseCommits:c withGoal:goal withHistory:history];

    }
}

Solution

  • The revwalk functions are designed for this kind of use case. Push the newer tag with git_revwalk_push_ref and hide the older tag with git_revwalk_hide_ref. Then walk over the range with git_revwalk_next.