@statewalker/vcs-commands 0.1.1 → 0.3.2

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.
Files changed (86) hide show
  1. package/README.md +180 -466
  2. package/dist/commands/commit-command.d.ts.map +1 -1
  3. package/dist/commands/init-command.d.ts.map +1 -1
  4. package/dist/commands/push-command.d.ts.map +1 -1
  5. package/dist/core-commands/checkout.command.impl.d.ts +1 -1
  6. package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
  7. package/dist/git-command.d.ts.map +1 -1
  8. package/package.json +25 -16
  9. package/src/commands/add-command.ts +494 -0
  10. package/src/commands/blame-command.ts +1241 -0
  11. package/src/commands/branch-command.ts +487 -0
  12. package/src/commands/checkout-command.ts +681 -0
  13. package/src/commands/cherry-pick-command.ts +544 -0
  14. package/src/commands/clean-command.ts +218 -0
  15. package/src/commands/clone-command.ts +532 -0
  16. package/src/commands/commit-command.ts +529 -0
  17. package/src/commands/describe-command.ts +454 -0
  18. package/src/commands/diff-command.ts +257 -0
  19. package/src/commands/fetch-command.ts +615 -0
  20. package/src/commands/gc-command.ts +155 -0
  21. package/src/commands/index.ts +31 -0
  22. package/src/commands/init-command.ts +365 -0
  23. package/src/commands/log-command.ts +549 -0
  24. package/src/commands/ls-remote-command.ts +237 -0
  25. package/src/commands/merge-command.ts +750 -0
  26. package/src/commands/pack-refs-command.ts +101 -0
  27. package/src/commands/pull-command.ts +258 -0
  28. package/src/commands/push-command.ts +682 -0
  29. package/src/commands/rebase-command.ts +665 -0
  30. package/src/commands/reflog-command.ts +68 -0
  31. package/src/commands/remote-command.ts +385 -0
  32. package/src/commands/reset-command.ts +292 -0
  33. package/src/commands/revert-command.ts +552 -0
  34. package/src/commands/rm-command.ts +178 -0
  35. package/src/commands/stash-apply-command.ts +402 -0
  36. package/src/commands/stash-create-command.ts +339 -0
  37. package/src/commands/stash-drop-command.ts +115 -0
  38. package/src/commands/stash-list-command.ts +116 -0
  39. package/src/commands/status-command.ts +173 -0
  40. package/src/commands/tag-command.ts +378 -0
  41. package/src/commands/tree-restore.ts +126 -0
  42. package/src/core-commands/add.command.impl.ts +274 -0
  43. package/src/core-commands/add.command.ts +81 -0
  44. package/src/core-commands/checkout.command.impl.ts +586 -0
  45. package/src/core-commands/checkout.command.ts +125 -0
  46. package/src/core-commands/index.ts +13 -0
  47. package/src/errors/add-errors.ts +20 -0
  48. package/src/errors/checkout-errors.ts +42 -0
  49. package/src/errors/command-errors.ts +68 -0
  50. package/src/errors/commit-errors.ts +40 -0
  51. package/src/errors/git-api-error.ts +11 -0
  52. package/src/errors/index.ts +12 -0
  53. package/src/errors/merge-errors.ts +94 -0
  54. package/src/errors/rebase-errors.ts +34 -0
  55. package/src/errors/ref-errors.ts +100 -0
  56. package/src/errors/remote-errors.ts +27 -0
  57. package/src/errors/stash-errors.ts +42 -0
  58. package/src/errors/tag-errors.ts +16 -0
  59. package/src/errors/transport-errors.ts +68 -0
  60. package/src/git-command.ts +421 -0
  61. package/src/git.ts +910 -0
  62. package/src/index.ts +18 -0
  63. package/src/pack-import/index.ts +8 -0
  64. package/src/pack-import/pack-import.ts +24 -0
  65. package/src/remote-config/index.ts +12 -0
  66. package/src/remote-config/remote-config-store.ts +285 -0
  67. package/src/rename/index.ts +5 -0
  68. package/src/rename/similarity-index.ts +185 -0
  69. package/src/results/cherry-pick-result.ts +35 -0
  70. package/src/results/clone-result.ts +25 -0
  71. package/src/results/diff-entry.ts +162 -0
  72. package/src/results/diff-formatter.ts +416 -0
  73. package/src/results/fetch-result.ts +91 -0
  74. package/src/results/index.ts +13 -0
  75. package/src/results/init-result.ts +29 -0
  76. package/src/results/merge-result.ts +135 -0
  77. package/src/results/pull-result.ts +27 -0
  78. package/src/results/push-result.ts +86 -0
  79. package/src/results/rebase-result.ts +140 -0
  80. package/src/results/revert-result.ts +35 -0
  81. package/src/results/stash-result.ts +60 -0
  82. package/src/results/status-result.ts +65 -0
  83. package/src/transport-command.ts +125 -0
  84. package/src/tree-merge/index.ts +8 -0
  85. package/src/tree-merge/tree-merge.ts +249 -0
  86. package/src/types.ts +70 -0
@@ -0,0 +1,339 @@
1
+ import type { ObjectId, PersonIdent } from "@statewalker/vcs-core";
2
+
3
+ import { NoHeadError, RefNotFoundError } from "../errors/index.js";
4
+ import { GitCommand } from "../git-command.js";
5
+ import { STASH_REF } from "./stash-list-command.js";
6
+
7
+ /**
8
+ * Get timezone offset string in Git format (+HHMM or -HHMM).
9
+ */
10
+ function getTimezoneOffset(): string {
11
+ const offset = new Date().getTimezoneOffset();
12
+ const sign = offset <= 0 ? "+" : "-";
13
+ const absOffset = Math.abs(offset);
14
+ const hours = Math.floor(absOffset / 60)
15
+ .toString()
16
+ .padStart(2, "0");
17
+ const minutes = (absOffset % 60).toString().padStart(2, "0");
18
+ return `${sign}${hours}${minutes}`;
19
+ }
20
+
21
+ /**
22
+ * Default message format for index commit.
23
+ */
24
+ const MSG_INDEX = "index on {0}: {1} {2}";
25
+
26
+ /**
27
+ * Default message format for working directory commit.
28
+ */
29
+ const MSG_WORKING_DIR = "WIP on {0}: {1} {2}";
30
+
31
+ /**
32
+ * Default message format for untracked files commit.
33
+ */
34
+ const MSG_UNTRACKED = "untracked files on {0}: {1} {2}";
35
+
36
+ /**
37
+ * Interface for providing working tree and index state.
38
+ *
39
+ * The stash create operation requires access to the working directory
40
+ * and index, which requires a WorkingCopy with worktree access.
41
+ */
42
+ export interface StashWorkingTreeProvider {
43
+ /**
44
+ * Get the current index tree (staged changes).
45
+ */
46
+ getIndexTree(): Promise<ObjectId>;
47
+
48
+ /**
49
+ * Get the working tree with uncommitted changes.
50
+ */
51
+ getWorkingTree(): Promise<ObjectId>;
52
+
53
+ /**
54
+ * Get tree of untracked files (optional).
55
+ */
56
+ getUntrackedTree?(): Promise<ObjectId | undefined>;
57
+
58
+ /**
59
+ * Check if there are local changes to stash.
60
+ */
61
+ hasLocalChanges(): Promise<boolean>;
62
+
63
+ /**
64
+ * Reset working directory to HEAD after stash.
65
+ */
66
+ resetHard(): Promise<void>;
67
+ }
68
+
69
+ /**
70
+ * Command to create a stash commit.
71
+ *
72
+ * Equivalent to `git stash` or `git stash push`.
73
+ *
74
+ * Based on JGit's StashCreateCommand.
75
+ *
76
+ * Note: This command requires a working tree provider to access
77
+ * uncommitted changes. Without a provider, it creates a stash
78
+ * from the current HEAD state (no actual changes stashed).
79
+ *
80
+ * @example
81
+ * ```typescript
82
+ * // Create a stash with working tree provider
83
+ * const stashCommit = await git.stashCreate()
84
+ * .setWorkingTreeProvider(provider)
85
+ * .setMessage("WIP: feature work")
86
+ * .call();
87
+ *
88
+ * // Create stash including untracked files
89
+ * const stashCommit = await git.stashCreate()
90
+ * .setWorkingTreeProvider(provider)
91
+ * .setIncludeUntracked(true)
92
+ * .call();
93
+ * ```
94
+ */
95
+ export class StashCreateCommand extends GitCommand<ObjectId | undefined> {
96
+ private indexMessage = MSG_INDEX;
97
+ private workingDirectoryMessage = MSG_WORKING_DIR;
98
+ private ref: string | null = STASH_REF;
99
+ private includeUntracked = false;
100
+ private workingTreeProvider?: StashWorkingTreeProvider;
101
+ private customMessage?: string;
102
+ private author?: PersonIdent;
103
+
104
+ /**
105
+ * Set the message used when committing index changes.
106
+ *
107
+ * The message will be formatted with the current branch, abbreviated
108
+ * commit ID, and short commit message.
109
+ *
110
+ * @param message The index message template
111
+ */
112
+ setIndexMessage(message: string): this {
113
+ this.checkCallable();
114
+ this.indexMessage = message;
115
+ return this;
116
+ }
117
+
118
+ /**
119
+ * Set the message used when committing working directory changes.
120
+ *
121
+ * @param message The working directory message template
122
+ */
123
+ setWorkingDirectoryMessage(message: string): this {
124
+ this.checkCallable();
125
+ this.workingDirectoryMessage = message;
126
+ return this;
127
+ }
128
+
129
+ /**
130
+ * Set a custom stash message.
131
+ *
132
+ * @param message The custom message
133
+ */
134
+ setMessage(message: string): this {
135
+ this.checkCallable();
136
+ this.customMessage = message;
137
+ return this;
138
+ }
139
+
140
+ /**
141
+ * Set the reference to update with the stashed commit ID.
142
+ * If null, no reference is updated.
143
+ *
144
+ * @param ref The ref name (default: refs/stash)
145
+ */
146
+ setRef(ref: string | null): this {
147
+ this.checkCallable();
148
+ this.ref = ref;
149
+ return this;
150
+ }
151
+
152
+ /**
153
+ * Set whether to include untracked files in the stash.
154
+ *
155
+ * @param includeUntracked true to include untracked files
156
+ */
157
+ setIncludeUntracked(includeUntracked: boolean): this {
158
+ this.checkCallable();
159
+ this.includeUntracked = includeUntracked;
160
+ return this;
161
+ }
162
+
163
+ /**
164
+ * Get whether untracked files will be included.
165
+ */
166
+ getIncludeUntracked(): boolean {
167
+ return this.includeUntracked;
168
+ }
169
+
170
+ /**
171
+ * Set the working tree provider for accessing uncommitted changes.
172
+ *
173
+ * @param provider The working tree provider
174
+ */
175
+ setWorkingTreeProvider(provider: StashWorkingTreeProvider): this {
176
+ this.checkCallable();
177
+ this.workingTreeProvider = provider;
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * Set the author/committer identity for the stash commits.
183
+ *
184
+ * @param author The author identity
185
+ */
186
+ setAuthor(author: PersonIdent): this {
187
+ this.checkCallable();
188
+ this.author = author;
189
+ return this;
190
+ }
191
+
192
+ /**
193
+ * Execute the stash create command.
194
+ *
195
+ * @returns The stash commit ID, or undefined if nothing to stash
196
+ */
197
+ async call(): Promise<ObjectId | undefined> {
198
+ this.checkCallable();
199
+ this.setCallable(false);
200
+
201
+ // Get current HEAD
202
+ const headRef = await this.refsStore.resolve("HEAD");
203
+ if (!headRef?.objectId) {
204
+ throw new NoHeadError("HEAD is required to stash");
205
+ }
206
+
207
+ const headCommit = await this.commits.load(headRef.objectId);
208
+ if (!headCommit) {
209
+ throw new RefNotFoundError(headRef.objectId, "HEAD commit not found");
210
+ }
211
+ const branch = await this.getCurrentBranchName();
212
+
213
+ // Get author/committer
214
+ const author: PersonIdent = this.author ?? {
215
+ name: "Unknown",
216
+ email: "unknown@example.com",
217
+ timestamp: Math.floor(Date.now() / 1000),
218
+ tzOffset: getTimezoneOffset(),
219
+ };
220
+
221
+ // Determine trees to stash
222
+ let indexTree: ObjectId;
223
+ let workingTree: ObjectId;
224
+ let untrackedTree: ObjectId | undefined;
225
+
226
+ if (this.workingTreeProvider) {
227
+ // Check if there are changes to stash
228
+ const hasChanges = await this.workingTreeProvider.hasLocalChanges();
229
+ if (!hasChanges) {
230
+ return undefined;
231
+ }
232
+
233
+ indexTree = await this.workingTreeProvider.getIndexTree();
234
+ workingTree = await this.workingTreeProvider.getWorkingTree();
235
+
236
+ if (this.includeUntracked && this.workingTreeProvider.getUntrackedTree) {
237
+ untrackedTree = await this.workingTreeProvider.getUntrackedTree();
238
+ }
239
+ } else {
240
+ // No working tree provider - use HEAD tree
241
+ // This is a degenerate case where there's nothing to actually stash
242
+ indexTree = headCommit.tree;
243
+ workingTree = headCommit.tree;
244
+ }
245
+
246
+ // Build commit messages
247
+ const abbrev = headRef.objectId.substring(0, 7);
248
+ const shortMessage = headCommit.message.split("\n")[0];
249
+
250
+ const indexMsg = this.formatMessage(this.indexMessage, branch, abbrev, shortMessage);
251
+ const workingMsg =
252
+ this.customMessage ??
253
+ this.formatMessage(this.workingDirectoryMessage, branch, abbrev, shortMessage);
254
+
255
+ // Create index commit (parent: HEAD)
256
+ const indexCommit = await this.commits.store({
257
+ tree: indexTree,
258
+ parents: [headRef.objectId],
259
+ author,
260
+ committer: author,
261
+ message: indexMsg,
262
+ });
263
+
264
+ // Create untracked commit if needed (no parents)
265
+ let untrackedCommit: ObjectId | undefined;
266
+ if (untrackedTree) {
267
+ const untrackedMsg = this.formatMessage(MSG_UNTRACKED, branch, abbrev, shortMessage);
268
+ untrackedCommit = await this.commits.store({
269
+ tree: untrackedTree,
270
+ parents: [],
271
+ author,
272
+ committer: author,
273
+ message: untrackedMsg,
274
+ });
275
+ }
276
+
277
+ // Create stash commit (working tree state)
278
+ // Parents: HEAD, index commit, [untracked commit]
279
+ const parents = [headRef.objectId, indexCommit];
280
+ if (untrackedCommit) {
281
+ parents.push(untrackedCommit);
282
+ }
283
+
284
+ const stashCommit = await this.commits.store({
285
+ tree: workingTree,
286
+ parents,
287
+ author,
288
+ committer: author,
289
+ message: workingMsg,
290
+ });
291
+
292
+ // Update stash ref
293
+ if (this.ref) {
294
+ await this.updateStashRef(this.ref, stashCommit, workingMsg);
295
+ }
296
+
297
+ // Reset working directory (if provider supports it)
298
+ if (this.workingTreeProvider) {
299
+ await this.workingTreeProvider.resetHard();
300
+ }
301
+
302
+ return stashCommit;
303
+ }
304
+
305
+ /**
306
+ * Format a message template.
307
+ */
308
+ private formatMessage(
309
+ template: string,
310
+ branch: string,
311
+ abbrev: string,
312
+ shortMessage: string,
313
+ ): string {
314
+ return template.replace("{0}", branch).replace("{1}", abbrev).replace("{2}", shortMessage);
315
+ }
316
+
317
+ /**
318
+ * Get the current branch name.
319
+ */
320
+ private async getCurrentBranchName(): Promise<string> {
321
+ const branch = await this.getCurrentBranch();
322
+ if (branch) {
323
+ // Strip refs/heads/ prefix
324
+ return branch.replace(/^refs\/heads\//, "");
325
+ }
326
+ return "HEAD";
327
+ }
328
+
329
+ /**
330
+ * Update the stash ref.
331
+ *
332
+ * Note: Reflog support is not part of the core RefStore interface.
333
+ * Stash history is tracked via parent commit chain instead.
334
+ */
335
+ private async updateStashRef(ref: string, commitId: ObjectId, _message: string): Promise<void> {
336
+ // Update ref
337
+ await this.refsStore.set(ref, commitId);
338
+ }
339
+ }
@@ -0,0 +1,115 @@
1
+ import type { ObjectId } from "@statewalker/vcs-core";
2
+
3
+ import { InvalidStashIndexError } from "../errors/index.js";
4
+ import { GitCommand } from "../git-command.js";
5
+ import { STASH_REF } from "./stash-list-command.js";
6
+
7
+ /**
8
+ * Command to drop (delete) a stashed commit.
9
+ *
10
+ * Equivalent to `git stash drop`.
11
+ *
12
+ * Based on JGit's StashDropCommand.
13
+ *
14
+ * Note: Without reflog support in RefStore, only the most recent stash (index 0)
15
+ * can be dropped. Dropping by index > 0 requires reflog support.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Drop most recent stash
20
+ * const result = await git.stashDrop().call();
21
+ *
22
+ * // Drop specific stash (requires reflog support)
23
+ * const result = await git.stashDrop()
24
+ * .setStashRef(2)
25
+ * .call();
26
+ *
27
+ * // Drop all stashes
28
+ * const result = await git.stashDrop()
29
+ * .setAll(true)
30
+ * .call();
31
+ * ```
32
+ */
33
+ export class StashDropCommand extends GitCommand<ObjectId | undefined> {
34
+ private stashRefEntry = 0;
35
+ private dropAll = false;
36
+
37
+ /**
38
+ * Set the stash reference to drop (0-based index).
39
+ *
40
+ * Defaults to 0 (most recent) if not specified.
41
+ *
42
+ * Note: Without reflog support, only index 0 is valid.
43
+ *
44
+ * @param stashRef The 0-based index of the stash to drop
45
+ */
46
+ setStashRef(stashRef: number): this {
47
+ this.checkCallable();
48
+ if (stashRef < 0) {
49
+ throw new InvalidStashIndexError(stashRef, "Stash index must be >= 0");
50
+ }
51
+ this.stashRefEntry = stashRef;
52
+ return this;
53
+ }
54
+
55
+ /**
56
+ * Get the stash index to drop.
57
+ */
58
+ getStashRef(): number {
59
+ return this.stashRefEntry;
60
+ }
61
+
62
+ /**
63
+ * Set whether to drop all stashed commits.
64
+ *
65
+ * @param all true to drop all stashes
66
+ */
67
+ setAll(all: boolean): this {
68
+ this.checkCallable();
69
+ this.dropAll = all;
70
+ return this;
71
+ }
72
+
73
+ /**
74
+ * Get whether all stashes will be dropped.
75
+ */
76
+ getAll(): boolean {
77
+ return this.dropAll;
78
+ }
79
+
80
+ /**
81
+ * Execute the stash drop command.
82
+ *
83
+ * @returns The new stash ref after drop, or undefined if no stashes remain
84
+ */
85
+ async call(): Promise<ObjectId | undefined> {
86
+ this.checkCallable();
87
+ this.setCallable(false);
88
+
89
+ // Check if stash ref exists
90
+ const stashRef = await this.refsStore.get(STASH_REF);
91
+ if (!stashRef || !("objectId" in stashRef) || !stashRef.objectId) {
92
+ return undefined;
93
+ }
94
+
95
+ // Drop all stashes - just delete the ref
96
+ if (this.dropAll) {
97
+ await this.refsStore.delete(STASH_REF);
98
+ return undefined;
99
+ }
100
+
101
+ // Without reflog support, we can only drop the most recent stash (index 0)
102
+ // For index > 0, we would need reflog support to access older stashes
103
+ if (this.stashRefEntry > 0) {
104
+ throw new InvalidStashIndexError(
105
+ this.stashRefEntry,
106
+ `Stash entry ${this.stashRefEntry} cannot be dropped without reflog support. ` +
107
+ `Only stash@{0} can be dropped.`,
108
+ );
109
+ }
110
+
111
+ // Drop stash@{0} by deleting the ref
112
+ await this.refsStore.delete(STASH_REF);
113
+ return undefined;
114
+ }
115
+ }
@@ -0,0 +1,116 @@
1
+ import type { ObjectId } from "@statewalker/vcs-core";
2
+
3
+ import { GitCommand } from "../git-command.js";
4
+ import type { StashEntry } from "../results/stash-result.js";
5
+
6
+ /**
7
+ * Reference name for stash.
8
+ */
9
+ export const STASH_REF = "refs/stash";
10
+
11
+ /**
12
+ * Command to list stashed commits.
13
+ *
14
+ * Equivalent to `git stash list`.
15
+ *
16
+ * Based on JGit's StashListCommand.
17
+ *
18
+ * @example
19
+ * ```typescript
20
+ * // List all stashes
21
+ * const stashes = await git.stashList().call();
22
+ * for (const stash of stashes) {
23
+ * console.log(`stash@{${stash.index}}: ${stash.message}`);
24
+ * }
25
+ * ```
26
+ */
27
+ export class StashListCommand extends GitCommand<StashEntry[]> {
28
+ /**
29
+ * Execute the stash list command.
30
+ *
31
+ * @returns Array of stash entries, most recent first
32
+ */
33
+ async call(): Promise<StashEntry[]> {
34
+ this.checkCallable();
35
+ this.setCallable(false);
36
+
37
+ // Check if stash ref exists
38
+ const stashRef = await this.refsStore.get(STASH_REF);
39
+ if (!stashRef || !("objectId" in stashRef) || !stashRef.objectId) {
40
+ return [];
41
+ }
42
+
43
+ // Get reflog for stash ref
44
+ const stashes: StashEntry[] = [];
45
+ const reflog = await this.getStashReflog();
46
+
47
+ let index = 0;
48
+ for (const entry of reflog) {
49
+ const stashEntry = await this.parseStashEntry(entry.commitId, entry.message, index);
50
+ if (stashEntry) {
51
+ stashes.push(stashEntry);
52
+ }
53
+ index++;
54
+ }
55
+
56
+ return stashes;
57
+ }
58
+
59
+ /**
60
+ * Get stash entries.
61
+ *
62
+ * Note: Without reflog support in the core RefStore interface,
63
+ * this returns only the most recent stash (if any).
64
+ */
65
+ private async getStashReflog(): Promise<Array<{ commitId: ObjectId; message: string }>> {
66
+ // Without reflog support, we can only return the current stash ref
67
+ const stashRef = await this.refsStore.resolve(STASH_REF);
68
+ if (stashRef?.objectId) {
69
+ // Get the commit message from the stash commit
70
+ const commit = await this.commits.load(stashRef.objectId);
71
+ if (commit) {
72
+ return [
73
+ {
74
+ commitId: stashRef.objectId,
75
+ message: commit.message.split("\n")[0],
76
+ },
77
+ ];
78
+ }
79
+ }
80
+
81
+ return [];
82
+ }
83
+
84
+ /**
85
+ * Parse a stash commit into a StashEntry.
86
+ */
87
+ private async parseStashEntry(
88
+ commitId: ObjectId,
89
+ message: string,
90
+ index: number,
91
+ ): Promise<StashEntry | undefined> {
92
+ try {
93
+ const commit = await this.commits.load(commitId);
94
+ if (!commit) {
95
+ return undefined;
96
+ }
97
+
98
+ // Stash commits should have 2 or 3 parents
99
+ if (commit.parents.length < 2) {
100
+ return undefined;
101
+ }
102
+
103
+ return {
104
+ commitId,
105
+ headCommit: commit.parents[0],
106
+ indexCommit: commit.parents[1],
107
+ untrackedCommit: commit.parents[2], // Optional
108
+ message: message || commit.message.split("\n")[0],
109
+ index,
110
+ timestamp: commit.committer.timestamp,
111
+ };
112
+ } catch {
113
+ return undefined;
114
+ }
115
+ }
116
+ }