forked from conwnet/github1s
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.ts
More file actions
146 lines (129 loc) · 5.5 KB
/
Copy pathcommit.ts
File metadata and controls
146 lines (129 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* @file GitHub1s Commit Related Commands
* @author netcon
*/
import * as vscode from 'vscode';
import router from '@/router';
import { CommitTreeItem, getCommitTreeItemDescription } from '@/views/commit-list';
import { commitTreeDataProvider, fileHistoryTreeDataProvider } from '@/views';
import { adapterManager } from '@/adapters';
import { Repository } from '@/repository';
export const checkCommitExists = async (repo: string, commitSha: string) => {
const adapter = adapterManager.getCurrentAdapter();
const dataSoruce = await adapter.resolveDataSource();
try {
return !!(await dataSoruce.provideCommit(repo, commitSha));
} catch (error) {
const errorMessage =
(error as any)?.response?.status === 404
? `No commit found for commitSha: ${commitSha}`
: error?.response?.data?.message;
vscode.window.showErrorMessage(errorMessage || `Get commit ${commitSha}} error`);
return false;
}
};
const commandSwitchToCommit = async (commitItemOrSha?: string | CommitTreeItem) => {
let commitSha: string | undefined = commitItemOrSha
? typeof commitItemOrSha === 'string'
? commitItemOrSha
: commitItemOrSha.commit.sha
: '';
const adapter = adapterManager.getCurrentAdapter();
const { repo } = await router.getState();
const repository = Repository.getInstance(adapter.scheme, repo);
// if the a commitSha isn't provided, use quickInput
if (!commitSha) {
// manual input a commit sha
const inputCommitShaItem: vscode.QuickPickItem = {
label: '$(git-commit) Manual input the commit sha',
alwaysShow: true,
};
// use the commit list as the candidates
const commits = await repository.getCommitList();
const commitItems: vscode.QuickPickItem[] = commits.map((commit) => ({
commitSha: commit.sha,
label: commit.message,
description: getCommitTreeItemDescription(commit),
}));
const quickPick = vscode.window.createQuickPick<vscode.QuickPickItem>();
quickPick.matchOnDescription = true;
quickPick.items = [inputCommitShaItem, ...commitItems];
quickPick.show();
const choice = (await new Promise<vscode.QuickPickItem | undefined>((resolve) =>
quickPick.onDidAccept(() => resolve(quickPick.activeItems[0]))
)) as vscode.QuickPickItem & { commitSha?: string };
quickPick.hide();
// select nothing
if (!choice) {
return;
}
// select `manual input the commit sha`
if (choice === inputCommitShaItem) {
commitSha = await vscode.window.showInputBox({
placeHolder: 'Please input the commit sha',
});
} else {
// select a commit sha
commitSha = choice.commitSha;
}
}
const routerParser = await router.resolveParser();
if (await checkCommitExists(repo, commitSha!)) {
router.replace(await routerParser.buildCommitPath(repo, commitSha!));
}
};
// this command is used in `source control commit list view`
const commandOpenCommitOnOfficialPage = async (commitItemOrSha?: string | CommitTreeItem) => {
const commitSha = commitItemOrSha
? typeof commitItemOrSha === 'string'
? commitItemOrSha
: commitItemOrSha.commit.sha
: '';
if (commitSha) {
const { repo } = await router.getState();
const routerParser = await router.resolveParser();
const commitPath = await routerParser.buildCommitPath(repo, commitSha);
const commitLink = await routerParser.buildExternalLink(commitPath);
return vscode.commands.executeCommand('vscode.open', vscode.Uri.parse(commitLink));
}
};
const commandRefreshCommitList = (forceUpdate = true) => {
return commitTreeDataProvider.updateTree(forceUpdate);
};
const commandLoadMoreCommits = async () => {
return commitTreeDataProvider.loadMoreCommits();
};
const commandLoadMoreCommitChangedFiles = async (commitSha: string) => {
return commitTreeDataProvider.loadMoreChangedFiles(commitSha);
};
const commandRefreshFileHistoryCommitList = (forceUpdate = true) => {
return fileHistoryTreeDataProvider.updateTree(forceUpdate);
};
const commandLoadMoreFileHistoryCommits = async () => {
return fileHistoryTreeDataProvider.loadMoreCommits();
};
const commandLoadMoreFileHistoryCommitChangedFiles = async (commitSha: string) => {
return fileHistoryTreeDataProvider.loadMoreChangedFiles(commitSha);
};
export const registerCommitCommands = (context: vscode.ExtensionContext) => {
return context.subscriptions.push(
vscode.commands.registerCommand('github1s.commands.refreshCommitList', commandRefreshCommitList),
vscode.commands.registerCommand('github1s.commands.searchCommit', commandSwitchToCommit),
vscode.commands.registerCommand('github1s.commands.switchToCommit', commandSwitchToCommit),
vscode.commands.registerCommand('github1s.commands.openCommitOnGitHub', commandOpenCommitOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openCommitOnGitLab', commandOpenCommitOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openCommitOnBitbucket', commandOpenCommitOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.openCommitOnOfficialPage', commandOpenCommitOnOfficialPage),
vscode.commands.registerCommand('github1s.commands.loadMoreCommits', commandLoadMoreCommits),
vscode.commands.registerCommand('github1s.commands.loadMoreCommitChangedFiles', commandLoadMoreCommitChangedFiles),
vscode.commands.registerCommand('github1s.commands.loadMoreFileHistoryCommits', commandLoadMoreFileHistoryCommits),
vscode.commands.registerCommand(
'github1s.commands.loadMoreFileHistoryCommitChangedFiles',
commandLoadMoreFileHistoryCommitChangedFiles
),
vscode.commands.registerCommand(
'github1s.commands.refreshFileHistoryCommitList',
commandRefreshFileHistoryCommitList
)
);
};