@statewalker/vcs-commands 0.1.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/commit-command.d.ts.map +1 -1
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
- package/dist/git-command.d.ts.map +1 -1
- package/package.json +19 -14
- package/src/commands/add-command.ts +494 -0
- package/src/commands/blame-command.ts +1241 -0
- package/src/commands/branch-command.ts +487 -0
- package/src/commands/checkout-command.ts +681 -0
- package/src/commands/cherry-pick-command.ts +544 -0
- package/src/commands/clean-command.ts +218 -0
- package/src/commands/clone-command.ts +532 -0
- package/src/commands/commit-command.ts +529 -0
- package/src/commands/describe-command.ts +454 -0
- package/src/commands/diff-command.ts +257 -0
- package/src/commands/fetch-command.ts +615 -0
- package/src/commands/gc-command.ts +155 -0
- package/src/commands/index.ts +31 -0
- package/src/commands/init-command.ts +365 -0
- package/src/commands/log-command.ts +549 -0
- package/src/commands/ls-remote-command.ts +237 -0
- package/src/commands/merge-command.ts +750 -0
- package/src/commands/pack-refs-command.ts +101 -0
- package/src/commands/pull-command.ts +258 -0
- package/src/commands/push-command.ts +682 -0
- package/src/commands/rebase-command.ts +665 -0
- package/src/commands/reflog-command.ts +68 -0
- package/src/commands/remote-command.ts +385 -0
- package/src/commands/reset-command.ts +292 -0
- package/src/commands/revert-command.ts +552 -0
- package/src/commands/rm-command.ts +178 -0
- package/src/commands/stash-apply-command.ts +402 -0
- package/src/commands/stash-create-command.ts +339 -0
- package/src/commands/stash-drop-command.ts +115 -0
- package/src/commands/stash-list-command.ts +116 -0
- package/src/commands/status-command.ts +173 -0
- package/src/commands/tag-command.ts +378 -0
- package/src/commands/tree-restore.ts +126 -0
- package/src/core-commands/add.command.impl.ts +274 -0
- package/src/core-commands/add.command.ts +81 -0
- package/src/core-commands/checkout.command.impl.ts +586 -0
- package/src/core-commands/checkout.command.ts +125 -0
- package/src/core-commands/index.ts +13 -0
- package/src/errors/add-errors.ts +20 -0
- package/src/errors/checkout-errors.ts +42 -0
- package/src/errors/command-errors.ts +68 -0
- package/src/errors/commit-errors.ts +40 -0
- package/src/errors/git-api-error.ts +11 -0
- package/src/errors/index.ts +12 -0
- package/src/errors/merge-errors.ts +94 -0
- package/src/errors/rebase-errors.ts +34 -0
- package/src/errors/ref-errors.ts +100 -0
- package/src/errors/remote-errors.ts +27 -0
- package/src/errors/stash-errors.ts +42 -0
- package/src/errors/tag-errors.ts +16 -0
- package/src/errors/transport-errors.ts +68 -0
- package/src/git-command.ts +421 -0
- package/src/git.ts +910 -0
- package/src/index.ts +18 -0
- package/src/pack-import/index.ts +8 -0
- package/src/pack-import/pack-import.ts +24 -0
- package/src/remote-config/index.ts +12 -0
- package/src/remote-config/remote-config-store.ts +285 -0
- package/src/rename/index.ts +5 -0
- package/src/rename/similarity-index.ts +185 -0
- package/src/results/cherry-pick-result.ts +35 -0
- package/src/results/clone-result.ts +25 -0
- package/src/results/diff-entry.ts +162 -0
- package/src/results/diff-formatter.ts +416 -0
- package/src/results/fetch-result.ts +91 -0
- package/src/results/index.ts +13 -0
- package/src/results/init-result.ts +29 -0
- package/src/results/merge-result.ts +135 -0
- package/src/results/pull-result.ts +27 -0
- package/src/results/push-result.ts +86 -0
- package/src/results/rebase-result.ts +140 -0
- package/src/results/revert-result.ts +35 -0
- package/src/results/stash-result.ts +60 -0
- package/src/results/status-result.ts +65 -0
- package/src/transport-command.ts +125 -0
- package/src/tree-merge/index.ts +8 -0
- package/src/tree-merge/tree-merge.ts +249 -0
- package/src/types.ts +70 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import type { ObjectId } from "@statewalker/vcs-core";
|
|
2
|
+
|
|
3
|
+
import { RefNotFoundError } from "../errors/ref-errors.js";
|
|
4
|
+
import { GitCommand } from "../git-command.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Result of describe operation.
|
|
8
|
+
*/
|
|
9
|
+
export interface DescribeResult {
|
|
10
|
+
/** The description string (tag name or tag-depth-gSHA format) */
|
|
11
|
+
readonly description: string | undefined;
|
|
12
|
+
/** The tag that was found (if any) */
|
|
13
|
+
readonly tag?: string;
|
|
14
|
+
/** Distance from the tag (number of commits) */
|
|
15
|
+
readonly depth?: number;
|
|
16
|
+
/** Abbreviated commit hash */
|
|
17
|
+
readonly abbrevHash?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Given a commit, show the most recent tag that is reachable from it.
|
|
22
|
+
*
|
|
23
|
+
* Equivalent to `git describe`.
|
|
24
|
+
*
|
|
25
|
+
* Based on JGit's DescribeCommand.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Describe HEAD
|
|
30
|
+
* const desc = await git.describe().call();
|
|
31
|
+
*
|
|
32
|
+
* // Describe a specific commit
|
|
33
|
+
* const desc = await git.describe()
|
|
34
|
+
* .setTarget(commitId)
|
|
35
|
+
* .call();
|
|
36
|
+
*
|
|
37
|
+
* // Use long format
|
|
38
|
+
* const desc = await git.describe()
|
|
39
|
+
* .setLong(true)
|
|
40
|
+
* .call();
|
|
41
|
+
*
|
|
42
|
+
* // Include lightweight tags
|
|
43
|
+
* const desc = await git.describe()
|
|
44
|
+
* .setTags(true)
|
|
45
|
+
* .call();
|
|
46
|
+
*
|
|
47
|
+
* // Always output something (fall back to commit hash)
|
|
48
|
+
* const desc = await git.describe()
|
|
49
|
+
* .setAlways(true)
|
|
50
|
+
* .call();
|
|
51
|
+
*
|
|
52
|
+
* // Match specific tag patterns
|
|
53
|
+
* const desc = await git.describe()
|
|
54
|
+
* .setMatch("v*")
|
|
55
|
+
* .call();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export class DescribeCommand extends GitCommand<DescribeResult> {
|
|
59
|
+
private target?: ObjectId;
|
|
60
|
+
private longFormat = false;
|
|
61
|
+
private useTags = false;
|
|
62
|
+
private useAll = false;
|
|
63
|
+
private always = false;
|
|
64
|
+
private abbrev = 7;
|
|
65
|
+
private matchPatterns: string[] = [];
|
|
66
|
+
private excludePatterns: string[] = [];
|
|
67
|
+
private maxCandidates = 10;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Set the commit to describe.
|
|
71
|
+
*
|
|
72
|
+
* @param target ObjectId of commit to describe
|
|
73
|
+
*/
|
|
74
|
+
setTarget(target: ObjectId): this {
|
|
75
|
+
this.checkCallable();
|
|
76
|
+
this.target = target;
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Set the commit to describe by reference.
|
|
82
|
+
*
|
|
83
|
+
* @param rev Reference string (branch, tag, commit ID)
|
|
84
|
+
*/
|
|
85
|
+
async setTargetRef(rev: string): Promise<this> {
|
|
86
|
+
this.checkCallable();
|
|
87
|
+
const resolved = await this.refsStore.resolve(rev);
|
|
88
|
+
if (!resolved?.objectId) {
|
|
89
|
+
throw new RefNotFoundError(`Cannot resolve: ${rev}`);
|
|
90
|
+
}
|
|
91
|
+
this.target = resolved.objectId;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Whether to always use long output format.
|
|
97
|
+
*
|
|
98
|
+
* When true, outputs tag-depth-gSHA even if commit matches a tag exactly.
|
|
99
|
+
*
|
|
100
|
+
* @param longFormat Whether to use long format (default: false)
|
|
101
|
+
*/
|
|
102
|
+
setLong(longFormat: boolean): this {
|
|
103
|
+
this.checkCallable();
|
|
104
|
+
this.longFormat = longFormat;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Whether to use any tag (including lightweight tags).
|
|
110
|
+
*
|
|
111
|
+
* By default, only annotated tags are considered.
|
|
112
|
+
*
|
|
113
|
+
* @param tags Whether to include lightweight tags (default: false)
|
|
114
|
+
*/
|
|
115
|
+
setTags(tags: boolean): this {
|
|
116
|
+
this.checkCallable();
|
|
117
|
+
this.useTags = tags;
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Whether to use any ref in refs/ namespace.
|
|
123
|
+
*
|
|
124
|
+
* Enables matching branches and remote-tracking branches.
|
|
125
|
+
*
|
|
126
|
+
* @param all Whether to use all refs (default: false)
|
|
127
|
+
*/
|
|
128
|
+
setAll(all: boolean): this {
|
|
129
|
+
this.checkCallable();
|
|
130
|
+
this.useAll = all;
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Always output something, even if no tag is found.
|
|
136
|
+
*
|
|
137
|
+
* Falls back to abbreviated commit hash.
|
|
138
|
+
*
|
|
139
|
+
* @param always Whether to always output (default: false)
|
|
140
|
+
*/
|
|
141
|
+
setAlways(always: boolean): this {
|
|
142
|
+
this.checkCallable();
|
|
143
|
+
this.always = always;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Set the abbreviation length for commit hash.
|
|
149
|
+
*
|
|
150
|
+
* @param abbrev Abbreviation length (default: 7)
|
|
151
|
+
*/
|
|
152
|
+
setAbbrev(abbrev: number): this {
|
|
153
|
+
this.checkCallable();
|
|
154
|
+
this.abbrev = Math.max(4, Math.min(40, abbrev));
|
|
155
|
+
return this;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Get the abbreviation length.
|
|
160
|
+
*/
|
|
161
|
+
getAbbrev(): number {
|
|
162
|
+
return this.abbrev;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Set glob patterns that tags must match.
|
|
167
|
+
*
|
|
168
|
+
* @param patterns Glob patterns (e.g., "v*", "release-*")
|
|
169
|
+
*/
|
|
170
|
+
setMatch(...patterns: string[]): this {
|
|
171
|
+
this.checkCallable();
|
|
172
|
+
this.matchPatterns = patterns;
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Set glob patterns to exclude tags.
|
|
178
|
+
*
|
|
179
|
+
* @param patterns Glob patterns to exclude
|
|
180
|
+
*/
|
|
181
|
+
setExclude(...patterns: string[]): this {
|
|
182
|
+
this.checkCallable();
|
|
183
|
+
this.excludePatterns = patterns;
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Set maximum number of tag candidates to consider.
|
|
189
|
+
*
|
|
190
|
+
* @param max Maximum candidates (default: 10)
|
|
191
|
+
*/
|
|
192
|
+
setMaxCandidates(max: number): this {
|
|
193
|
+
this.checkCallable();
|
|
194
|
+
this.maxCandidates = max;
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Execute the describe command.
|
|
200
|
+
*
|
|
201
|
+
* @returns Description result with tag name, depth, and/or commit hash
|
|
202
|
+
*/
|
|
203
|
+
async call(): Promise<DescribeResult> {
|
|
204
|
+
this.checkCallable();
|
|
205
|
+
this.setCallable(false);
|
|
206
|
+
|
|
207
|
+
// Default to HEAD if no target specified
|
|
208
|
+
if (!this.target) {
|
|
209
|
+
this.target = await this.resolveHead();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Collect all tags (or refs if useAll)
|
|
213
|
+
const tagMap = await this.collectTags();
|
|
214
|
+
|
|
215
|
+
// Check if target directly matches a tag
|
|
216
|
+
const directMatch = tagMap.get(this.target);
|
|
217
|
+
if (directMatch && directMatch.length > 0) {
|
|
218
|
+
const bestTag = this.selectBestTag(directMatch);
|
|
219
|
+
if (bestTag) {
|
|
220
|
+
const tagName = this.formatTagName(bestTag);
|
|
221
|
+
if (!this.longFormat) {
|
|
222
|
+
return {
|
|
223
|
+
description: tagName,
|
|
224
|
+
tag: tagName,
|
|
225
|
+
depth: 0,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
description: this.formatLong(tagName, 0, this.target),
|
|
230
|
+
tag: tagName,
|
|
231
|
+
depth: 0,
|
|
232
|
+
abbrevHash: this.abbreviate(this.target),
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Walk ancestors to find reachable tags
|
|
238
|
+
const candidates = await this.findCandidates(tagMap, this.target);
|
|
239
|
+
|
|
240
|
+
if (candidates.length === 0) {
|
|
241
|
+
if (this.always) {
|
|
242
|
+
const abbrev = this.abbreviate(this.target);
|
|
243
|
+
return {
|
|
244
|
+
description: abbrev,
|
|
245
|
+
abbrevHash: abbrev,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
return { description: undefined };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Select the closest tag
|
|
252
|
+
const best = candidates.reduce((a, b) => (a.depth <= b.depth ? a : b));
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
description: this.formatLong(best.tag, best.depth, this.target),
|
|
256
|
+
tag: best.tag,
|
|
257
|
+
depth: best.depth,
|
|
258
|
+
abbrevHash: this.abbreviate(this.target),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Collect all tags/refs into a map by commit ID.
|
|
264
|
+
*/
|
|
265
|
+
private async collectTags(): Promise<Map<ObjectId, string[]>> {
|
|
266
|
+
const tagMap = new Map<ObjectId, string[]>();
|
|
267
|
+
const prefix = this.useAll ? "refs/" : "refs/tags/";
|
|
268
|
+
|
|
269
|
+
for await (const ref of this.refsStore.list()) {
|
|
270
|
+
if (!ref.name.startsWith(prefix)) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Skip if not matching patterns
|
|
275
|
+
if (!this.matchesPatterns(ref.name)) {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Get the commit ID (peel annotated tags)
|
|
280
|
+
let commitId: ObjectId | undefined;
|
|
281
|
+
if ("objectId" in ref && ref.objectId) {
|
|
282
|
+
commitId = ref.objectId;
|
|
283
|
+
|
|
284
|
+
// Try to peel annotated tags
|
|
285
|
+
if (!this.useTags && !this.useAll && ref.name.startsWith("refs/tags/")) {
|
|
286
|
+
// For annotated tags, we should resolve to the commit
|
|
287
|
+
// Skip lightweight tags unless useTags is set
|
|
288
|
+
try {
|
|
289
|
+
const tag = await this.tagsStore?.loadTag(ref.objectId);
|
|
290
|
+
if (tag) {
|
|
291
|
+
commitId = tag.object;
|
|
292
|
+
}
|
|
293
|
+
} catch {
|
|
294
|
+
// Not an annotated tag - skip unless useTags
|
|
295
|
+
if (!this.useTags) {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if (commitId) {
|
|
303
|
+
const tags = tagMap.get(commitId) ?? [];
|
|
304
|
+
tags.push(ref.name);
|
|
305
|
+
tagMap.set(commitId, tags);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return tagMap;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Check if a ref name matches the patterns.
|
|
314
|
+
*/
|
|
315
|
+
private matchesPatterns(refName: string): boolean {
|
|
316
|
+
const tagName = this.formatTagName(refName);
|
|
317
|
+
|
|
318
|
+
// Check exclude patterns first
|
|
319
|
+
for (const pattern of this.excludePatterns) {
|
|
320
|
+
if (this.globMatch(tagName, pattern)) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// If no match patterns, accept all
|
|
326
|
+
if (this.matchPatterns.length === 0) {
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Check match patterns
|
|
331
|
+
for (const pattern of this.matchPatterns) {
|
|
332
|
+
if (this.globMatch(tagName, pattern)) {
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Simple glob matching (supports * and ?).
|
|
342
|
+
*/
|
|
343
|
+
private globMatch(text: string, pattern: string): boolean {
|
|
344
|
+
const regex = pattern
|
|
345
|
+
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
346
|
+
.replace(/\*/g, ".*")
|
|
347
|
+
.replace(/\?/g, ".");
|
|
348
|
+
return new RegExp(`^${regex}$`).test(text);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Select the best tag from multiple candidates.
|
|
353
|
+
*/
|
|
354
|
+
private selectBestTag(tags: string[]): string | undefined {
|
|
355
|
+
if (tags.length === 0) return undefined;
|
|
356
|
+
|
|
357
|
+
// Filter and sort - prefer annotated tags, then alphabetically
|
|
358
|
+
const filtered = tags.filter((t) => this.matchesPatterns(t));
|
|
359
|
+
if (filtered.length === 0) return undefined;
|
|
360
|
+
|
|
361
|
+
filtered.sort();
|
|
362
|
+
return filtered[0];
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Find tag candidates by walking ancestors.
|
|
367
|
+
*/
|
|
368
|
+
private async findCandidates(
|
|
369
|
+
tagMap: Map<ObjectId, string[]>,
|
|
370
|
+
target: ObjectId,
|
|
371
|
+
): Promise<Array<{ tag: string; depth: number }>> {
|
|
372
|
+
const candidates: Array<{ tag: string; depth: number; commitId: ObjectId }> = [];
|
|
373
|
+
const seen = new Set<ObjectId>();
|
|
374
|
+
let depth = 0;
|
|
375
|
+
|
|
376
|
+
// BFS through ancestors
|
|
377
|
+
const queue: ObjectId[] = [target];
|
|
378
|
+
|
|
379
|
+
while (queue.length > 0 && candidates.length < this.maxCandidates) {
|
|
380
|
+
const nextQueue: ObjectId[] = [];
|
|
381
|
+
|
|
382
|
+
for (const commitId of queue) {
|
|
383
|
+
if (seen.has(commitId)) continue;
|
|
384
|
+
seen.add(commitId);
|
|
385
|
+
|
|
386
|
+
// Check if this commit has tags
|
|
387
|
+
const tags = tagMap.get(commitId);
|
|
388
|
+
if (tags && tags.length > 0) {
|
|
389
|
+
const bestTag = this.selectBestTag(tags);
|
|
390
|
+
if (bestTag) {
|
|
391
|
+
candidates.push({
|
|
392
|
+
tag: this.formatTagName(bestTag),
|
|
393
|
+
depth,
|
|
394
|
+
commitId,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Add parents to queue
|
|
400
|
+
try {
|
|
401
|
+
const commit = await this.commits.load(commitId);
|
|
402
|
+
if (commit) {
|
|
403
|
+
for (const parentId of commit.parents) {
|
|
404
|
+
if (!seen.has(parentId)) {
|
|
405
|
+
nextQueue.push(parentId);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
} catch {
|
|
410
|
+
// Commit not found - skip
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
queue.length = 0;
|
|
415
|
+
queue.push(...nextQueue);
|
|
416
|
+
depth++;
|
|
417
|
+
|
|
418
|
+
// Safety limit
|
|
419
|
+
if (depth > 10000) break;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
return candidates;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Format a ref name to tag name.
|
|
427
|
+
*/
|
|
428
|
+
private formatTagName(refName: string): string {
|
|
429
|
+
if (refName.startsWith("refs/tags/")) {
|
|
430
|
+
return refName.slice("refs/tags/".length);
|
|
431
|
+
}
|
|
432
|
+
if (refName.startsWith("refs/")) {
|
|
433
|
+
return refName.slice("refs/".length);
|
|
434
|
+
}
|
|
435
|
+
return refName;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Format long description: tag-depth-gSHA.
|
|
440
|
+
*/
|
|
441
|
+
private formatLong(tag: string, depth: number, commitId: ObjectId): string {
|
|
442
|
+
if (this.abbrev === 0) {
|
|
443
|
+
return tag;
|
|
444
|
+
}
|
|
445
|
+
return `${tag}-${depth}-g${this.abbreviate(commitId)}`;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Abbreviate a commit ID.
|
|
450
|
+
*/
|
|
451
|
+
private abbreviate(commitId: ObjectId): string {
|
|
452
|
+
return commitId.slice(0, this.abbrev);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import type { FileModeValue, ObjectId, TreeEntry } from "@statewalker/vcs-core";
|
|
2
|
+
|
|
3
|
+
import { GitCommand } from "../git-command.js";
|
|
4
|
+
import {
|
|
5
|
+
ChangeType,
|
|
6
|
+
createAddEntry,
|
|
7
|
+
createDeleteEntry,
|
|
8
|
+
createModifyEntry,
|
|
9
|
+
type DiffEntry,
|
|
10
|
+
} from "../results/diff-entry.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Show changes between commits, trees, and staging.
|
|
14
|
+
*
|
|
15
|
+
* Equivalent to `git diff`.
|
|
16
|
+
*
|
|
17
|
+
* Based on JGit's DiffCommand.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Compare two commits
|
|
22
|
+
* const entries = await git.diff()
|
|
23
|
+
* .setOldTree(commitA)
|
|
24
|
+
* .setNewTree(commitB)
|
|
25
|
+
* .call();
|
|
26
|
+
*
|
|
27
|
+
* // Compare HEAD to staging
|
|
28
|
+
* const staged = await git.diff()
|
|
29
|
+
* .setCached(true)
|
|
30
|
+
* .call();
|
|
31
|
+
*
|
|
32
|
+
* // Filter by path prefix
|
|
33
|
+
* const srcChanges = await git.diff()
|
|
34
|
+
* .setOldTree(commitA)
|
|
35
|
+
* .setNewTree(commitB)
|
|
36
|
+
* .setPathFilter("src/")
|
|
37
|
+
* .call();
|
|
38
|
+
*
|
|
39
|
+
* for (const entry of entries) {
|
|
40
|
+
* console.log(`${entry.changeType}: ${entry.newPath ?? entry.oldPath}`);
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export class DiffCommand extends GitCommand<DiffEntry[]> {
|
|
45
|
+
private oldTree?: string;
|
|
46
|
+
private newTree?: string;
|
|
47
|
+
private cached = false;
|
|
48
|
+
private pathFilter?: string;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Set the old (base) tree/commit for comparison.
|
|
52
|
+
*
|
|
53
|
+
* Can be a commit ID, branch name, tag name, or tree ID.
|
|
54
|
+
* If not set, defaults to HEAD.
|
|
55
|
+
*
|
|
56
|
+
* @param refOrId Commit-ish or tree ID
|
|
57
|
+
*/
|
|
58
|
+
setOldTree(refOrId: string): this {
|
|
59
|
+
this.checkCallable();
|
|
60
|
+
this.oldTree = refOrId;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Set the new tree/commit for comparison.
|
|
66
|
+
*
|
|
67
|
+
* Can be a commit ID, branch name, tag name, or tree ID.
|
|
68
|
+
* If not set and cached=true, compares to staging.
|
|
69
|
+
* If not set and cached=false, compares to HEAD.
|
|
70
|
+
*
|
|
71
|
+
* @param refOrId Commit-ish or tree ID
|
|
72
|
+
*/
|
|
73
|
+
setNewTree(refOrId: string): this {
|
|
74
|
+
this.checkCallable();
|
|
75
|
+
this.newTree = refOrId;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Compare HEAD to staging area instead of working tree.
|
|
81
|
+
*
|
|
82
|
+
* Equivalent to `git diff --cached` or `git diff --staged`.
|
|
83
|
+
*
|
|
84
|
+
* @param cached Whether to compare to staging
|
|
85
|
+
*/
|
|
86
|
+
setCached(cached: boolean): this {
|
|
87
|
+
this.checkCallable();
|
|
88
|
+
this.cached = cached;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Filter results to only paths starting with this prefix.
|
|
94
|
+
*
|
|
95
|
+
* @param path Path prefix to filter by
|
|
96
|
+
*/
|
|
97
|
+
setPathFilter(path: string): this {
|
|
98
|
+
this.checkCallable();
|
|
99
|
+
this.pathFilter = path;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Execute the diff.
|
|
105
|
+
*
|
|
106
|
+
* @returns List of diff entries representing changes
|
|
107
|
+
*/
|
|
108
|
+
async call(): Promise<DiffEntry[]> {
|
|
109
|
+
this.checkCallable();
|
|
110
|
+
this.setCallable(false);
|
|
111
|
+
|
|
112
|
+
// Resolve old tree
|
|
113
|
+
const oldTreeId = await this.resolveTreeId(this.oldTree ?? "HEAD");
|
|
114
|
+
|
|
115
|
+
// Resolve new tree
|
|
116
|
+
let newTreeId: ObjectId;
|
|
117
|
+
if (this.newTree) {
|
|
118
|
+
newTreeId = await this.resolveTreeId(this.newTree);
|
|
119
|
+
} else if (this.cached) {
|
|
120
|
+
// Compare to staging area
|
|
121
|
+
newTreeId = await this.staging.writeTree(this.trees);
|
|
122
|
+
} else {
|
|
123
|
+
// Default: compare HEAD to HEAD (no changes)
|
|
124
|
+
// In a real implementation with working tree, this would compare to working tree
|
|
125
|
+
const headRef = await this.refsStore.resolve("HEAD");
|
|
126
|
+
if (!headRef?.objectId) {
|
|
127
|
+
return [];
|
|
128
|
+
}
|
|
129
|
+
const headCommit = await this.commits.load(headRef.objectId);
|
|
130
|
+
if (!headCommit) {
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
newTreeId = headCommit.tree;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Perform tree comparison
|
|
137
|
+
return this.diffTrees(oldTreeId, newTreeId);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Resolve a ref to its tree ID.
|
|
142
|
+
*/
|
|
143
|
+
private async resolveTreeId(refOrId: string): Promise<ObjectId> {
|
|
144
|
+
// Try to resolve as a commit ref first
|
|
145
|
+
try {
|
|
146
|
+
const commitId = await this.resolveRef(refOrId);
|
|
147
|
+
const commit = await this.commits.load(commitId);
|
|
148
|
+
if (!commit) {
|
|
149
|
+
// Assume it's a tree ID directly
|
|
150
|
+
return refOrId;
|
|
151
|
+
}
|
|
152
|
+
return commit.tree;
|
|
153
|
+
} catch {
|
|
154
|
+
// Assume it's a tree ID directly
|
|
155
|
+
return refOrId;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Compare two trees and return list of differences.
|
|
161
|
+
*/
|
|
162
|
+
private async diffTrees(oldTreeId: ObjectId, newTreeId: ObjectId): Promise<DiffEntry[]> {
|
|
163
|
+
const entries: DiffEntry[] = [];
|
|
164
|
+
|
|
165
|
+
// Collect entries from both trees
|
|
166
|
+
const oldPaths = new Map<string, TreeEntry>();
|
|
167
|
+
const newPaths = new Map<string, TreeEntry>();
|
|
168
|
+
|
|
169
|
+
await this.collectTreeEntries(oldTreeId, "", oldPaths);
|
|
170
|
+
await this.collectTreeEntries(newTreeId, "", newPaths);
|
|
171
|
+
|
|
172
|
+
// Find deleted and modified files
|
|
173
|
+
for (const [path, oldEntry] of oldPaths) {
|
|
174
|
+
if (this.pathFilter && !path.startsWith(this.pathFilter)) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const newEntry = newPaths.get(path);
|
|
179
|
+
if (!newEntry) {
|
|
180
|
+
// File was deleted
|
|
181
|
+
entries.push(createDeleteEntry(path, oldEntry.id, oldEntry.mode as FileModeValue));
|
|
182
|
+
} else if (oldEntry.id !== newEntry.id || oldEntry.mode !== newEntry.mode) {
|
|
183
|
+
// File was modified
|
|
184
|
+
entries.push(
|
|
185
|
+
createModifyEntry(
|
|
186
|
+
path,
|
|
187
|
+
oldEntry.id,
|
|
188
|
+
newEntry.id,
|
|
189
|
+
oldEntry.mode as FileModeValue,
|
|
190
|
+
newEntry.mode as FileModeValue,
|
|
191
|
+
),
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Find added files
|
|
197
|
+
for (const [path, newEntry] of newPaths) {
|
|
198
|
+
if (this.pathFilter && !path.startsWith(this.pathFilter)) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (!oldPaths.has(path)) {
|
|
203
|
+
entries.push(createAddEntry(path, newEntry.id, newEntry.mode as FileModeValue));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Sort entries by path for consistent output
|
|
208
|
+
entries.sort((a, b) => {
|
|
209
|
+
const pathA = a.newPath ?? a.oldPath ?? "";
|
|
210
|
+
const pathB = b.newPath ?? b.oldPath ?? "";
|
|
211
|
+
return pathA.localeCompare(pathB);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
return entries;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Collect all blob entries from a tree recursively.
|
|
219
|
+
*/
|
|
220
|
+
private async collectTreeEntries(
|
|
221
|
+
treeId: ObjectId,
|
|
222
|
+
prefix: string,
|
|
223
|
+
entries: Map<string, TreeEntry>,
|
|
224
|
+
): Promise<void> {
|
|
225
|
+
const TREE_MODE = 0o40000;
|
|
226
|
+
|
|
227
|
+
for await (const entry of this.trees.loadTree(treeId)) {
|
|
228
|
+
const path = prefix ? `${prefix}/${entry.name}` : entry.name;
|
|
229
|
+
|
|
230
|
+
if (entry.mode === TREE_MODE) {
|
|
231
|
+
// Recurse into subtree
|
|
232
|
+
await this.collectTreeEntries(entry.id, path, entries);
|
|
233
|
+
} else {
|
|
234
|
+
// Blob entry
|
|
235
|
+
entries.set(path, entry);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Format a DiffEntry for display.
|
|
243
|
+
*/
|
|
244
|
+
export function formatDiffEntry(entry: DiffEntry): string {
|
|
245
|
+
switch (entry.changeType) {
|
|
246
|
+
case ChangeType.ADD:
|
|
247
|
+
return `A\t${entry.newPath}`;
|
|
248
|
+
case ChangeType.DELETE:
|
|
249
|
+
return `D\t${entry.oldPath}`;
|
|
250
|
+
case ChangeType.MODIFY:
|
|
251
|
+
return `M\t${entry.newPath}`;
|
|
252
|
+
case ChangeType.RENAME:
|
|
253
|
+
return `R${entry.score ?? 0}\t${entry.oldPath}\t${entry.newPath}`;
|
|
254
|
+
case ChangeType.COPY:
|
|
255
|
+
return `C${entry.score ?? 0}\t${entry.oldPath}\t${entry.newPath}`;
|
|
256
|
+
}
|
|
257
|
+
}
|