feat(FN-4623): complete Step 3 — add backend-aware path resolution
Fusion-Task-Id: FN-4623 Fusion-Task-Lineage: 58122853-cab7-4102-9649-4ceda4c5959e
This commit is contained in:
committed by
gsxdsm
parent
c98a4e2f82
commit
0f372f67c3
@@ -136,7 +136,7 @@ describe("acquireTaskWorktree foreign start-point warning", () => {
|
||||
return Promise.resolve({ stdout: "", stderr: "" });
|
||||
};
|
||||
|
||||
vi.doMock("node:child_process", () => ({ exec: execMock }));
|
||||
vi.doMock("node:child_process", () => ({ exec: execMock, execFile: execMock }));
|
||||
const mod = await import("../worktree-acquisition.js");
|
||||
|
||||
await mod.acquireTaskWorktree({
|
||||
|
||||
@@ -3,7 +3,7 @@ import { exec } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import type { RunMutationContext, Settings, Task, TaskStore } from "@fusion/core";
|
||||
import { generateWorktreeName, slugify } from "./worktree-names.js";
|
||||
import { resolveTaskWorktreePath } from "./worktree-paths.js";
|
||||
import { resolveTaskWorktreePathForBackend } from "./worktree-paths.js";
|
||||
import { hydrateWorktreeDb } from "./worktree-db-hydrate.js";
|
||||
import { formatError } from "./logger.js";
|
||||
import { isBranchConflictError } from "./branch-conflicts.js";
|
||||
@@ -110,7 +110,7 @@ export async function acquireTaskWorktree(opts: AcquireTaskWorktreeOptions): Pro
|
||||
: naming === "task-title"
|
||||
? slugify(task.title || task.description.slice(0, 60))
|
||||
: generateWorktreeName(rootDir, settings);
|
||||
worktreePath = resolveTaskWorktreePath(rootDir, settings, worktreeName);
|
||||
worktreePath = await resolveTaskWorktreePathForBackend(rootDir, worktreeName, settings, backend, branchName);
|
||||
}
|
||||
|
||||
let isResume = Boolean(task.worktree && existsSync(worktreePath));
|
||||
@@ -118,7 +118,8 @@ export async function acquireTaskWorktree(opts: AcquireTaskWorktreeOptions): Pro
|
||||
logger?.log(`${task.id}: assigned worktree is not usable; creating a fresh worktree instead: ${worktreePath}`);
|
||||
await store.logEntry(task.id, "Assigned worktree is not a registered, usable git worktree; creating a fresh worktree instead", worktreePath, runContext);
|
||||
await store.updateTask(task.id, { worktree: null, branch: null });
|
||||
worktreePath = resolveTaskWorktreePath(rootDir, settings, generateWorktreeName(rootDir, settings));
|
||||
const fallbackName = generateWorktreeName(rootDir, settings);
|
||||
worktreePath = await resolveTaskWorktreePathForBackend(rootDir, fallbackName, settings, backend, branchName);
|
||||
isResume = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { exec, execFile } from "node:child_process";
|
||||
import { access } from "node:fs/promises";
|
||||
import { promisify } from "node:util";
|
||||
import type { Settings } from "@fusion/core";
|
||||
import { resolveTaskWorktreePath } from "./worktree-paths.js";
|
||||
import { inspectBranchConflict } from "./branch-conflicts.js";
|
||||
import { formatError } from "./logger.js";
|
||||
|
||||
@@ -69,6 +70,7 @@ export interface WorktreeBackend {
|
||||
remove(input: WorktreeRemoveInput): Promise<void>;
|
||||
sync(input: WorktreeSyncInput): Promise<{ skipped: boolean }>;
|
||||
prune(input: WorktreePruneInput): Promise<void>;
|
||||
resolveWorktreePath(input: { rootDir: string; worktreeName: string; branch: string }): Promise<string>;
|
||||
}
|
||||
|
||||
export type WorktrunkOperationCode =
|
||||
@@ -137,7 +139,12 @@ function parseWorktreesFromPorcelain(porcelain: string): Array<{ path: string; b
|
||||
export class NativeWorktreeBackend implements WorktreeBackend {
|
||||
readonly kind: WorktreeBackendKind = "native";
|
||||
|
||||
constructor(private readonly deps: { logger?: { log: (m: string) => void; warn: (m: string) => void } } = {}) {}
|
||||
constructor(
|
||||
private readonly deps: {
|
||||
logger?: { log: (m: string) => void; warn: (m: string) => void };
|
||||
settings?: Pick<Settings, "worktreesDir">;
|
||||
} = {},
|
||||
) {}
|
||||
|
||||
async create(input: WorktreeCreateInput): Promise<WorktreeCreateResult> {
|
||||
const startArg = input.startPoint ? ` ${quoteShellArg(input.startPoint)}` : "";
|
||||
@@ -229,6 +236,10 @@ export class NativeWorktreeBackend implements WorktreeBackend {
|
||||
maxBuffer: MAX_BUFFER,
|
||||
});
|
||||
}
|
||||
|
||||
async resolveWorktreePath(input: { rootDir: string; worktreeName: string; branch: string }): Promise<string> {
|
||||
return resolveTaskWorktreePath(input.rootDir, this.deps.settings, input.worktreeName);
|
||||
}
|
||||
}
|
||||
|
||||
type WorktrunkOperation = keyof typeof WORKTRUNK_TIMEOUTS_MS;
|
||||
@@ -393,6 +404,23 @@ export class WorktrunkWorktreeBackend implements WorktreeBackend {
|
||||
await this.remove({ rootDir: input.rootDir, worktreePath: row.path, branch: row.branch });
|
||||
}
|
||||
}
|
||||
|
||||
async resolveWorktreePath(input: { rootDir: string; worktreeName: string; branch: string }): Promise<string> {
|
||||
const template = await this.resolveWorktrunkTemplate(input.rootDir);
|
||||
const sanitizedBranch = input.branch.replace(/[\\/]/g, "-");
|
||||
return template.replace(/\{\{\s*branch\s*\|\s*sanitize\s*\}\}/g, sanitizedBranch).replace(/\{\{\s*branch\s*\}\}/g, input.branch);
|
||||
}
|
||||
|
||||
private async resolveWorktrunkTemplate(rootDir: string): Promise<string> {
|
||||
const { stdout } = await execAsync("git config --get worktrunk.worktree-path", {
|
||||
cwd: rootDir,
|
||||
encoding: "utf-8",
|
||||
timeout: WORKTRUNK_TIMEOUTS_MS.layout,
|
||||
maxBuffer: MAX_BUFFER,
|
||||
});
|
||||
const configured = stdout.trim();
|
||||
return configured || `${rootDir}/.worktrees/{{ branch | sanitize }}`;
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveWorktreeBackend(
|
||||
@@ -406,5 +434,5 @@ export function resolveWorktreeBackend(
|
||||
});
|
||||
}
|
||||
|
||||
return new NativeWorktreeBackend({ logger: deps.logger });
|
||||
return new NativeWorktreeBackend({ logger: deps.logger, settings });
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { homedir } from "node:os";
|
||||
import { basename, isAbsolute, join, relative, resolve } from "node:path";
|
||||
import type { Settings } from "@fusion/core";
|
||||
import type { WorktreeBackendKind } from "./worktree-backend.js";
|
||||
import { canonicalizePath } from "./worktree-pool.js";
|
||||
|
||||
export function resolveWorktreesDir(
|
||||
@@ -25,6 +26,23 @@ export function resolveTaskWorktreePath(
|
||||
return join(resolveWorktreesDir(rootDir, settings), worktreeName);
|
||||
}
|
||||
|
||||
// Structural backend input avoids importing the full WorktreeBackend interface here.
|
||||
export async function resolveTaskWorktreePathForBackend(
|
||||
rootDir: string,
|
||||
worktreeName: string,
|
||||
settings: Pick<Settings, "worktreesDir"> | undefined,
|
||||
backend: {
|
||||
kind: WorktreeBackendKind;
|
||||
resolveWorktreePath?: (input: { rootDir: string; worktreeName: string; branch: string }) => Promise<string>;
|
||||
},
|
||||
branch: string,
|
||||
): Promise<string> {
|
||||
if (backend.kind === "worktrunk" && backend.resolveWorktreePath) {
|
||||
return backend.resolveWorktreePath({ rootDir, worktreeName, branch });
|
||||
}
|
||||
return resolveTaskWorktreePath(rootDir, settings, worktreeName);
|
||||
}
|
||||
|
||||
export function isInsideConfiguredWorktreesDir(
|
||||
rootDir: string,
|
||||
settings: Pick<Settings, "worktreesDir"> | undefined,
|
||||
|
||||
Reference in New Issue
Block a user