From 90150b10590c57694a3bf4ef06bb7c825d33eff5 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Mon, 16 Jun 2025 07:04:50 +0100 Subject: [PATCH 1/2] Default to current Git branch --- src/extension.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 86a4e7b..5c8f1da 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -193,6 +193,7 @@ async function getRepo(currentRepo?: string) { const options = { placeHolder: "Select a repository", canPickMany: false, + ignoreFocusOut: true, }; return await vscode.window.showQuickPick(repos, options).then((item) => item?.label); @@ -232,11 +233,34 @@ async function getRemote(repoName: string, currentConfig?: { repo?: string; remo const options = { placeHolder: "Select a remote", canPickMany: false, + ignoreFocusOut: true, }; return await vscode.window.showQuickPick(remotes, options).then((item) => item?.label); } +async function getBranch(repoName: string, config?: { branch?: string }) { + const git = getGitAPI(); + const repo = git.repositories.find((r) => path.basename(r.rootUri.fsPath) === repoName); + if (!repo) { + throw new Error(`Repository '${repoName}' not found`); + } + + // The head name will be undefined if, for example, we're on a detached head. + const branchName = repo.state.HEAD?.name || config?.branch || "master"; + + // Always ask the branch name, as this is what changes most often. + // Copy pasting this from the PR isn't a big deal, and it's not often one we've used before. + const branch = await vscode.window.showInputBox({ + prompt: "Enter the branch name", + placeHolder: "hello-branch", + value: branchName, + ignoreFocusOut: true, + }); + + return branch; +} + async function pushRepoSettings() { const config = getSyncData(); @@ -252,13 +276,7 @@ async function pushRepoSettings() { return; } - // Always ask the branch name, as this is what changes most often. - // Copy pasting this from the PR isn't a big deal, and it's not often one we've used before. - const branch = await vscode.window.showInputBox({ - prompt: "Enter the branch name", - placeHolder: "hello-branch", - value: config?.branch || "master", - }); + const branch = await getBranch(repo, config); if (!branch) { vscode.window.showErrorMessage(`${extensionName}: Cannot sync, no branch specified`); return; From 74058167e5fee7cc8c65942243a4da5b69df7326 Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Mon, 16 Jun 2025 09:04:11 +0100 Subject: [PATCH 2/2] refactor: rename functions for clarity and extract repo retrieval logic --- src/extension.ts | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 5c8f1da..fa312a4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -170,9 +170,18 @@ function getGitAPI() { return git; } +function getRepo(repoName: string) { + const git = getGitAPI(); + const repo = git.repositories.find((r) => path.basename(r.rootUri.fsPath) === repoName); + if (!repo) { + throw new Error(`Repository '${repoName}' not found`); + } + return repo; +} + // Always show the list of remotes but pre-select the one that was used last. // We could save the last used repo in the workspace config, but for now we'll ask. -async function getRepo(currentRepo?: string) { +async function showRepoInput(currentRepo?: string) { const git = getGitAPI(); const repos = git.repositories.map((repo) => ({ label: path.basename(repo.rootUri.fsPath), @@ -203,19 +212,17 @@ async function getRepo(currentRepo?: string) { // otherwise show all remotes since it's either a new repo or the remote wasn't chosen before. // Don't show all remotes every time, as this makes it a bit tedious; usually we only want to set // the remote once and use it for all future syncs. This chan be changed in `settings.json`. -async function getRemote(repoName: string, currentConfig?: { repo?: string; remote?: string }) { +async function showRemoteInput( + repoName: string, + currentConfig?: { repo?: string; remote?: string }, +) { const { repo: currentRepo, remote: currentRemote } = currentConfig || {}; if (currentRepo === repoName && currentRemote) { console.log(`${logTag} Repo not changed, using existing remote: ${currentRemote}`); return currentRemote; } - const git = getGitAPI(); - const repo = git.repositories.find((r) => path.basename(r.rootUri.fsPath) === repoName); - if (!repo) { - throw new Error(`Repository '${repoName}' not found`); - } - + const repo = getRepo(repoName); const remotes = repo.state.remotes.map((remote) => ({ label: remote.name, description: remote.fetchUrl, @@ -239,12 +246,8 @@ async function getRemote(repoName: string, currentConfig?: { repo?: string; remo return await vscode.window.showQuickPick(remotes, options).then((item) => item?.label); } -async function getBranch(repoName: string, config?: { branch?: string }) { - const git = getGitAPI(); - const repo = git.repositories.find((r) => path.basename(r.rootUri.fsPath) === repoName); - if (!repo) { - throw new Error(`Repository '${repoName}' not found`); - } +async function showBranchInput(repoName: string, config?: { branch?: string }) { + const repo = getRepo(repoName); // The head name will be undefined if, for example, we're on a detached head. const branchName = repo.state.HEAD?.name || config?.branch || "master"; @@ -264,19 +267,19 @@ async function getBranch(repoName: string, config?: { branch?: string }) { async function pushRepoSettings() { const config = getSyncData(); - const repo = await getRepo(config.repo); + const repo = await showRepoInput(config.repo); if (!repo) { vscode.window.showErrorMessage(`${extensionName}: Cannot sync, no repo specified`); return; } - const remote = await getRemote(repo, config); + const remote = await showRemoteInput(repo, config); if (!remote) { vscode.window.showErrorMessage(`${extensionName}: Cannot sync, no remote specified`); return; } - const branch = await getBranch(repo, config); + const branch = await showBranchInput(repo, config); if (!branch) { vscode.window.showErrorMessage(`${extensionName}: Cannot sync, no branch specified`); return;