@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,292 @@
|
|
|
1
|
+
import type { ObjectId, Ref } from "@statewalker/vcs-core";
|
|
2
|
+
import { isSymbolicRef } from "@statewalker/vcs-core";
|
|
3
|
+
|
|
4
|
+
import { RefNotFoundError } from "../errors/ref-errors.js";
|
|
5
|
+
import { GitCommand } from "../git-command.js";
|
|
6
|
+
import { ResetMode } from "../types.js";
|
|
7
|
+
import { planTreeRestore, writeTreeRestorePlan } from "./tree-restore.js";
|
|
8
|
+
|
|
9
|
+
/** Names this operation in restore failure messages. */
|
|
10
|
+
const RESET_HARD = "reset --hard";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Reset HEAD to a specified state.
|
|
14
|
+
*
|
|
15
|
+
* Equivalent to `git reset`.
|
|
16
|
+
*
|
|
17
|
+
* Based on JGit's ResetCommand.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* // Soft reset - move HEAD only
|
|
22
|
+
* await git.reset()
|
|
23
|
+
* .setRef("HEAD~1")
|
|
24
|
+
* .setMode(ResetMode.SOFT)
|
|
25
|
+
* .call();
|
|
26
|
+
*
|
|
27
|
+
* // Mixed reset (default) - move HEAD and reset staging
|
|
28
|
+
* await git.reset()
|
|
29
|
+
* .setRef("abc123")
|
|
30
|
+
* .call();
|
|
31
|
+
*
|
|
32
|
+
* // Hard reset - move HEAD, reset staging, and reset working tree
|
|
33
|
+
* await git.reset()
|
|
34
|
+
* .setRef("HEAD~1")
|
|
35
|
+
* .setMode(ResetMode.HARD)
|
|
36
|
+
* .call();
|
|
37
|
+
*
|
|
38
|
+
* // Reset specific paths
|
|
39
|
+
* await git.reset()
|
|
40
|
+
* .addPath("src/file.ts")
|
|
41
|
+
* .call();
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export class ResetCommand extends GitCommand<Ref> {
|
|
45
|
+
private ref?: string;
|
|
46
|
+
private mode = ResetMode.MIXED;
|
|
47
|
+
private paths: string[] = [];
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set the ref to reset to.
|
|
51
|
+
*
|
|
52
|
+
* Can be a commit ID, branch name, tag name, or relative ref like HEAD~1.
|
|
53
|
+
* If not set, defaults to HEAD.
|
|
54
|
+
*
|
|
55
|
+
* @param ref Commit-ish to reset to
|
|
56
|
+
*/
|
|
57
|
+
setRef(ref: string): this {
|
|
58
|
+
this.checkCallable();
|
|
59
|
+
this.ref = ref;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Set the reset mode.
|
|
65
|
+
*
|
|
66
|
+
* - SOFT: Move HEAD only
|
|
67
|
+
* - MIXED (default): Move HEAD and reset staging
|
|
68
|
+
* - HARD: Move HEAD, reset staging, and reset working tree. DESTRUCTIVE -
|
|
69
|
+
* uncommitted changes to tracked files are discarded and tracked files
|
|
70
|
+
* absent from the target commit are deleted from the working tree.
|
|
71
|
+
*
|
|
72
|
+
* @param mode Reset mode
|
|
73
|
+
*/
|
|
74
|
+
setMode(mode: ResetMode): this {
|
|
75
|
+
this.checkCallable();
|
|
76
|
+
this.mode = mode;
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Add a path to reset.
|
|
82
|
+
*
|
|
83
|
+
* When paths are specified, only those paths are reset in the staging area.
|
|
84
|
+
* The ref must be HEAD when using paths.
|
|
85
|
+
*
|
|
86
|
+
* @param path Repository-relative path
|
|
87
|
+
*/
|
|
88
|
+
addPath(path: string): this {
|
|
89
|
+
this.checkCallable();
|
|
90
|
+
this.paths.push(path);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Execute the reset.
|
|
96
|
+
*
|
|
97
|
+
* @returns The ref that HEAD now points to
|
|
98
|
+
* @throws RefNotFoundError if ref cannot be resolved
|
|
99
|
+
*/
|
|
100
|
+
async call(): Promise<Ref> {
|
|
101
|
+
this.checkCallable();
|
|
102
|
+
this.setCallable(false);
|
|
103
|
+
|
|
104
|
+
// If paths are specified, do path reset
|
|
105
|
+
if (this.paths.length > 0) {
|
|
106
|
+
return this.resetPaths();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Resolve target commit
|
|
110
|
+
const targetRef = this.ref ?? "HEAD";
|
|
111
|
+
const targetId = await this.resolveRef(targetRef);
|
|
112
|
+
|
|
113
|
+
// For HARD, capture the currently tracked paths BEFORE staging is reset:
|
|
114
|
+
// a path tracked now but absent from the target tree must be deleted from
|
|
115
|
+
// the working tree, and after resetStaging() that information is gone.
|
|
116
|
+
const trackedBefore =
|
|
117
|
+
this.mode === ResetMode.HARD ? await this.collectTrackedPaths() : undefined;
|
|
118
|
+
|
|
119
|
+
// Move HEAD/branch
|
|
120
|
+
await this.moveHead(targetId);
|
|
121
|
+
|
|
122
|
+
// Reset staging area (for MIXED and HARD)
|
|
123
|
+
if (this.mode === ResetMode.MIXED || this.mode === ResetMode.HARD) {
|
|
124
|
+
await this.resetStaging(targetId);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// Reset working tree (for HARD) - this DESTROYS uncommitted changes
|
|
128
|
+
if (this.mode === ResetMode.HARD) {
|
|
129
|
+
await this.resetWorkingTree(targetId, trackedBefore ?? new Set());
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Return the updated HEAD ref
|
|
133
|
+
const headRef = await this.refsStore.resolve("HEAD");
|
|
134
|
+
return headRef as Ref;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Reset specific paths in staging area.
|
|
139
|
+
*/
|
|
140
|
+
private async resetPaths(): Promise<Ref> {
|
|
141
|
+
// For path reset, we reset the staging entries to match the ref
|
|
142
|
+
const targetRef = this.ref ?? "HEAD";
|
|
143
|
+
const targetId = await this.resolveRef(targetRef);
|
|
144
|
+
|
|
145
|
+
// Load target commit's tree
|
|
146
|
+
const targetCommit = await this.commits.load(targetId);
|
|
147
|
+
if (!targetCommit) {
|
|
148
|
+
throw new RefNotFoundError(targetId, "Target commit not found");
|
|
149
|
+
}
|
|
150
|
+
const treeId = targetCommit.tree;
|
|
151
|
+
|
|
152
|
+
// Reset each path in staging
|
|
153
|
+
const editor = this.staging.createEditor();
|
|
154
|
+
|
|
155
|
+
for (const path of this.paths) {
|
|
156
|
+
const entry = await this.getTreeEntryForPath(treeId, path);
|
|
157
|
+
if (entry) {
|
|
158
|
+
// Path exists in target - update staging to match
|
|
159
|
+
editor.add({
|
|
160
|
+
path,
|
|
161
|
+
apply: () => ({
|
|
162
|
+
path,
|
|
163
|
+
mode: entry.mode,
|
|
164
|
+
objectId: entry.objectId,
|
|
165
|
+
stage: 0,
|
|
166
|
+
size: 0,
|
|
167
|
+
mtime: Date.now(),
|
|
168
|
+
}),
|
|
169
|
+
});
|
|
170
|
+
} else {
|
|
171
|
+
// Path doesn't exist in target - remove from staging
|
|
172
|
+
editor.add({
|
|
173
|
+
path,
|
|
174
|
+
apply: () => undefined,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
await editor.finish();
|
|
180
|
+
await this.staging.write();
|
|
181
|
+
|
|
182
|
+
const headRef = await this.refsStore.resolve("HEAD");
|
|
183
|
+
return headRef as Ref;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Move HEAD (and branch if applicable) to target commit.
|
|
188
|
+
*/
|
|
189
|
+
private async moveHead(targetId: ObjectId): Promise<void> {
|
|
190
|
+
const head = await this.refsStore.get("HEAD");
|
|
191
|
+
|
|
192
|
+
if (head && isSymbolicRef(head)) {
|
|
193
|
+
// HEAD points to a branch - update the branch
|
|
194
|
+
await this.refsStore.set(head.target, targetId);
|
|
195
|
+
} else {
|
|
196
|
+
// Detached HEAD - update HEAD directly
|
|
197
|
+
await this.refsStore.set("HEAD", targetId);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Collect every path currently tracked in the staging area.
|
|
203
|
+
*/
|
|
204
|
+
private async collectTrackedPaths(): Promise<Set<string>> {
|
|
205
|
+
const paths = new Set<string>();
|
|
206
|
+
for await (const entry of this.staging.entries()) {
|
|
207
|
+
paths.add(entry.path);
|
|
208
|
+
}
|
|
209
|
+
return paths;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Make the working tree match the target commit's tree (HARD reset).
|
|
214
|
+
*
|
|
215
|
+
* This is destructive: uncommitted modifications to tracked files are
|
|
216
|
+
* overwritten, and tracked files missing from the target tree are deleted.
|
|
217
|
+
* Untracked files are left alone - removing those is `git clean`.
|
|
218
|
+
*
|
|
219
|
+
* A repository without a worktree (bare) has no working tree to reset, so
|
|
220
|
+
* this is a no-op there. The restore is planned and validated in full before
|
|
221
|
+
* any file is touched, so a tree this command cannot restore faithfully
|
|
222
|
+
* (a symlink, a missing blob) fails with the working tree still intact.
|
|
223
|
+
*
|
|
224
|
+
* @param targetId Commit to reset the working tree to
|
|
225
|
+
* @param trackedBefore Paths tracked before staging was reset
|
|
226
|
+
*/
|
|
227
|
+
private async resetWorkingTree(targetId: ObjectId, trackedBefore: Set<string>): Promise<void> {
|
|
228
|
+
const worktree = this.worktreeAccess;
|
|
229
|
+
if (!worktree) {
|
|
230
|
+
// Bare repository: there is no working tree to reset.
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const commit = await this.commits.load(targetId);
|
|
235
|
+
if (!commit) {
|
|
236
|
+
throw new RefNotFoundError(targetId, "Target commit not found");
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Plan the whole restore first. Anything unrestorable then fails while the
|
|
240
|
+
// working tree is still untouched, instead of half-way through rewriting it.
|
|
241
|
+
const plan = await planTreeRestore(this.trees, this.blobs, commit.tree, RESET_HARD);
|
|
242
|
+
|
|
243
|
+
await writeTreeRestorePlan(worktree, this.blobs, plan, RESET_HARD);
|
|
244
|
+
|
|
245
|
+
// Delete files that were tracked but are not in the target tree.
|
|
246
|
+
for (const path of trackedBefore) {
|
|
247
|
+
if (plan.paths.has(path)) continue;
|
|
248
|
+
await worktree.remove(path);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Reset staging area to match a tree.
|
|
254
|
+
*/
|
|
255
|
+
private async resetStaging(commitId: ObjectId): Promise<void> {
|
|
256
|
+
const commit = await this.commits.load(commitId);
|
|
257
|
+
if (commit) {
|
|
258
|
+
await this.staging.readTree(this.trees, commit.tree);
|
|
259
|
+
await this.staging.write();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Get tree entry for a path.
|
|
265
|
+
*/
|
|
266
|
+
private async getTreeEntryForPath(
|
|
267
|
+
treeId: ObjectId,
|
|
268
|
+
path: string,
|
|
269
|
+
): Promise<{ objectId: ObjectId; mode: number } | undefined> {
|
|
270
|
+
const parts = path.split("/").filter((p) => p.length > 0);
|
|
271
|
+
let currentTreeId = treeId;
|
|
272
|
+
|
|
273
|
+
for (let i = 0; i < parts.length; i++) {
|
|
274
|
+
const name = parts[i];
|
|
275
|
+
const isLast = i === parts.length - 1;
|
|
276
|
+
|
|
277
|
+
const entry = await this.trees.getEntry(currentTreeId, name);
|
|
278
|
+
|
|
279
|
+
if (!entry) {
|
|
280
|
+
return undefined;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (isLast) {
|
|
284
|
+
return { objectId: entry.id, mode: entry.mode };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
currentTreeId = entry.id;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
return undefined;
|
|
291
|
+
}
|
|
292
|
+
}
|