Skip to content

Commit 605b253

Browse files
committed
添加对节点深度的搜索的支持
1 parent 30569ee commit 605b253

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/ViewModels/Histories.cs

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

449-
public List<Models.Commit> GetChildrensInCurHistory(Models.Commit commit)
449+
public List<Models.Commit> GetChildrensInCurHistory(Models.Commit commit, uint depth = 0)
450450
{
451451
if (commit == null || commit.Index == 0) return new List<Models.Commit>();
452-
return _commits[..(commit.Index)].Where(x => x.Parents.Contains(commit.SHA)).ToList();
452+
int start = 0;
453+
int stop = commit.Index;
454+
if (_commits.Count < commit.Index)
455+
stop = _commits.Count;
456+
if (depth != 0 && (stop - depth) > 0) //,else show be 0 by default, 3, 2
457+
start = (int)(stop - depth);
458+
459+
return _commits[start..stop].Where(x => x.Parents.Contains(commit.SHA)).ToList();
453460
}
454461

455-
public List<Models.Commit> GetTopHeadsInCurHistory(Models.Commit commit)
462+
public List<Models.Commit> GetTopHeadsInCurHistory(Models.Commit commit, out List<int> pathindex, uint depth = 0)
456463
{
457-
// if (commit == null || commit.Index <= 0) return new List<Models.Commit>();
464+
pathindex = new List<int>();
458465
var heads = new List<Models.Commit>();
459466
if (commit == null || commit?.Index <= 0)
460467
return heads;
468+
461469
var queue = new Queue<Models.Commit>();
462-
var visited = new HashSet<string> { commit.SHA };
470+
var visited = new HashSet<int>();
463471

464472
queue.Enqueue(commit);
465473

474+
var toplimitIndex = commit.Index - depth;
475+
if (toplimitIndex < 0)
476+
toplimitIndex = 0;
477+
466478
while (queue.Any())
467479
{
468480
var currentCommit = queue.Dequeue();
469-
var children = GetChildrensInCurHistory(currentCommit);
481+
if (depth != 0 && currentCommit.Index - toplimitIndex < 1) // at least one
482+
continue;
483+
var children = GetChildrensInCurHistory(currentCommit, (uint)(currentCommit.Index - toplimitIndex));
470484
// var children = _commits.Where(x => x.Parents.Contains(currentCommit.SHA)).ToList();
471485
if (children.Any())
472486
{
@@ -475,15 +489,15 @@ private void NavigateTo(Models.Commit commit)
475489
foreach (var child in children)
476490
{
477491
// 如果这个子提交还没有被访问过,则加入队列并标记为已访问
478-
if (visited.Add(child.SHA))
492+
if (visited.Add(child.Index))
479493
{
480494
queue.Enqueue(child);
481495
heads.Add(child);
482496
}
483497
}
484498
}
485499
}
486-
500+
pathindex = visited.ToList();
487501
return heads;
488502
}
489503

0 commit comments

Comments
 (0)