fix(worktrees): remove duplicate ownership discovery

This commit is contained in:
Mohamed Boudra
2026-07-17 10:06:16 +00:00
parent 98fd021b95
commit c05104d4f4
3 changed files with 11 additions and 78 deletions

View File

@@ -9,7 +9,6 @@ import type { ForgeService } from "../services/forge-service.js";
import {
deletePaseoWorktree,
isPaseoOwnedWorktreeCwd,
resolvePaseoWorktreeRootForCwd,
WorktreeTeardownError,
} from "../utils/worktree.js";
import type { TerminalManager } from "../terminal/terminal-manager.js";
@@ -243,16 +242,6 @@ async function resolveBackingDirectory(
paseoHome: dependencies.paseoHome,
worktreesRoot: dependencies.paseoWorktreesBaseRoot,
};
const resolvedWorktree = await resolvePaseoWorktreeRootForCwd(cwd, options);
if (resolvedWorktree) {
return {
path: resolve(resolvedWorktree.worktreePath),
isPaseoOwnedWorktree: true,
mainRepoRoot: resolvedWorktree.repoRoot,
paseoWorktreesRoot: resolvedWorktree.worktreeRoot,
};
}
const ownership = await isPaseoOwnedWorktreeCwd(cwd, options);
return {
path: resolve(ownership.allowed && ownership.worktreePath ? ownership.worktreePath : cwd),

View File

@@ -5,7 +5,6 @@ import {
deletePaseoWorktree,
isPaseoOwnedWorktreeCwd,
mapWorkspaceCwdToWorktree,
resolvePaseoWorktreeRootForCwd,
slugify,
type CreateWorktreeOptions,
type WorktreeConfig,
@@ -114,7 +113,7 @@ describe("paseo worktree manager", () => {
expect(ownership.allowed).toBe(false);
});
it("resolves the source checkout root separately from Git's common directory", async () => {
it("reports the source checkout root separately from Git's common directory", async () => {
const created = await createLegacyWorktreeForTest({
branchName: "placement-root-branch",
cwd: repoDir,
@@ -123,11 +122,11 @@ describe("paseo worktree manager", () => {
paseoHome,
});
const resolved = await resolvePaseoWorktreeRootForCwd(created.worktreePath, { paseoHome });
const ownership = await isPaseoOwnedWorktreeCwd(created.worktreePath, { paseoHome });
expect(resolved).not.toBeNull();
expect(createRealpathAwarePathMatcher(repoDir)(resolved?.repoRoot ?? "")).toBe(true);
expect(createRealpathAwarePathMatcher(created.worktreePath)(resolved?.worktreePath ?? "")).toBe(
expect(ownership.allowed).toBe(true);
expect(createRealpathAwarePathMatcher(repoDir)(ownership.repoRoot ?? "")).toBe(true);
expect(createRealpathAwarePathMatcher(created.worktreePath)(ownership.worktreePath ?? "")).toBe(
true,
);
});

View File

@@ -35,12 +35,7 @@ import { resolvePaseoHome } from "../server/paseo-home.js";
import { createExternalProcessEnv } from "../server/paseo-env.js";
import { parseGitRevParsePath, resolveGitRevParsePath } from "./git-rev-parse-path.js";
import { validateBranchSlug } from "@getpaseo/protocol/branch-slug";
import {
createRealpathAwarePathMatcher,
expandTilde,
getRealpathAwareRelativePath,
isPathInsideRoot,
} from "./path.js";
import { expandTilde, getRealpathAwareRelativePath, isPathInsideRoot } from "./path.js";
export { slugify, validateBranchSlug } from "@getpaseo/protocol/branch-slug";
@@ -1055,55 +1050,6 @@ export async function resolveExistingWorktreeForSlug({
};
}
export async function resolvePaseoWorktreeRootForCwd(
cwd: string,
options?: WorktreeRootOptions,
): Promise<{ repoRoot: string; worktreeRoot: string; worktreePath: string } | null> {
let gitCommonDir: string;
try {
gitCommonDir = await getGitCommonDir(cwd);
} catch {
return null;
}
const worktreesRoot = await getPaseoWorktreesRoot(
cwd,
options?.paseoHome,
options?.worktreesRoot,
);
let worktreeRoot: string | null = null;
try {
const { stdout } = await runGitCommand(["rev-parse", "--show-toplevel"], {
cwd,
envOverlay: READ_ONLY_GIT_ENV,
});
worktreeRoot = parseGitRevParsePath(stdout);
} catch {
worktreeRoot = null;
}
if (!worktreeRoot) {
return null;
}
const knownWorktrees = await listPaseoWorktrees({
cwd,
paseoHome: options?.paseoHome,
worktreesRoot: options?.worktreesRoot,
});
const matchesWorktreeRoot = createRealpathAwarePathMatcher(worktreeRoot);
const match = knownWorktrees.find((entry) => matchesWorktreeRoot(entry.path));
if (!match) {
return null;
}
return {
repoRoot: resolveRepoRootFromGitCommonDir(gitCommonDir),
worktreeRoot: worktreesRoot,
worktreePath: match.path,
};
}
export async function deletePaseoWorktree({
cwd,
worktreePath,
@@ -1139,13 +1085,12 @@ export async function deletePaseoWorktree({
const requestedPath = worktreePath ?? join(resolvedWorktreesRoot, worktreeSlug!);
const resolvedRequested = normalizePathForOwnership(requestedPath);
const ownership = await isPaseoOwnedWorktreeCwd(requestedPath, {
paseoHome,
worktreesRoot: worktreesBaseRoot,
});
const resolvedWorktree =
(
await resolvePaseoWorktreeRootForCwd(requestedPath, {
paseoHome,
worktreesRoot: worktreesBaseRoot,
})
)?.worktreePath ?? resolvedRequested;
ownership.allowed && ownership.worktreePath ? ownership.worktreePath : resolvedRequested;
const relativeWorktreePath = getRealpathAwareRelativePath(
resolvedWorktreesRoot,