Skip to content

Commit 93061d4

Browse files
committed
feat: 添加获取当前历史中子提交路径的方法,并反转子提交顺序
1 parent 605b253 commit 93061d4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/ViewModels/Histories.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ private void NavigateTo(Models.Commit commit)
482482
continue;
483483
var children = GetChildrensInCurHistory(currentCommit, (uint)(currentCommit.Index - toplimitIndex));
484484
// var children = _commits.Where(x => x.Parents.Contains(currentCommit.SHA)).ToList();
485+
children.Reverse();
485486
if (children.Any())
486487
{
487488
heads.Remove(currentCommit);
@@ -501,6 +502,49 @@ private void NavigateTo(Models.Commit commit)
501502
return heads;
502503
}
503504

505+
public List<Models.Commit> GetChildrensPathInCurHistory(Models.Commit commit, out List<int> pathindex, uint depth = 0)
506+
{
507+
pathindex = new List<int>();
508+
var childrensPath = new List<Models.Commit>();
509+
if (commit == null || commit?.Index <= 0)
510+
return childrensPath;
511+
512+
var queue = new Queue<Models.Commit>();
513+
var visited = new HashSet<int>();
514+
515+
queue.Enqueue(commit);
516+
517+
var toplimitIndex = commit.Index - depth;
518+
if (toplimitIndex < 0)
519+
toplimitIndex = 0;
520+
521+
while (queue.Any())
522+
{
523+
var currentCommit = queue.Dequeue();
524+
if (depth != 0 && currentCommit.Index - toplimitIndex < 1) // at least one
525+
continue;
526+
var children = GetChildrensInCurHistory(currentCommit, (uint)(currentCommit.Index - toplimitIndex));
527+
children.Reverse();
528+
// var children = _commits.Where(x => x.Parents.Contains(currentCommit.SHA)).ToList();
529+
if (children.Any())
530+
{
531+
// childrensPath.Remove(currentCommit);
532+
533+
foreach (var child in children)
534+
{
535+
// 如果这个子提交还没有被访问过,则加入队列并标记为已访问
536+
if (visited.Add(child.Index))
537+
{
538+
queue.Enqueue(child);
539+
childrensPath.Add(child);
540+
}
541+
}
542+
}
543+
}
544+
pathindex = visited.ToList();
545+
return childrensPath;
546+
}
547+
504548
private Repository _repo = null;
505549
private bool _isLoading = true;
506550
private List<Models.Commit> _commits = new List<Models.Commit>();

0 commit comments

Comments
 (0)