diff --git a/cli/__tests__/ship-origin-base.test.mts b/cli/__tests__/ship-origin-base.test.mts index 43f52b4d..c6862a16 100644 --- a/cli/__tests__/ship-origin-base.test.mts +++ b/cli/__tests__/ship-origin-base.test.mts @@ -404,6 +404,18 @@ describe('origin-base.sh — ship_suggested_base prefers a default that contains expect(suggestedOf(dir)).toMatchObject({ status: 0, out: 'default main' }); }); + it('still answers the default when grep -q would exit before the advertisement is written', () => { + // Piped into `grep -q` under pipefail, a writer still mid-advertisement took SIGPIPE: "default moved". + // A 3-line advertisement lost that race under load; refs/heads/*/HEAD (ls-remote tail-matches) force it. + const { dir, bare } = seedAdvancedDefault(); + const tip = execFileSync('git', ['-C', bare, 'rev-parse', 'main'], { encoding: 'utf8' }).trim(); + const pads = Array.from({ length: 3000 }, (_, i) => `create refs/heads/pad${i}/HEAD ${tip}\n`); + execFileSync('git', ['-C', bare, 'update-ref', '--stdin'], { input: pads.join('') }); + execFileSync('git', ['-C', bare, 'pack-refs', '--all']); + + expect(suggestedOf(dir)).toMatchObject({ status: 0, out: 'default main' }); + }); + it('fetches an unseen default commit to judge it, without writing any ref', () => { const { dir, git, bare } = seedAdvancedDefault(); git(['update-ref', '-d', 'refs/remotes/origin/main']); diff --git a/cli/lib/ship/origin-base.sh b/cli/lib/ship/origin-base.sh index b3698869..921905e9 100644 --- a/cli/lib/ship/origin-base.sh +++ b/cli/lib/ship/origin-base.sh @@ -268,20 +268,15 @@ ship_origin_branch_oid() { printf '%s\n' "$line" } -# ship_origin_default_still [oid] -# Is STILL origin's default, AND (when is given) still at it -- else merely present? -# Both read from ONE ls-remote advertisement, so a HEAD repointed to another branch, or the default -# force-pushed, since it was judged cannot pass as the same answer. Anything that fails to read is "no". +# ship_origin_default_still +# Is STILL origin's default, AND still at ? Both read from ONE ls-remote advertisement, +# so a HEAD repointed or a default force-pushed since it was judged cannot pass. A failed read is "no". +# Matched in-shell, never piped into `grep -q`: under pipefail its early exit SIGPIPEs the writer. ship_origin_default_still() { - local repo=$1 def=$2 oid=${3-} out= tab - tab=$(printf '\t') + local repo=$1 def=$2 oid=$3 out= nl=$'\n' tab=$'\t' out=$(git -C "$repo" ls-remote --symref origin HEAD "refs/heads/$def" 2>/dev/null) || return 1 - printf '%s\n' "$out" | grep -qxF "ref: refs/heads/$def${tab}HEAD" || return 1 - if [ -n "$oid" ]; then - printf '%s\n' "$out" | grep -qxF "$oid${tab}refs/heads/$def" - else - printf '%s\n' "$out" | cut -f2 | grep -qxF "refs/heads/$def" - fi + case "$nl$out$nl" in *"${nl}ref: refs/heads/$def${tab}HEAD$nl"*) ;; *) return 1 ;; esac + case "$nl$out$nl" in *"$nl$oid${tab}refs/heads/$def$nl"*) return 0 ;; *) return 1 ;; esac } # ship_origin_default_contains