Skip to content

Commit b7bbb4f

Browse files
committed
fix(pr): join worktree discovery subprocesses
1 parent c481033 commit b7bbb4f

2 files changed

Lines changed: 96 additions & 23 deletions

File tree

‎scripts/pr-lib/common.sh‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ file_list_is_docsish_only() {
2626
if ! path_is_docsish "$path"; then
2727
return 1
2828
fi
29-
done < <(printf '%s\n' "$files")
29+
done <<< "$files"
3030

3131
[ "$saw_any" = "true" ]
3232
}
@@ -185,30 +185,33 @@ common_repo_root() {
185185
worktree_path_for_branch() {
186186
local branch="$1"
187187
local ref="refs/heads/$branch"
188-
local field worktree=""
189-
while IFS= read -r -d '' field; do
190-
case "$field" in
191-
worktree\ *) worktree="${field#worktree }" ;;
192-
"branch $ref")
193-
[ -n "$worktree" ] || return 1
194-
printf '%s\n' "$worktree"
195-
return 0
196-
;;
197-
"") worktree="" ;;
198-
esac
199-
done < <(git worktree list --porcelain -z)
200-
return 1
188+
local field worktree="" match=""
189+
# Drain foreground Git before the supervisor checks for leftover children.
190+
git worktree list --porcelain -z | {
191+
while IFS= read -r -d '' field; do
192+
case "$field" in
193+
worktree\ *) worktree="${field#worktree }" ;;
194+
"branch $ref") match="$worktree" ;;
195+
"") worktree="" ;;
196+
esac
197+
done
198+
[ -n "$match" ] || return 1
199+
printf '%s\n' "$match"
200+
}
201201
}
202202

203203
worktree_is_registered() {
204204
local path="$1"
205-
local field
206-
while IFS= read -r -d '' field; do
207-
case "$field" in
208-
worktree\ *) [ "${field#worktree }" = "$path" ] && return 0 ;;
209-
esac
210-
done < <(git worktree list --porcelain -z)
211-
return 1
205+
local field found=false
206+
# Git must finish before a successful operation can release its lock.
207+
git worktree list --porcelain -z | {
208+
while IFS= read -r -d '' field; do
209+
case "$field" in
210+
worktree\ *) [ "${field#worktree }" != "$path" ] || found=true ;;
211+
esac
212+
done
213+
[ "$found" = true ]
214+
}
212215
}
213216

214217
resolve_existing_dir_path() {

‎test/scripts/pr-operation-lock.test.ts‎

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,37 @@ describePosix("scripts/pr per-PR operation lock", () => {
13581358
}
13591359
});
13601360

1361+
it("joins worktree-list producers before releasing a successful operation lock", async () => {
1362+
const repoDir = createRepo();
1363+
const producerExited = join(repoDir, "worktree-producer-exited");
1364+
const fixture = writeOperationFixture(repoDir, "joined-worktree-operation.sh", [
1365+
"acquire_pr_operation_lock 42",
1366+
"git() {",
1367+
' if [ "$1" = worktree ] && [ "$2" = list ]; then',
1368+
" printf 'worktree %s\\0branch refs/heads/pr-42\\0\\0' \"$PWD\"",
1369+
" exec 1>&-",
1370+
" sleep 0.1",
1371+
" : >worktree-producer-exited",
1372+
" return 0",
1373+
" fi",
1374+
' command git "$@"',
1375+
"}",
1376+
'worktree_is_registered "$PWD"',
1377+
"test -f worktree-producer-exited",
1378+
"rm worktree-producer-exited",
1379+
'resolved="$(worktree_path_for_branch pr-42)"',
1380+
'test "$resolved" = "$PWD"',
1381+
"test -f worktree-producer-exited",
1382+
]);
1383+
1384+
const result = await runSupervisedFixture(repoDir, fixture);
1385+
1386+
expect(result.status, result.stdout + "\n" + result.stderr).toBe(0);
1387+
expect(existsSync(producerExited)).toBe(true);
1388+
expect(refExists(repoDir)).toBe(false);
1389+
expect(result.stderr).not.toContain("process group remained active after wrapper exit");
1390+
});
1391+
13611392
it("retains a failed operation lock when a detached child outlives its launcher", async () => {
13621393
const repoDir = createRepo();
13631394
const nestedPidFile = join(repoDir, "failed-nested-pgid");
@@ -2083,8 +2114,8 @@ describePosix("scripts/pr per-PR operation lock", () => {
20832114
expect(existsSync(worktreeDir)).toBe(true);
20842115
});
20852116

2086-
it("removes a registered relative worktree under a repo path with escapes", () => {
2087-
const repoDir = createRepo("repo with space \\ backslash");
2117+
it("removes a registered relative worktree under a NUL-framed escaped Unicode path", () => {
2118+
const repoDir = createRepo("repo with space \\ backslash\n雪");
20882119
const worktreeDir = join(repoDir, ".worktrees", "pr-42");
20892120
mkdirSync(dirname(worktreeDir), { recursive: true });
20902121
execFileSync("git", ["worktree", "add", "-q", "-b", "pr-42", worktreeDir], {
@@ -2113,6 +2144,45 @@ describePosix("scripts/pr per-PR operation lock", () => {
21132144
).toBe(1);
21142145
});
21152146

2147+
it("propagates producer failures from NUL-framed worktree listings", () => {
2148+
const repoDir = createRepo();
2149+
const result = runLockShell(repoDir, [
2150+
"git() {",
2151+
' if [ "$1" = worktree ] && [ "$2" = list ]; then',
2152+
" printf 'worktree %s\\0branch refs/heads/pr-42\\0\\0' \"$PWD\"",
2153+
" return 23",
2154+
" fi",
2155+
' command git "$@"',
2156+
"}",
2157+
"set +e",
2158+
'worktree_is_registered "$PWD"',
2159+
'registered_status="$?"',
2160+
"worktree_path_for_branch pr-42 >/dev/null",
2161+
'branch_status="$?"',
2162+
'printf "%s %s\\n" "$registered_status" "$branch_status"',
2163+
]);
2164+
2165+
expect(result.status, result.stdout + "\n" + result.stderr).toBe(0);
2166+
expect(result.stdout.trim()).toBe("23 23");
2167+
});
2168+
2169+
it("accepts only nonempty docs-only file lists", () => {
2170+
const repoDir = createRepo();
2171+
const result = runLockShell(repoDir, [
2172+
"set +e",
2173+
"file_list_is_docsish_only ''",
2174+
'empty_status="$?"',
2175+
"file_list_is_docsish_only $'docs/guide.md\\nREADME.md'",
2176+
'docs_status="$?"',
2177+
"file_list_is_docsish_only $'docs/guide.md\\nsrc/index.ts'",
2178+
'mixed_status="$?"',
2179+
'printf "%s %s %s\\n" "$empty_status" "$docs_status" "$mixed_status"',
2180+
]);
2181+
2182+
expect(result.status, result.stdout + "\n" + result.stderr).toBe(0);
2183+
expect(result.stdout.trim()).toBe("1 0 1");
2184+
});
2185+
21162186
it("prunes a registered worktree whose directory is already gone", () => {
21172187
const repoDir = createRepo();
21182188
const worktreeDir = join(repoDir, ".worktrees", "pr-42");

0 commit comments

Comments
 (0)