@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,162 @@
|
|
|
1
|
+
import type { FileModeValue, ObjectId } from "@statewalker/vcs-core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Type of change in a diff entry.
|
|
5
|
+
*/
|
|
6
|
+
export enum ChangeType {
|
|
7
|
+
/** File was added */
|
|
8
|
+
ADD = "ADD",
|
|
9
|
+
|
|
10
|
+
/** File was modified */
|
|
11
|
+
MODIFY = "MODIFY",
|
|
12
|
+
|
|
13
|
+
/** File was deleted */
|
|
14
|
+
DELETE = "DELETE",
|
|
15
|
+
|
|
16
|
+
/** File was renamed */
|
|
17
|
+
RENAME = "RENAME",
|
|
18
|
+
|
|
19
|
+
/** File was copied */
|
|
20
|
+
COPY = "COPY",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Represents a change to a single file between two trees/commits.
|
|
25
|
+
*
|
|
26
|
+
* Based on JGit's DiffEntry class.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* for (const entry of diffEntries) {
|
|
31
|
+
* switch (entry.changeType) {
|
|
32
|
+
* case ChangeType.ADD:
|
|
33
|
+
* console.log(`Added: ${entry.newPath}`);
|
|
34
|
+
* break;
|
|
35
|
+
* case ChangeType.DELETE:
|
|
36
|
+
* console.log(`Deleted: ${entry.oldPath}`);
|
|
37
|
+
* break;
|
|
38
|
+
* case ChangeType.MODIFY:
|
|
39
|
+
* console.log(`Modified: ${entry.newPath}`);
|
|
40
|
+
* break;
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export interface DiffEntry {
|
|
46
|
+
/** Type of change */
|
|
47
|
+
changeType: ChangeType;
|
|
48
|
+
|
|
49
|
+
/** Path in the old tree (null for ADD) */
|
|
50
|
+
oldPath?: string;
|
|
51
|
+
|
|
52
|
+
/** Path in the new tree (null for DELETE) */
|
|
53
|
+
newPath?: string;
|
|
54
|
+
|
|
55
|
+
/** Object ID in the old tree (null for ADD) */
|
|
56
|
+
oldId?: ObjectId;
|
|
57
|
+
|
|
58
|
+
/** Object ID in the new tree (null for DELETE) */
|
|
59
|
+
newId?: ObjectId;
|
|
60
|
+
|
|
61
|
+
/** File mode in the old tree (null for ADD) */
|
|
62
|
+
oldMode?: FileModeValue;
|
|
63
|
+
|
|
64
|
+
/** File mode in the new tree (null for DELETE) */
|
|
65
|
+
newMode?: FileModeValue;
|
|
66
|
+
|
|
67
|
+
/** Similarity score for RENAME/COPY (0-100) */
|
|
68
|
+
score?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Create a DiffEntry for an added file.
|
|
73
|
+
*/
|
|
74
|
+
export function createAddEntry(path: string, id: ObjectId, mode: FileModeValue): DiffEntry {
|
|
75
|
+
return {
|
|
76
|
+
changeType: ChangeType.ADD,
|
|
77
|
+
newPath: path,
|
|
78
|
+
newId: id,
|
|
79
|
+
newMode: mode,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Create a DiffEntry for a deleted file.
|
|
85
|
+
*/
|
|
86
|
+
export function createDeleteEntry(path: string, id: ObjectId, mode: FileModeValue): DiffEntry {
|
|
87
|
+
return {
|
|
88
|
+
changeType: ChangeType.DELETE,
|
|
89
|
+
oldPath: path,
|
|
90
|
+
oldId: id,
|
|
91
|
+
oldMode: mode,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Create a DiffEntry for a modified file.
|
|
97
|
+
*/
|
|
98
|
+
export function createModifyEntry(
|
|
99
|
+
path: string,
|
|
100
|
+
oldId: ObjectId,
|
|
101
|
+
newId: ObjectId,
|
|
102
|
+
oldMode: FileModeValue,
|
|
103
|
+
newMode: FileModeValue,
|
|
104
|
+
): DiffEntry {
|
|
105
|
+
return {
|
|
106
|
+
changeType: ChangeType.MODIFY,
|
|
107
|
+
oldPath: path,
|
|
108
|
+
newPath: path,
|
|
109
|
+
oldId,
|
|
110
|
+
newId,
|
|
111
|
+
oldMode,
|
|
112
|
+
newMode,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Create a DiffEntry for a renamed file.
|
|
118
|
+
*/
|
|
119
|
+
export function createRenameEntry(
|
|
120
|
+
oldPath: string,
|
|
121
|
+
newPath: string,
|
|
122
|
+
oldId: ObjectId,
|
|
123
|
+
newId: ObjectId,
|
|
124
|
+
oldMode: FileModeValue,
|
|
125
|
+
newMode: FileModeValue,
|
|
126
|
+
score: number,
|
|
127
|
+
): DiffEntry {
|
|
128
|
+
return {
|
|
129
|
+
changeType: ChangeType.RENAME,
|
|
130
|
+
oldPath,
|
|
131
|
+
newPath,
|
|
132
|
+
oldId,
|
|
133
|
+
newId,
|
|
134
|
+
oldMode,
|
|
135
|
+
newMode,
|
|
136
|
+
score,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Create a DiffEntry for a copied file.
|
|
142
|
+
*/
|
|
143
|
+
export function createCopyEntry(
|
|
144
|
+
oldPath: string,
|
|
145
|
+
newPath: string,
|
|
146
|
+
oldId: ObjectId,
|
|
147
|
+
newId: ObjectId,
|
|
148
|
+
oldMode: FileModeValue,
|
|
149
|
+
newMode: FileModeValue,
|
|
150
|
+
score: number,
|
|
151
|
+
): DiffEntry {
|
|
152
|
+
return {
|
|
153
|
+
changeType: ChangeType.COPY,
|
|
154
|
+
oldPath,
|
|
155
|
+
newPath,
|
|
156
|
+
oldId,
|
|
157
|
+
newId,
|
|
158
|
+
oldMode,
|
|
159
|
+
newMode,
|
|
160
|
+
score,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formatter for generating unified diff output from DiffEntry objects.
|
|
3
|
+
*
|
|
4
|
+
* Based on JGit's DiffFormatter class.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { Blobs, ObjectId } from "@statewalker/vcs-core";
|
|
8
|
+
import {
|
|
9
|
+
DEFAULT_ALGORITHM,
|
|
10
|
+
type DiffAlgorithm,
|
|
11
|
+
type EditList,
|
|
12
|
+
getAlgorithm,
|
|
13
|
+
RawText,
|
|
14
|
+
RawTextComparator,
|
|
15
|
+
type SupportedAlgorithm,
|
|
16
|
+
} from "@statewalker/vcs-utils";
|
|
17
|
+
|
|
18
|
+
import { ChangeType, type DiffEntry } from "./diff-entry.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Options for diff formatting.
|
|
22
|
+
*/
|
|
23
|
+
export interface DiffFormatterOptions {
|
|
24
|
+
/** Number of context lines around changes (default: 3) */
|
|
25
|
+
contextLines?: number;
|
|
26
|
+
/** Whether to include file headers (default: true) */
|
|
27
|
+
includeHeaders?: boolean;
|
|
28
|
+
/** Whether to abbreviate object IDs (default: true) */
|
|
29
|
+
abbreviateIds?: boolean;
|
|
30
|
+
/** Length of abbreviated object IDs (default: 7) */
|
|
31
|
+
abbreviationLength?: number;
|
|
32
|
+
/** Diff algorithm to use (default: histogram) */
|
|
33
|
+
algorithm?: SupportedAlgorithm;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A hunk in unified diff format.
|
|
38
|
+
*/
|
|
39
|
+
export interface DiffHunk {
|
|
40
|
+
/** Starting line in old file (1-based) */
|
|
41
|
+
oldStart: number;
|
|
42
|
+
/** Number of lines from old file */
|
|
43
|
+
oldCount: number;
|
|
44
|
+
/** Starting line in new file (1-based) */
|
|
45
|
+
newStart: number;
|
|
46
|
+
/** Number of lines from new file */
|
|
47
|
+
newCount: number;
|
|
48
|
+
/** Lines in the hunk (prefixed with ' ', '+', or '-') */
|
|
49
|
+
lines: string[];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Content diff result for a single file.
|
|
54
|
+
*/
|
|
55
|
+
export interface FileContentDiff {
|
|
56
|
+
/** The original diff entry */
|
|
57
|
+
entry: DiffEntry;
|
|
58
|
+
/** Whether the file is binary */
|
|
59
|
+
isBinary: boolean;
|
|
60
|
+
/** File header line (e.g., "diff --git a/path b/path") */
|
|
61
|
+
header: string;
|
|
62
|
+
/** Old file header (e.g., "--- a/path") */
|
|
63
|
+
oldFileHeader?: string;
|
|
64
|
+
/** New file header (e.g., "+++ b/path") */
|
|
65
|
+
newFileHeader?: string;
|
|
66
|
+
/** Index line (e.g., "index abc123..def456 100644") */
|
|
67
|
+
indexLine?: string;
|
|
68
|
+
/** Hunks of changes */
|
|
69
|
+
hunks: DiffHunk[];
|
|
70
|
+
/** Raw old content (for binary display or further processing) */
|
|
71
|
+
oldContent?: Uint8Array;
|
|
72
|
+
/** Raw new content (for binary display or further processing) */
|
|
73
|
+
newContent?: Uint8Array;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Format DiffEntry objects into unified diff format.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const formatter = new DiffFormatter(store.blobs);
|
|
82
|
+
*
|
|
83
|
+
* // Get diff entries from DiffCommand
|
|
84
|
+
* const entries = await git.diff().setOldTree(commitA).setNewTree(commitB).call();
|
|
85
|
+
*
|
|
86
|
+
* // Format as unified diff
|
|
87
|
+
* for (const entry of entries) {
|
|
88
|
+
* const diff = await formatter.format(entry);
|
|
89
|
+
* console.log(formatter.toString(diff));
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export class DiffFormatter {
|
|
94
|
+
private contextLines: number;
|
|
95
|
+
private includeHeaders: boolean;
|
|
96
|
+
private abbreviateIds: boolean;
|
|
97
|
+
private abbreviationLength: number;
|
|
98
|
+
private diffAlgorithm: DiffAlgorithm;
|
|
99
|
+
|
|
100
|
+
constructor(
|
|
101
|
+
private readonly blobs: Blobs,
|
|
102
|
+
options: DiffFormatterOptions = {},
|
|
103
|
+
) {
|
|
104
|
+
this.contextLines = options.contextLines ?? 3;
|
|
105
|
+
this.includeHeaders = options.includeHeaders ?? true;
|
|
106
|
+
this.abbreviateIds = options.abbreviateIds ?? true;
|
|
107
|
+
this.abbreviationLength = options.abbreviationLength ?? 7;
|
|
108
|
+
this.diffAlgorithm = getAlgorithm(options.algorithm ?? DEFAULT_ALGORITHM);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Format a DiffEntry into a content diff.
|
|
113
|
+
*
|
|
114
|
+
* @param entry The diff entry to format
|
|
115
|
+
* @returns Content diff with hunks
|
|
116
|
+
*/
|
|
117
|
+
async format(entry: DiffEntry): Promise<FileContentDiff> {
|
|
118
|
+
// Load content for old and new
|
|
119
|
+
const oldContent = entry.oldId ? await this.loadContent(entry.oldId) : null;
|
|
120
|
+
const newContent = entry.newId ? await this.loadContent(entry.newId) : null;
|
|
121
|
+
|
|
122
|
+
// Check for binary content
|
|
123
|
+
const isBinary =
|
|
124
|
+
(oldContent !== null && RawText.isBinary(oldContent)) ||
|
|
125
|
+
(newContent !== null && RawText.isBinary(newContent));
|
|
126
|
+
|
|
127
|
+
// Build headers
|
|
128
|
+
const result: FileContentDiff = {
|
|
129
|
+
entry,
|
|
130
|
+
isBinary,
|
|
131
|
+
header: this.buildHeader(entry),
|
|
132
|
+
hunks: [],
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
if (this.includeHeaders) {
|
|
136
|
+
result.indexLine = this.buildIndexLine(entry);
|
|
137
|
+
result.oldFileHeader = this.buildOldFileHeader(entry);
|
|
138
|
+
result.newFileHeader = this.buildNewFileHeader(entry);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (oldContent) {
|
|
142
|
+
result.oldContent = oldContent;
|
|
143
|
+
}
|
|
144
|
+
if (newContent) {
|
|
145
|
+
result.newContent = newContent;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Don't generate text diff for binary files
|
|
149
|
+
if (isBinary) {
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Generate hunks for text files
|
|
154
|
+
const oldText = oldContent ? new RawText(oldContent) : RawText.EMPTY_TEXT;
|
|
155
|
+
const newText = newContent ? new RawText(newContent) : RawText.EMPTY_TEXT;
|
|
156
|
+
|
|
157
|
+
const edits = this.diffAlgorithm(RawTextComparator.DEFAULT, oldText, newText);
|
|
158
|
+
|
|
159
|
+
if (edits.length > 0) {
|
|
160
|
+
result.hunks = this.formatEdits(oldText, newText, edits);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Convert a FileContentDiff to string format.
|
|
168
|
+
*
|
|
169
|
+
* @param diff The content diff to stringify
|
|
170
|
+
* @returns Unified diff string
|
|
171
|
+
*/
|
|
172
|
+
toString(diff: FileContentDiff): string {
|
|
173
|
+
const lines: string[] = [];
|
|
174
|
+
|
|
175
|
+
if (this.includeHeaders) {
|
|
176
|
+
lines.push(diff.header);
|
|
177
|
+
if (diff.indexLine) {
|
|
178
|
+
lines.push(diff.indexLine);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (diff.isBinary) {
|
|
183
|
+
lines.push(
|
|
184
|
+
`Binary files ${diff.entry.oldPath ?? "/dev/null"} and ${diff.entry.newPath ?? "/dev/null"} differ`,
|
|
185
|
+
);
|
|
186
|
+
return lines.join("\n");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (this.includeHeaders && diff.hunks.length > 0) {
|
|
190
|
+
if (diff.oldFileHeader) {
|
|
191
|
+
lines.push(diff.oldFileHeader);
|
|
192
|
+
}
|
|
193
|
+
if (diff.newFileHeader) {
|
|
194
|
+
lines.push(diff.newFileHeader);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
for (const hunk of diff.hunks) {
|
|
199
|
+
lines.push(this.formatHunkHeader(hunk));
|
|
200
|
+
lines.push(...hunk.lines);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return lines.join("\n");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Format all diff entries to a single string.
|
|
208
|
+
*
|
|
209
|
+
* @param entries Array of diff entries
|
|
210
|
+
* @returns Complete unified diff output
|
|
211
|
+
*/
|
|
212
|
+
async formatAll(entries: DiffEntry[]): Promise<string> {
|
|
213
|
+
const parts: string[] = [];
|
|
214
|
+
|
|
215
|
+
for (const entry of entries) {
|
|
216
|
+
const diff = await this.format(entry);
|
|
217
|
+
parts.push(this.toString(diff));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return parts.join("\n");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Load blob content from storage.
|
|
225
|
+
*/
|
|
226
|
+
private async loadContent(objectId: ObjectId): Promise<Uint8Array> {
|
|
227
|
+
const content = await this.blobs.load(objectId);
|
|
228
|
+
if (!content) {
|
|
229
|
+
throw new Error(`Blob not found: ${objectId}`);
|
|
230
|
+
}
|
|
231
|
+
const chunks: Uint8Array[] = [];
|
|
232
|
+
for await (const chunk of content) {
|
|
233
|
+
chunks.push(chunk);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Combine chunks
|
|
237
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
238
|
+
const result = new Uint8Array(totalLength);
|
|
239
|
+
let offset = 0;
|
|
240
|
+
for (const chunk of chunks) {
|
|
241
|
+
result.set(chunk, offset);
|
|
242
|
+
offset += chunk.length;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Build the diff header line.
|
|
250
|
+
*/
|
|
251
|
+
private buildHeader(entry: DiffEntry): string {
|
|
252
|
+
const oldPath = entry.oldPath ?? entry.newPath;
|
|
253
|
+
const newPath = entry.newPath ?? entry.oldPath;
|
|
254
|
+
return `diff --git a/${oldPath} b/${newPath}`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Build the index line.
|
|
259
|
+
*/
|
|
260
|
+
private buildIndexLine(entry: DiffEntry): string {
|
|
261
|
+
const oldId = this.abbreviateId(entry.oldId ?? "0000000");
|
|
262
|
+
const newId = this.abbreviateId(entry.newId ?? "0000000");
|
|
263
|
+
const mode = entry.newMode ?? entry.oldMode;
|
|
264
|
+
const modeStr = mode ? ` ${mode.toString(8)}` : "";
|
|
265
|
+
return `index ${oldId}..${newId}${modeStr}`;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Build the old file header line.
|
|
270
|
+
*/
|
|
271
|
+
private buildOldFileHeader(entry: DiffEntry): string {
|
|
272
|
+
if (entry.changeType === ChangeType.ADD) {
|
|
273
|
+
return "--- /dev/null";
|
|
274
|
+
}
|
|
275
|
+
return `--- a/${entry.oldPath}`;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Build the new file header line.
|
|
280
|
+
*/
|
|
281
|
+
private buildNewFileHeader(entry: DiffEntry): string {
|
|
282
|
+
if (entry.changeType === ChangeType.DELETE) {
|
|
283
|
+
return "+++ /dev/null";
|
|
284
|
+
}
|
|
285
|
+
return `+++ b/${entry.newPath}`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Abbreviate an object ID.
|
|
290
|
+
*/
|
|
291
|
+
private abbreviateId(id: string): string {
|
|
292
|
+
if (!this.abbreviateIds) {
|
|
293
|
+
return id;
|
|
294
|
+
}
|
|
295
|
+
return id.slice(0, this.abbreviationLength);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Format the hunk header.
|
|
300
|
+
*/
|
|
301
|
+
private formatHunkHeader(hunk: DiffHunk): string {
|
|
302
|
+
return `@@ -${hunk.oldStart},${hunk.oldCount} +${hunk.newStart},${hunk.newCount} @@`;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Format edits into hunks.
|
|
307
|
+
*/
|
|
308
|
+
private formatEdits(oldText: RawText, newText: RawText, edits: EditList): DiffHunk[] {
|
|
309
|
+
const hunks: DiffHunk[] = [];
|
|
310
|
+
|
|
311
|
+
// Merge edits that are close together
|
|
312
|
+
const combinedEdits = this.combineEdits(edits, oldText.size(), newText.size());
|
|
313
|
+
|
|
314
|
+
for (const combinedEdit of combinedEdits) {
|
|
315
|
+
const hunk = this.createHunk(oldText, newText, combinedEdit.edits, combinedEdit.contextStart);
|
|
316
|
+
if (hunk.lines.length > 0) {
|
|
317
|
+
hunks.push(hunk);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return hunks;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Combine edits that are within context range of each other.
|
|
326
|
+
*/
|
|
327
|
+
private combineEdits(
|
|
328
|
+
edits: EditList,
|
|
329
|
+
_oldSize: number,
|
|
330
|
+
_newSize: number,
|
|
331
|
+
): { edits: EditList; contextStart: number }[] {
|
|
332
|
+
if (edits.length === 0) {
|
|
333
|
+
return [];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const result: { edits: EditList; contextStart: number }[] = [];
|
|
337
|
+
let currentEdits: EditList = [edits[0]];
|
|
338
|
+
let contextStart = Math.max(0, edits[0].beginA - this.contextLines);
|
|
339
|
+
|
|
340
|
+
for (let i = 1; i < edits.length; i++) {
|
|
341
|
+
const prevEdit = currentEdits[currentEdits.length - 1];
|
|
342
|
+
const nextEdit = edits[i];
|
|
343
|
+
|
|
344
|
+
// Check if edits are close enough to merge
|
|
345
|
+
const gap = nextEdit.beginA - prevEdit.endA;
|
|
346
|
+
if (gap <= this.contextLines * 2) {
|
|
347
|
+
// Merge into current group
|
|
348
|
+
currentEdits.push(nextEdit);
|
|
349
|
+
} else {
|
|
350
|
+
// Start new group
|
|
351
|
+
result.push({ edits: currentEdits, contextStart });
|
|
352
|
+
currentEdits = [nextEdit];
|
|
353
|
+
contextStart = Math.max(0, nextEdit.beginA - this.contextLines);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
result.push({ edits: currentEdits, contextStart });
|
|
358
|
+
return result;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Create a hunk from a group of edits.
|
|
363
|
+
*/
|
|
364
|
+
private createHunk(
|
|
365
|
+
oldText: RawText,
|
|
366
|
+
newText: RawText,
|
|
367
|
+
edits: EditList,
|
|
368
|
+
contextStart: number,
|
|
369
|
+
): DiffHunk {
|
|
370
|
+
const lines: string[] = [];
|
|
371
|
+
|
|
372
|
+
const firstEdit = edits[0];
|
|
373
|
+
const lastEdit = edits[edits.length - 1];
|
|
374
|
+
|
|
375
|
+
// Calculate hunk bounds
|
|
376
|
+
const oldStart = contextStart;
|
|
377
|
+
const oldEnd = Math.min(oldText.size(), lastEdit.endA + this.contextLines);
|
|
378
|
+
const newStart = contextStart + (firstEdit.beginB - firstEdit.beginA);
|
|
379
|
+
const newEnd = Math.min(newText.size(), lastEdit.endB + this.contextLines);
|
|
380
|
+
|
|
381
|
+
let oldPos = oldStart;
|
|
382
|
+
|
|
383
|
+
for (const edit of edits) {
|
|
384
|
+
// Add context lines before this edit
|
|
385
|
+
while (oldPos < edit.beginA) {
|
|
386
|
+
lines.push(` ${oldText.getString(oldPos)}`);
|
|
387
|
+
oldPos++;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// Add deleted lines
|
|
391
|
+
for (let i = edit.beginA; i < edit.endA; i++) {
|
|
392
|
+
lines.push(`-${oldText.getString(i)}`);
|
|
393
|
+
}
|
|
394
|
+
oldPos = edit.endA;
|
|
395
|
+
|
|
396
|
+
// Add inserted lines
|
|
397
|
+
for (let i = edit.beginB; i < edit.endB; i++) {
|
|
398
|
+
lines.push(`+${newText.getString(i)}`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Add trailing context
|
|
403
|
+
while (oldPos < oldEnd) {
|
|
404
|
+
lines.push(` ${oldText.getString(oldPos)}`);
|
|
405
|
+
oldPos++;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return {
|
|
409
|
+
oldStart: oldStart + 1, // Convert to 1-based
|
|
410
|
+
oldCount: oldEnd - oldStart,
|
|
411
|
+
newStart: newStart + 1, // Convert to 1-based
|
|
412
|
+
newCount: newEnd - newStart,
|
|
413
|
+
lines,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { ObjectId } from "@statewalker/vcs-core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Result of a ref update during fetch.
|
|
5
|
+
*
|
|
6
|
+
* Based on JGit's TrackingRefUpdate.
|
|
7
|
+
*/
|
|
8
|
+
export interface TrackingRefUpdate {
|
|
9
|
+
/** Local ref name that was updated */
|
|
10
|
+
localRef: string;
|
|
11
|
+
/** Remote ref name this tracks */
|
|
12
|
+
remoteRef: string;
|
|
13
|
+
/** Old object ID (before update) */
|
|
14
|
+
oldObjectId?: ObjectId;
|
|
15
|
+
/** New object ID (after update) */
|
|
16
|
+
newObjectId: ObjectId;
|
|
17
|
+
/** Update result status */
|
|
18
|
+
status: RefUpdateStatus;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Status of a ref update.
|
|
23
|
+
*
|
|
24
|
+
* Based on JGit's RefUpdate.Result.
|
|
25
|
+
*/
|
|
26
|
+
export enum RefUpdateStatus {
|
|
27
|
+
/** Update was performed successfully */
|
|
28
|
+
NEW = "new",
|
|
29
|
+
/** Update was a fast-forward */
|
|
30
|
+
FAST_FORWARD = "fast-forward",
|
|
31
|
+
/** Update was forced (non-fast-forward) */
|
|
32
|
+
FORCED = "forced",
|
|
33
|
+
/** No update needed (already up to date) */
|
|
34
|
+
NO_CHANGE = "no-change",
|
|
35
|
+
/** Update rejected (non-fast-forward without force) */
|
|
36
|
+
REJECTED = "rejected",
|
|
37
|
+
/** Update rejected (ref is locked) */
|
|
38
|
+
LOCK_FAILURE = "lock-failure",
|
|
39
|
+
/** Ref was deleted */
|
|
40
|
+
DELETED = "deleted",
|
|
41
|
+
/** Update renamed the ref */
|
|
42
|
+
RENAMED = "renamed",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Result of a fetch operation.
|
|
47
|
+
*
|
|
48
|
+
* Based on JGit's FetchResult.
|
|
49
|
+
*/
|
|
50
|
+
export interface FetchResult {
|
|
51
|
+
/** URI of the remote that was fetched from */
|
|
52
|
+
uri: string;
|
|
53
|
+
/** Refs advertised by the remote */
|
|
54
|
+
advertisedRefs: Map<string, ObjectId>;
|
|
55
|
+
/** Tracking ref updates that were performed */
|
|
56
|
+
trackingRefUpdates: TrackingRefUpdate[];
|
|
57
|
+
/**
|
|
58
|
+
* Default branch of the remote, exactly as advertised: the target of the
|
|
59
|
+
* `symref=HEAD:` capability, so a fully qualified `refs/heads/<name>` from
|
|
60
|
+
* any remote that qualifies it. Deliberately not normalised — see
|
|
61
|
+
* {@link CloneResult.defaultBranch} for the bare branch name.
|
|
62
|
+
*/
|
|
63
|
+
defaultBranch?: string;
|
|
64
|
+
/** Total bytes received */
|
|
65
|
+
bytesReceived: number;
|
|
66
|
+
/** Whether the remote repository was empty */
|
|
67
|
+
isEmpty: boolean;
|
|
68
|
+
/** Messages from the remote */
|
|
69
|
+
messages: string[];
|
|
70
|
+
/** Submodule fetch results (if any) */
|
|
71
|
+
submodules?: Map<string, FetchResult>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Check if fetch result has any updates.
|
|
76
|
+
*/
|
|
77
|
+
export function hasUpdates(result: FetchResult): boolean {
|
|
78
|
+
return result.trackingRefUpdates.some((u) => u.status !== RefUpdateStatus.NO_CHANGE);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Get successful updates from fetch result.
|
|
83
|
+
*/
|
|
84
|
+
export function getSuccessfulUpdates(result: FetchResult): TrackingRefUpdate[] {
|
|
85
|
+
return result.trackingRefUpdates.filter(
|
|
86
|
+
(u) =>
|
|
87
|
+
u.status === RefUpdateStatus.NEW ||
|
|
88
|
+
u.status === RefUpdateStatus.FAST_FORWARD ||
|
|
89
|
+
u.status === RefUpdateStatus.FORCED,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export * from "./cherry-pick-result.js";
|
|
2
|
+
export * from "./clone-result.js";
|
|
3
|
+
export * from "./diff-entry.js";
|
|
4
|
+
export * from "./diff-formatter.js";
|
|
5
|
+
export * from "./fetch-result.js";
|
|
6
|
+
export * from "./init-result.js";
|
|
7
|
+
export * from "./merge-result.js";
|
|
8
|
+
export * from "./pull-result.js";
|
|
9
|
+
export * from "./push-result.js";
|
|
10
|
+
export * from "./rebase-result.js";
|
|
11
|
+
export * from "./revert-result.js";
|
|
12
|
+
export * from "./stash-result.js";
|
|
13
|
+
export * from "./status-result.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { History } from "@statewalker/vcs-core";
|
|
2
|
+
import type { WorkingCopy } from "@statewalker/vcs-working-tree";
|
|
3
|
+
|
|
4
|
+
import type { Git } from "../git.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Result of repository initialization.
|
|
8
|
+
*
|
|
9
|
+
* Based on JGit's InitCommand result (returns Git instance).
|
|
10
|
+
*/
|
|
11
|
+
export interface InitResult {
|
|
12
|
+
/** Git facade for command execution */
|
|
13
|
+
git: Git;
|
|
14
|
+
|
|
15
|
+
/** WorkingCopy for direct repository access */
|
|
16
|
+
workingCopy: WorkingCopy;
|
|
17
|
+
|
|
18
|
+
/** The underlying History (repository) */
|
|
19
|
+
repository: History;
|
|
20
|
+
|
|
21
|
+
/** Initial branch name */
|
|
22
|
+
initialBranch: string;
|
|
23
|
+
|
|
24
|
+
/** Whether this is a bare repository */
|
|
25
|
+
bare: boolean;
|
|
26
|
+
|
|
27
|
+
/** Path to the .git directory */
|
|
28
|
+
gitDir: string;
|
|
29
|
+
}
|