public static IEnumerable <Commit> CommitsRandomOrder(string vcsPath) { var commitsAlreadySeen = new HashSet <ObjectHash>(); var commits = ReadCommitsFromRefs(vcsPath); while (commits.TryPop(out var commit)) { if (!commitsAlreadySeen.Add(commit.Hash)) { continue; } yield return(commit); foreach (var parent in commit.Parents.Where(parent => !commitsAlreadySeen.Contains(parent))) { commits.Push(GitObjectFactory.ReadCommit(vcsPath, parent)); } } }
public static IEnumerable <Commit> CommitsInOrder(string vcsPath) { var commits = ReadCommitsFromRefs(vcsPath); var parentsSeen = new HashSet <ObjectHash>(); var commitsProcessed = new HashSet <ObjectHash>(); while (commits.TryPop(out var commit)) { if (commitsProcessed.Contains(commit.Hash)) { parentsSeen.Remove(commit.Hash); } else { if (!parentsSeen.Add(commit.Hash) || !commit.HasParents) { commitsProcessed.Add(commit.Hash); yield return(commit); } else { commits.Push(commit); foreach (var parent in commit.Parents) { if (!commitsProcessed.Contains(parent)) { var parentCommit = GitObjectFactory.ReadCommit(vcsPath, parent); if (parentCommit == null) { throw new Exception("Commit not found: " + parent); } commits.Push(parentCommit); } } } } } }