Skip to content

Commit 30569ee

Browse files
committed
添加一个相对快速的方法找到对应的头
1 parent f4e490c commit 30569ee

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/Commands/QueryCommits.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
6262
if (!rs.IsSuccess)
6363
return _commits;
6464

65+
var commitIdx = 0;
6566
var nextPartIdx = 0;
6667
var start = 0;
6768
var end = rs.StdOut.IndexOf('\n', start);
@@ -71,7 +72,7 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
7172
switch (nextPartIdx)
7273
{
7374
case 0:
74-
_current = new Models.Commit() { SHA = line };
75+
_current = new Models.Commit() { SHA = line, Index = commitIdx++ };
7576
_current.IsCommitFilterHead = _patterns.Count > 0 && _patterns.Any(f => line.StartsWith(f));
7677
_commits.Add(_current);
7778
break;

src/Models/Commit.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public static double OpacityForNotMerged
5555
public bool IsCommitterVisible => !Author.Equals(Committer) || AuthorTime != CommitterTime;
5656
public bool IsCurrentHead => Decorators.Find(x => x.Type is DecoratorType.CurrentBranchHead or DecoratorType.CurrentCommitHead) != null;
5757

58+
/// <summary>
59+
/// the index of commit in current history's commits
60+
/// </summary>
61+
public int Index { get; set; } = 0;
62+
5863
public int Color { get; set; } = 0;
5964
public double Opacity => IsMerged ? 1 : OpacityForNotMerged;
6065
public FontWeight FontWeight => IsCurrentHead ? FontWeight.Bold : FontWeight.Regular;

src/ViewModels/Histories.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,47 @@ private void NavigateTo(Models.Commit commit)
446446
}
447447
}
448448

449+
public List<Models.Commit> GetChildrensInCurHistory(Models.Commit commit)
450+
{
451+
if (commit == null || commit.Index == 0) return new List<Models.Commit>();
452+
return _commits[..(commit.Index)].Where(x => x.Parents.Contains(commit.SHA)).ToList();
453+
}
454+
455+
public List<Models.Commit> GetTopHeadsInCurHistory(Models.Commit commit)
456+
{
457+
// if (commit == null || commit.Index <= 0) return new List<Models.Commit>();
458+
var heads = new List<Models.Commit>();
459+
if (commit == null || commit?.Index <= 0)
460+
return heads;
461+
var queue = new Queue<Models.Commit>();
462+
var visited = new HashSet<string> { commit.SHA };
463+
464+
queue.Enqueue(commit);
465+
466+
while (queue.Any())
467+
{
468+
var currentCommit = queue.Dequeue();
469+
var children = GetChildrensInCurHistory(currentCommit);
470+
// var children = _commits.Where(x => x.Parents.Contains(currentCommit.SHA)).ToList();
471+
if (children.Any())
472+
{
473+
heads.Remove(currentCommit);
474+
475+
foreach (var child in children)
476+
{
477+
// 如果这个子提交还没有被访问过,则加入队列并标记为已访问
478+
if (visited.Add(child.SHA))
479+
{
480+
queue.Enqueue(child);
481+
heads.Add(child);
482+
}
483+
}
484+
}
485+
}
486+
487+
return heads;
488+
}
489+
449490
private Repository _repo = null;
450491
private bool _isLoading = true;
451492
private List<Models.Commit> _commits = new List<Models.Commit>();

0 commit comments

Comments
 (0)