-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathgitHelper.ts
More file actions
364 lines (323 loc) · 13 KB
/
Copy pathgitHelper.ts
File metadata and controls
364 lines (323 loc) · 13 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
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
import * as path from 'path';
import { promises as fs } from 'fs';
import { workspace, OutputChannel, WorkspaceFolder } from 'vscode';
import { Git, Repository } from './git/git';
import { Ref, Branch } from './git/api/git';
import { normalizePath } from './fsUtils';
import { API as GitAPI } from './typings/git';
export async function createGit(gitApi: GitAPI, outputChannel: OutputChannel): Promise<Git> {
outputChannel.appendLine(`Using git from ${gitApi.git.path}`);
return new Git({
gitPath: gitApi.git.path,
userAgent: '',
version: '',
});
}
export function getWorkspaceFolders(repositoryFolder: string): WorkspaceFolder[] {
const normRepoFolder = normalizePath(repositoryFolder);
const allWorkspaceFolders = workspace.workspaceFolders || [];
const workspaceFolders = allWorkspaceFolders.filter(ws => {
const normWsFolder = normalizePath(ws.uri.fsPath);
return normWsFolder === normRepoFolder ||
// workspace folder is subfolder of repository (or equal)
normWsFolder.startsWith(normRepoFolder + path.sep) ||
// repository is subfolder of workspace folder
normRepoFolder.startsWith(normWsFolder + path.sep);
});
return workspaceFolders;
}
export function getGitRepositoryFolders(git: GitAPI, selectedFirst=false): string[] {
let repos = git.repositories;
if (selectedFirst) {
repos = [...repos];
repos.sort((r1, r2) => (r2.ui.selected as any) - (r1.ui.selected as any));
}
const rootPaths = repos.map(r => r.rootUri.fsPath).filter(p => getWorkspaceFolders(p).length > 0);
return rootPaths;
}
export async function getAbsGitDir(repo: Repository): Promise<string> {
// We don't use --absolute-git-dir here as that requires git >= 2.13.
let res = await repo.exec(['rev-parse', '--git-dir']);
let dir = res.stdout.trim();
if (!path.isAbsolute(dir)) {
dir = path.join(repo.root, dir);
}
return dir;
}
export async function getAbsGitCommonDir(repo: Repository): Promise<string> {
let res = await repo.exec(['rev-parse', '--git-common-dir']);
let dir = res.stdout.trim();
if (!path.isAbsolute(dir)) {
dir = path.join(repo.root, dir);
}
return dir;
}
export interface IWorktreeInfo {
path: string;
head: string;
branch: string | undefined;
}
export async function listWorktrees(repo: Repository): Promise<IWorktreeInfo[]> {
const result = await repo.exec(['worktree', 'list', '--porcelain']);
const worktrees: IWorktreeInfo[] = [];
let currentPath: string | undefined;
let currentHead: string | undefined;
let currentBranch: string | undefined;
const flush = () => {
if (currentPath && currentHead) {
worktrees.push({
path: normalizePath(currentPath),
head: currentHead,
branch: currentBranch,
});
}
currentPath = undefined;
currentHead = undefined;
currentBranch = undefined;
};
for (const line of result.stdout.split('\n')) {
if (line.startsWith('worktree ')) {
flush();
currentPath = line.slice('worktree '.length);
} else if (line.startsWith('HEAD ')) {
currentHead = line.slice('HEAD '.length);
} else if (line.startsWith('branch refs/heads/')) {
currentBranch = line.slice('branch refs/heads/'.length);
}
}
flush();
return worktrees;
}
export async function getDefaultBranch(repo: Repository, head: Ref): Promise<string | undefined> {
// determine which remote HEAD is tracking
let remote: string
if (head.name) {
let headBranch: Branch;
try {
headBranch = await repo.getBranch(head.name);
} catch (e) {
// this can happen on a newly initialized repo without commits
return;
}
if (!headBranch.upstream) {
return;
}
remote = headBranch.upstream.remote;
} else {
// detached HEAD, fall-back and try 'origin'
remote = 'origin';
}
// determine default branch for the remote
const remoteHead = `refs/remotes/${remote}/HEAD`;
try {
const result = await repo.exec(['symbolic-ref', '--short', remoteHead]);
const remoteHeadBranch = result.stdout.trim();
return remoteHeadBranch;
} catch (e) {
return;
}
}
export async function getBranchCommit(branchName: string, repo: Repository): Promise<string> {
// a cheaper alternative to repo.getBranch()
// Uses git rev-parse which works with all ref storage formats (traditional, packed-refs, reftable)
try {
const result = await repo.exec(['rev-parse', `refs/heads/${branchName}`]);
const commit = result.stdout.trim();
if (commit) {
return commit;
}
} catch (e) {
// Branch doesn't exist or other error
}
throw new Error(`Could not determine commit for "${branchName}"`);
}
export async function getHeadModificationDate(absGitDir: string): Promise<Date> {
const headPath = path.join(absGitDir, 'HEAD');
const stats = await fs.stat(headPath);
return stats.mtime;
}
export interface IDiffStats {
insertions: number | undefined;
deletions: number | undefined;
isBinary: boolean;
}
export interface IDiffStatus {
/**
* A Addition of a file
* D Deletion of a file
* M Modification of file contents
* R Renaming of a file
* C File has merge conflicts
* U Untracked file
* T Type change (regular/symlink etc.)
*/
status: StatusCode
/** absolute path to src file on disk */
srcAbsPath: string
/** absolute path to dst file on disk */
dstAbsPath: string
/** True if this was or is a submodule */
isSubmodule: boolean
/** Per-file insertion/deletion counts (undefined when stats are disabled or unavailable) */
stats: IDiffStats | undefined;
}
const MODE_REGULAR_FILE = '100644';
const MODE_EMPTY = '000000';
const MODE_SUBMODULE = '160000';
class DiffStatus implements IDiffStatus {
readonly srcAbsPath: string;
readonly dstAbsPath: string;
readonly isSubmodule: boolean;
stats: IDiffStats | undefined;
constructor(repoRoot: string, public status: StatusCode, srcRelPath: string, dstRelPath: string | undefined, srcMode: string, dstMode: string) {
this.srcAbsPath = path.join(repoRoot, srcRelPath);
this.dstAbsPath = dstRelPath ? path.join(repoRoot, dstRelPath) : this.srcAbsPath;
this.isSubmodule = srcMode == MODE_SUBMODULE || dstMode == MODE_SUBMODULE;
}
}
export type StatusCode = 'A' | 'D' | 'M' | 'C' | 'U' | 'T' | 'R';
function sanitizeStatus(status: string): StatusCode {
if (status == 'U') {
return 'C';
}
if (status.length != 1 || 'ADMTR'.indexOf(status) == -1) {
throw new Error('unsupported git status: ' + status);
}
return status as StatusCode;
}
// https://git-scm.com/docs/git-diff-index#_raw_output_format
const MODE_LEN = 6;
const SHA1_LEN = 40;
const SRC_MODE_OFFSET = 1;
const DST_MODE_OFFSET = 2 + MODE_LEN;
const STATUS_OFFSET = 2 * MODE_LEN + 2 * SHA1_LEN + 5;
function parseDiffIndexOutput(repoRoot: string, out: string): IDiffStatus[] {
const entries: IDiffStatus[] = [];
while (out) {
const srcMode = out.substr(SRC_MODE_OFFSET, MODE_LEN);
const dstMode = out.substr(DST_MODE_OFFSET, MODE_LEN);
const status = out[STATUS_OFFSET];
out = out.substr(STATUS_OFFSET + 1);
let srcPathStart = out.indexOf('\0') + 1;
out = out.substr(srcPathStart);
let nextNul = out.indexOf('\0');
const srcPath = out.substring(0, nextNul);
out = out.substr(nextNul + 1);
let dstPath: string | undefined;
if (status === 'C' || status === 'R') {
nextNul = out.indexOf('\0');
dstPath = out.substring(0, nextNul);
out = out.substr(nextNul + 1);
}
entries.push(new DiffStatus(
repoRoot,
sanitizeStatus(status),
srcPath, dstPath,
srcMode, dstMode));
}
return entries;
}
function parseDiffNumstat(repoRoot: string, out: string): Map<string, IDiffStats> {
const stats = new Map<string, IDiffStats>();
const lines = out.split('\n').filter(line => line.length > 0);
for (const line of lines) {
const parts = line.split('\t');
if (parts.length < 3) {
continue;
}
const [ins, del, ...pathParts] = parts;
const relPath = pathParts.join('\t');
const absPath = path.join(repoRoot, relPath);
if (ins === '-' && del === '-') {
stats.set(absPath, { insertions: undefined, deletions: undefined, isBinary: true });
} else {
stats.set(absPath, { insertions: parseInt(ins, 10), deletions: parseInt(del, 10), isBinary: false });
}
}
return stats;
}
const BINARY_CHECK_BYTES = 8192;
async function computeUntrackedStats(entries: IDiffStatus[]): Promise<void> {
await Promise.all(entries.map(async (entry) => {
try {
const buf = Buffer.alloc(BINARY_CHECK_BYTES);
const handle = await fs.open(entry.dstAbsPath, 'r');
try {
const { bytesRead } = await handle.read(buf, 0, BINARY_CHECK_BYTES, 0);
if (buf.subarray(0, bytesRead).includes(0)) {
entry.stats = { insertions: undefined, deletions: undefined, isBinary: true };
return;
}
} finally {
await handle.close();
}
const content = await fs.readFile(entry.dstAbsPath, 'utf-8');
const lines = content.split('\n');
const lineCount = content.endsWith('\n') ? lines.length - 1 : lines.length;
entry.stats = { insertions: lineCount, deletions: 0, isBinary: false };
} catch {
// File may be inaccessible
}
}));
}
export async function diffIndex(repo: Repository, ref: string, refreshIndex: boolean, findRenames: boolean, renameThreshold: number, omitUntrackedFiles: boolean, omitUnstagedChanges: boolean, showDiffStats: boolean = false): Promise<IDiffStatus[]> {
if (refreshIndex) {
// avoid superfluous diff entries if files only got touched
// (see https://github.com/letmaik/vscode-git-tree-compare/issues/37)
try {
await repo.exec(['update-index', '--refresh', '-q']);
} catch (e) {
// ignore errors as this is a bonus anyway
}
}
// exceptions can happen with newly initialized repos without commits, or when git is busy
const repoRoot = normalizePath(repo.root);
const renamesFlag = findRenames ? `--find-renames=${renameThreshold}%` : '--no-renames';
const diffIndexArgs = ['diff-index', '-z', renamesFlag];
if (omitUnstagedChanges) {
diffIndexArgs.push('--cached');
}
diffIndexArgs.push(ref, '--');
let diffIndexResult = await repo.exec(diffIndexArgs);
let untrackedStatuses: IDiffStatus[] = [];
if (!omitUntrackedFiles) {
let untrackedResult = await repo.exec(['ls-files', '-z', '--others', '--exclude-standard']);
untrackedStatuses = untrackedResult.stdout.split('\0')
.slice(0, -1)
.map(line => new DiffStatus(repoRoot, 'U' as 'U', line, undefined, MODE_EMPTY, MODE_REGULAR_FILE));
}
const diffIndexStatuses = parseDiffIndexOutput(repoRoot, diffIndexResult.stdout);
const untrackedAbsPaths = new Set(untrackedStatuses.map(status => status.dstAbsPath))
// If a file was removed (D in diff-index) but was then re-introduced and not committed yet,
// then that file also appears as untracked (in ls-files). We need to decide which status to keep.
// Since the untracked status is newer it gets precedence.
const filteredDiffIndexStatuses = diffIndexStatuses.filter(status => !untrackedAbsPaths.has(status.srcAbsPath));
const statuses = filteredDiffIndexStatuses.concat(untrackedStatuses);
if (showDiffStats) {
const numstatResult = await repo.exec(['diff', '--numstat', renamesFlag, ref, '--']);
const numstatMap = parseDiffNumstat(repoRoot, numstatResult.stdout);
for (const entry of statuses) {
const fileStats = numstatMap.get(entry.dstAbsPath) || numstatMap.get(entry.srcAbsPath);
if (fileStats) {
entry.stats = fileStats;
}
}
if (untrackedStatuses.length > 0) {
await computeUntrackedStats(untrackedStatuses);
}
}
statuses.sort((s1, s2) => s1.dstAbsPath.localeCompare(s2.dstAbsPath))
return statuses;
}
export async function hasUncommittedChanges(repo: Repository, path: string, ignoreUntracked: boolean = false): Promise<boolean> {
const args = ['status', '-z'];
if (ignoreUntracked) {
args.push('-uno');
}
args.push(path);
const result = await repo.exec(args);
return result.stdout.trim() !== '';
}
export async function rmFile(repo: Repository, absPath: string): Promise<void> {
await repo.exec(['rm', '-f', absPath]);
}