Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 059a8db

Browse files
committed
Added an assert and handle something going wrong.
If `PullRequestService.SwitchToBranch` is called with no local branch, handle it but assert.
1 parent b890f32 commit 059a8db

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
using System.Reactive;
1515
using System.Collections.Generic;
1616
using LibGit2Sharp;
17-
using PullRequest = Octokit.PullRequest;
17+
using PullRequest = Octokit.PullRequest;
18+
using System.Diagnostics;
1819

1920
namespace GitHub.Services
2021
{
@@ -167,29 +168,34 @@ public IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, IPullR
167168
return Observable.Defer(async () =>
168169
{
169170
var repo = gitService.GetRepository(repository.LocalPath);
170-
var branchName = GetLocalBranchesInternal(repository, repo, pullRequest).First();
171+
var branchName = GetLocalBranchesInternal(repository, repo, pullRequest).FirstOrDefault();
171172

172-
await gitClient.Fetch(repo, "origin");
173-
174-
var branch = repo.Branches[branchName];
173+
Debug.Assert(branchName != null, "PullRequestService.SwitchToBranch called but no local branch found.");
175174

176-
if (branch == null)
175+
if (branchName != null)
177176
{
178-
var trackedBranchName = $"refs/remotes/origin/" + branchName;
179-
var trackedBranch = repo.Branches[trackedBranchName];
177+
await gitClient.Fetch(repo, "origin");
180178

181-
if (trackedBranch != null)
182-
{
183-
branch = repo.CreateBranch(branchName, trackedBranch.Tip);
184-
await gitClient.SetTrackingBranch(repo, branchName, trackedBranchName);
185-
}
186-
else
179+
var branch = repo.Branches[branchName];
180+
181+
if (branch == null)
187182
{
188-
throw new InvalidOperationException($"Could not find branch '{trackedBranchName}'.");
183+
var trackedBranchName = $"refs/remotes/origin/" + branchName;
184+
var trackedBranch = repo.Branches[trackedBranchName];
185+
186+
if (trackedBranch != null)
187+
{
188+
branch = repo.CreateBranch(branchName, trackedBranch.Tip);
189+
await gitClient.SetTrackingBranch(repo, branchName, trackedBranchName);
190+
}
191+
else
192+
{
193+
throw new InvalidOperationException($"Could not find branch '{trackedBranchName}'.");
194+
}
189195
}
190-
}
191196

192-
await gitClient.Checkout(repo, branchName);
197+
await gitClient.Checkout(repo, branchName);
198+
}
193199

194200
return Observable.Empty<Unit>();
195201
});

0 commit comments

Comments
 (0)