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

Commit 683e795

Browse files
committed
Correctly check out branch from same repo.
It may not be checked out as a local branch, so look for it in `refs/remotes/origin` if not.
1 parent 0e746fc commit 683e795

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,33 @@ public IObservable<IBranch> GetLocalBranches(ILocalRepositoryModel repository, P
149149

150150
public IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, PullRequest pullRequest)
151151
{
152-
return Observable.Defer(() =>
152+
return Observable.Defer(async () =>
153153
{
154154
var repo = gitService.GetRepository(repository.LocalPath);
155-
var branch = GetLocalBranchesInternal(repo, repository.CloneUrl, pullRequest).First();
156-
gitClient.Fetch(repo, "origin");
157-
gitClient.Checkout(repo, branch);
155+
var branchName = GetLocalBranchesInternal(repo, repository.CloneUrl, pullRequest).First();
156+
157+
await gitClient.Fetch(repo, "origin");
158+
159+
var branch = repo.Branches[branchName];
160+
161+
if (branch == null)
162+
{
163+
var trackedBranchName = $"refs/remotes/origin/" + branchName;
164+
var trackedBranch = repo.Branches[trackedBranchName];
165+
166+
if (trackedBranch != null)
167+
{
168+
branch = repo.CreateBranch(branchName, trackedBranch.Tip);
169+
await gitClient.SetTrackingBranch(repo, branchName, trackedBranchName);
170+
}
171+
else
172+
{
173+
throw new InvalidOperationException($"Could not find branch '{trackedBranchName}'.");
174+
}
175+
}
176+
177+
await gitClient.Checkout(repo, branchName);
178+
158179
return Observable.Empty<Unit>();
159180
});
160181
}

0 commit comments

Comments
 (0)