libgit2sharp icon indicating copy to clipboard operation
libgit2sharp copied to clipboard

Fetching history for a file VERY slow using libgit2sharp

Open SuRFDescartes opened this issue 5 years ago • 4 comments

I am attempting to use libgit2sharp to get the list of commits for a specific file in a repository. The code works, but it is VERY slow. How can I get git command line like performance from libgit2sharp?

Reproduction steps

Run the following code:

using System; using System.Collections.Generic; using System.Diagnostics; using LibGit2Sharp;

namespace GitHistoryTime { class Program { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Format is GitHistoryTime "); } else { string repoName = args[0]; string fileName = args[1]; Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew(); Repository repo = new Repository(repoName); if (repo == null) { Console.WriteLine("Could not find git repository for " + repoName); } else { CommitFilter filter = new CommitFilter { SortBy = CommitSortStrategies.Topological }; IEnumerable<LogEntry> log = repo.Commits.QueryBy(fileName, filter); if (log != null) { foreach (LogEntry entry in log) { Console.WriteLine(entry.Commit.Sha); } } Console.WriteLine("Time to get the log: {0}", stopwatch.Elapsed.ToString()); } } } } }

Expected behavior

I would expect to get times similar to doing git --no-pager log --fileName. On my system, git took 0.43 seconds to return the full history

Actual behavior

On my machine, the above code took 6 minutes and 45 seconds! That is roughly 800 times slower than the git command line.

Due to the comments in https://github.com/libgit2/libgit2/issues/4428, I first tried to use a sort strategy of None, but LibGit2Sharp threw an exception that None was not valid. Why?

Version of LibGit2Sharp (release number or SHA1)

7fc4be5193dbdd08538b4b150332b5a73770e0f6

Operating system(s) tested; .NET runtime tested

Windows 10 2004 .NET 4.6.1

SuRFDescartes avatar Oct 30 '20 12:10 SuRFDescartes

I have exactly the same problem:

string filePathFromBaseRepository = "prod/yomvi/android.cell/config";
DateTime afterDate = new DateTime(2021,10,10, 0,0,0);
DateTime beforeDate = DateTime.Now;
var logEntryQueryable = this.repository.Commits.QueryBy(filePathFromBaseRepository, new CommitFilter() { SortBy = CommitSortStrategies.Topological });
incrementalLogEntry = logEntryQueryable.Where(x => x.Commit.Committer.When >= afterDate && x.Commit.Committer.When < beforeDate).ToList();

The Where takes forever. Overpasses 2 minutes sometimes. It is true that it may be hundreds of commits to check but doing the same thing on shell it is some seconds at the most.

andoniripollpro avatar Oct 13 '21 14:10 andoniripollpro

May be here: https://github.com/libgit2/libgit2sharp/issues/1705 Someone said: "I met same issue and I have to wrap a git.exe and use git log to speed up the log reading."

andoniripollpro avatar Oct 13 '21 14:10 andoniripollpro

Same issue here. We get times around 30 seconds when looking for file history. We're considering calling git.exe and parsing its output. If anyone has already achieved that, we'd appreciate if you shared.

blackboxlogic avatar Oct 13 '21 15:10 blackboxlogic

Is there anything new on this. I don't find the workaround of running git.exe and parsing the output very helpful.

renemoc avatar Feb 14 '22 15:02 renemoc