Skip to content

Commit 418d4a3

Browse files
committed
enhance: manually update local branch tree after creating a new local branch (#2169)
Signed-off-by: leo <longshuang@msn.cn>
1 parent 11158d0 commit 418d4a3

File tree

5 files changed

+80
-28
lines changed

5 files changed

+80
-28
lines changed

src/ViewModels/Checkout.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override async Task<bool> Sure()
8282
.Use(log)
8383
.PopAsync("stash@{0}");
8484

85-
_repo.FastRefreshBranchesAfterCheckout(_branch);
85+
_repo.RefreshAfterCheckoutBranch(_branch);
8686
}
8787
else
8888
{

src/ViewModels/CheckoutAndFastForward.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public override async Task<bool> Sure()
8585
LocalBranch.Head = RemoteBranch.Head;
8686
LocalBranch.CommitterDate = RemoteBranch.CommitterDate;
8787

88-
_repo.FastRefreshBranchesAfterCheckout(LocalBranch);
88+
_repo.RefreshAfterCheckoutBranch(LocalBranch);
8989
}
9090
else
9191
{

src/ViewModels/CreateBranch.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public CreateBranch(Repository repo, Models.Branch branch)
5858
{
5959
_repo = repo;
6060
_baseOnRevision = branch.Head;
61+
_committerDate = branch.CommitterDate;
62+
_head = branch.Head;
6163

6264
if (!branch.IsLocal)
6365
Name = branch.Name;
@@ -70,6 +72,8 @@ public CreateBranch(Repository repo, Models.Commit commit)
7072
{
7173
_repo = repo;
7274
_baseOnRevision = commit.SHA;
75+
_committerDate = commit.CommitterTime;
76+
_head = commit.SHA;
7377

7478
BasedOn = commit;
7579
DiscardLocalChanges = false;
@@ -79,6 +83,8 @@ public CreateBranch(Repository repo, Models.Tag tag)
7983
{
8084
_repo = repo;
8185
_baseOnRevision = tag.SHA;
86+
_committerDate = tag.CreatorDate;
87+
_head = tag.SHA;
8288

8389
BasedOn = tag;
8490
DiscardLocalChanges = false;
@@ -125,6 +131,15 @@ public override async Task<bool> Sure()
125131
}
126132
}
127133

134+
Models.Branch created = new()
135+
{
136+
Name = _name,
137+
FullName = $"refs/heads/{_name}",
138+
CommitterDate = _committerDate,
139+
Head = _head,
140+
IsLocal = true,
141+
};
142+
128143
bool succ;
129144
if (CheckoutAfterCreated && !_repo.IsBare)
130145
{
@@ -168,43 +183,33 @@ public override async Task<bool> Sure()
168183
.CreateAsync(_baseOnRevision, _allowOverwrite);
169184
}
170185

171-
if (succ && BasedOn is Models.Branch { IsLocal: false } basedOn && _name.Equals(basedOn.Name, StringComparison.Ordinal))
186+
if (succ)
172187
{
173-
await new Commands.Branch(_repo.FullPath, _name)
188+
if (BasedOn is Models.Branch { IsLocal: false } basedOn && _name.Equals(basedOn.Name, StringComparison.Ordinal))
189+
{
190+
await new Commands.Branch(_repo.FullPath, _name)
174191
.Use(log)
175192
.SetUpstreamAsync(basedOn);
176-
}
177-
178-
log.Complete();
179-
180-
if (succ && CheckoutAfterCreated)
181-
{
182-
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{_name}" };
183-
if (BasedOn is Models.Branch { IsLocal: false } based)
184-
fake.Upstream = based.FullName;
185193

186-
var folderEndIdx = fake.FullName.LastIndexOf('/');
187-
if (folderEndIdx > 10)
188-
_repo.UIStates.ExpandedBranchNodesInSideBar.Add(fake.FullName.Substring(0, folderEndIdx));
194+
created.Upstream = basedOn.FullName;
195+
}
189196

190-
if (_repo.HistoryFilterMode == Models.FilterMode.Included)
191-
_repo.SetBranchFilterMode(fake, Models.FilterMode.Included, false, false);
197+
_repo.RefreshAfterCreateBranch(created, CheckoutAfterCreated);
192198
}
193-
194-
_repo.MarkBranchesDirtyManually();
195-
196-
if (CheckoutAfterCreated)
199+
else
197200
{
198-
ProgressDescription = "Waiting for branch updated...";
199-
await Task.Delay(400);
201+
_repo.MarkWorkingCopyDirtyManually();
200202
}
201203

204+
log.Complete();
202205
return true;
203206
}
204207

205208
private readonly Repository _repo = null;
206-
private string _name = null;
207209
private readonly string _baseOnRevision = null;
210+
private readonly ulong _committerDate = 0;
211+
private readonly string _head = string.Empty;
212+
private string _name = null;
208213
private bool _allowOverwrite = false;
209214
}
210215
}

src/ViewModels/RenameBranch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override async Task<bool> Sure()
6060
.RenameAsync(_name);
6161

6262
if (succ)
63-
_repo.FastRefreshBranchesAfterRenaming(Target, _name);
63+
_repo.RefreshAfterRenameBranch(Target, _name);
6464

6565
log.Complete();
6666
return succ;

src/ViewModels/Repository.cs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,54 @@ public IDisposable LockWatcher()
788788
return _watcher?.Lock();
789789
}
790790

791-
public void FastRefreshBranchesAfterCheckout(Models.Branch checkouted)
791+
public void RefreshAfterCreateBranch(Models.Branch created, bool checkout)
792+
{
793+
_watcher?.MarkBranchUpdated();
794+
_watcher?.MarkWorkingCopyUpdated();
795+
796+
_branches.Add(created);
797+
798+
if (checkout)
799+
{
800+
if (_currentBranch.IsDetachedHead)
801+
{
802+
_branches.Remove(_currentBranch);
803+
}
804+
else
805+
{
806+
_currentBranch.IsCurrent = false;
807+
_currentBranch.WorktreePath = null;
808+
}
809+
810+
created.IsCurrent = true;
811+
created.WorktreePath = FullPath;
812+
813+
var folderEndIdx = created.FullName.LastIndexOf('/');
814+
if (folderEndIdx > 10)
815+
_uiStates.ExpandedBranchNodesInSideBar.Add(created.FullName.Substring(0, folderEndIdx));
816+
817+
if (_historyFilterMode == Models.FilterMode.Included)
818+
SetBranchFilterMode(created, Models.FilterMode.Included, false, false);
819+
820+
CurrentBranch = created;
821+
}
822+
823+
List<Models.Branch> locals = [];
824+
foreach (var b in _branches)
825+
{
826+
if (b.IsLocal)
827+
locals.Add(b);
828+
}
829+
830+
var builder = BuildBranchTree(locals, []);
831+
LocalBranchTrees = builder.Locals;
832+
833+
RefreshCommits();
834+
RefreshWorkingCopyChanges();
835+
RefreshWorktrees();
836+
}
837+
838+
public void RefreshAfterCheckoutBranch(Models.Branch checkouted)
792839
{
793840
_watcher?.MarkBranchUpdated();
794841
_watcher?.MarkWorkingCopyUpdated();
@@ -824,7 +871,7 @@ public void FastRefreshBranchesAfterCheckout(Models.Branch checkouted)
824871
RefreshWorktrees();
825872
}
826873

827-
public void FastRefreshBranchesAfterRenaming(Models.Branch b, string newName)
874+
public void RefreshAfterRenameBranch(Models.Branch b, string newName)
828875
{
829876
_watcher?.MarkBranchUpdated();
830877

0 commit comments

Comments
 (0)