From 0c6719ba7d1b9c99c431cbda0cc69520994affa8 Mon Sep 17 00:00:00 2001 From: Matthew Bentley Date: Wed, 24 Jul 2019 11:34:48 -0700 Subject: [PATCH 1/3] feat: add options to better support self-hosted sourcegraph This adds two options: sourcegraph.ignoreRemoteHostname: strips the hostname from a git-based remote. For example, `git@company.com:repo` becomes just `repo` if this is true sourcegraph.remoteUrlPrepend: a mapping from string -> string that allows simple remapping of remote names. If the remote hostname (user@host) matches the key, the value is prepended to the remote. For example, if this is `{"git@secondary.company.com": "two/"}, the repo `git@secondary.company.com:repo` becomes `two/repo` --- README.md | 2 ++ package.json | 16 +++++++++++++++- src/config.ts | 12 ++++++++++++ src/git.ts | 15 ++++++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 24664de4..73b7253f 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ Keyboard Shortcuts: This extension contributes the following settings: - `sourcegraph.url`: The Sourcegraph instance to use. Specify your on-premises Sourcegraph instance here, if applicable. +- `sourcegraph.ignoreRemoteHostname`: Strip the hostname from remote names for git-based repos, useful for self-hosted sourcegraph. For example, if this is true git@git.company.com:repo will become just repo when opening a file. Defaults to `false`. +- `sourcegraph.remoteUrlPrepend`: Prepend to the remote Url based on the hostname. Takes an object of string->string, if the remote url matches the key, the value is prepended to the remote url. Requires `ignoreRemoteHostname` to be `true`. For example, if the remote is `git@othergit.company.com:repo`, and this setting is `{"git@othergit.company.com": "othergit/"}`, the repo will become `othergit/repo`. Defaults to an empty object ## Questions & Feedback diff --git a/package.json b/package.json index 55a8d9a2..1e2efdd5 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "url": "https://github.com/sourcegraph/sourcegraph-vscode.git" }, "engines": { - "vscode": "^1.11.0" + "vscode": "^1.12.0" }, "categories": [ "Other" @@ -56,6 +56,20 @@ ], "default": "https://sourcegraph.com", "description": "The base URL of the Sourcegraph instance to use." + }, + "sourcegraph.ignoreRemoteHostname": { + "type": [ + "boolean" + ], + "default": false, + "description": "Strip the hostname from remote names for git-based repos, useful for self-hosted sourcegraph. For example, if this is true git@git.company.com:repo will become just repo when opening a file." + }, + "sourcegraph.remoteUrlPrepend": { + "type": [ + "object" + ], + "default": {}, + "description": "Prepend to the remote Url based on the hostname. Takes a map of string->string, if the remote url matches the key, the value is prepended to the remote Url. Requires ignoreRemoteHostname to be true. For example, if the remote is git@othergit.company.com:repo, and this setting is {\"git@othergit.company.com\": \"othergit/\"}, the repo will become othergit/repo." } } } diff --git a/src/config.ts b/src/config.ts index 5e2bd58b..dbddaa09 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,3 +7,15 @@ export function getSourcegraphUrl(): string { } return url } + +export function getIgnoreRemoteHostname(): boolean { + const ignoreRemoteHostname = vscode.workspace.getConfiguration('sourcegraph').get('ignoreRemoteHostname')! // has default value + return ignoreRemoteHostname +} + +export function getRemoteUrlPrepend(): Record { + const remoteUrlPrepend = vscode.workspace + .getConfiguration('sourcegraph') + .get>('remoteUrlPrepend')! // had default value + return remoteUrlPrepend +} diff --git a/src/git.ts b/src/git.ts index 0e0d2db3..b1be09e1 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,6 +1,7 @@ import execa from 'execa' import * as path from 'path' import { log } from './log' +import { getIgnoreRemoteHostname, getRemoteUrlPrepend } from './config' /** * Returns the names of all git remotes, e.g. ["origin", "foobar"] @@ -16,7 +17,19 @@ async function gitRemotes(repoDir: string): Promise { */ async function gitRemoteURL(repoDir: string, remoteName: string): Promise { const { stdout } = await execa('git', ['remote', 'get-url', remoteName], { cwd: repoDir }) - return stdout + if (!getIgnoreRemoteHostname()) { + return stdout + } + const split = stdout.split(':') + if (split.length == 1) { + return split[0] + } + const remoteHostname = split[0] + const prepend = getRemoteUrlPrepend()[remoteHostname] + if (prepend) { + return `${prepend}${split[1]}` + } + return split[1] } /** From 6cb0953d92bed7f989eaa53ec246f834bb847867 Mon Sep 17 00:00:00 2001 From: Matthew Bentley Date: Wed, 24 Jul 2019 15:41:19 -0700 Subject: [PATCH 2/3] feat: simplify remote url remap config Also update vscode engine version to work with remote development --- README.md | 3 +-- package.json | 15 ++++----------- src/config.ts | 13 ++++--------- src/git.ts | 21 ++++++++------------- 4 files changed, 17 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 73b7253f..767f84e4 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,7 @@ Keyboard Shortcuts: This extension contributes the following settings: - `sourcegraph.url`: The Sourcegraph instance to use. Specify your on-premises Sourcegraph instance here, if applicable. -- `sourcegraph.ignoreRemoteHostname`: Strip the hostname from remote names for git-based repos, useful for self-hosted sourcegraph. For example, if this is true git@git.company.com:repo will become just repo when opening a file. Defaults to `false`. -- `sourcegraph.remoteUrlPrepend`: Prepend to the remote Url based on the hostname. Takes an object of string->string, if the remote url matches the key, the value is prepended to the remote url. Requires `ignoreRemoteHostname` to be `true`. For example, if the remote is `git@othergit.company.com:repo`, and this setting is `{"git@othergit.company.com": "othergit/"}`, the repo will become `othergit/repo`. Defaults to an empty object +- `sourcegraph.remoteUrlReplacements`: Object, where each `key` is replaced by `value` in the remote url. ## Questions & Feedback diff --git a/package.json b/package.json index 1e2efdd5..fec0d6dc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "sourcegraph", "displayName": "Sourcegraph", "description": "Sourcegraph for VS Code", - "version": "1.0.12", + "version": "1.0.16", "publisher": "sourcegraph", "license": "MIT", "icon": "images/logo.png", @@ -11,7 +11,7 @@ "url": "https://github.com/sourcegraph/sourcegraph-vscode.git" }, "engines": { - "vscode": "^1.12.0" + "vscode": "^1.36.0" }, "categories": [ "Other" @@ -57,19 +57,12 @@ "default": "https://sourcegraph.com", "description": "The base URL of the Sourcegraph instance to use." }, - "sourcegraph.ignoreRemoteHostname": { - "type": [ - "boolean" - ], - "default": false, - "description": "Strip the hostname from remote names for git-based repos, useful for self-hosted sourcegraph. For example, if this is true git@git.company.com:repo will become just repo when opening a file." - }, - "sourcegraph.remoteUrlPrepend": { + "sourcegraph.remoteUrlReplacements": { "type": [ "object" ], "default": {}, - "description": "Prepend to the remote Url based on the hostname. Takes a map of string->string, if the remote url matches the key, the value is prepended to the remote Url. Requires ignoreRemoteHostname to be true. For example, if the remote is git@othergit.company.com:repo, and this setting is {\"git@othergit.company.com\": \"othergit/\"}, the repo will become othergit/repo." + "description": "For each item in this object, replace key with value in the remote url." } } } diff --git a/src/config.ts b/src/config.ts index dbddaa09..9956d15e 100644 --- a/src/config.ts +++ b/src/config.ts @@ -8,14 +8,9 @@ export function getSourcegraphUrl(): string { return url } -export function getIgnoreRemoteHostname(): boolean { - const ignoreRemoteHostname = vscode.workspace.getConfiguration('sourcegraph').get('ignoreRemoteHostname')! // has default value - return ignoreRemoteHostname -} - -export function getRemoteUrlPrepend(): Record { - const remoteUrlPrepend = vscode.workspace +export function getRemoteUrlReplacements(): Record { + const replacements = vscode.workspace .getConfiguration('sourcegraph') - .get>('remoteUrlPrepend')! // had default value - return remoteUrlPrepend + .get>('remoteUrlReplacements')! // has default value + return replacements } diff --git a/src/git.ts b/src/git.ts index b1be09e1..d2b2e8f9 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 { getIgnoreRemoteHostname, getRemoteUrlPrepend } from './config' +import { getRemoteUrlReplacements } from './config' /** * Returns the names of all git remotes, e.g. ["origin", "foobar"] @@ -17,19 +17,14 @@ async function gitRemotes(repoDir: string): Promise { */ async function gitRemoteURL(repoDir: string, remoteName: string): Promise { const { stdout } = await execa('git', ['remote', 'get-url', remoteName], { cwd: repoDir }) - if (!getIgnoreRemoteHostname()) { - return stdout - } - const split = stdout.split(':') - if (split.length == 1) { - return split[0] - } - const remoteHostname = split[0] - const prepend = getRemoteUrlPrepend()[remoteHostname] - if (prepend) { - return `${prepend}${split[1]}` + const replacementsList = getRemoteUrlReplacements() + var remote = stdout + + for (let r in replacementsList) { + remote = remote.replace(r, replacementsList[r]) } - return split[1] + + return remote } /** From 4059b8796ed90a463dd814f5e9908db5ac4a34f2 Mon Sep 17 00:00:00 2001 From: Matthew Bentley Date: Thu, 15 Aug 2019 17:39:18 -0700 Subject: [PATCH 3/3] fix: fixup types for getRemoteURL --- package.json | 2 +- src/config.ts | 4 +++- src/git.ts | 11 ++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 7ced1cdc..5d83f194 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "url": "https://github.com/sourcegraph/sourcegraph-vscode.git" }, "engines": { - "vscode": "^1.36.0" + "vscode": "^1.37.0" }, "categories": [ "Other" diff --git a/src/config.ts b/src/config.ts index 525c45b2..47e123be 100644 --- a/src/config.ts +++ b/src/config.ts @@ -11,8 +11,10 @@ export function getSourcegraphUrl(): string { } export function getRemoteUrlReplacements(): Record { + // has default value + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const replacements = vscode.workspace .getConfiguration('sourcegraph') - .get>('remoteUrlReplacements')! // has default value + .get>('remoteUrlReplacements')! return replacements } diff --git a/src/git.ts b/src/git.ts index d2b2e8f9..3118764c 100644 --- a/src/git.ts +++ b/src/git.ts @@ -16,15 +16,16 @@ async function gitRemotes(repoDir: string): Promise { * e.g. `origin` -> `git@github.com:foo/bar` */ async function gitRemoteURL(repoDir: string, remoteName: string): Promise { - const { stdout } = await execa('git', ['remote', 'get-url', remoteName], { cwd: repoDir }) + let { stdout } = await execa('git', ['remote', 'get-url', remoteName], { cwd: repoDir }) const replacementsList = getRemoteUrlReplacements() - var remote = stdout - for (let r in replacementsList) { - remote = remote.replace(r, replacementsList[r]) + for (const r in replacementsList) { + if (typeof r === 'string') { + stdout = stdout.replace(r, replacementsList[r]) + } } - return remote + return stdout } /**