Get commit count of a sub folder in a repo

721 Views Asked by At

Im using NGit with C# to get data from our repo

I'm trying to figure out how to get the commit count of a sub folder in a repo from the current branch.

Something like (but with subfolder support)

git rev-list --count HEAD

edit: This works but it feels like there must be a better and faster way. for a large repo this would take time to complete

var git = Git.Open(@"repoPath");
var allCommits = git.Log().Call().OrderBy(c => c.CommitTime);

var commit = git.Log().AddPath("SubPath").Call().OrderByDescending(c => c.CommitTime).First();

var index = allCommits
    .Select((c, i) => new {Commit = c, Index = i})
    .First(c => c.Commit.Id.Name == commit.Id.Name)
    .Index;
1

There are 1 best solutions below

3
On

I don't really know NGit, but couldn't you just count commits directly instead of allCommits?

var commits = git.Log().AddPath("SubPath").Call().OrderByDescending(c => c.CommitTime);
var index = commits
.Select((c, i) => new {Commit = c, Index = i})
.Index;