libgit2sharp get all commits since the last push

2019-07-31 23:56发布

I would like to view all of the commits since the last time the user pushed from their machine.

    using (var repo = new Repository(repositoryDirectory))
{
    var c = repo.Lookup<Commit>(shaHashOfCommit);

    // Let's only consider the refs that lead to this commit...
    var refs = repo.Refs.ReachableFrom(new []{c});

   //...and create a filter that will retrieve all the commits...
    var cf = new CommitFilter
    {
        Since = refs,       // ...reachable from all those refs...
        Until = c           // ...until this commit is met
    };

    var cs = repo.Commits.QueryBy(cf);

    foreach (var co in cs)
    {
        Console.WriteLine("{0}: {1}", co.Id.ToString(7), co.MessageShort);
    }       
}

I got this code from another post, but I am not sure how to modify it to get the commits since the date of the last push.

1条回答
太酷不给撩
2楼-- · 2019-08-01 00:32

You want the commits that are reachable from c, excluding the ones that are reachable from the remote commit.

If you're talking about master, in a typical setup, the tracking branch for this will be remotes/origin/master. refs/remotes/origin/master will be updated when you push to the remote master branch.

So your CommitFilter should look like:

new CommitFilter { Since = repo.Refs["refs/remotes/origin/master"], Until = c }

Which is equivalent to git log refs/remotes/origin/master..c.

查看更多
登录 后发表回答