Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
feat: add option to remap URLs before opening (#24)
Browse files Browse the repository at this point in the history
This adds two options:
sourcegraph.ignoreRemoteHostname: strips the hostname from a git-based
remote. For example, `[email protected]: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 `{"[email protected]": "two/"}, the repo
`[email protected]:repo` becomes `two/repo`

Fixes #8
  • Loading branch information
Matthew Bentley authored and felixfbecker committed Nov 6, 2019
1 parent a9f62a9 commit e2ee51d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -11,7 +11,7 @@
"url": "https://github.com/sourcegraph/sourcegraph-vscode.git"
},
"engines": {
"vscode": "^1.11.0"
"vscode": "^1.37.0"
},
"categories": [
"Other"
Expand Down Expand Up @@ -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."
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ export function getSourcegraphUrl(): string {
}
return url
}

export function getRemoteUrlReplacements(): Record<string, string> {
// has default value
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const replacements = vscode.workspace
.getConfiguration('sourcegraph')
.get<Record<string, string>>('remoteUrlReplacements')!
return replacements
}
11 changes: 10 additions & 1 deletion src/git.ts
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -15,7 +16,15 @@ async function gitRemotes(repoDir: string): Promise<string[]> {
* e.g. `origin` -> `[email protected]:foo/bar`
*/
async function gitRemoteURL(repoDir: string, remoteName: string): Promise<string> {
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
}

Expand Down

0 comments on commit e2ee51d

Please sign in to comment.