Skip to content

Commit 5bbf965

Browse files
committed
fix: crash when trying to checkout a remote branch with a local branch has no upstream (#2664)
1 parent ce07209 commit 5bbf965

5 files changed

Lines changed: 18 additions & 13 deletions

File tree

src/ViewModels/CreateBranch.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ public override async Task<bool> Sure()
229229

230230
created.Upstream = basedOn.FullName;
231231
}
232+
else
233+
{
234+
created.Upstream = string.Empty;
235+
}
232236

233237
_repo.RefreshAfterCreateBranch(created, CheckoutAfterCreated);
234238
}

src/ViewModels/Pull.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private void PostRemoteSelected()
205205
{
206206
foreach (var branch in branches)
207207
{
208-
if (Current.Upstream == branch.FullName)
208+
if (Current.Upstream.Equals(branch.FullName, System.StringComparison.Ordinal))
209209
{
210210
SelectedBranch = branch;
211211
autoSelectedBranch = true;

src/ViewModels/Push.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,23 +136,23 @@ public Push(Repository repo, Models.Branch localBranch)
136136
}
137137

138138
// Find preferred remote if selected local branch has upstream.
139-
if (!string.IsNullOrEmpty(_selectedLocalBranch?.Upstream) && !_selectedLocalBranch.IsUpstreamGone)
139+
if (_selectedLocalBranch != null)
140140
{
141-
_tracking = false;
142-
143-
foreach (var branch in repo.Branches)
141+
var upstream = _selectedLocalBranch.Upstream;
142+
if (!string.IsNullOrEmpty(upstream) && !_selectedLocalBranch.IsUpstreamGone)
144143
{
145-
if (!branch.IsLocal && _selectedLocalBranch.Upstream == branch.FullName)
144+
_tracking = false;
145+
146+
foreach (var branch in repo.Branches)
146147
{
147-
_selectedRemote = repo.Remotes.Find(x => x.Name == branch.Remote);
148-
break;
148+
if (!branch.IsLocal && upstream.Equals(branch.FullName, StringComparison.Ordinal))
149+
{
150+
_selectedRemote = repo.Remotes.Find(x => x.Name == branch.Remote);
151+
break;
152+
}
149153
}
150154
}
151155
}
152-
else
153-
{
154-
_tracking = true;
155-
}
156156

157157
// Set default remote to the first if it has not been set.
158158
if (_selectedRemote == null)

src/ViewModels/Repository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@ public async Task CheckoutBranchAsync(Models.Branch branch)
14511451
foreach (var b in _branches)
14521452
{
14531453
if (b.IsLocal &&
1454+
!string.IsNullOrEmpty(b.Upstream) &&
14541455
b.Upstream.Equals(branch.FullName, StringComparison.Ordinal) &&
14551456
b.Ahead.Count == 0)
14561457
{

src/Views/BranchTree.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ private ContextMenu CreateContextMenuForLocalBranch(ViewModels.Repository repo,
704704
{
705705
var current = repo.CurrentBranch;
706706
var menu = new ContextMenu();
707-
var upstream = repo.Branches.Find(x => x.FullName.Equals(branch.Upstream, StringComparison.Ordinal));
707+
var upstream = !string.IsNullOrEmpty(branch.Upstream) ? repo.Branches.Find(x => x.FullName.Equals(branch.Upstream, StringComparison.Ordinal)) : null;
708708

709709
var push = new MenuItem();
710710
push.Header = App.Text("BranchCM.Push", branch.Name);

0 commit comments

Comments
 (0)