@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,274 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AddCommand - Stages files from working tree to index.
|
|
3
|
+
*
|
|
4
|
+
* Implements `git add` functionality:
|
|
5
|
+
* - Adds new files to index
|
|
6
|
+
* - Updates modified files in index
|
|
7
|
+
* - Optionally removes deleted files (--all mode)
|
|
8
|
+
*
|
|
9
|
+
* Reference: jgit/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { type Blobs, FileMode } from "@statewalker/vcs-core";
|
|
13
|
+
import {
|
|
14
|
+
DeleteStagingEntry,
|
|
15
|
+
type Staging,
|
|
16
|
+
UpdateStagingEntry,
|
|
17
|
+
type Worktree,
|
|
18
|
+
} from "@statewalker/vcs-working-tree";
|
|
19
|
+
import type { Add, AddOptions, AddResult } from "./add.command.js";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Options for creating an AddCommand.
|
|
23
|
+
*/
|
|
24
|
+
export interface AddCommandOptions {
|
|
25
|
+
/** Working tree iterator */
|
|
26
|
+
worktree: Worktree;
|
|
27
|
+
|
|
28
|
+
/** Blob storage for file content */
|
|
29
|
+
blobs: Blobs;
|
|
30
|
+
|
|
31
|
+
/** Staging area (index) */
|
|
32
|
+
staging: Staging;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Check if a path matches any pattern.
|
|
37
|
+
*
|
|
38
|
+
* Supports simple glob patterns:
|
|
39
|
+
* - * matches any string (including path separators for extension patterns like *.ts)
|
|
40
|
+
* - ** matches any path segments
|
|
41
|
+
* - ? matches single character
|
|
42
|
+
*/
|
|
43
|
+
function matchesPattern(path: string, patterns: string[]): boolean {
|
|
44
|
+
for (const pattern of patterns) {
|
|
45
|
+
if (matchGlob(path, pattern)) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Simple glob matching.
|
|
54
|
+
*/
|
|
55
|
+
function matchGlob(path: string, pattern: string): boolean {
|
|
56
|
+
// Exact match
|
|
57
|
+
if (path === pattern) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Directory prefix (add "dir" matches "dir/file")
|
|
62
|
+
if (path.startsWith(`${pattern}/`)) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Handle extension patterns like "*.ts" - should match any .ts file anywhere
|
|
67
|
+
if (pattern.startsWith("*.") && !pattern.includes("/")) {
|
|
68
|
+
const extension = pattern.slice(1); // e.g., ".ts"
|
|
69
|
+
return path.endsWith(extension);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Handle **/* pattern (matches all files)
|
|
73
|
+
if (pattern === "**/*") {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Simple * glob (e.g., "src/*" or "**/*.ts")
|
|
78
|
+
if (pattern.includes("*")) {
|
|
79
|
+
// Escape special regex characters except * and ?
|
|
80
|
+
const escapedPattern = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
81
|
+
|
|
82
|
+
// ** matches anything including path separators
|
|
83
|
+
// * matches anything except path separators
|
|
84
|
+
const regexPattern = escapedPattern
|
|
85
|
+
.replace(/\*\*/g, "<<<DOUBLESTAR>>>")
|
|
86
|
+
.replace(/\*/g, "[^/]*")
|
|
87
|
+
.replace(/<<<DOUBLESTAR>>>/g, ".*")
|
|
88
|
+
.replace(/\?/g, ".");
|
|
89
|
+
|
|
90
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
91
|
+
return regex.test(path);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* AddCommand implementation.
|
|
99
|
+
*/
|
|
100
|
+
export class AddCommand implements Add {
|
|
101
|
+
private readonly worktree: Worktree;
|
|
102
|
+
private readonly blobs: Blobs;
|
|
103
|
+
private readonly staging: Staging;
|
|
104
|
+
|
|
105
|
+
constructor(options: AddCommandOptions) {
|
|
106
|
+
this.worktree = options.worktree;
|
|
107
|
+
this.blobs = options.blobs;
|
|
108
|
+
this.staging = options.staging;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Add files matching patterns to staging area.
|
|
113
|
+
*/
|
|
114
|
+
async add(filePatterns: string[], options: AddOptions = {}): Promise<AddResult> {
|
|
115
|
+
const { update = false, all = false, intentToAdd = false, force = false, onProgress } = options;
|
|
116
|
+
|
|
117
|
+
const added: string[] = [];
|
|
118
|
+
const skipped: string[] = [];
|
|
119
|
+
const removed: string[] = [];
|
|
120
|
+
|
|
121
|
+
// Collect files to process
|
|
122
|
+
const filesToProcess: Array<{ path: string; exists: boolean; isIgnored: boolean }> = [];
|
|
123
|
+
|
|
124
|
+
// Walk worktree to find matching files
|
|
125
|
+
for await (const entry of this.worktree.walk({ includeIgnored: true })) {
|
|
126
|
+
if (entry.isDirectory) continue;
|
|
127
|
+
|
|
128
|
+
// Check if file matches any pattern
|
|
129
|
+
if (!matchesPattern(entry.path, filePatterns)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Skip ignored files unless force
|
|
134
|
+
if (entry.isIgnored && !force) {
|
|
135
|
+
skipped.push(entry.path);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
filesToProcess.push({
|
|
140
|
+
path: entry.path,
|
|
141
|
+
exists: true,
|
|
142
|
+
isIgnored: entry.isIgnored,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// If update or all mode, check for deleted files in index
|
|
147
|
+
if (update || all) {
|
|
148
|
+
const indexedPaths = new Set<string>();
|
|
149
|
+
for await (const entry of this.staging.entries()) {
|
|
150
|
+
if (entry.stage === 0) {
|
|
151
|
+
indexedPaths.add(entry.path);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Find paths in index that match patterns but not in worktree
|
|
156
|
+
for (const indexPath of Array.from(indexedPaths)) {
|
|
157
|
+
if (!matchesPattern(indexPath, filePatterns)) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const worktreeEntry = await this.worktree.getEntry(indexPath);
|
|
162
|
+
if (!worktreeEntry) {
|
|
163
|
+
// File was deleted from worktree
|
|
164
|
+
filesToProcess.push({
|
|
165
|
+
path: indexPath,
|
|
166
|
+
exists: false,
|
|
167
|
+
isIgnored: false,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// If update mode, filter to only tracked files
|
|
174
|
+
if (update) {
|
|
175
|
+
const trackedPaths = new Set<string>();
|
|
176
|
+
for await (const entry of this.staging.entries()) {
|
|
177
|
+
if (entry.stage === 0) {
|
|
178
|
+
trackedPaths.add(entry.path);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const filteredFiles = filesToProcess.filter((f) => trackedPaths.has(f.path));
|
|
183
|
+
filesToProcess.length = 0;
|
|
184
|
+
filesToProcess.push(...filteredFiles);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Process files
|
|
188
|
+
const editor = this.staging.createEditor();
|
|
189
|
+
const total = filesToProcess.length;
|
|
190
|
+
let current = 0;
|
|
191
|
+
|
|
192
|
+
for (const file of filesToProcess) {
|
|
193
|
+
current++;
|
|
194
|
+
onProgress?.(file.path, current, total);
|
|
195
|
+
|
|
196
|
+
if (!file.exists) {
|
|
197
|
+
// File deleted from worktree - remove from index
|
|
198
|
+
editor.add(new DeleteStagingEntry(file.path));
|
|
199
|
+
removed.push(file.path);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (intentToAdd) {
|
|
204
|
+
// Just mark as intent-to-add (placeholder)
|
|
205
|
+
const worktreeEntry = await this.worktree.getEntry(file.path);
|
|
206
|
+
const mode = worktreeEntry?.mode ?? FileMode.REGULAR_FILE;
|
|
207
|
+
editor.add(
|
|
208
|
+
new UpdateStagingEntry(file.path, "", mode, {
|
|
209
|
+
size: 0,
|
|
210
|
+
mtime: Date.now(),
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
added.push(file.path);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Get file info
|
|
218
|
+
const worktreeEntry = await this.worktree.getEntry(file.path);
|
|
219
|
+
if (!worktreeEntry) {
|
|
220
|
+
// File disappeared during processing
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Hash and store content
|
|
225
|
+
const objectId = await this.storeFileContent(file.path);
|
|
226
|
+
|
|
227
|
+
// Add to staging
|
|
228
|
+
editor.add(
|
|
229
|
+
new UpdateStagingEntry(file.path, objectId, worktreeEntry.mode, {
|
|
230
|
+
size: worktreeEntry.size,
|
|
231
|
+
mtime: worktreeEntry.mtime,
|
|
232
|
+
}),
|
|
233
|
+
);
|
|
234
|
+
added.push(file.path);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Apply changes
|
|
238
|
+
await editor.finish();
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
added,
|
|
242
|
+
skipped,
|
|
243
|
+
removed,
|
|
244
|
+
totalProcessed: added.length + removed.length,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Add all files in working tree to staging area.
|
|
250
|
+
*/
|
|
251
|
+
async addAll(options: AddOptions = {}): Promise<AddResult> {
|
|
252
|
+
return this.add(["**/*"], { ...options, all: true });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Store file content as blob and return object ID.
|
|
257
|
+
*/
|
|
258
|
+
private async storeFileContent(path: string): Promise<string> {
|
|
259
|
+
// Read content from working tree
|
|
260
|
+
const content = this.worktree.readContent(path);
|
|
261
|
+
// Store using BlobStore which handles Git header automatically
|
|
262
|
+
return await this.blobs.store(content);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Create an AddCommand.
|
|
268
|
+
*
|
|
269
|
+
* @param options Command options
|
|
270
|
+
* @returns New AddCommand instance
|
|
271
|
+
*/
|
|
272
|
+
export function createAddCommand(options: AddCommandOptions): Add {
|
|
273
|
+
return new AddCommand(options);
|
|
274
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add command interface for staging files.
|
|
3
|
+
*
|
|
4
|
+
* Adds files from the working tree to the staging area (index).
|
|
5
|
+
* Similar to `git add` command.
|
|
6
|
+
*
|
|
7
|
+
* Reference: jgit/org.eclipse.jgit/src/org/eclipse/jgit/api/AddCommand.java
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Options for adding files.
|
|
12
|
+
*/
|
|
13
|
+
export interface AddOptions {
|
|
14
|
+
/**
|
|
15
|
+
* Update only files already tracked in the index.
|
|
16
|
+
* Default: false (add new files too)
|
|
17
|
+
*/
|
|
18
|
+
update?: boolean;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Also remove deleted files from index.
|
|
22
|
+
* Default: false
|
|
23
|
+
*/
|
|
24
|
+
all?: boolean;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Only add intent-to-add entries (placeholders).
|
|
28
|
+
* Default: false
|
|
29
|
+
*/
|
|
30
|
+
intentToAdd?: boolean;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Force adding ignored files.
|
|
34
|
+
* Default: false
|
|
35
|
+
*/
|
|
36
|
+
force?: boolean;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Progress callback.
|
|
40
|
+
*/
|
|
41
|
+
onProgress?: (path: string, current: number, total: number) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Result of add operation.
|
|
46
|
+
*/
|
|
47
|
+
export interface AddResult {
|
|
48
|
+
/** Paths that were added or updated */
|
|
49
|
+
added: string[];
|
|
50
|
+
|
|
51
|
+
/** Paths that were skipped (ignored) */
|
|
52
|
+
skipped: string[];
|
|
53
|
+
|
|
54
|
+
/** Paths that were removed (deleted from worktree, all mode) */
|
|
55
|
+
removed: string[];
|
|
56
|
+
|
|
57
|
+
/** Total files processed */
|
|
58
|
+
totalProcessed: number;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Add command interface.
|
|
63
|
+
*/
|
|
64
|
+
export interface Add {
|
|
65
|
+
/**
|
|
66
|
+
* Add files matching patterns to staging area.
|
|
67
|
+
*
|
|
68
|
+
* @param filePatterns File patterns (globs or exact paths)
|
|
69
|
+
* @param options Add options
|
|
70
|
+
* @returns Add result
|
|
71
|
+
*/
|
|
72
|
+
add(filePatterns: string[], options?: AddOptions): Promise<AddResult>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Add all files in working tree to staging area.
|
|
76
|
+
*
|
|
77
|
+
* @param options Add options
|
|
78
|
+
* @returns Add result
|
|
79
|
+
*/
|
|
80
|
+
addAll(options?: AddOptions): Promise<AddResult>;
|
|
81
|
+
}
|