Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@
"reveal": "never",
},
},
{
"type": "npm",
"label": "build",
"script": "build",
},
Comment on lines +12 to +16

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running the Launch Setting task resulted in an error. Adding this task fixed the issue

],
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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`."
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ export function getRemoteUrlReplacements(): Record<string, string> {
.get<Record<string, string>>('remoteUrlReplacements')!
return replacements
}

export function getDefaultRemote(): string | undefined {
// has default value
return vscode.workspace.getConfiguration('sourcegraph').get<string>('defaultRemote')
}
23 changes: 15 additions & 8 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -32,14 +32,21 @@ async function gitRemoteURL(repoDir: string, remoteName: string): Promise<string
* Returns the remote URL of the first Git remote found.
*/
async function gitDefaultRemoteURL(repoDir: string): Promise<string> {
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)
}

/**
Expand Down