RewriteHistory extremely slow
Reproduction steps
Find a repo with 3000+ commits Run the below code against that repo to rewrite the repo history:
private static void RewriteHistory(string repoPath, ICollection<string> subFolders)
{
using (var repo = new Repository(repoPath))
{
var refs = repo.Refs.Where(r => r.CanonicalName.EndsWith("develop")).OrderBy(r => r.CanonicalName).ToList();
var commits = repo.Commits.QueryBy(new CommitFilter { IncludeReachableFrom = refs }).ToArray();
var count = 0;
var stopwatch = Stopwatch.StartNew();
Logger.InfoFormat("Found {0} commits", commits.Length);
Logger.InfoFormat("Rewrite history started");
repo.Refs.RewriteHistory(new RewriteHistoryOptions
{
OnError = OnError,
OnSucceeding = OnSucceeding,
PruneEmptyCommits = true,
CommitTreeRewriter = commit =>
{
Logger.InfoFormat("Commit {0}/{1}", ++count, commits.Length);
var tree = TreeDefinition.From(commit);
foreach (var treeEntry in commit.Tree)
{
var match = subFolders.FirstOrDefault(s => treeEntry.Path.StartsWithIgnoreCase(s));
if (match == null)
tree.Remove(treeEntry.Path);
}
return tree;
}
}, commits);
Logger.InfoFormat("Rewrite history ended. Took {0}", stopwatch.Elapsed);
repo.Reset(ResetMode.Hard, repo.Head.Commits.First());
}
}
Expected behavior
This should take a reasonable time. I expect it to take a few minutes for sure but not hours. For example when running something similar in git from the command like it takes minutes. I'm sorry I don't have specific times but I can try to provide more data to compare performance if necessary.
Actual behavior
This takes 1h 43 minutes of which 1h 40 to just start the rewrite and about a couple of minutes to go through all the commits. INFO : Found 3486 commits INFO : Rewrite history started [between these two log messages there is a 1h 40m gap] INFO : Commit 1/3486 INFO : Commit 2/3486 ... INFO : Rewrite history ended. Took 01:43:51.5536779
Version of LibGit2Sharp (release number or SHA1)
0.26.0 + 2.0.267 (native)
Operating system(s) tested; .NET runtime tested
.net 4.6.1 - Win 2012R2
Have you tried disabling logging? I experience the same slowness with logging enabled.
@ViktorHofer : Can you elaborate? I don't see log in my output, so I'm not sure if it's relevant.
The branch I'm working on has 140K commits. I only submit 43. it takes forever.
Perhaps there is a lock somewhere that is blocking the process?
Version of LibGit2Sharp (release number or SHA1) 0.26.1 + gd638aa1da1.libgit2-7ce88e6
Operating system(s) tested; .NET runtime tested .net standard 2.0 - Under .net core 3.0 - Windows 10
My problem with this line at the beginning of the rewrite:
var refsToRewrite = repo.Refs.ReachableFrom(targetedCommits).ToList();
Which was taking forever to complete. I think it is because my repo has thousands of tags and it was taking forever to compute the refs. Changed that line to just go through all the refs, I had no problems since.
Personnaly, I used a different method to identify the commits.
The line in my code that is taking forever is:
repo.Refs.RewriteHistory(new RewriteHistoryOptions
{
OnError = OnError,
OnSucceeding = OnSucceeding,
CommitHeaderRewriter = c => CommitRewriteInfo.From(c, UpdateMessage(c.Message, mappings)),
}, commitsToRewrite);
As it turns out, I have a logger before and after, and inside the UpdateMessage function.
here is an excerpt of my log:
2019-10-22 17:56:19.471 -04:00 [INF] Rewriting 83 commits. This might take a while...
2019-10-22 18:37:28.974 -04:00 [INF] Starting UpdateMessage 1/83
...
2019-10-22 18:37:29.948 -04:00 [INF] End UpdateMessage 83/83
2019-10-22 18:37:29.995 -04:00 [INF] Done rewriting 83 commits
(...)
2019-10-22 18:37:46.536 -04:00 [INF] Rewriting 9 commits. This might take a while...
... (Skipped Update Message, all in same second of Done rewriting
2019-10-22 19:07:13.940 -04:00 [INF] Done rewriting 9 commits.
(...)
2019-10-22 19:07:48.068 -04:00 [INF] Rewriting 43 commits. This might take a while...
... (Skipped Update Message, all in same second of Done rewriting
2019-10-22 21:29:20.617 -04:00 [INF] Done 43 commits.
In summary, I updated, in the course of the evening, 3 batches:
- 83 commits, in ~45 minutes
- 9 commits, in ~30 minutes
- 43 commits, in less than 2 hours and 30 minutes!!!
I ran it again today, for 39 commits in 15 minutes (not in the log).
As you can see, there is no direct correlation between the number of commits and the time it process.
I tried to debug the component, but as I can't seem to be able to build it, I stopped trying.
@jspetit seems like you have the same identical problem I had. How many tags/branches do you have in your repo? From your logs the actual rewrite takes milliseconds. The problem is in that line that I highlighted were the more commits you have the higher the complexity to pass that line (and so the higher the time) which is not linear. What I ended up doing is copy the HistoryRewriter class, renamed it HistoryRewriter2, and pass to the Execute method the list of refs. It is also an easy way to debug the issue as you can step debug the "Execute" method.
@gigi81 I have a lot of branches... It may be the cause, but since Git bash / Tortoise can perform way faster, I think it could be improved.
I have finished rewriting my commits, but I will keep your idea for debugging as I only changed a small subsets of our commits and I may be asked to change more later...