Skip to content

Commit ad36984

Browse files
committed
enhance: manually update local branch tree after checking out a exiting local branch (#2169)
Since the remote branch tree did not changed after checkout, and the changes in the local branch tree are completely predictable, update it manually. Signed-off-by: leo <longshuang@msn.cn>
1 parent 54ea6cd commit ad36984

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/ViewModels/Checkout.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,15 @@ public override async Task<bool> Sure()
8080
await new Commands.Stash(_repo.FullPath)
8181
.Use(log)
8282
.PopAsync("stash@{0}");
83+
84+
_repo.FastRefreshBranchesAfterCheckout(Branch);
85+
}
86+
else
87+
{
88+
_repo.MarkWorkingCopyDirtyManually();
8389
}
8490

8591
log.Complete();
86-
87-
var b = _repo.Branches.Find(x => x.IsLocal && x.Name == Branch);
88-
if (b != null && _repo.HistoryFilterMode == Models.FilterMode.Included)
89-
_repo.SetBranchFilterMode(b, Models.FilterMode.Included, false, false);
90-
91-
_repo.MarkBranchesDirtyManually();
92-
93-
ProgressDescription = "Waiting for branch updated...";
94-
await Task.Delay(400);
9592
return succ;
9693
}
9794

src/ViewModels/Repository.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,50 @@ public IDisposable LockWatcher()
788788
return _watcher?.Lock();
789789
}
790790

791+
public void FastRefreshBranchesAfterCheckout(string checkouted)
792+
{
793+
_watcher?.MarkBranchUpdated();
794+
_watcher?.MarkWorkingCopyUpdated();
795+
796+
var oldWorktree = _currentBranch.WorktreePath;
797+
_currentBranch.WorktreePath = null;
798+
799+
List<Models.Branch> locals = [];
800+
Models.Branch detached = null;
801+
foreach (var b in _branches)
802+
{
803+
if (b.IsLocal)
804+
{
805+
if (b.IsDetachedHead)
806+
{
807+
detached = b;
808+
continue;
809+
}
810+
811+
b.IsCurrent = b.Name.Equals(checkouted, StringComparison.Ordinal);
812+
if (b.IsCurrent)
813+
{
814+
CurrentBranch = b;
815+
b.WorktreePath = oldWorktree;
816+
if (_historyFilterMode == Models.FilterMode.Included)
817+
SetBranchFilterMode(b, Models.FilterMode.Included, false, false);
818+
}
819+
820+
locals.Add(b);
821+
}
822+
}
823+
824+
if (detached != null)
825+
_branches.Remove(detached);
826+
827+
var builder = BuildBranchTree(locals, []);
828+
LocalBranchTrees = builder.Locals;
829+
830+
RefreshCommits();
831+
RefreshWorkingCopyChanges();
832+
RefreshWorktrees();
833+
}
834+
791835
public void MarkBranchesDirtyManually()
792836
{
793837
_watcher?.MarkBranchUpdated();

0 commit comments

Comments
 (0)