|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
6 | 6 | import * as vscode from 'vscode';
|
7 |
| -import { API as GitAPI, RefType } from './typings/git'; |
| 7 | +import { API as GitAPI, RefType, Repository } from './typings/git'; |
8 | 8 | import { publishRepository } from './publish';
|
9 | 9 | import { DisposableStore, getRepositoryFromUrl } from './util';
|
10 | 10 | import { LinkContext, getCommitLink, getLink, getVscodeDevHost } from './links';
|
@@ -34,6 +34,29 @@ async function openVscodeDevLink(gitAPI: GitAPI): Promise<vscode.Uri | undefined
|
34 | 34 | }
|
35 | 35 | }
|
36 | 36 |
|
| 37 | +async function openOnGitHub(repository: Repository, commit: string): Promise<void> { |
| 38 | + // Get the unique remotes that contain the commit |
| 39 | + const branches = await repository.getBranches({ contains: commit, remote: true }); |
| 40 | + const remoteNames = new Set(branches.filter(b => b.type === RefType.RemoteHead && b.remote).map(b => b.remote!)); |
| 41 | + |
| 42 | + // GitHub remotes that contain the commit |
| 43 | + const remotes = repository.state.remotes |
| 44 | + .filter(r => remoteNames.has(r.name) && r.fetchUrl && getRepositoryFromUrl(r.fetchUrl)); |
| 45 | + |
| 46 | + if (remotes.length === 0) { |
| 47 | + vscode.window.showInformationMessage(vscode.l10n.t('No GitHub remotes found that contain this commit.')); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // upstream -> origin -> first |
| 52 | + const remote = remotes.find(r => r.name === 'upstream') |
| 53 | + ?? remotes.find(r => r.name === 'origin') |
| 54 | + ?? remotes[0]; |
| 55 | + |
| 56 | + const link = getCommitLink(remote.fetchUrl!, commit); |
| 57 | + vscode.env.openExternal(vscode.Uri.parse(link)); |
| 58 | +} |
| 59 | + |
37 | 60 | export function registerCommands(gitAPI: GitAPI): vscode.Disposable {
|
38 | 61 | const disposables = new DisposableStore();
|
39 | 62 |
|
@@ -72,26 +95,20 @@ export function registerCommands(gitAPI: GitAPI): vscode.Disposable {
|
72 | 95 | return;
|
73 | 96 | }
|
74 | 97 |
|
75 |
| - // Get the unique remotes that contain the commit |
76 |
| - const branches = await apiRepository.getBranches({ contains: historyItem.id, remote: true }); |
77 |
| - const remoteNames = new Set(branches.filter(b => b.type === RefType.RemoteHead && b.remote).map(b => b.remote!)); |
78 |
| - |
79 |
| - // GitHub remotes that contain the commit |
80 |
| - const remotes = apiRepository.state.remotes |
81 |
| - .filter(r => remoteNames.has(r.name) && r.fetchUrl && getRepositoryFromUrl(r.fetchUrl)); |
| 98 | + await openOnGitHub(apiRepository, historyItem.id); |
| 99 | + })); |
82 | 100 |
|
83 |
| - if (remotes.length === 0) { |
84 |
| - vscode.window.showInformationMessage(vscode.l10n.t('No GitHub remotes found that contain this commit.')); |
| 101 | + disposables.add(vscode.commands.registerCommand('github.timeline.openOnGitHub', async (item: vscode.TimelineItem, uri: vscode.Uri) => { |
| 102 | + if (!item.id || !uri) { |
85 | 103 | return;
|
86 | 104 | }
|
87 | 105 |
|
88 |
| - // upstream -> origin -> first |
89 |
| - const remote = remotes.find(r => r.name === 'upstream') |
90 |
| - ?? remotes.find(r => r.name === 'origin') |
91 |
| - ?? remotes[0]; |
| 106 | + const apiRepository = gitAPI.getRepository(uri); |
| 107 | + if (!apiRepository) { |
| 108 | + return; |
| 109 | + } |
92 | 110 |
|
93 |
| - const link = getCommitLink(remote.fetchUrl!, historyItem.id); |
94 |
| - vscode.env.openExternal(vscode.Uri.parse(link)); |
| 111 | + await openOnGitHub(apiRepository, item.id); |
95 | 112 | }));
|
96 | 113 |
|
97 | 114 | disposables.add(vscode.commands.registerCommand('github.openOnVscodeDev', async () => {
|
|
0 commit comments