问题
Is there any way to list commits that have been fetched, but not merged using LibGit2Sharp?
For example I can run the following git command:
C:\Users\Tom\SourceLog>git log origin
commit f3beb4960b2f4bf5641d5b512b5b8c3081512a56
Author: Tom Hunter
Date: Wed Jan 9 22:58:51 2013 +0000
Rollback change to icon
Seemed to crash on windows xp for some reason..
commit d95f29a6cbfea9cb6009c3095a67d71f86d9e8bd
Author: Tom Hunter
Date: Mon Jan 7 21:34:36 2013 +0000
Updating Icon
commit 8785ed4ff3c46bef46ea6e2398c115b4b0203b2c
Author: Tom Hunter
Date: Mon Jan 7 21:22:54 2013 +0000
Fixing tests
Moved GenerateFlowDocuments method to LogEntry class.
Have somehow managed to greatly simplify tests..
...
The top two commits have not yet been merged into my local repository. I.e. this is what I get if I leave out origin:
C:\Users\Tom\SourceLog>git log
commit 8785ed4ff3c46bef46ea6e2398c115b4b0203b2c
Author: Tom Hunter
Date: Mon Jan 7 21:22:54 2013 +0000
Fixing tests
Moved GenerateFlowDocuments method to LogEntry class.
Have somehow managed to greatly simplify tests..
...
The following code gives me only 8785ed. How can I get a list including the yet-to-be merged commits?
using (var repo = new Repository(@"C:\Users\Tom\SourceLog"))
{
var c = repo.Commits.First();
}
回答1:
You can get the commits of your remote origin branch with:
repo.Branches["origin/HEAD"].Commits
And you can use First()
the get the latest commit by the default sorting. However you get all the commits of the remote branch not just the not merged one.
If you want to have a collection of the not merged commits you should use QueryBy(filter)
to get the commits. nulltoken's asnwer described the solution but it contains an error. The correct usage is:
var filter = new Filter
{
Since = repo.Branches["origin/HEAD"],
Until = repo.Head
};
var notMergedCommits = repo.Commits.QueryBy(filter);
Because:
- Since: A pointer to a commit object or a list of pointers to consider as starting points.
- Until: A pointer to a commit object or a list of pointers which will be excluded (along with ancestors) from the enumeration.
So with the above code you want to have the commits from the branch "origin/HEAD"
and exclude the ones which are included in your "HEAD"
which means return all the commits which are in the fetched branch and not yet merged to the "HEAD"
.
来源:https://stackoverflow.com/questions/14295805/libgit2sharp-log-remote