diff --git a/.vscode/tasks.json b/.vscode/tasks.json index e7b2b0c8..74ac810e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,5 +9,10 @@ "reveal": "never", }, }, + { + "type": "npm", + "label": "build", + "script": "build", + }, ], } diff --git a/README.md b/README.md index 767f84e4..2ef07046 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ This extension contributes the following settings: - `sourcegraph.url`: The Sourcegraph instance to use. Specify your on-premises Sourcegraph instance here, if applicable. - `sourcegraph.remoteUrlReplacements`: Object, where each `key` is replaced by `value` in the remote url. +- `sourcegraph.defaultRemote`: An optional default remote to always use when opening Sourcegraph links. ## Questions & Feedback diff --git a/package.json b/package.json index 7dc5993a..d838bf2d 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,13 @@ ], "default": {}, "description": "For each item in this object, replace key with value in the remote url." + }, + "sourcegraph.defaultRemote": { + "type": [ + "string" + ], + "default": null, + "description": "The default git remote to use. If null, will use the first remote returned from `git remote`." } } } diff --git a/src/config.ts b/src/config.ts index 47e123be..8e13739c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,3 +18,8 @@ export function getRemoteUrlReplacements(): Record { .get>('remoteUrlReplacements')! return replacements } + +export function getDefaultRemote(): string | undefined { + // has default value + return vscode.workspace.getConfiguration('sourcegraph').get('defaultRemote') +} diff --git a/src/git.ts b/src/git.ts index 3118764c..5901b24d 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,7 +1,7 @@ import execa from 'execa' import * as path from 'path' import { log } from './log' -import { getRemoteUrlReplacements } from './config' +import { getDefaultRemote, getRemoteUrlReplacements } from './config' /** * Returns the names of all git remotes, e.g. ["origin", "foobar"] @@ -32,14 +32,21 @@ async function gitRemoteURL(repoDir: string, remoteName: string): Promise { - const remotes = await gitRemotes(repoDir) - if (remotes.length === 0) { - throw new Error('no configured git remotes') - } - if (remotes.length > 1) { - log.appendLine(`using first git remote: ${remotes[0]}`) + let remote = getDefaultRemote() + + // if there is no default remote configured, retrieve remote to use via 'git remote' command + if (remote === undefined) { + const remotes = await gitRemotes(repoDir) + if (remotes.length === 0) { + throw new Error('no configured git remotes') + } + if (remotes.length > 1) { + log.appendLine(`using first git remote: ${remotes[0]}`) + } + remote = remotes[0] } - return await gitRemoteURL(repoDir, remotes[0]) + + return await gitRemoteURL(repoDir, remote) } /**