-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
commitsView.ts
409 lines (356 loc) · 10.7 KB
/
commitsView.ts
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
'use strict';
import {
CancellationToken,
commands,
ConfigurationChangeEvent,
Disposable,
ProgressLocation,
TreeItem,
TreeItemCollapsibleState,
window,
} from 'vscode';
import { CommitsViewConfig, configuration, ViewFilesLayout, ViewShowBranchComparison } from '../configuration';
import { ContextKeys, GlyphChars, setContext } from '../constants';
import { Container } from '../container';
import {
GitLogCommit,
GitReference,
GitRevisionReference,
Repository,
RepositoryChange,
RepositoryChangeComparisonMode,
RepositoryChangeEvent,
} from '../git/git';
import { GitUri } from '../git/gitUri';
import { debug, Functions, gate, Strings } from '../system';
import {
BranchNode,
BranchTrackingStatusNode,
RepositoryFolderNode,
RepositoryNode,
unknownGitUri,
ViewNode,
} from './nodes';
import { ViewBase } from './viewBase';
export class CommitsRepositoryNode extends RepositoryFolderNode<CommitsView, BranchNode> {
async getChildren(): Promise<ViewNode[]> {
if (this.child == null) {
const branch = await this.repo.getBranch();
if (branch == null) {
this.view.message = 'No commits could be found.';
return [];
}
this.view.message = undefined;
let authors;
if (this.view.state.myCommitsOnly) {
const user = await Container.git.getCurrentUser(this.repo.path);
if (user != null) {
authors = [`^${user.name} <${user.email}>$`];
}
}
this.child = new BranchNode(this.uri, this.view, this, branch, true, {
expanded: true,
limitCommits: !this.splatted,
showComparison: this.view.config.showBranchComparison,
showCurrent: false,
showTracking: true,
authors: authors,
});
}
return this.child.getChildren();
}
@gate()
@debug()
override async refresh(reset: boolean = false) {
if (reset) {
this.child = undefined;
} else {
void this.parent?.triggerChange(false);
}
await this.ensureSubscription();
}
@debug()
protected override async subscribe() {
const lastFetched = (await this.repo?.getLastFetched()) ?? 0;
const interval = Repository.getLastFetchedUpdateInterval(lastFetched);
if (lastFetched !== 0 && interval > 0) {
return Disposable.from(
await super.subscribe(),
Functions.interval(() => {
// Check if the interval should change, and if so, reset it
if (interval !== Repository.getLastFetchedUpdateInterval(lastFetched)) {
void this.resetSubscription();
}
if (this.splatted) {
void this.view.triggerNodeChange(this.parent ?? this);
} else {
void this.view.triggerNodeChange(this);
}
}, interval),
);
}
return super.subscribe();
}
protected changed(e: RepositoryChangeEvent) {
return e.changed(
RepositoryChange.Config,
RepositoryChange.Heads,
RepositoryChange.Index,
RepositoryChange.Remotes,
RepositoryChange.RemoteProviders,
RepositoryChange.Status,
RepositoryChange.Unknown,
RepositoryChangeComparisonMode.Any,
);
}
}
export class CommitsViewNode extends ViewNode<CommitsView> {
protected override splatted = true;
private children: CommitsRepositoryNode[] | undefined;
constructor(view: CommitsView) {
super(unknownGitUri, view);
}
async getChildren(): Promise<ViewNode[]> {
if (this.children == null) {
const repositories = await Container.git.getOrderedRepositories();
if (repositories.length === 0) {
this.view.message = 'No commits could be found.';
return [];
}
this.view.message = undefined;
const splat = repositories.length === 1;
this.children = repositories.map(
r =>
new CommitsRepositoryNode(GitUri.fromRepoPath(r.path), this.view, this, r, splat, {
showBranchAndLastFetched: true,
}),
);
}
if (this.children.length === 1) {
const [child] = this.children;
const branch = await child.repo.getBranch();
if (branch != null) {
const lastFetched = (await child.repo.getLastFetched()) ?? 0;
const status = branch.getTrackingStatus();
this.view.description = `${status ? `${status} ${GlyphChars.Dot} ` : ''}${branch.name}${
branch.rebasing ? ' (Rebasing)' : ''
}${lastFetched ? ` ${GlyphChars.Dot} Last fetched ${Repository.formatLastFetched(lastFetched)}` : ''}${
child.repo.supportsChangeEvents
? ''
: `${Strings.pad(GlyphChars.Warning, 3, 2)}Auto-refresh unavailable`
}`;
} else {
this.view.description = child.repo.supportsChangeEvents
? undefined
: `${Strings.pad(GlyphChars.Warning, 1, 2)}Auto-refresh unavailable`;
}
return child.getChildren();
}
return this.children;
}
getTreeItem(): TreeItem {
const item = new TreeItem('Commits', TreeItemCollapsibleState.Expanded);
return item;
}
override async getSplattedChild() {
if (this.children == null) {
await this.getChildren();
}
return this.children?.length === 1 ? this.children[0] : undefined;
}
@gate()
@debug()
override refresh(reset: boolean = false) {
if (reset && this.children != null) {
for (const child of this.children) {
child.dispose();
}
this.children = undefined;
}
}
}
interface CommitsViewState {
myCommitsOnly?: boolean;
}
export class CommitsView extends ViewBase<CommitsViewNode, CommitsViewConfig> {
protected readonly configKey = 'commits';
constructor() {
super('gitlens.views.commits', 'Commits');
}
private readonly _state: CommitsViewState = {};
get state(): CommitsViewState {
return this._state;
}
getRoot() {
return new CommitsViewNode(this);
}
protected registerCommands(): Disposable[] {
void Container.viewCommands;
return [
commands.registerCommand(
this.getQualifiedCommand('copy'),
() => commands.executeCommand('gitlens.views.copy', this.selection),
this,
),
commands.registerCommand(
this.getQualifiedCommand('refresh'),
async () => {
await Container.git.resetCaches('branches', 'status', 'tags');
return this.refresh(true);
},
this,
),
commands.registerCommand(
this.getQualifiedCommand('setFilesLayoutToAuto'),
() => this.setFilesLayout(ViewFilesLayout.Auto),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setFilesLayoutToList'),
() => this.setFilesLayout(ViewFilesLayout.List),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setFilesLayoutToTree'),
() => this.setFilesLayout(ViewFilesLayout.Tree),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setMyCommitsOnlyOn'),
() => this.setMyCommitsOnly(true),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setMyCommitsOnlyOff'),
() => this.setMyCommitsOnly(false),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setShowAvatarsOn'),
() => this.setShowAvatars(true),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setShowAvatarsOff'),
() => this.setShowAvatars(false),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setShowBranchComparisonOn'),
() => this.setShowBranchComparison(true),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setShowBranchComparisonOff'),
() => this.setShowBranchComparison(false),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setShowBranchPullRequestOn'),
() => this.setShowBranchPullRequest(true),
this,
),
commands.registerCommand(
this.getQualifiedCommand('setShowBranchPullRequestOff'),
() => this.setShowBranchPullRequest(false),
this,
),
];
}
protected override filterConfigurationChanged(e: ConfigurationChangeEvent) {
const changed = super.filterConfigurationChanged(e);
if (
!changed &&
!configuration.changed(e, 'defaultDateFormat') &&
!configuration.changed(e, 'defaultDateShortFormat') &&
!configuration.changed(e, 'defaultDateSource') &&
!configuration.changed(e, 'defaultDateStyle') &&
!configuration.changed(e, 'defaultGravatarsStyle') &&
!configuration.changed(e, 'defaultTimeFormat')
) {
return false;
}
return true;
}
async findCommit(commit: GitLogCommit | { repoPath: string; ref: string }, token?: CancellationToken) {
const repoNodeId = RepositoryNode.getId(commit.repoPath);
const branch = await Container.git.getBranch(commit.repoPath);
if (branch == null) return undefined;
// Check if the commit exists on the current branch
if (!(await Container.git.branchContainsCommit(commit.repoPath, branch.name, commit.ref))) return undefined;
return this.findNode((n: any) => n.commit?.ref === commit.ref, {
allowPaging: true,
maxDepth: 2,
canTraverse: async n => {
if (n instanceof CommitsViewNode) {
let node: ViewNode | undefined = await n.getSplattedChild?.();
if (node instanceof CommitsRepositoryNode) {
node = await node.getSplattedChild?.();
if (node instanceof BranchNode) {
await node.loadMore({ until: commit.ref });
}
}
return true;
}
if (n instanceof CommitsRepositoryNode) {
if (n.id.startsWith(repoNodeId)) {
const node = await n.getSplattedChild?.();
if (node instanceof BranchNode) {
await node.loadMore({ until: commit.ref });
return true;
}
}
}
if (n instanceof BranchTrackingStatusNode) {
return n.id.startsWith(repoNodeId);
}
return false;
},
token: token,
});
}
@gate(() => '')
async revealCommit(
commit: GitRevisionReference,
options?: {
select?: boolean;
focus?: boolean;
expand?: boolean | number;
},
) {
return window.withProgress(
{
location: ProgressLocation.Notification,
title: `Revealing ${GitReference.toString(commit, { icon: false })} in the side bar...`,
cancellable: true,
},
async (progress, token) => {
const node = await this.findCommit(commit, token);
if (node == null) return undefined;
await this.ensureRevealNode(node, options);
return node;
},
);
}
private setFilesLayout(layout: ViewFilesLayout) {
return configuration.updateEffective(`views.${this.configKey}.files.layout` as const, layout);
}
private setMyCommitsOnly(enabled: boolean) {
void setContext(ContextKeys.ViewsCommitsMyCommitsOnly, enabled);
this.state.myCommitsOnly = enabled;
void this.refresh(true);
}
private setShowAvatars(enabled: boolean) {
return configuration.updateEffective(`views.${this.configKey}.avatars` as const, enabled);
}
private setShowBranchComparison(enabled: boolean) {
return configuration.updateEffective(
`views.${this.configKey}.showBranchComparison` as const,
enabled ? ViewShowBranchComparison.Working : false,
);
}
private async setShowBranchPullRequest(enabled: boolean) {
await configuration.updateEffective(`views.${this.configKey}.pullRequests.showForBranches` as const, enabled);
await configuration.updateEffective(`views.${this.configKey}.pullRequests.enabled` as const, enabled);
}
}