Skip to content

Commit

Permalink
Merge pull request #512 from dotnet/nbgvGetCommitsPerf
Browse files Browse the repository at this point in the history
Improve `nbgv get-commits <version>` perf
  • Loading branch information
pr-autocomplete[bot] committed Sep 18, 2020
2 parents 2d57861 + 8870d21 commit fc28ab1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 32 deletions.
44 changes: 14 additions & 30 deletions src/NerdBank.GitVersioning/GitExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public static IEnumerable<Commit> GetCommitsFromVersion(this Repository repo, Ve
Requires.NotNull(version, nameof(version));

var tracker = new GitWalkTracker(repoRelativeProjectDirectory);
var possibleCommits = from commit in GetCommitsReachableFromRefs(repo).Distinct()
var possibleCommits = from commit in GetCommitsReachableFromRefs(repo)
let commitVersionOptions = tracker.GetVersion(commit)
where commitVersionOptions != null
where !IsCommitIdMismatch(version, commitVersionOptions, commit)
Expand Down Expand Up @@ -880,45 +880,29 @@ private static IEnumerable<Commit> GetCommitsReachableFromRefs(Repository repo)
{
Requires.NotNull(repo, nameof(repo));

var commits = new HashSet<Commit>();
var visitedCommitIds = new HashSet<ObjectId>();
var breadthFirstQueue = new Queue<Commit>();

// Start the discovery with HEAD, and all commits that have refs pointing to them.
breadthFirstQueue.Enqueue(repo.Head.Tip);
foreach (var reference in repo.Refs)
{
var commit = reference.ResolveToDirectReference()?.Target as Commit;
if (commit != null)
if (commit is object)
{
AddReachableCommitsFrom(commit, commits);
breadthFirstQueue.Enqueue(commit);
}
}

return commits;
}

/// <summary>
/// Adds a commit and all its ancestors to a set.
/// </summary>
/// <param name="startingCommit">The starting commit to add.</param>
/// <param name="set">
/// The set into which the <paramref name="startingCommit"/>
/// and all its ancestors are to be added.
/// </param>
private static void AddReachableCommitsFrom(Commit startingCommit, HashSet<Commit> set)
{
Requires.NotNull(startingCommit, nameof(startingCommit));
Requires.NotNull(set, nameof(set));

var stack = new Stack<Commit>();
stack.Push(startingCommit);
while (stack.Count > 0)
while (breadthFirstQueue.Count > 0)
{
var currentCommit = stack.Pop();
if (set.Add(currentCommit))
Commit head = breadthFirstQueue.Dequeue();
if (visitedCommitIds.Add(head.Id))
{
foreach (var parent in currentCommit.Parents)
yield return head;
foreach (Commit parent in head.Parents)
{
if (!set.Contains(parent))
{
stack.Push(parent);
}
breadthFirstQueue.Enqueue(parent);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/nbgv/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ private static ExitCodes OnGetCommitsCommand(string projectPath, string version,
}

string repoRelativeProjectDir = GetRepoRelativePath(searchPath, repository);
var candidateCommits = GitExtensions.GetCommitsFromVersion(repository, parsedVersion, repoRelativeProjectDir).ToList();
var candidateCommits = GitExtensions.GetCommitsFromVersion(repository, parsedVersion, repoRelativeProjectDir);
PrintCommits(quiet, searchPath, repository, candidateCommits);

return ExitCodes.OK;
Expand Down Expand Up @@ -772,7 +772,7 @@ private static string ShouldHaveTrailingDirectorySeparator(string path)
return path + Path.DirectorySeparatorChar;
}

private static void PrintCommits(bool quiet, string projectDirectory, LibGit2Sharp.Repository repository, List<LibGit2Sharp.Commit> candidateCommits, bool includeOptions = false)
private static void PrintCommits(bool quiet, string projectDirectory, LibGit2Sharp.Repository repository, IEnumerable<LibGit2Sharp.Commit> candidateCommits, bool includeOptions = false)
{
int index = 1;
foreach (var commit in candidateCommits)
Expand Down

0 comments on commit fc28ab1

Please sign in to comment.