@devosurf/vynt 0.1.4 → 0.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devosurf/vynt",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "bin",
package/src/bridge.ts CHANGED
@@ -383,14 +383,9 @@ async function applySelectionsWithActiveRollback(
383
383
  throw error
384
384
  }
385
385
 
386
- const allowDirtyFiles = collectActiveSelectionChangedFiles(state)
387
- if (allowDirtyFiles.length === 0) {
388
- throw new Error(
389
- `${message}. No active changedFiles available for safe auto-rollback; run rollback first or register variants with changed files.`,
390
- )
391
- }
392
-
393
- await rollbackWorkspaceToSnapshot(projectRoot, undefined, { allowDirtyFiles })
386
+ await rollbackWorkspaceToSnapshot(projectRoot, undefined, {
387
+ allowDirtyFiles: collectActiveSelectionChangedFiles(state),
388
+ })
394
389
  return applySelectionsToWorkspace(projectRoot, state.baseRef, selections)
395
390
  }
396
391
  }
package/src/switch.ts CHANGED
@@ -24,10 +24,6 @@ export interface StoredSwitchSnapshot extends SwitchSnapshot {
24
24
  filePath: string
25
25
  }
26
26
 
27
- export interface RollbackWorkspaceOptions {
28
- allowDirtyFiles?: string[]
29
- }
30
-
31
27
  async function runGit(projectRoot: string, args: string[]): Promise<string> {
32
28
  try {
33
29
  const { stdout } = await execFileAsync("git", args, {
@@ -94,18 +90,65 @@ async function listDirtyTrackedFiles(projectRoot: string): Promise<string[]> {
94
90
  return [...new Set(files)]
95
91
  }
96
92
 
97
- async function ensureRollbackSafety(projectRoot: string, allowDirtyFiles: string[]): Promise<void> {
93
+ async function listFilesFromPatch(patchFile: string): Promise<string[]> {
94
+ const raw = await readFile(patchFile, "utf8")
95
+ const matches = raw.matchAll(/^diff --git a\/(.+?) b\/(.+)$/gm)
96
+ const files = new Set<string>()
97
+
98
+ for (const match of matches) {
99
+ const fromPath = normalizeRepoPath(match[1] ?? "")
100
+ const toPath = normalizeRepoPath(match[2] ?? "")
101
+
102
+ if (toPath.length > 0 && toPath !== "dev/null") {
103
+ files.add(toPath)
104
+ continue
105
+ }
106
+
107
+ if (fromPath.length > 0 && fromPath !== "dev/null") {
108
+ files.add(fromPath)
109
+ }
110
+ }
111
+
112
+ return [...files]
113
+ }
114
+
115
+ async function collectRollbackTargetFiles(snapshot: StoredSwitchSnapshot): Promise<string[]> {
116
+ const fromMetadata = snapshot.selections
117
+ .flatMap((selection) => selection.changedFiles ?? [])
118
+ .map(normalizeRepoPath)
119
+ .filter((file) => file.length > 0)
120
+
121
+ const fromPatchFiles = await Promise.all(snapshot.selections.map((selection) => listFilesFromPatch(selection.patchFile)))
122
+ const flattenedPatchFiles = fromPatchFiles.flat().map(normalizeRepoPath).filter((file) => file.length > 0)
123
+
124
+ return [...new Set([...fromMetadata, ...flattenedPatchFiles])]
125
+ }
126
+
127
+ async function ensureRollbackSafety(
128
+ projectRoot: string,
129
+ rollbackTargetFiles: string[],
130
+ allowDirtyFiles: string[],
131
+ ): Promise<void> {
98
132
  const dirtyFiles = await listDirtyTrackedFiles(projectRoot)
99
133
  if (dirtyFiles.length === 0) return
100
134
 
101
- const allowed = new Set(allowDirtyFiles.map(normalizeRepoPath))
102
- const disallowed = dirtyFiles.filter((file) => !allowed.has(normalizeRepoPath(file)))
135
+ const normalizedTargets = new Set(rollbackTargetFiles.map(normalizeRepoPath).filter((file) => file.length > 0))
136
+ if (normalizedTargets.size === 0) {
137
+ await ensureCleanWorkspace(projectRoot)
138
+ return
139
+ }
140
+
141
+ const overlapping = dirtyFiles.filter((file) => normalizedTargets.has(normalizeRepoPath(file)))
142
+ if (overlapping.length === 0) return
143
+
144
+ const allowed = new Set(allowDirtyFiles.map(normalizeRepoPath).filter((file) => file.length > 0))
145
+ const disallowed = overlapping.filter((file) => !allowed.has(normalizeRepoPath(file)))
103
146
  if (disallowed.length === 0) return
104
147
 
105
148
  const listed = disallowed.slice(0, 5).join(", ")
106
149
  const remainder = disallowed.length > 5 ? `, +${disallowed.length - 5} more` : ""
107
150
  throw new Error(
108
- `Rollback would overwrite uncommitted changes outside active variant files: ${listed}${remainder}. Commit or stash these changes before rollback.`,
151
+ `Rollback would overwrite uncommitted changes in files touched by the snapshot: ${listed}${remainder}. Commit or stash these changes before rollback.`,
109
152
  )
110
153
  }
111
154
 
@@ -159,6 +202,9 @@ function parseSnapshot(content: unknown, filePath: string): StoredSwitchSnapshot
159
202
  objectiveId: item.objectiveId,
160
203
  variantId: item.variantId,
161
204
  patchFile: item.patchFile,
205
+ changedFiles: Array.isArray(item.changedFiles)
206
+ ? item.changedFiles.filter((file): file is string => typeof file === "string")
207
+ : undefined,
162
208
  }
163
209
  })
164
210
 
@@ -329,10 +375,14 @@ export async function rollbackWorkspaceToSnapshot(
329
375
  throw new Error("No snapshots found. Run apply/profile apply first.")
330
376
  }
331
377
 
332
- await ensureRollbackSafety(projectRoot, options.allowDirtyFiles ?? [])
378
+ const rollbackTargetFiles = await collectRollbackTargetFiles(snapshot)
379
+ await ensureRollbackSafety(projectRoot, rollbackTargetFiles, options.allowDirtyFiles ?? [])
333
380
 
334
381
  await verifyBaseRef(projectRoot, snapshot.head)
335
- await restoreWorkspaceToBase(projectRoot, snapshot.head)
382
+ await restoreWorkspaceToBase(projectRoot, snapshot.head, rollbackTargetFiles)
336
383
 
337
384
  return { snapshot }
338
385
  }
386
+ export interface RollbackWorkspaceOptions {
387
+ allowDirtyFiles?: string[]
388
+ }