/
commit.ts
600 lines (509 loc) · 16.9 KB
/
commit.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
import { Uri } from 'vscode';
import { getAvatarUri } from '../../avatars';
import { DateSource, DateStyle, GravatarDefaultStyle } from '../../configuration';
import { GlyphChars } from '../../constants';
import { Container } from '../../container';
import { formatDate, fromNow } from '../../system/date';
import { gate } from '../../system/decorators/gate';
import { memoize } from '../../system/decorators/memoize';
import { cancellable } from '../../system/promise';
import { pad, pluralize } from '../../system/string';
import { PreviousLineComparisonUrisResult } from '../gitProvider';
import { GitUri } from '../gitUri';
import { RichRemoteProvider } from '../remotes/provider';
import { GitFile, GitFileChange, GitFileWorkingTreeStatus } from './file';
import { PullRequest } from './pullRequest';
import { GitReference, GitRevision, GitRevisionReference, GitStashReference } from './reference';
import { GitRemote } from './remote';
import { Repository } from './repository';
const stashNumberRegex = /stash@{(\d+)}/;
export class GitCommit implements GitRevisionReference {
static is(commit: any): commit is GitCommit {
return commit instanceof GitCommit;
}
static isStash(commit: any): commit is GitStashCommit {
return commit instanceof GitCommit && commit.refType === 'stash' && Boolean(commit.stashName);
}
static isOfRefType(commit: GitReference | undefined): boolean {
return commit?.refType === 'revision' || commit?.refType === 'stash';
}
static hasFullDetails(commit: GitCommit): commit is GitCommit & SomeNonNullable<GitCommit, 'message' | 'files'> {
return (
commit.message != null &&
commit.files != null &&
commit.parents.length !== 0 &&
(commit.refType !== 'stash' || commit._stashUntrackedFilesLoaded)
);
}
private _stashUntrackedFilesLoaded = false;
private _recomputeStats = false;
readonly lines: GitCommitLine[];
readonly ref: string;
readonly refType: GitRevisionReference['refType'];
readonly shortSha: string;
readonly stashName: string | undefined;
// TODO@eamodio rename to stashNumber
readonly number: string | undefined;
readonly stashOnRef: string | undefined;
constructor(
private readonly container: Container,
public readonly repoPath: string,
public readonly sha: string,
public readonly author: GitCommitIdentity,
public readonly committer: GitCommitIdentity,
summary: string,
public readonly parents: string[],
message?: string | undefined,
files?: GitFileChange | GitFileChange[] | { file?: GitFileChange; files?: GitFileChange[] } | undefined,
stats?: GitCommitStats,
lines?: GitCommitLine | GitCommitLine[] | undefined,
stashName?: string | undefined,
stashOnRef?: string | undefined,
) {
this.ref = this.sha;
this.shortSha = this.sha.substring(0, this.container.CommitShaFormatting.length);
if (stashName) {
this.refType = 'stash';
this.stashName = stashName || undefined;
this.stashOnRef = stashOnRef || undefined;
this.number = stashNumberRegex.exec(stashName)?.[1];
} else {
this.refType = 'revision';
}
// Add an ellipsis to the summary if there is or might be more message
if (message != null) {
this._message = message;
if (summary !== message) {
this._summary = `${summary} ${GlyphChars.Ellipsis}`;
} else {
this._summary = summary;
}
} else {
this._summary = `${summary} ${GlyphChars.Ellipsis}`;
}
// Keep this above files, because we check this in computing the stats
if (stats != null) {
this._stats = stats;
}
if (files != null) {
if (Array.isArray(files)) {
this._files = files;
} else if (files instanceof GitFileChange) {
this._file = files;
if (GitRevision.isUncommitted(sha, true)) {
this._files = [files];
}
} else {
if (files.file != null) {
this._file = files.file;
}
if (files.files != null) {
this._files = files.files;
}
}
this._recomputeStats = true;
}
if (lines != null) {
if (Array.isArray(lines)) {
this.lines = lines;
} else {
this.lines = [lines];
}
} else {
this.lines = [];
}
}
get date(): Date {
return this.container.CommitDateFormatting.dateSource === DateSource.Committed
? this.committer.date
: this.author.date;
}
private _file: GitFileChange | undefined;
get file(): GitFileChange | undefined {
return this._file;
}
private _files: GitFileChange[] | undefined;
get files(): readonly GitFileChange[] | undefined {
return this._files;
}
get formattedDate(): string {
return this.container.CommitDateFormatting.dateStyle === DateStyle.Absolute
? this.formatDate(this.container.CommitDateFormatting.dateFormat)
: this.formatDateFromNow();
}
@memoize()
get isUncommitted(): boolean {
return GitRevision.isUncommitted(this.sha);
}
@memoize()
get isUncommittedStaged(): boolean {
return GitRevision.isUncommittedStaged(this.sha);
}
private _message: string | undefined;
get message(): string | undefined {
return this._message;
}
get name(): string {
return this.stashName ? this.stashName : this.shortSha;
}
private _stats: GitCommitStats | undefined;
get stats(): GitCommitStats | undefined {
if (this._recomputeStats) {
this.computeFileStats();
}
return this._stats;
}
private _summary: string;
get summary(): string {
return this._summary;
}
private _resolvedPreviousSha: string | undefined;
get unresolvedPreviousSha(): string {
return (
this._resolvedPreviousSha ?? (this.file != null ? this.file.previousSha : this.parents[0]) ?? `${this.sha}^`
);
}
@gate()
async ensureFullDetails(): Promise<void> {
if (this.isUncommitted || GitCommit.hasFullDetails(this)) return;
const [commitResult, untrackedResult] = await Promise.allSettled([
this.refType !== 'stash' ? this.container.git.getCommit(this.repoPath, this.sha) : undefined,
// Check for any untracked files -- since git doesn't return them via `git stash list` :(
// See https://stackoverflow.com/questions/12681529/
this.refType === 'stash' && !this._stashUntrackedFilesLoaded
? this.container.git.getCommit(this.repoPath, `${this.stashName}^3`)
: undefined,
this.getPreviousSha(),
]);
let commit;
if (commitResult.status === 'fulfilled' && commitResult.value != null) {
commit = commitResult.value;
this.parents.push(...(commit.parents ?? []));
this._summary = commit.summary;
this._message = commit.message;
this._files = commit.files as GitFileChange[];
if (this._file != null) {
const file = this._files.find(f => f.path === this._file!.path);
if (file != null) {
this._file = new GitFileChange(
file.repoPath,
file.path,
file.status,
file.originalPath ?? this._file.originalPath,
file.previousSha ?? this._file.previousSha,
file.stats ?? this._file.stats,
);
}
}
}
if (untrackedResult.status === 'fulfilled' && untrackedResult.value != null) {
this._stashUntrackedFilesLoaded = true;
commit = untrackedResult.value;
if (commit?.files != null && commit.files.length !== 0) {
// Since these files are untracked -- make them look that way
const files = commit.files.map(
f => new GitFileChange(this.repoPath, f.path, GitFileWorkingTreeStatus.Untracked, f.originalPath),
);
if (this._files == null) {
this._files = files;
} else {
this._files.push(...files);
}
}
}
this._recomputeStats = true;
}
private computeFileStats(): void {
if (!this._recomputeStats || this._files == null) return;
this._recomputeStats = false;
const changedFiles = {
added: 0,
deleted: 0,
changed: 0,
};
let additions = 0;
let deletions = 0;
for (const file of this._files) {
if (file.stats != null) {
additions += file.stats.additions;
deletions += file.stats.deletions;
}
switch (file.status) {
case 'A':
case '?':
changedFiles.added++;
break;
case 'D':
changedFiles.deleted++;
break;
default:
changedFiles.changed++;
break;
}
}
if (this._stats != null) {
if (additions === 0 && this._stats.additions !== 0) {
additions = this._stats.additions;
}
if (deletions === 0 && this._stats.deletions !== 0) {
deletions = this._stats.deletions;
}
}
this._stats = { ...this._stats, changedFiles: changedFiles, additions: additions, deletions: deletions };
}
async findFile(path: string): Promise<GitFileChange | undefined>;
async findFile(uri: Uri): Promise<GitFileChange | undefined>;
async findFile(pathOrUri: string | Uri): Promise<GitFileChange | undefined> {
if (!GitCommit.hasFullDetails(this)) {
await this.ensureFullDetails();
if (this._files == null) return undefined;
}
const relativePath = this.container.git.getRelativePath(pathOrUri, this.repoPath);
return this._files?.find(f => f.path === relativePath);
}
formatDate(format?: string | null) {
return this.container.CommitDateFormatting.dateSource === DateSource.Committed
? this.committer.formatDate(format)
: this.author.formatDate(format);
}
formatDateFromNow(short?: boolean) {
return this.container.CommitDateFormatting.dateSource === DateSource.Committed
? this.committer.fromNow(short)
: this.author.fromNow(short);
}
formatStats(options?: {
compact?: boolean;
empty?: string;
expand?: boolean;
prefix?: string;
sectionSeparator?: string;
separator?: string;
suffix?: string;
}): string {
const stats = this.stats;
if (stats == null) return options?.empty ?? '';
const { changedFiles, additions, deletions } = stats;
if (changedFiles <= 0 && additions <= 0 && deletions <= 0) return options?.empty ?? '';
const {
compact = false,
expand = false,
prefix = '',
sectionSeparator = ` ${pad(GlyphChars.Dot, 1, 1, GlyphChars.Space)} `,
separator = ' ',
suffix = '',
} = options ?? {};
let status = prefix;
if (typeof changedFiles === 'number') {
if (changedFiles) {
status += expand ? `${pluralize('file', changedFiles)} changed` : `~${changedFiles}`;
}
} else {
const { added, changed, deleted } = changedFiles;
if (added) {
status += expand ? `${pluralize('file', added)} added` : `+${added}`;
} else if (!expand && !compact) {
status += '+0';
}
if (changed) {
status += `${added ? separator : ''}${
expand ? `${pluralize('file', changed)} changed` : `~${changed}`
}`;
} else if (!expand && !compact) {
status += '~0';
}
if (deleted) {
status += `${changed | additions ? separator : ''}${
expand ? `${pluralize('file', deleted)} deleted` : `-${deleted}`
}`;
} else if (!expand && !compact) {
status += '-0';
}
}
if (expand) {
if (additions) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
status += `${changedFiles ? sectionSeparator : ''}${pluralize('addition', additions)}`;
}
if (deletions) {
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
status += `${changedFiles || additions ? separator : ''}${pluralize('deletion', deletions)}`;
}
}
status += suffix;
return status;
}
private _pullRequest: Promise<PullRequest | undefined> | undefined;
async getAssociatedPullRequest(options?: {
remote?: GitRemote<RichRemoteProvider>;
timeout?: number;
}): Promise<PullRequest | undefined> {
if (this._pullRequest == null) {
async function getCore(this: GitCommit): Promise<PullRequest | undefined> {
const remote =
options?.remote ?? (await this.container.git.getBestRemoteWithRichProvider(this.repoPath));
if (remote?.provider == null) return undefined;
return this.container.git.getPullRequestForCommit(this.ref, remote, options);
}
this._pullRequest = getCore.call(this);
}
return cancellable(this._pullRequest, options?.timeout);
}
getAvatarUri(options?: { defaultStyle?: GravatarDefaultStyle; size?: number }): Uri | Promise<Uri> {
return this.author.getAvatarUri(this, options);
}
async getCommitForFile(file: string | GitFile): Promise<GitCommit | undefined> {
const path = typeof file === 'string' ? this.container.git.getRelativePath(file, this.repoPath) : file.path;
const foundFile = await this.findFile(path);
if (foundFile == null) return undefined;
const commit = this.with({ files: { file: foundFile } });
return commit;
}
async getCommitsForFiles(): Promise<GitCommit[]> {
if (!GitCommit.hasFullDetails(this)) {
await this.ensureFullDetails();
if (this._files == null) return [];
}
const commits = this._files?.map(f => this.with({ files: { file: f } }));
return commits ?? [];
}
@memoize()
getGitUri(previous: boolean = false): GitUri {
const uri = this._file?.uri ?? this.container.git.getAbsoluteUri(this.repoPath, this.repoPath);
if (!previous) return new GitUri(uri, this);
return new GitUri(this._file?.originalUri ?? uri, {
repoPath: this.repoPath,
sha: this.unresolvedPreviousSha,
});
}
@memoize<GitCommit['getPreviousComparisonUrisForLine']>((el, ref) => `${el}|${ref ?? ''}`)
getPreviousComparisonUrisForLine(
editorLine: number,
ref?: string,
): Promise<PreviousLineComparisonUrisResult | undefined> {
return this.file != null
? this.container.git.getPreviousComparisonUrisForLine(
this.repoPath,
this.file.uri,
editorLine,
ref ?? (this.sha === GitRevision.uncommitted ? undefined : this.sha),
)
: Promise.resolve(undefined);
}
private _previousShaPromise: Promise<string | undefined> | undefined;
async getPreviousSha(): Promise<string | undefined> {
if (this._previousShaPromise == null) {
async function getCore(this: GitCommit) {
if (this.file != null) {
if (this.file.previousSha != null && GitRevision.isSha(this.file.previousSha)) {
return this.file.previousSha;
}
const sha = await this.container.git.resolveReference(
this.repoPath,
GitRevision.isUncommitted(this.sha, true) ? 'HEAD' : `${this.sha}^`,
this.file.originalPath ?? this.file.path,
);
this._resolvedPreviousSha = sha;
return sha;
}
const parent = this.parents[0];
if (parent != null && GitRevision.isSha(parent)) return parent;
const sha = await this.container.git.resolveReference(
this.repoPath,
GitRevision.isUncommitted(this.sha, true) ? 'HEAD' : `${this.sha}^`,
);
this._resolvedPreviousSha = sha;
return sha;
}
this._previousShaPromise = getCore.call(this);
}
return this._previousShaPromise;
}
getRepository(): Repository | undefined {
return this.container.git.getRepository(this.repoPath);
}
@gate()
async isPushed(): Promise<boolean> {
return this.container.git.hasCommitBeenPushed(this.repoPath, this.ref);
}
with(changes: {
sha?: string;
parents?: string[];
files?: { file?: GitFileChange | null; files?: GitFileChange[] | null } | null;
lines?: GitCommitLine[];
}): GitCommit {
let files;
if (changes.files != null) {
files = { file: this._file, files: this._files };
if (changes.files.file != null) {
files.file = changes.files.file;
} else if (changes.files.file === null) {
files.file = undefined;
}
if (changes.files.files != null) {
files.files = changes.files.files;
} else if (changes.files.files === null) {
files.files = undefined;
}
} else if (changes.files === null) {
files = undefined;
}
return new GitCommit(
this.container,
this.repoPath,
changes.sha ?? this.sha,
this.author,
this.committer,
this.summary,
this.getChangedValue(changes.parents, this.parents) ?? [],
this.message,
files,
this.stats,
this.getChangedValue(changes.lines, this.lines),
this.stashName,
);
}
protected getChangedValue<T>(change: T | null | undefined, original: T | undefined): T | undefined {
if (change === undefined) return original;
return change !== null ? change : undefined;
}
}
export class GitCommitIdentity {
constructor(
public readonly name: string,
public readonly email: string | undefined,
public readonly date: Date,
private readonly avatarUrl?: string | undefined,
) {}
@memoize<GitCommitIdentity['formatDate']>(format => format ?? 'MMMM Do, YYYY h:mma')
formatDate(format?: string | null): string {
return formatDate(this.date, format ?? 'MMMM Do, YYYY h:mma');
}
fromNow(short?: boolean): string {
return fromNow(this.date, short);
}
getAvatarUri(
commit: GitCommit,
options?: { defaultStyle?: GravatarDefaultStyle; size?: number },
): Uri | Promise<Uri> {
if (this.avatarUrl != null) return Uri.parse(this.avatarUrl);
return getAvatarUri(this.email, commit, options);
}
}
export interface GitCommitLine {
sha: string;
previousSha?: string | undefined;
/** The original (previous) line number prior to this commit; 1-based */
originalLine: number;
/** The current line number in this commit; 1-based */
line: number;
}
export interface GitCommitStats {
readonly additions: number;
readonly deletions: number;
readonly changedFiles: number | { added: number; deleted: number; changed: number };
}
export interface GitStashCommit extends GitCommit {
readonly refType: GitStashReference['refType'];
readonly stashName: string;
readonly number: string;
}