Repository navigation
Expand file tree
/
Copy pathcommit-list.ts
More file actions
195 lines (173 loc) · 6.62 KB
/
Copy pathcommit-list.ts
File metadata and controls
195 lines (173 loc) · 6.62 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @file GitHub Commit List View
* @author netcon
*/
import * as vscode from 'vscode';
import router from '@/router';
import { Barrier } from '@/helpers/async';
import { Repository } from '@/repository';
import * as queryString from 'query-string';
import { relativeTimeTo } from '@/helpers/date';
import adapterManager from '@/adapters/manager';
import * as adapterTypes from '@/adapters/types';
import { getChangedFileDiffCommand, getCommitChangedFiles } from '@/changes/files';
import { GitHub1sSourceControlDecorationProvider } from '@/providers/decorations/source-control';
export const getCommitTreeItemDescription = (commit: adapterTypes.Commit): string => {
const shortCommitSha = commit.sha.slice(0, 7);
const relativeTimeStr = commit.createTime ? relativeTimeTo(commit.createTime) : null;
return [shortCommitSha, commit.author, relativeTimeStr].filter(Boolean).join(', ');
};
export interface CommitTreeItem extends vscode.TreeItem {
commit: adapterTypes.Commit;
}
export class CommitTreeDataProvider implements vscode.TreeDataProvider<vscode.TreeItem> {
public static viewType = 'github1s.views.commitList';
protected _forceUpdate = false;
protected _loadingBarrier: Barrier | null = null;
protected _onDidChangeTreeData = new vscode.EventEmitter<void>();
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
protected loadMoreCommitItem: vscode.TreeItem = {
label: 'Load more',
tooltip: 'Load more commits',
command: {
title: 'Load more commits',
command: 'github1s.commands.loadMoreCommits',
tooltip: 'Load more commits',
},
};
protected createLoadMoreChangedFilesItem = (commitSha: string): vscode.TreeItem => ({
label: 'Load more',
tooltip: 'Load more changed files',
command: {
title: 'Load more changed files',
command: 'github1s.commands.loadMoreCommitChangedFiles',
tooltip: 'Load more changed files',
arguments: [commitSha],
},
});
async resolveFilePath() {
return '';
}
public updateTree(forceUpdate = true) {
this._forceUpdate = forceUpdate;
this._onDidChangeTreeData.fire();
}
public async loadMoreCommits() {
if (!this._loadingBarrier || this._loadingBarrier.isOpen()) {
this._loadingBarrier = new Barrier(5000);
this.updateTree(false);
const scheme = adapterManager.getCurrentScheme();
const { repo, ref } = await router.getState();
const repository = Repository.getInstance(scheme, repo);
await repository.loadMoreCommits(ref, await this.resolveFilePath());
this._loadingBarrier.open();
}
}
public async loadMoreChangedFiles(commitSha: string) {
if (!this._loadingBarrier || this._loadingBarrier.isOpen()) {
this._loadingBarrier = new Barrier(5000);
this.updateTree(false);
const scheme = adapterManager.getCurrentScheme();
const { repo } = await router.getState();
await Repository.getInstance(scheme, repo).loadMoreCommitChangedFiles(commitSha);
this._loadingBarrier.open();
}
}
async getCommitItems(): Promise<vscode.TreeItem[]> {
this._loadingBarrier && (await this._loadingBarrier.wait());
const filePath = await this.resolveFilePath();
const currentAdapter = adapterManager.getCurrentAdapter();
const { repo, ref } = await router.getState();
const repository = Repository.getInstance(currentAdapter.scheme, repo);
const repositoryCommits = await repository.getCommitList(ref, filePath, this._forceUpdate);
const commitTreeItems = repositoryCommits.map((commit) => {
const label = commit.message.split(/[\r\n]/)[0];
const description = getCommitTreeItemDescription(commit);
const tooltip = `${commit.message}\n(${description})`;
const iconPath = vscode.Uri.parse(commit.avatarUrl || '');
const contextValue = 'github1s:viewItems:commitListItem';
return {
commit,
label,
iconPath,
description,
tooltip,
contextValue,
resourceUri: vscode.Uri.parse('').with({
scheme: GitHub1sSourceControlDecorationProvider.commitSchema,
query: queryString.stringify({ sha: commit.sha }),
}),
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
};
});
this._forceUpdate = false;
const hasMore = await repository.hasMoreCommits(ref, filePath);
return hasMore ? [...commitTreeItems, this.loadMoreCommitItem] : commitTreeItems;
}
async getCommitFileItems(commit: adapterTypes.Commit): Promise<vscode.TreeItem[]> {
const changedFiles = await getCommitChangedFiles(commit);
const changedFileItems = changedFiles.map((changedFile) => {
const filePath = changedFile.headFileUri.path;
const id = `${commit.sha} ${filePath}`;
const command = getChangedFileDiffCommand(changedFile);
return {
id,
command,
description: true,
resourceUri: changedFile.headFileUri.with({
query: queryString.stringify({ changeStatus: changedFile.status }),
}),
collapsibleState: vscode.TreeItemCollapsibleState.None,
};
});
const scheme = adapterManager.getCurrentScheme();
const { repo } = await router.getState();
const repository = Repository.getInstance(scheme, repo);
const hasMore = await repository.hasMoreCommitChangedFiles(commit.sha);
const loadMoreChangedFilesItem = this.createLoadMoreChangedFilesItem(commit.sha);
return hasMore ? [...changedFileItems, loadMoreChangedFilesItem] : changedFileItems;
}
getTreeItem(element: vscode.TreeItem): vscode.TreeItem | Thenable<vscode.TreeItem> {
return element;
}
getChildren(element?: vscode.TreeItem): vscode.ProviderResult<vscode.TreeItem[]> {
if (!element) {
return this.getCommitItems();
}
const commit = (element as CommitTreeItem)?.commit;
return commit ? this.getCommitFileItems(commit) : [];
}
}
export class FileHistoryTreeDataProvider extends CommitTreeDataProvider {
public static viewType = 'github1s.views.fileHistory';
protected loadMoreCommitItem: vscode.TreeItem = {
label: 'Load more',
tooltip: 'Load more commits',
command: {
title: 'Load more commits',
command: 'github1s.commands.loadMoreFileHistoryCommits',
tooltip: 'Load more commits',
},
};
protected createLoadMoreChangedFilesItem = (commitSha: string): vscode.TreeItem => ({
label: 'Load more',
tooltip: 'Load more changed files',
command: {
title: 'Load more changed files',
command: 'github1s.commands.loadMoreFileHistoryCommitChangedFiles',
tooltip: 'Load more changed files',
arguments: [commitSha],
},
});
async resolveFilePath() {
const activeDocumentUri = vscode.window.activeTextEditor?.document?.uri;
const currentScheme = adapterManager.getCurrentScheme();
return activeDocumentUri?.scheme === currentScheme ? activeDocumentUri.path.slice(1) : '';
}
async getCommitItems() {
if (!(await this.resolveFilePath())) {
return [];
}
return super.getCommitItems();
}
}