From 7bb4e355bd8d1d5376c6376dc6c63e3b855d474e Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 13 May 2025 19:28:52 +0800 Subject: [PATCH] feature: show branches count in branch tree (#1306) --- src/Commands/QueryBranches.cs | 6 +++++- src/ViewModels/BranchTreeNode.cs | 12 ++++++++++++ src/ViewModels/Repository.cs | 10 +++++++++- src/Views/BranchTree.axaml | 6 ++++-- src/Views/Repository.axaml | 10 ++++++++-- 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Commands/QueryBranches.cs b/src/Commands/QueryBranches.cs index 19514954..39794090 100644 --- a/src/Commands/QueryBranches.cs +++ b/src/Commands/QueryBranches.cs @@ -17,8 +17,10 @@ namespace SourceGit.Commands Args = "branch -l --all -v --format=\"%(refname)%00%(committerdate:unix)%00%(objectname)%00%(HEAD)%00%(upstream)%00%(upstream:trackshort)\""; } - public List Result() + public List Result(out int localBranchesCount) { + localBranchesCount = 0; + var branches = new List(); var rs = ReadToEnd(); if (!rs.IsSuccess) @@ -34,6 +36,8 @@ namespace SourceGit.Commands branches.Add(b); if (!b.IsLocal) remoteBranches.Add(b.FullName); + else + localBranchesCount++; } } diff --git a/src/ViewModels/BranchTreeNode.cs b/src/ViewModels/BranchTreeNode.cs index ee78d113..d2940d3f 100644 --- a/src/ViewModels/BranchTreeNode.cs +++ b/src/ViewModels/BranchTreeNode.cs @@ -14,6 +14,7 @@ namespace SourceGit.ViewModels public int Depth { get; set; } = 0; public bool IsSelected { get; set; } = false; public List Children { get; private set; } = new List(); + public int Counter { get; set; } = 0; public Models.FilterMode FilterMode { @@ -48,6 +49,11 @@ namespace SourceGit.ViewModels get => Backend is Models.Branch { IsUpstreamGone: true }; } + public string BranchesCount + { + get => Counter > 0 ? $"({Counter})" : string.Empty; + } + public string Tooltip { get => Backend is Models.Branch b ? b.FriendlyName : null; @@ -107,7 +113,10 @@ namespace SourceGit.ViewModels var rk = $"refs/remotes/{branch.Remote}"; if (folders.TryGetValue(rk, out var remote)) + { + remote.Counter++; MakeBranchNode(branch, remote.Children, folders, rk, bForceExpanded); + } } foreach (var path in _expanded) @@ -157,6 +166,7 @@ namespace SourceGit.ViewModels if (folders.TryGetValue(folder, out var val)) { lastFolder = val; + lastFolder.Counter++; lastFolder.TimeToSort = Math.Max(lastFolder.TimeToSort, time); if (!lastFolder.IsExpanded) lastFolder.IsExpanded |= (branch.IsCurrent || _expanded.Contains(folder)); @@ -169,6 +179,7 @@ namespace SourceGit.ViewModels Path = folder, IsExpanded = bForceExpanded || branch.IsCurrent || _expanded.Contains(folder), TimeToSort = time, + Counter = 1, }; roots.Add(lastFolder); folders.Add(folder, lastFolder); @@ -181,6 +192,7 @@ namespace SourceGit.ViewModels Path = folder, IsExpanded = bForceExpanded || branch.IsCurrent || _expanded.Contains(folder), TimeToSort = time, + Counter = 1, }; lastFolder.Children.Add(cur); folders.Add(folder, cur); diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 4d1a321a..ae377e81 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -228,6 +228,12 @@ namespace SourceGit.ViewModels private set => SetProperty(ref _stashesCount, value); } + public int LocalBranchesCount + { + get => _localBranchesCount; + private set => SetProperty(ref _localBranchesCount, value); + } + public bool IncludeUntracked { get => _settings.IncludeUntrackedInLocalChanges; @@ -1020,7 +1026,7 @@ namespace SourceGit.ViewModels public void RefreshBranches() { - var branches = new Commands.QueryBranches(_fullpath).Result(); + var branches = new Commands.QueryBranches(_fullpath).Result(out var localBranchesCount); var remotes = new Commands.QueryRemotes(_fullpath).Result(); var builder = BuildBranchTree(branches, remotes); @@ -1033,6 +1039,7 @@ namespace SourceGit.ViewModels CurrentBranch = branches.Find(x => x.IsCurrent); LocalBranchTrees = builder.Locals; RemoteBranchTrees = builder.Remotes; + LocalBranchesCount = localBranchesCount; if (_workingCopy != null) _workingCopy.HasRemotes = remotes.Count > 0; @@ -2726,6 +2733,7 @@ namespace SourceGit.ViewModels private int _selectedViewIndex = 0; private object _selectedView = null; + private int _localBranchesCount = 0; private int _localChangesCount = 0; private int _stashesCount = 0; diff --git a/src/Views/BranchTree.axaml b/src/Views/BranchTree.axaml index c1fecf63..61a08613 100644 --- a/src/Views/BranchTree.axaml +++ b/src/Views/BranchTree.axaml @@ -60,9 +60,11 @@ + TextTrimming="CharacterEllipsis"> + + + - + + + +