Skip to content

Commit f3055f6

Browse files
authored
fix(pr): keep landing mutations inside the dedicated PR worktree (#114723)
1 parent f0371e8 commit f3055f6

2 files changed

Lines changed: 201 additions & 20 deletions

File tree

‎scripts/pr-lib/worktree.sh‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,48 @@ enter_worktree() {
4343

4444
cd "$root"
4545
ensure_gh_api_auth
46-
git fetch origin main
46+
git -C "$root" fetch origin main
4747

48-
local dir=".worktrees/pr-$pr"
49-
if [ -d "$dir" ]; then
50-
cd "$dir"
51-
git fetch origin main
52-
if [ "$reset_to_main" = "true" ]; then
53-
git checkout -B "temp/pr-$pr" origin/main
54-
fi
55-
else
56-
local resolved_parent=""
57-
resolved_parent=$(resolve_existing_dir_path "$(dirname "$dir")" 2>/dev/null || true)
58-
local resolved_dir=""
59-
if [ -n "$resolved_parent" ]; then
60-
resolved_dir="$resolved_parent/$(basename "$dir")"
61-
fi
62-
if [ ! -e "$dir" ] && [ -n "$resolved_dir" ] && worktree_is_registered "$resolved_dir"; then
63-
echo "Pruning stale worktree registration for $dir"
64-
git worktree prune
48+
# Resolve through the parent, never through the leaf: a missing directory has
49+
# no real path of its own, and resolving a leaf symlink would silently adopt
50+
# whichever worktree it aliases.
51+
local dir="$root/.worktrees/pr-$pr"
52+
local resolved_parent resolved_dir=""
53+
resolved_parent=$(resolve_existing_dir_path "$(dirname "$dir")" 2>/dev/null || true)
54+
[ -z "$resolved_parent" ] || resolved_dir="$resolved_parent/pr-$pr"
55+
56+
if [ ! -d "$dir" ] || [ -z "$resolved_dir" ] || ! worktree_is_registered "$resolved_dir"; then
57+
if [ -e "$dir" ] || { [ -n "$resolved_dir" ] && worktree_is_registered "$resolved_dir"; }; then
58+
echo "Pruning stale worktree registration for .worktrees/pr-$pr"
59+
git -C "$root" worktree prune
60+
remove_worktree_if_present "$dir"
61+
[ ! -e "$dir" ] || {
62+
echo "Refusing scripts/pr operation for PR #$pr: $dir is not a registered worktree and could not be cleared; scripts/pr refuses to mutate the shared canonical checkout." >&2
63+
return 1
64+
}
6565
fi
6666
# Per-PR locking makes resetting this script-owned branch namespace safe.
67-
git worktree add "$dir" -B "temp/pr-$pr" origin/main
68-
cd "$dir"
67+
git -C "$root" worktree add "$dir" -B "temp/pr-$pr" origin/main
68+
resolved_dir="$(resolve_existing_dir_path "$(dirname "$dir")")/pr-$pr"
69+
fi
70+
71+
cd "$resolved_dir"
72+
73+
# Containment, not repair: every mutation below runs against ambient cwd, so
74+
# prove Git resolves it to this worktree before any branch moves. A directory
75+
# that is not a worktree lets discovery escape up into the shared canonical
76+
# checkout, where a sibling session's branch would be clobbered.
77+
local actual_toplevel
78+
actual_toplevel=$(resolve_existing_dir_path "$(git rev-parse --path-format=absolute --show-toplevel 2>/dev/null)" 2>/dev/null || true)
79+
if [ "$actual_toplevel" != "$resolved_dir" ]; then
80+
echo "Refusing scripts/pr operation for PR #$pr: expected worktree $resolved_dir, Git resolved ${actual_toplevel:-no repository}; scripts/pr refuses to mutate the shared canonical checkout." >&2
81+
return 1
6982
fi
7083

84+
git fetch origin main
85+
if [ "$reset_to_main" = "true" ]; then
86+
git checkout -B "temp/pr-$pr" origin/main
87+
fi
7188
mkdir -p .local
7289
}
7390

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { spawnSync } from "node:child_process";
2+
import { mkdirSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";
6+
7+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
8+
const repoRoot = process.cwd();
9+
const commonScript = join(repoRoot, "scripts/pr-lib/common.sh");
10+
const worktreeScript = join(repoRoot, "scripts/pr-lib/worktree.sh");
11+
const reviewScript = join(repoRoot, "scripts/pr-lib/review.sh");
12+
const describePosix = process.platform === "win32" ? describe.skip : describe;
13+
14+
type Fixture = {
15+
root: string;
16+
mainSha: string;
17+
siblingBranch: string;
18+
siblingSha: string;
19+
};
20+
21+
function git(root: string, ...args: string[]) {
22+
const result = spawnSync("git", args, { cwd: root, encoding: "utf8" });
23+
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
24+
return result.stdout.trim();
25+
}
26+
27+
function createFixture(): Fixture {
28+
const root = realpathSync(tempDirs.make("openclaw-pr-worktree-containment-"));
29+
git(root, "init", "--initial-branch=main");
30+
git(root, "config", "user.name", "OpenClaw Test");
31+
git(root, "config", "user.email", "[email protected]");
32+
writeFileSync(join(root, "fixture.txt"), "main\n");
33+
git(root, "add", "fixture.txt");
34+
git(root, "commit", "-m", "main fixture");
35+
const mainSha = git(root, "rev-parse", "HEAD");
36+
git(root, "remote", "add", "origin", root);
37+
git(root, "fetch", "origin");
38+
git(root, "checkout", "-b", "sibling/work");
39+
writeFileSync(join(root, "fixture.txt"), "sibling\n");
40+
git(root, "commit", "-am", "sibling fixture");
41+
return {
42+
root,
43+
mainSha,
44+
siblingBranch: git(root, "branch", "--show-current"),
45+
siblingSha: git(root, "rev-parse", "HEAD"),
46+
};
47+
}
48+
49+
function makeStaleWorktreeDir(fixture: Fixture) {
50+
mkdirSync(join(fixture.root, ".worktrees", "pr-42"), { recursive: true });
51+
}
52+
53+
function runShell(fixture: Fixture, commands: string[]) {
54+
return spawnSync(
55+
"bash",
56+
[
57+
"-c",
58+
[
59+
"set -euo pipefail",
60+
'source "$1"',
61+
'source "$2"',
62+
'source "$3"',
63+
'fixture_root="$4"',
64+
'repo_root() { printf "%s\\n" "$fixture_root"; }',
65+
"ensure_gh_api_auth() { :; }",
66+
"mark_pr_operation_side_effects_started() { :; }",
67+
"set_review_mode() { :; }",
68+
...commands,
69+
].join("\n"),
70+
"pr-worktree-containment",
71+
commonScript,
72+
worktreeScript,
73+
reviewScript,
74+
fixture.root,
75+
],
76+
{ cwd: fixture.root, encoding: "utf8" },
77+
);
78+
}
79+
80+
function expectCanonicalCheckoutUnchanged(fixture: Fixture) {
81+
expect(git(fixture.root, "branch", "--show-current")).toBe(fixture.siblingBranch);
82+
expect(git(fixture.root, "rev-parse", "HEAD")).toBe(fixture.siblingSha);
83+
}
84+
85+
describePosix("scripts/pr worktree containment", () => {
86+
it("stale .worktrees/pr-<N> directory does not clobber the canonical checkout", () => {
87+
const fixture = createFixture();
88+
makeStaleWorktreeDir(fixture);
89+
90+
runShell(fixture, ["enter_worktree 42 true"]);
91+
92+
expectCanonicalCheckoutUnchanged(fixture);
93+
});
94+
95+
it("review_checkout_main cannot detach the canonical checkout", () => {
96+
const fixture = createFixture();
97+
makeStaleWorktreeDir(fixture);
98+
99+
const result = runShell(fixture, ["review_checkout_main 42"]);
100+
101+
expectCanonicalCheckoutUnchanged(fixture);
102+
if (result.status !== 0) {
103+
expect(result.stderr).toContain("scripts/pr refuses to mutate the shared canonical checkout");
104+
}
105+
});
106+
107+
it("failure midway leaves the canonical checkout untouched", () => {
108+
const fixture = createFixture();
109+
const brokenWorktree = join(fixture.root, ".worktrees", "pr-42");
110+
git(fixture.root, "worktree", "add", brokenWorktree, "-b", "temp/pr-42", "origin/main");
111+
rmSync(join(brokenWorktree, ".git"));
112+
113+
const result = runShell(fixture, [
114+
"enter_worktree 42 false",
115+
"git checkout --detach origin/main",
116+
"exit 1",
117+
]);
118+
119+
expect(result.status).not.toBe(0);
120+
expectCanonicalCheckoutUnchanged(fixture);
121+
});
122+
123+
it("refuses a symlink alias pointing at another PR's worktree", () => {
124+
const fixture = createFixture();
125+
const worktrees = join(fixture.root, ".worktrees");
126+
mkdirSync(worktrees, { recursive: true });
127+
git(
128+
fixture.root,
129+
"worktree",
130+
"add",
131+
join(worktrees, "pr-99"),
132+
"-b",
133+
"temp/pr-99",
134+
"origin/main",
135+
);
136+
symlinkSync("pr-99", join(worktrees, "pr-42"), "dir");
137+
138+
const result = runShell(fixture, ["enter_worktree 42 true"]);
139+
140+
expect(result.status).not.toBe(0);
141+
expect(result.stderr).toContain("refuses to mutate the shared canonical checkout");
142+
expect(git(join(worktrees, "pr-99"), "branch", "--show-current")).toBe("temp/pr-99");
143+
expectCanonicalCheckoutUnchanged(fixture);
144+
});
145+
146+
it("reuses a properly registered PR worktree", () => {
147+
const fixture = createFixture();
148+
const expectedWorktree = join(fixture.root, ".worktrees", "pr-42");
149+
git(fixture.root, "worktree", "add", expectedWorktree, "-b", "temp/pr-42", "origin/main");
150+
151+
const result = runShell(fixture, [
152+
"enter_worktree 42 false",
153+
'printf "cwd=%s\\n" "$PWD"',
154+
'printf "branch=%s\\n" "$(git branch --show-current)"',
155+
'printf "head=%s\\n" "$(git rev-parse HEAD)"',
156+
]);
157+
158+
expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
159+
expect(result.stdout).toContain(`cwd=${realpathSync(expectedWorktree)}`);
160+
expect(result.stdout).toContain("branch=temp/pr-42");
161+
expect(result.stdout).toContain(`head=${fixture.mainSha}`);
162+
expectCanonicalCheckoutUnchanged(fixture);
163+
});
164+
});

0 commit comments

Comments
 (0)