/
logCommit.ts
200 lines (181 loc) · 6.37 KB
/
logCommit.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
'use strict';
import * as paths from 'path';
import { Uri } from 'vscode';
import { Strings } from '../../system';
import { Git } from '../git';
import { GitUri } from '../gitUri';
import { GitCommit, GitCommitType } from './commit';
import { GitFile, GitFileStatus } from './file';
export class GitLogCommit extends GitCommit {
nextSha?: string;
nextFileName?: string;
constructor(
type: GitCommitType,
repoPath: string,
sha: string,
author: string,
email: string | undefined,
date: Date,
public readonly committedDate: Date,
message: string,
fileName: string,
public readonly files: GitFile[],
public readonly status: GitFileStatus | undefined,
originalFileName: string | undefined,
previousSha: string | undefined,
previousFileName: string | undefined,
public readonly parentShas?: string[]
) {
super(
type,
repoPath,
sha,
author,
email,
date,
committedDate,
message,
fileName,
originalFileName,
previousSha,
previousFileName
);
}
get isMerge() {
return this.parentShas && this.parentShas.length > 1;
}
get nextShortSha() {
return this.nextSha && Git.shortenSha(this.nextSha);
}
get nextUri(): Uri {
return this.nextFileName ? GitUri.resolveToUri(this.nextFileName, this.repoPath) : this.uri;
}
get previousFileSha(): string {
if (this._resolvedPreviousFileSha !== undefined) return this._resolvedPreviousFileSha;
return this.isFile && this.previousSha ? this.previousSha : `${this.sha}^`;
}
private _diff?: {
added: number;
deleted: number;
changed: number;
};
getDiffStatus() {
if (this._diff === undefined) {
this._diff = {
added: 0,
deleted: 0,
changed: 0
};
if (this.files.length !== 0) {
for (const f of this.files) {
switch (f.status) {
case 'A':
case '?':
this._diff.added++;
break;
case 'D':
this._diff.deleted++;
break;
default:
this._diff.changed++;
break;
}
}
}
}
return this._diff;
}
getFormattedDiffStatus(
options: {
compact?: boolean;
empty?: string;
expand?: boolean;
prefix?: string;
separator?: string;
suffix?: string;
} = {}
): string {
const { added, changed, deleted } = this.getDiffStatus();
if (added === 0 && changed === 0 && deleted === 0) return options.empty || '';
const { compact, expand, prefix = '', separator = ' ', suffix = '' } = options;
if (expand) {
let status = '';
if (added) {
status += `${Strings.pluralize('file', added)} added`;
}
if (changed) {
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', changed)} changed`;
}
if (deleted) {
status += `${status.length === 0 ? '' : separator}${Strings.pluralize('file', deleted)} deleted`;
}
return `${prefix}${status}${suffix}`;
}
return `${prefix}${compact && added === 0 ? '' : `+${added}${separator}`}${
compact && changed === 0 ? '' : `~${changed}${separator}`
}${compact && deleted === 0 ? '' : `-${deleted}`}${suffix}`;
}
toFileCommit(fileName: string): GitLogCommit | undefined;
toFileCommit(file: GitFile): GitLogCommit;
toFileCommit(fileNameOrFile: string | GitFile): GitLogCommit | undefined {
let file: GitFile | undefined;
if (typeof fileNameOrFile === 'string') {
const fileName = Strings.normalizePath(paths.relative(this.repoPath, fileNameOrFile));
file = this.files.find(f => f.fileName === fileName);
if (file === undefined) return undefined;
}
else {
file = fileNameOrFile;
}
let sha;
// If this is a stash commit with an untracked file
if (this.type === GitCommitType.Stash && file.status === '?') {
sha = `${this.sha}^3`;
}
// If this isn't a single-file commit, we can't trust the previousSha
const previousSha = this.isFile ? this.previousSha : `${this.sha}^`;
return this.with({
type: this.isStash ? GitCommitType.StashFile : GitCommitType.File,
sha: sha,
fileName: file.fileName,
originalFileName: file.originalFileName,
previousSha: previousSha,
previousFileName: file.originalFileName || file.fileName,
status: file.status,
files: [file]
});
}
with(changes: {
type?: GitCommitType;
sha?: string | null;
fileName?: string;
author?: string;
email?: string;
date?: Date;
committedDate?: Date;
message?: string;
originalFileName?: string | null;
previousFileName?: string | null;
previousSha?: string | null;
status?: GitFileStatus;
files?: GitFile[] | null;
}): GitLogCommit {
return new GitLogCommit(
changes.type || this.type,
this.repoPath,
this.getChangedValue(changes.sha, this.sha)!,
changes.author || this.author,
changes.email || this.email,
changes.date || this.date,
changes.committedDate || this.committedDate,
changes.message || this.message,
changes.fileName || this.fileName,
this.getChangedValue(changes.files, this.files) || [],
changes.status || this.status,
this.getChangedValue(changes.originalFileName, this.originalFileName),
this.getChangedValue(changes.previousSha, this.previousSha),
this.getChangedValue(changes.previousFileName, this.previousFileName),
undefined
);
}
}