From 6ff48609e4c1bd1536c3a2d42ce14f2fd9cd71f2 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sun, 27 Sep 2020 17:29:48 -0600 Subject: [PATCH 1/7] feat: added default remote setting --- .vscode/tasks.json | 5 +++++ package.json | 7 +++++++ src/config.ts | 7 +++++++ src/git.ts | 26 +++++++++++++++++--------- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index e7b2b0c8..8855bca4 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,5 +9,10 @@ "reveal": "never", }, }, + { + "type": "npm", + "label": "build", + "script": "build" + } ], } diff --git a/package.json b/package.json index 605afff5..549eb01b 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." } } } diff --git a/src/config.ts b/src/config.ts index 47e123be..3cf8a816 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,3 +18,10 @@ 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..b08e57eb 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,19 +32,27 @@ 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 currentRemotes = await gitRemotes(repoDir) + if (currentRemotes.length === 0) { + throw new Error('no configured git remotes') + } + if (currentRemotes.length > 1) { + log.appendLine(`using first git remote: ${currentRemotes[0]}`) + } + remote = currentRemotes[0] } - return await gitRemoteURL(repoDir, remotes[0]) + + return await gitRemoteURL(repoDir, remote) } /** * Returns the repository root directory for any directory within the - * repository. + * repositorcurrentRemotesy. */ async function gitRootDir(repoDir: string): Promise { const { stdout } = await execa('git', ['rev-parse', '--show-toplevel'], { cwd: repoDir }) From 2e49aff38f9edbf08eb7ed3939cb7ee08c0a0b2c Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sun, 27 Sep 2020 17:33:05 -0600 Subject: [PATCH 2/7] fix: formatting and docs --- package.json | 2 +- src/git.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 549eb01b..bd1e79ac 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "string" ], "default": null, - "description": "The default git remote to use." + "description": "The default git remote to use. If null, will use the first remote returned from `git remote`" } } } diff --git a/src/git.ts b/src/git.ts index b08e57eb..543655c9 100644 --- a/src/git.ts +++ b/src/git.ts @@ -32,7 +32,6 @@ async function gitRemoteURL(repoDir: string, remoteName: string): Promise { - let remote = getDefaultRemote(); // if there is no default remote configured, retrieve remote to use via 'git remote' command @@ -52,7 +51,7 @@ async function gitDefaultRemoteURL(repoDir: string): Promise { /** * Returns the repository root directory for any directory within the - * repositorcurrentRemotesy. + * repository. */ async function gitRootDir(repoDir: string): Promise { const { stdout } = await execa('git', ['rev-parse', '--show-toplevel'], { cwd: repoDir }) From 37074f4cd20c5ad18ee5804927d45e3aaddd7163 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sun, 27 Sep 2020 17:34:02 -0600 Subject: [PATCH 3/7] fix: formatting --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bd1e79ac..8eb511e0 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "string" ], "default": null, - "description": "The default git remote to use. If null, will use the first remote returned from `git remote`" + "description": "The default git remote to use. If null, will use the first remote returned from `git remote`." } } } From 46ff5695ce761245ed89ee3a38becf97d4342721 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sun, 27 Sep 2020 17:39:25 -0600 Subject: [PATCH 4/7] docs: added new setting to documentation --- README.md | 1 + 1 file changed, 1 insertion(+) 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 From 90cacfdfbcdd42f7aa49c277fb8a2d8aab1d58ff Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sun, 27 Sep 2020 17:43:51 -0600 Subject: [PATCH 5/7] fix: addressed build error --- src/git.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index 543655c9..009618f8 100644 --- a/src/git.ts +++ b/src/git.ts @@ -35,7 +35,7 @@ async function gitDefaultRemoteURL(repoDir: string): Promise { let remote = getDefaultRemote(); // if there is no default remote configured, retrieve remote to use via 'git remote' command - if (remote == undefined) { + if (remote === undefined) { const currentRemotes = await gitRemotes(repoDir) if (currentRemotes.length === 0) { throw new Error('no configured git remotes') From 4178bc0b27a15d12868d82a26f9fc802138936e0 Mon Sep 17 00:00:00 2001 From: Matthew Nitschke Date: Sun, 27 Sep 2020 17:49:42 -0600 Subject: [PATCH 6/7] fix: ran formatter --- .vscode/tasks.json | 4 ++-- src/config.ts | 4 +--- src/git.ts | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 8855bca4..74ac810e 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -12,7 +12,7 @@ { "type": "npm", "label": "build", - "script": "build" - } + "script": "build", + }, ], } diff --git a/src/config.ts b/src/config.ts index 3cf8a816..8e13739c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -21,7 +21,5 @@ export function getRemoteUrlReplacements(): Record { export function getDefaultRemote(): string | undefined { // has default value - return vscode.workspace - .getConfiguration('sourcegraph') - .get('defaultRemote') + return vscode.workspace.getConfiguration('sourcegraph').get('defaultRemote') } diff --git a/src/git.ts b/src/git.ts index 009618f8..a63878df 100644 --- a/src/git.ts +++ b/src/git.ts @@ -32,7 +32,7 @@ async function gitRemoteURL(repoDir: string, remoteName: string): Promise { - let remote = getDefaultRemote(); + let remote = getDefaultRemote() // if there is no default remote configured, retrieve remote to use via 'git remote' command if (remote === undefined) { From 51f8fd97b1e80e828a50d263200c8eb206452323 Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Thu, 12 Nov 2020 15:00:08 -0800 Subject: [PATCH 7/7] Update src/git.ts --- src/git.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/git.ts b/src/git.ts index a63878df..5901b24d 100644 --- a/src/git.ts +++ b/src/git.ts @@ -36,14 +36,14 @@ async function gitDefaultRemoteURL(repoDir: string): Promise { // if there is no default remote configured, retrieve remote to use via 'git remote' command if (remote === undefined) { - const currentRemotes = await gitRemotes(repoDir) - if (currentRemotes.length === 0) { + const remotes = await gitRemotes(repoDir) + if (remotes.length === 0) { throw new Error('no configured git remotes') } - if (currentRemotes.length > 1) { - log.appendLine(`using first git remote: ${currentRemotes[0]}`) + if (remotes.length > 1) { + log.appendLine(`using first git remote: ${remotes[0]}`) } - remote = currentRemotes[0] + remote = remotes[0] } return await gitRemoteURL(repoDir, remote)