Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cli/__tests__/ship-origin-base.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
19 changes: 7 additions & 12 deletions cli/lib/ship/origin-base.sh
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,15 @@ ship_origin_branch_oid() {
printf '%s\n' "$line"
}

# ship_origin_default_still <repo> <default> [oid]
# Is <default> STILL origin's default, AND (when <oid> 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 <repo> <default> <oid>
# Is <default> STILL origin's default, AND still at <oid>? 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 <repo> <default> <oid>
Expand Down