|
| 1 | +import { SourcegraphTreeDataProvider } from '../file-system/SourcegraphTreeDataProvider' |
| 2 | +import { log } from '../log' |
| 3 | +import { GitReference, gitReferencesQuery } from '../queries/gitReferencesQuery' |
| 4 | +import { openSourcegraphUriCommand } from './openSourcegraphUriCommand' |
| 5 | +import { SourcegraphQuickPick } from './SourcegraphQuickPick' |
| 6 | + |
| 7 | +export async function switchGitRevisionCommand(tree: SourcegraphTreeDataProvider): Promise<void> { |
| 8 | + const quick = new SourcegraphQuickPick(tree.fs) |
| 9 | + quick.pick.title = 'Search for a git branch, git tag or a git commit' |
| 10 | + const activeTextDocument = tree.activeTextDocument() |
| 11 | + if (!activeTextDocument || !activeTextDocument.path) { |
| 12 | + return |
| 13 | + } |
| 14 | + const activeTextDocumentPath = activeTextDocument.path |
| 15 | + const metadata = await tree.fs.repositoryMetadata(activeTextDocument.repositoryName) |
| 16 | + quick.onDidChangeValue(async query => { |
| 17 | + quick.pick.busy = true |
| 18 | + log.appendLine(`gitReferences: ${query.text}`) |
| 19 | + const references = await gitReferencesQuery( |
| 20 | + { query: query.text, repositoryId: metadata?.id || '' }, |
| 21 | + query.token |
| 22 | + ) |
| 23 | + log.appendLine(`gitReferences: ${query.text} ${JSON.stringify(references)}`) |
| 24 | + quick.pick.busy = false |
| 25 | + quick.pick.items = references.map(reference => ({ |
| 26 | + label: gitReferenceTag(reference) + reference.displayName, |
| 27 | + uri: `sourcegraph://${activeTextDocument.host}${reference.url}/-/blob/${activeTextDocumentPath}`, |
| 28 | + })) |
| 29 | + }) |
| 30 | + const uri = await quick.showQuickPickAndGetUserInput() |
| 31 | + await openSourcegraphUriCommand(tree.fs, uri) |
| 32 | +} |
| 33 | + |
| 34 | +function gitReferenceTag(reference: GitReference): string { |
| 35 | + switch (reference.type) { |
| 36 | + case 'GIT_TAG': |
| 37 | + return ' $(tag)' |
| 38 | + case 'GIT_BRANCH': |
| 39 | + return ' $(git-branch)' |
| 40 | + case 'GIT_COMMIT': |
| 41 | + return ' $(git-commit)' |
| 42 | + default: |
| 43 | + return '' |
| 44 | + } |
| 45 | +} |
0 commit comments