Skip to content

Commit 5f40795

Browse files
Copilotalexr00
andcommitted
Fix fork PR checkout to properly set upstream tracking branch
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 49875b8 commit 5f40795

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/github/pullRequestGitHelper.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,21 @@ export class PullRequestGitHelper {
5959
}
6060

6161
// fetch the branch
62-
const ref = `${pullRequest.head.ref}:${localBranchName}`;
63-
Logger.debug(`Fetch ${remoteName}/${pullRequest.head.ref}:${localBranchName} - start`, PullRequestGitHelper.ID);
64-
progress.report({ message: vscode.l10n.t('Fetching branch {0}', ref) });
65-
await repository.fetch(remoteName, ref);
66-
Logger.debug(`Fetch ${remoteName}/${pullRequest.head.ref}:${localBranchName} - done`, PullRequestGitHelper.ID);
67-
progress.report({ message: vscode.l10n.t('Checking out {0}', ref) });
62+
Logger.debug(`Fetch ${remoteName}/${pullRequest.head.ref} - start`, PullRequestGitHelper.ID);
63+
progress.report({ message: vscode.l10n.t('Fetching branch {0}', pullRequest.head.ref) });
64+
await repository.fetch(remoteName, pullRequest.head.ref);
65+
Logger.debug(`Fetch ${remoteName}/${pullRequest.head.ref} - done`, PullRequestGitHelper.ID);
66+
67+
// Create local branch from the remote tracking branch
68+
const trackedBranchName = `refs/remotes/${remoteName}/${pullRequest.head.ref}`;
69+
const trackedBranch = await repository.getBranch(trackedBranchName);
70+
progress.report({ message: vscode.l10n.t('Creating branch {0}', localBranchName) });
71+
await repository.createBranch(localBranchName, false, trackedBranch.commit);
72+
73+
progress.report({ message: vscode.l10n.t('Checking out {0}', localBranchName) });
6874
await repository.checkout(localBranchName);
6975
// set remote tracking branch for the local branch
70-
await repository.setBranchUpstream(localBranchName, `refs/remotes/${remoteName}/${pullRequest.head.ref}`);
76+
await repository.setBranchUpstream(localBranchName, trackedBranchName);
7177
await PullRequestGitHelper.associateBranchWithPullRequest(repository, pullRequest, localBranchName);
7278
}
7379

src/test/github/pullRequestGitHelper.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ describe('PullRequestGitHelper', function () {
157157
gitHubRepository,
158158
);
159159

160-
repository.expectFetch('you', 'my-branch:pr/me/100');
161-
repository.expectPull(true);
160+
// Setup: Create remote tracking branch that will be created by fetch
161+
await repository.createBranch('refs/remotes/you/my-branch', false, 'remote-commit-hash');
162+
163+
repository.expectFetch('you', 'my-branch');
162164

163165
const pullRequest = new PullRequestModel(credentialStore, telemetry, gitHubRepository, remote, prItem);
164166

@@ -179,7 +181,7 @@ describe('PullRequestGitHelper', function () {
179181
assert.deepStrictEqual(repository.state.HEAD, {
180182
type: RefType.Head,
181183
name: 'pr/me/100',
182-
commit: undefined,
184+
commit: 'remote-commit-hash',
183185
upstream: {
184186
remote: 'you',
185187
name: 'my-branch',

0 commit comments

Comments
 (0)