Skip to content

Commit 9d63078

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 9d63078

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

src/ViewModels/Checkout.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace SourceGit.ViewModels
44
{
55
public class Checkout : Popup
66
{
7-
public string Branch
7+
public string BranchName
88
{
9-
get;
9+
get => _branch.Name;
1010
}
1111

1212
public bool DiscardLocalChanges
@@ -15,10 +15,10 @@ public bool DiscardLocalChanges
1515
set;
1616
}
1717

18-
public Checkout(Repository repo, string branch)
18+
public Checkout(Repository repo, Models.Branch branch)
1919
{
2020
_repo = repo;
21-
Branch = branch;
21+
_branch = branch;
2222
DiscardLocalChanges = false;
2323
}
2424

@@ -30,9 +30,10 @@ public override bool CanStartDirectly()
3030
public override async Task<bool> Sure()
3131
{
3232
using var lockWatcher = _repo.LockWatcher();
33-
ProgressDescription = $"Checkout '{Branch}' ...";
33+
var branchName = BranchName;
34+
ProgressDescription = $"Checkout '{branchName}' ...";
3435

35-
var log = _repo.CreateLog($"Checkout '{Branch}'");
36+
var log = _repo.CreateLog($"Checkout '{branchName}'");
3637
Use(log);
3738

3839
if (_repo.CurrentBranch is { IsDetachedHead: true })
@@ -70,7 +71,7 @@ public override async Task<bool> Sure()
7071

7172
succ = await new Commands.Checkout(_repo.FullPath)
7273
.Use(log)
73-
.BranchAsync(Branch, DiscardLocalChanges);
74+
.BranchAsync(branchName, DiscardLocalChanges);
7475

7576
if (succ)
7677
{
@@ -80,21 +81,19 @@ public override async Task<bool> Sure()
8081
await new Commands.Stash(_repo.FullPath)
8182
.Use(log)
8283
.PopAsync("stash@{0}");
84+
85+
_repo.FastRefreshBranchesAfterCheckout(_branch);
86+
}
87+
else
88+
{
89+
_repo.MarkWorkingCopyDirtyManually();
8390
}
8491

8592
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);
9593
return succ;
9694
}
9795

9896
private readonly Repository _repo = null;
97+
private readonly Models.Branch _branch = null;
9998
}
10099
}

src/ViewModels/Repository.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,42 @@ public IDisposable LockWatcher()
788788
return _watcher?.Lock();
789789
}
790790

791+
public void FastRefreshBranchesAfterCheckout(Models.Branch checkouted)
792+
{
793+
_watcher?.MarkBranchUpdated();
794+
_watcher?.MarkWorkingCopyUpdated();
795+
796+
if (_currentBranch.IsDetachedHead)
797+
{
798+
_branches.Remove(_currentBranch);
799+
}
800+
else
801+
{
802+
_currentBranch.IsCurrent = false;
803+
_currentBranch.WorktreePath = null;
804+
}
805+
806+
checkouted.IsCurrent = true;
807+
checkouted.WorktreePath = FullPath;
808+
if (_historyFilterMode == Models.FilterMode.Included)
809+
SetBranchFilterMode(checkouted, Models.FilterMode.Included, false, false);
810+
811+
List<Models.Branch> locals = [];
812+
foreach (var b in _branches)
813+
{
814+
if (b.IsLocal)
815+
locals.Add(b);
816+
}
817+
818+
var builder = BuildBranchTree(locals, []);
819+
LocalBranchTrees = builder.Locals;
820+
CurrentBranch = checkouted;
821+
822+
RefreshCommits();
823+
RefreshWorkingCopyChanges();
824+
RefreshWorktrees();
825+
}
826+
791827
public void MarkBranchesDirtyManually()
792828
{
793829
_watcher?.MarkBranchUpdated();
@@ -1304,7 +1340,7 @@ public async Task CheckoutBranchAsync(Models.Branch branch)
13041340

13051341
if (branch.IsLocal)
13061342
{
1307-
await ShowAndStartPopupAsync(new Checkout(this, branch.Name));
1343+
await ShowAndStartPopupAsync(new Checkout(this, branch));
13081344
}
13091345
else
13101346
{

src/Views/Checkout.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Text="{DynamicResource Text.Checkout.Target}"/>
3131
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal">
3232
<Path Width="14" Height="14" Margin="4,0" Data="{StaticResource Icons.Branch}"/>
33-
<TextBlock Text="{Binding Branch}"/>
33+
<TextBlock Text="{Binding BranchName}"/>
3434
</StackPanel>
3535

3636
<TextBlock Grid.Row="1" Grid.Column="0"

0 commit comments

Comments
 (0)