From e2ee51d1ab637acefec16a9a4acf875757dd9fe0 Mon Sep 17 00:00:00 2001 From: Matthew Bentley Date: Wed, 6 Nov 2019 08:24:24 -0800 Subject: [PATCH] feat: add option to remap URLs before opening (#24) 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` Fixes #8 --- README.md | 1 + package.json | 11 +++++++++-- src/config.ts | 9 +++++++++ src/git.ts | 11 ++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 24664de4..767f84e4 100644 --- a/README.md +++ b/README.md @@ -25,6 +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.remoteUrlReplacements`: Object, where each `key` is replaced by `value` in the remote url. ## Questions & Feedback diff --git a/package.json b/package.json index 25679d89..e2c6986b 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.11.0" + "vscode": "^1.37.0" }, "categories": [ "Other" @@ -56,6 +56,13 @@ ], "default": "https://sourcegraph.com", "description": "The base URL of the Sourcegraph instance to use." + }, + "sourcegraph.remoteUrlReplacements": { + "type": [ + "object" + ], + "default": {}, + "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 0ae328d6..47e123be 100644 --- a/src/config.ts +++ b/src/config.ts @@ -9,3 +9,12 @@ export function getSourcegraphUrl(): string { } return url } + +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')! + return replacements +} diff --git a/src/git.ts b/src/git.ts index 0e0d2db3..3118764c 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 { getRemoteUrlReplacements } from './config' /** * Returns the names of all git remotes, e.g. ["origin", "foobar"] @@ -15,7 +16,15 @@ 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() + + for (const r in replacementsList) { + if (typeof r === 'string') { + stdout = stdout.replace(r, replacementsList[r]) + } + } + return stdout }