From 4ddb52bc531600c3c60de97e92a5c7b4f44a7a4c Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Tue, 14 Mar 2023 19:16:22 -0400 Subject: [PATCH] Improves worktree creation flow - Honors `createBranch` flag for naming the worktree folder - Falls back to the qualified remote branch name if the branch name already exists - Always uses the qualified remote branch name via GHPR command --- CHANGELOG.md | 1 + package.json | 2 +- src/commands/ghpr/openOrCreateWorktree.ts | 17 ++++++++--------- src/commands/git/worktree.ts | 18 +++++++++++------- src/git/actions/worktree.ts | 17 +++++++++++++++-- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3be21db..fd06d45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - Improves the display of items in the _Commit Graph_ - When showing local branches, we now always display the upstream branches in the minimap, scrollbar markers, and graph rows - When laying out lanes in the Graph column, we now bias to be left aligned when possible for an easier to read and compact graph visualization +- Improves _Open Worktree for Pull Request via GitLens..._ command to use the qualified remote branch name, e.g. `owner/branch`, when creating the worktree - Removes Insiders edition in favor of the pre-release edition ### Fixed diff --git a/package.json b/package.json index e15e7b4..bd94ece 100644 --- a/package.json +++ b/package.json @@ -11766,7 +11766,7 @@ { "command": "gitlens.ghpr.views.openOrCreateWorktree", "when": "!gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders && view == pr:github && viewItem =~ /pullrequest(:local)?:nonactive|description/", - "group": "pullrequest@2" + "group": "2_gitlens@1" } ], "webview/context": [ diff --git a/src/commands/ghpr/openOrCreateWorktree.ts b/src/commands/ghpr/openOrCreateWorktree.ts index a578d9b..ca4eb0e 100644 --- a/src/commands/ghpr/openOrCreateWorktree.ts +++ b/src/commands/ghpr/openOrCreateWorktree.ts @@ -79,8 +79,10 @@ export class OpenOrCreateWorktreeCommand extends Command { return; } + const branchName = `${remoteOwner}/${ref}`; + const worktrees = await repo.getWorktrees(); - const worktree = worktrees.find(w => w.branch === ref); + const worktree = worktrees.find(w => w.branch === branchName); if (worktree != null) { void openWorktree(worktree); @@ -118,27 +120,24 @@ export class OpenOrCreateWorktreeCommand extends Command { await createWorktree( repo, undefined, - createReference(`${remote.name}/${ref}`, repo.path, { - refType: 'branch', - name: `${remote.name}/${ref}`, - remote: true, - }), + createReference(branchName, repo.path, { refType: 'branch', name: branchName, remote: true }), + { createBranch: branchName }, ); // Ensure that the worktree was created - const worktree = await this.container.git.getWorktree(repo.path, w => w.branch === ref); + const worktree = await this.container.git.getWorktree(repo.path, w => w.branch === branchName); if (worktree == null) return; // Save the PR number in the branch config // https://github.com/Microsoft/vscode-pull-request-github/blob/0c556c48c69a3df2f9cf9a45ed2c40909791b8ab/src/github/pullRequestGitHelper.ts#L18 void this.container.git.setConfig( repo.path, - `branch.${ref}.github-pr-owner-number`, + `branch.${branchName}.github-pr-owner-number`, `${rootOwner}#${rootRepository}#${number}`, ); } catch (ex) { Logger.error(ex, 'CreateWorktreeCommand', 'Unable to create worktree'); - void window.showErrorMessage(`Unable to create worktree for ${ref}`); + void window.showErrorMessage(`Unable to create worktree for ${remoteOwner}:${ref}`); } } } diff --git a/src/commands/git/worktree.ts b/src/commands/git/worktree.ts index ac71dad..54efeca 100644 --- a/src/commands/git/worktree.ts +++ b/src/commands/git/worktree.ts @@ -367,7 +367,12 @@ export class WorktreeGitCommand extends QuickCommand { const isRemoteBranch = state.reference?.refType === 'branch' && state.reference?.remote; if (isRemoteBranch && !state.flags.includes('-b')) { state.flags.push('-b'); + state.createBranch = getNameWithoutRemote(state.reference); + const branch = await state.repo.getBranch(state.createBranch); + if (branch != null) { + state.createBranch = state.reference.name; + } } if (state.flags.includes('-b') && state.createBranch == null) { @@ -576,13 +581,12 @@ export class WorktreeGitCommand extends QuickCommand { canCreateDirectlyInPicked = false; } - const recommendedUri = - state.reference != null - ? Uri.joinPath( - recommendedRootUri, - ...getNameWithoutRemote(state.reference).replace(/\\/g, '/').split('/'), - ) - : recommendedRootUri; + const branchName = + state.createBranch ?? (state.reference != null ? getNameWithoutRemote(state.reference) : undefined); + + const recommendedUri = branchName + ? Uri.joinPath(recommendedRootUri, ...branchName.replace(/\\/g, '/').split('/')) + : recommendedRootUri; const recommendedFriendlyPath = truncateLeft(GitWorktree.getFriendlyPath(recommendedUri), 65); const recommendedNewBranchFriendlyPath = truncateLeft( diff --git a/src/git/actions/worktree.ts b/src/git/actions/worktree.ts index b7e2813..a2f5b95 100644 --- a/src/git/actions/worktree.ts +++ b/src/git/actions/worktree.ts @@ -10,10 +10,23 @@ import type { GitReference } from '../models/reference'; import type { Repository } from '../models/repository'; import type { GitWorktree } from '../models/worktree'; -export function create(repo?: string | Repository, uri?: Uri, ref?: GitReference, options?: { reveal?: boolean }) { +export function create( + repo?: string | Repository, + uri?: Uri, + ref?: GitReference, + options?: { createBranch?: string; reveal?: boolean }, +) { return executeGitCommand({ command: 'worktree', - state: { subcommand: 'create', repo: repo, uri: uri, reference: ref, reveal: options?.reveal }, + state: { + subcommand: 'create', + repo: repo, + uri: uri, + reference: ref, + createBranch: options?.createBranch, + flags: options?.createBranch ? ['-b'] : undefined, + reveal: options?.reveal, + }, }); }