Browse Source

Fixes #2583 regression in pr -> open worktree cmd

main
Eric Amodio 1 year ago
parent
commit
b8b66e62a5
2 changed files with 35 additions and 24 deletions
  1. +4
    -0
      CHANGELOG.md
  2. +31
    -24
      src/commands/ghpr/openOrCreateWorktree.ts

+ 4
- 0
CHANGELOG.md View File

@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Changes _Open Associated Pull Request_ command to support opening associated pull requests with the current branch or the HEAD commit if no branch association was found — closes [#2559](https://github.com/gitkraken/vscode-gitlens/issues/2559) - Changes _Open Associated Pull Request_ command to support opening associated pull requests with the current branch or the HEAD commit if no branch association was found — closes [#2559](https://github.com/gitkraken/vscode-gitlens/issues/2559)
### Fixed
- Fixes [#2583](https://github.com/gitkraken/vscode-gitlens/issues/2583) - Regression with _Open Worktree for Pull Request via GitLens..._ command
## [13.4.0] - 2023-03-16 ## [13.4.0] - 2023-03-16
### Added ### Added

+ 31
- 24
src/commands/ghpr/openOrCreateWorktree.ts View File

@ -19,8 +19,9 @@ interface GHPRPullRequestNode {
interface GHPRPullRequest { interface GHPRPullRequest {
readonly base: { readonly base: {
readonly repositoryCloneUrl: { readonly repositoryCloneUrl: {
readonly owner: string;
readonly repositoryName: string; readonly repositoryName: string;
readonly owner: string;
readonly url: Uri;
}; };
}; };
readonly githubRepository: { readonly githubRepository: {
@ -30,6 +31,7 @@ interface GHPRPullRequest {
readonly ref: string; readonly ref: string;
readonly sha: string; readonly sha: string;
readonly repositoryCloneUrl: { readonly repositoryCloneUrl: {
readonly repositoryName: string;
readonly owner: string; readonly owner: string;
readonly url: Uri; readonly url: Uri;
}; };
@ -57,9 +59,9 @@ export class OpenOrCreateWorktreeCommand extends Command {
const { const {
base: { base: {
repositoryCloneUrl: { owner: rootOwner, repositoryName: rootRepository },
repositoryCloneUrl: { url: rootUri, owner: rootOwner, repositoryName: rootRepository },
}, },
githubRepository: { rootUri },
githubRepository: { rootUri: localUri },
head: { head: {
repositoryCloneUrl: { url: remoteUri, owner: remoteOwner }, repositoryCloneUrl: { url: remoteUri, owner: remoteOwner },
ref, ref,
@ -67,26 +69,15 @@ export class OpenOrCreateWorktreeCommand extends Command {
item: { number }, item: { number },
} = pr; } = pr;
let repo = this.container.git.getRepository(rootUri);
let repo = this.container.git.getRepository(localUri);
if (repo == null) { if (repo == null) {
void window.showWarningMessage(`Unable to find repository(${rootUri.toString()}) for PR #${number}`);
void window.showWarningMessage(`Unable to find repository(${localUri.toString()}) for PR #${number}`);
return; return;
} }
repo = await repo.getMainRepository(); repo = await repo.getMainRepository();
if (repo == null) { if (repo == null) {
void window.showWarningMessage(`Unable to find main repository(${rootUri.toString()}) for PR #${number}`);
return;
}
const branchName = `${remoteOwner}/${ref}`;
const prBranchName = `pr/${branchName}`;
const worktrees = await repo.getWorktrees();
const worktree = worktrees.find(w => w.branch === branchName || w.branch === prBranchName);
if (worktree != null) {
void openWorktree(worktree);
void window.showWarningMessage(`Unable to find main repository(${localUri.toString()}) for PR #${number}`);
return; return;
} }
@ -95,7 +86,10 @@ export class OpenOrCreateWorktreeCommand extends Command {
let remote: GitRemote | undefined; let remote: GitRemote | undefined;
[remote] = await repo.getRemotes({ filter: r => r.matches(remoteDomain, remotePath) }); [remote] = await repo.getRemotes({ filter: r => r.matches(remoteDomain, remotePath) });
if (remote == null) {
if (remote != null) {
// Ensure we have the latest from the remote
await this.container.git.fetch(repo.path, { remote: remote.name });
} else {
const result = await window.showInformationMessage( const result = await window.showInformationMessage(
`Unable to find a remote for '${remoteUrl}'. Would you like to add a new remote?`, `Unable to find a remote for '${remoteUrl}'. Would you like to add a new remote?`,
{ modal: true }, { modal: true },
@ -111,8 +105,17 @@ export class OpenOrCreateWorktreeCommand extends Command {
}); });
[remote] = await repo.getRemotes({ filter: r => r.url === remoteUrl }); [remote] = await repo.getRemotes({ filter: r => r.url === remoteUrl });
if (remote == null) return; if (remote == null) return;
} else {
await this.container.git.fetch(repo.path, { remote: remote.name });
}
const remoteBranchName = `${remote.name}/${ref}`;
const localBranchName = `pr/${rootUri.toString() === remoteUri.toString() ? ref : remoteBranchName}`;
const worktrees = await repo.getWorktrees();
const worktree = worktrees.find(w => w.branch === localBranchName);
if (worktree != null) {
void openWorktree(worktree);
return;
} }
await waitUntilNextTick(); await waitUntilNextTick();
@ -121,19 +124,23 @@ export class OpenOrCreateWorktreeCommand extends Command {
await createWorktree( await createWorktree(
repo, repo,
undefined, undefined,
createReference(branchName, repo.path, { refType: 'branch', name: branchName, remote: true }),
{ createBranch: prBranchName },
createReference(remoteBranchName, repo.path, {
refType: 'branch',
name: remoteBranchName,
remote: true,
}),
{ createBranch: localBranchName },
); );
// Ensure that the worktree was created // Ensure that the worktree was created
const worktree = await this.container.git.getWorktree(repo.path, w => w.branch === prBranchName);
const worktree = await this.container.git.getWorktree(repo.path, w => w.branch === localBranchName);
if (worktree == null) return; if (worktree == null) return;
// Save the PR number in the branch config // Save the PR number in the branch config
// https://github.com/Microsoft/vscode-pull-request-github/blob/0c556c48c69a3df2f9cf9a45ed2c40909791b8ab/src/github/pullRequestGitHelper.ts#L18 // https://github.com/Microsoft/vscode-pull-request-github/blob/0c556c48c69a3df2f9cf9a45ed2c40909791b8ab/src/github/pullRequestGitHelper.ts#L18
void this.container.git.setConfig( void this.container.git.setConfig(
repo.path, repo.path,
`branch.${prBranchName}.github-pr-owner-number`,
`branch.${localBranchName}.github-pr-owner-number`,
`${rootOwner}#${rootRepository}#${number}`, `${rootOwner}#${rootRepository}#${number}`,
); );
} catch (ex) { } catch (ex) {

Loading…
Cancel
Save