@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.
- package/README.md +180 -466
- 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 +25 -16
- 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,178 @@
|
|
|
1
|
+
import type { MergeStageValue } from "@statewalker/vcs-working-tree";
|
|
2
|
+
|
|
3
|
+
import { NoFilepatternError } from "../errors/index.js";
|
|
4
|
+
import { GitCommand } from "../git-command.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Result of RmCommand execution.
|
|
8
|
+
*/
|
|
9
|
+
export interface RmResult {
|
|
10
|
+
/** Paths that were removed from the index */
|
|
11
|
+
removedPaths: string[];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Remove files from the index (and optionally from working tree).
|
|
16
|
+
*
|
|
17
|
+
* Equivalent to `git rm`.
|
|
18
|
+
*
|
|
19
|
+
* Based on JGit's RmCommand.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* // Remove file from index and working tree
|
|
24
|
+
* await git.rm()
|
|
25
|
+
* .addFilepattern("file.txt")
|
|
26
|
+
* .call();
|
|
27
|
+
*
|
|
28
|
+
* // Remove from index only (keep in working tree)
|
|
29
|
+
* await git.rm()
|
|
30
|
+
* .addFilepattern("file.txt")
|
|
31
|
+
* .setCached(true)
|
|
32
|
+
* .call();
|
|
33
|
+
*
|
|
34
|
+
* // Remove directory recursively
|
|
35
|
+
* await git.rm()
|
|
36
|
+
* .addFilepattern("src/old/")
|
|
37
|
+
* .call();
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export class RmCommand extends GitCommand<RmResult> {
|
|
41
|
+
private filepatterns: string[] = [];
|
|
42
|
+
private cached = false;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Add a file pattern to remove.
|
|
46
|
+
*
|
|
47
|
+
* @param filepattern Path pattern to remove (with `/` as separator)
|
|
48
|
+
*/
|
|
49
|
+
addFilepattern(filepattern: string): this {
|
|
50
|
+
this.checkCallable();
|
|
51
|
+
this.filepatterns.push(filepattern);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Set whether to only remove from index (keep in working tree).
|
|
57
|
+
*
|
|
58
|
+
* When true, files are only removed from the staging area
|
|
59
|
+
* but remain in the working directory (like `git rm --cached`).
|
|
60
|
+
*
|
|
61
|
+
* @param cached true to only remove from index
|
|
62
|
+
*/
|
|
63
|
+
setCached(cached: boolean): this {
|
|
64
|
+
this.checkCallable();
|
|
65
|
+
this.cached = cached;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get whether cached mode is enabled.
|
|
71
|
+
*/
|
|
72
|
+
getCached(): boolean {
|
|
73
|
+
return this.cached;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Execute the rm command.
|
|
78
|
+
*
|
|
79
|
+
* @returns Result with list of removed paths
|
|
80
|
+
*/
|
|
81
|
+
async call(): Promise<RmResult> {
|
|
82
|
+
this.checkCallable();
|
|
83
|
+
this.setCallable(false);
|
|
84
|
+
|
|
85
|
+
if (this.filepatterns.length === 0) {
|
|
86
|
+
throw new NoFilepatternError("At least one file pattern is required");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const removedPaths: string[] = [];
|
|
90
|
+
|
|
91
|
+
// Read current staging area
|
|
92
|
+
await this.staging.read();
|
|
93
|
+
|
|
94
|
+
// Get all entries from staging
|
|
95
|
+
const entriesToKeep: Array<{
|
|
96
|
+
path: string;
|
|
97
|
+
mode: number;
|
|
98
|
+
objectId: string;
|
|
99
|
+
stage: MergeStageValue;
|
|
100
|
+
}> = [];
|
|
101
|
+
|
|
102
|
+
for await (const entry of this.staging.entries()) {
|
|
103
|
+
const shouldRemove = this.matchesPattern(entry.path);
|
|
104
|
+
|
|
105
|
+
if (shouldRemove) {
|
|
106
|
+
removedPaths.push(entry.path);
|
|
107
|
+
} else {
|
|
108
|
+
entriesToKeep.push({
|
|
109
|
+
path: entry.path,
|
|
110
|
+
mode: entry.mode,
|
|
111
|
+
objectId: entry.objectId,
|
|
112
|
+
stage: entry.stage,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Rebuild staging with entries that weren't removed
|
|
118
|
+
const builder = this.staging.createBuilder();
|
|
119
|
+
for (const entry of entriesToKeep) {
|
|
120
|
+
builder.add(entry);
|
|
121
|
+
}
|
|
122
|
+
await builder.finish();
|
|
123
|
+
await this.staging.write();
|
|
124
|
+
|
|
125
|
+
// Note: Working tree removal is not implemented here since
|
|
126
|
+
// WorkingCopy may not have worktree access for file deletion.
|
|
127
|
+
// The cached flag controls whether working tree should be modified,
|
|
128
|
+
// but actual deletion requires filesystem access which is
|
|
129
|
+
// implementation-specific.
|
|
130
|
+
|
|
131
|
+
return { removedPaths };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Check if a path matches any of the configured patterns.
|
|
136
|
+
*/
|
|
137
|
+
private matchesPattern(path: string): boolean {
|
|
138
|
+
for (const pattern of this.filepatterns) {
|
|
139
|
+
// Exact match
|
|
140
|
+
if (path === pattern) {
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Directory pattern (ends with /)
|
|
145
|
+
if (pattern.endsWith("/")) {
|
|
146
|
+
const dirPrefix = pattern;
|
|
147
|
+
if (path.startsWith(dirPrefix)) {
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Prefix match for directories without trailing slash
|
|
153
|
+
if (path.startsWith(`${pattern}/`)) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Simple glob matching for * patterns
|
|
158
|
+
if (pattern.includes("*")) {
|
|
159
|
+
const regex = this.patternToRegex(pattern);
|
|
160
|
+
if (regex.test(path)) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Convert a simple glob pattern to regex.
|
|
170
|
+
*/
|
|
171
|
+
private patternToRegex(pattern: string): RegExp {
|
|
172
|
+
// Escape special regex characters except *
|
|
173
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
|
|
174
|
+
// Convert * to .*
|
|
175
|
+
const regexStr = escaped.replace(/\*/g, ".*");
|
|
176
|
+
return new RegExp(`^${regexStr}$`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import type { ObjectId } from "@statewalker/vcs-core";
|
|
2
|
+
import type { Worktree } from "@statewalker/vcs-working-tree";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
InvalidArgumentError,
|
|
6
|
+
NoHeadError,
|
|
7
|
+
RefNotFoundError,
|
|
8
|
+
StashApplyFailedError,
|
|
9
|
+
StoreNotAvailableError,
|
|
10
|
+
} from "../errors/index.js";
|
|
11
|
+
import { GitCommand } from "../git-command.js";
|
|
12
|
+
import { type ContentMergeStrategy, MergeStrategy } from "../results/merge-result.js";
|
|
13
|
+
import type { StashApplyResult } from "../results/stash-result.js";
|
|
14
|
+
import { StashApplyStatus } from "../results/stash-result.js";
|
|
15
|
+
import { mergeTreesThreeWay } from "../tree-merge/index.js";
|
|
16
|
+
import { STASH_REF } from "./stash-list-command.js";
|
|
17
|
+
import { planTreeRestore, type TreeRestorePlan, writeTreeRestorePlan } from "./tree-restore.js";
|
|
18
|
+
|
|
19
|
+
/** Names this operation in restore failure messages. */
|
|
20
|
+
const STASH_APPLY = "stash apply";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Command to apply a stashed commit.
|
|
24
|
+
*
|
|
25
|
+
* Equivalent to `git stash apply`.
|
|
26
|
+
*
|
|
27
|
+
* Based on JGit's StashApplyCommand.
|
|
28
|
+
*
|
|
29
|
+
* The stashed changes are three-way merged against the current HEAD and the
|
|
30
|
+
* result is written back: the merged working tree always, the merged index
|
|
31
|
+
* when `restoreIndex` is set, and the stashed untracked files when
|
|
32
|
+
* `restoreUntracked` is set. A conflict aborts before anything is written.
|
|
33
|
+
*
|
|
34
|
+
* Known limits, shared with `reset --hard`:
|
|
35
|
+
* - a symlink in the merged tree is refused, because the `Worktree` interface
|
|
36
|
+
* has no primitive to create one - writing the target as a regular file
|
|
37
|
+
* would silently produce a working tree that does not match;
|
|
38
|
+
* - a gitlink's contents are not restored, as in git;
|
|
39
|
+
* - directories left empty by a deletion are not pruned;
|
|
40
|
+
* - the restore is not atomic across the working tree and the index.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Apply most recent stash
|
|
45
|
+
* const result = await git.stashApply().call();
|
|
46
|
+
*
|
|
47
|
+
* // Apply specific stash
|
|
48
|
+
* const result = await git.stashApply()
|
|
49
|
+
* .setStashRef("stash@{2}")
|
|
50
|
+
* .call();
|
|
51
|
+
*
|
|
52
|
+
* // Apply without restoring index
|
|
53
|
+
* const result = await git.stashApply()
|
|
54
|
+
* .setRestoreIndex(false)
|
|
55
|
+
* .call();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export class StashApplyCommand extends GitCommand<StashApplyResult> {
|
|
59
|
+
private stashRef?: string;
|
|
60
|
+
private restoreIndex = true;
|
|
61
|
+
private restoreUntracked = true;
|
|
62
|
+
private strategy = MergeStrategy.RECURSIVE;
|
|
63
|
+
private contentStrategy?: ContentMergeStrategy;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Set the stash reference to apply.
|
|
67
|
+
*
|
|
68
|
+
* Defaults to stash@{0} (most recent) if not specified.
|
|
69
|
+
*
|
|
70
|
+
* @param stashRef Name of the stash ref to apply
|
|
71
|
+
*/
|
|
72
|
+
setStashRef(stashRef: string): this {
|
|
73
|
+
this.checkCallable();
|
|
74
|
+
this.stashRef = stashRef;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get the stash reference.
|
|
80
|
+
*/
|
|
81
|
+
getStashRef(): string | undefined {
|
|
82
|
+
return this.stashRef;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Set whether to restore the index state.
|
|
87
|
+
*
|
|
88
|
+
* @param restoreIndex true to restore index (default)
|
|
89
|
+
*/
|
|
90
|
+
setRestoreIndex(restoreIndex: boolean): this {
|
|
91
|
+
this.checkCallable();
|
|
92
|
+
this.restoreIndex = restoreIndex;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get whether index will be restored.
|
|
98
|
+
*/
|
|
99
|
+
getRestoreIndex(): boolean {
|
|
100
|
+
return this.restoreIndex;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Set whether to restore untracked files.
|
|
105
|
+
*
|
|
106
|
+
* @param restoreUntracked true to restore untracked files (default)
|
|
107
|
+
*/
|
|
108
|
+
setRestoreUntracked(restoreUntracked: boolean): this {
|
|
109
|
+
this.checkCallable();
|
|
110
|
+
this.restoreUntracked = restoreUntracked;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get whether untracked files will be restored.
|
|
116
|
+
*/
|
|
117
|
+
getRestoreUntracked(): boolean {
|
|
118
|
+
return this.restoreUntracked;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Set whether to ignore repository state when applying.
|
|
123
|
+
*
|
|
124
|
+
* Note: Currently not implemented - stash apply always ignores repo state.
|
|
125
|
+
*
|
|
126
|
+
* @param _ignore true to ignore state
|
|
127
|
+
*/
|
|
128
|
+
setIgnoreRepositoryState(_ignore: boolean): this {
|
|
129
|
+
this.checkCallable();
|
|
130
|
+
// Not currently implemented - reserved for future use
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Set the merge strategy to use.
|
|
136
|
+
*
|
|
137
|
+
* @param strategy The merge strategy
|
|
138
|
+
*/
|
|
139
|
+
setStrategy(strategy: MergeStrategy): this {
|
|
140
|
+
this.checkCallable();
|
|
141
|
+
this.strategy = strategy;
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Get the merge strategy.
|
|
147
|
+
*/
|
|
148
|
+
getStrategy(): MergeStrategy {
|
|
149
|
+
return this.strategy;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Set the content merge strategy for file-level conflicts.
|
|
154
|
+
*
|
|
155
|
+
* @param strategy The content merge strategy
|
|
156
|
+
*/
|
|
157
|
+
setContentMergeStrategy(strategy: ContentMergeStrategy): this {
|
|
158
|
+
this.checkCallable();
|
|
159
|
+
this.contentStrategy = strategy;
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Get the content merge strategy.
|
|
165
|
+
*/
|
|
166
|
+
getContentMergeStrategy(): ContentMergeStrategy | undefined {
|
|
167
|
+
return this.contentStrategy;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Execute the stash apply command.
|
|
172
|
+
*
|
|
173
|
+
* @returns Result of the apply operation
|
|
174
|
+
*/
|
|
175
|
+
async call(): Promise<StashApplyResult> {
|
|
176
|
+
this.checkCallable();
|
|
177
|
+
this.setCallable(false);
|
|
178
|
+
|
|
179
|
+
// Applying a stash means putting files back. A repository with no working
|
|
180
|
+
// tree has nowhere to put them, and reporting OK there would be the same
|
|
181
|
+
// silent no-op this command used to be.
|
|
182
|
+
const worktree = this.worktreeAccess;
|
|
183
|
+
if (!worktree) {
|
|
184
|
+
throw new StoreNotAvailableError("worktree", "stash apply requires a working tree");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Get HEAD commit
|
|
188
|
+
const headRef = await this.refsStore.resolve("HEAD");
|
|
189
|
+
if (!headRef?.objectId) {
|
|
190
|
+
throw new NoHeadError("HEAD is required to apply stash");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Get stash commit
|
|
194
|
+
const stashId = await this.resolveStashRef();
|
|
195
|
+
const stashCommit = await this.commits.load(stashId);
|
|
196
|
+
if (!stashCommit) {
|
|
197
|
+
throw new RefNotFoundError(stashId, "Stash commit not found");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Validate stash commit structure
|
|
201
|
+
if (stashCommit.parents.length < 2 || stashCommit.parents.length > 3) {
|
|
202
|
+
throw new InvalidArgumentError(
|
|
203
|
+
"stashRef",
|
|
204
|
+
stashId,
|
|
205
|
+
`Stash commit ${stashId} has invalid number of parents: ${stashCommit.parents.length}`,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const stashHeadCommit = stashCommit.parents[0];
|
|
210
|
+
const stashIndexCommit = stashCommit.parents[1];
|
|
211
|
+
const stashUntrackedCommit = stashCommit.parents[2]; // Optional
|
|
212
|
+
|
|
213
|
+
// Three-way merge: stash base -> current HEAD with stash changes
|
|
214
|
+
const headCommit = await this.commits.load(headRef.objectId);
|
|
215
|
+
const stashHeadCommitObj = await this.commits.load(stashHeadCommit);
|
|
216
|
+
if (!headCommit || !stashHeadCommitObj) {
|
|
217
|
+
throw new RefNotFoundError(headRef.objectId, "Head or stash commit not found");
|
|
218
|
+
}
|
|
219
|
+
const stashHeadTree = stashHeadCommitObj.tree;
|
|
220
|
+
const stashWorkingTree = stashCommit.tree;
|
|
221
|
+
|
|
222
|
+
// Merge working tree changes. The base is the tree of the commit the stash
|
|
223
|
+
// was taken from, so base->ours is exactly the stashed change and
|
|
224
|
+
// base->theirs is what the branch did since.
|
|
225
|
+
const mergeResult = await mergeTreesThreeWay(
|
|
226
|
+
this.trees,
|
|
227
|
+
stashHeadTree,
|
|
228
|
+
stashWorkingTree,
|
|
229
|
+
headCommit.tree,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
if (mergeResult.conflicts && mergeResult.conflicts.length > 0) {
|
|
233
|
+
return {
|
|
234
|
+
status: StashApplyStatus.CONFLICTS,
|
|
235
|
+
stashCommit: stashId,
|
|
236
|
+
conflicts: mergeResult.conflicts,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// If restoreIndex, also merge index changes. Both merges are resolved
|
|
241
|
+
// before anything is written, so a conflict in either leaves the working
|
|
242
|
+
// tree and the index exactly as they were.
|
|
243
|
+
let mergedIndexTree: ObjectId | undefined;
|
|
244
|
+
if (this.restoreIndex) {
|
|
245
|
+
const stashIndexCommitObj = await this.commits.load(stashIndexCommit);
|
|
246
|
+
const stashIndexTree = stashIndexCommitObj?.tree ?? stashHeadTree;
|
|
247
|
+
const indexMergeResult = await mergeTreesThreeWay(
|
|
248
|
+
this.trees,
|
|
249
|
+
stashHeadTree,
|
|
250
|
+
stashIndexTree,
|
|
251
|
+
headCommit.tree,
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
if (indexMergeResult.conflicts && indexMergeResult.conflicts.length > 0) {
|
|
255
|
+
return {
|
|
256
|
+
status: StashApplyStatus.CONFLICTS,
|
|
257
|
+
stashCommit: stashId,
|
|
258
|
+
conflicts: indexMergeResult.conflicts,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
mergedIndexTree = indexMergeResult.tree;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Plan the whole restore - the merged working tree, the paths it deletes,
|
|
266
|
+
// and the untracked files - and validate it before touching anything. A
|
|
267
|
+
// stash this command cannot restore faithfully then fails with the working
|
|
268
|
+
// tree intact rather than half applied.
|
|
269
|
+
const worktreePlan = await planTreeRestore(
|
|
270
|
+
this.trees,
|
|
271
|
+
this.blobs,
|
|
272
|
+
mergeResult.tree,
|
|
273
|
+
STASH_APPLY,
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// A tracked path the merged tree does not account for is one the stash
|
|
277
|
+
// deleted; it has to go, or the delete half of the stash is silently
|
|
278
|
+
// dropped. Tracked means "in the index", as it does for `reset --hard`:
|
|
279
|
+
// untracked files are none of this command's business, and a path removed
|
|
280
|
+
// from the index is no longer tracked whatever HEAD's tree still says.
|
|
281
|
+
const trackedPaths = new Set<string>();
|
|
282
|
+
for await (const entry of this.staging.entries()) {
|
|
283
|
+
trackedPaths.add(entry.path);
|
|
284
|
+
}
|
|
285
|
+
const removals = [...trackedPaths].filter((path) => !worktreePlan.paths.has(path));
|
|
286
|
+
|
|
287
|
+
const untrackedPlan = await this.planUntrackedRestore(
|
|
288
|
+
stashUntrackedCommit,
|
|
289
|
+
worktreePlan,
|
|
290
|
+
worktree,
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
// Everything below this line writes.
|
|
294
|
+
await writeTreeRestorePlan(worktree, this.blobs, worktreePlan, STASH_APPLY);
|
|
295
|
+
for (const path of removals) {
|
|
296
|
+
await worktree.remove(path);
|
|
297
|
+
}
|
|
298
|
+
if (untrackedPlan) {
|
|
299
|
+
// Restored untracked files are written to the working tree only - they
|
|
300
|
+
// were untracked when stashed and stay untracked now.
|
|
301
|
+
await writeTreeRestorePlan(worktree, this.blobs, untrackedPlan, STASH_APPLY);
|
|
302
|
+
}
|
|
303
|
+
if (mergedIndexTree !== undefined) {
|
|
304
|
+
await this.staging.readTree(this.trees, mergedIndexTree);
|
|
305
|
+
await this.staging.write();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
status: StashApplyStatus.OK,
|
|
310
|
+
stashCommit: stashId,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Plan the restore of the stash's untracked files, if there are any to restore.
|
|
316
|
+
*
|
|
317
|
+
* Untracked files are stashed in their own parentless commit and are not
|
|
318
|
+
* merged with anything: they are simply written back. A stashed untracked
|
|
319
|
+
* file whose path is already taken - by a file on disk, or by one the merged
|
|
320
|
+
* tree is about to write - is refused rather than overwritten, since nothing
|
|
321
|
+
* in the stash records what the displaced content was.
|
|
322
|
+
*
|
|
323
|
+
* @param untrackedCommitId The stash's third parent, if it has one
|
|
324
|
+
* @param worktreePlan The already-planned restore of the merged working tree
|
|
325
|
+
* @param worktree The working tree, checked for existing files
|
|
326
|
+
* @returns The plan, or undefined if there is nothing to restore
|
|
327
|
+
*/
|
|
328
|
+
private async planUntrackedRestore(
|
|
329
|
+
untrackedCommitId: ObjectId | undefined,
|
|
330
|
+
worktreePlan: TreeRestorePlan,
|
|
331
|
+
worktree: Worktree,
|
|
332
|
+
): Promise<TreeRestorePlan | undefined> {
|
|
333
|
+
if (!this.restoreUntracked || !untrackedCommitId) {
|
|
334
|
+
return undefined;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
const untrackedCommit = await this.commits.load(untrackedCommitId);
|
|
338
|
+
if (!untrackedCommit) {
|
|
339
|
+
throw new RefNotFoundError(untrackedCommitId, "Stash untracked commit not found");
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const plan = await planTreeRestore(this.trees, this.blobs, untrackedCommit.tree, STASH_APPLY);
|
|
343
|
+
|
|
344
|
+
for (const { path } of plan.entries) {
|
|
345
|
+
if (worktreePlan.paths.has(path) || (await worktree.exists(path))) {
|
|
346
|
+
throw new StashApplyFailedError(
|
|
347
|
+
untrackedCommitId,
|
|
348
|
+
undefined,
|
|
349
|
+
`stash apply would overwrite ${path} with a stashed untracked file`,
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return plan;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Resolve the stash reference to a commit ID.
|
|
359
|
+
*/
|
|
360
|
+
private async resolveStashRef(): Promise<ObjectId> {
|
|
361
|
+
const refName = this.stashRef ?? `${STASH_REF}@{0}`;
|
|
362
|
+
|
|
363
|
+
// Handle stash@{N} syntax
|
|
364
|
+
const match = refName.match(/^(.+)@\{(\d+)\}$/);
|
|
365
|
+
if (match) {
|
|
366
|
+
let baseRef = match[1];
|
|
367
|
+
const index = parseInt(match[2], 10);
|
|
368
|
+
|
|
369
|
+
// Normalize stash to refs/stash
|
|
370
|
+
if (baseRef === "stash") {
|
|
371
|
+
baseRef = STASH_REF;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// For index 0, just resolve the ref
|
|
375
|
+
if (index === 0) {
|
|
376
|
+
const ref = await this.refsStore.resolve(baseRef);
|
|
377
|
+
if (!ref?.objectId) {
|
|
378
|
+
throw new RefNotFoundError(refName);
|
|
379
|
+
}
|
|
380
|
+
return ref.objectId;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
// For other indices, need reflog support
|
|
384
|
+
// Note: The core RefStore interface doesn't include reflog methods.
|
|
385
|
+
// Without reflog, we can only access the most recent stash (index 0).
|
|
386
|
+
throw new RefNotFoundError(refName, `Stash entry ${index} not found (reflog not supported)`);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Try as direct ref
|
|
390
|
+
const ref = await this.refsStore.resolve(refName);
|
|
391
|
+
if (ref?.objectId) {
|
|
392
|
+
return ref.objectId;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Try as commit ID
|
|
396
|
+
if (await this.commits.has(refName)) {
|
|
397
|
+
return refName;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
throw new RefNotFoundError(refName);
|
|
401
|
+
}
|
|
402
|
+
}
|