Skip to content

Phase: git init falls back when -b is unsupported - #1676

Merged
neuromechanist merged 19 commits into
feature/issue-1671-epic-bru-upload-fixesfrom
feature/issue-1645-git-init-fallback
Oct 8, 2026
Merged

neuromechanist merged 19 commits into
feature/issue-1671-epic-bru-upload-fixesfrom
feature/issue-1645-git-init-fallback

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

initDataset ran git init -b main, which git older than 2.28 rejects as an unknown switch, so uploads stopped at "Initializing git-annex dataset" (issue #1645).
The initial branch is now set on any git version: git init -b main first, and when git answers with its usage-error status, a plain git init followed by pointing an unborn HEAD at refs/heads/main.

The first commit is the contributor's original fix (PR #1650, bruAristimunha), preserved with authorship.
The commits on top are review refinements:

  • the fallback keys on git's usage-error exit status (129) and reads no stderr text; git localizes its messages (Unbekannter Schalter, bascule inconnue), so a text match would miss the fallback on a non-English locale; every other failure is reported as before (a fatal path error exits 128)
  • the fallback names HEAD only when it is genuinely unborn: rev-parse -q --verify HEAD exiting 0 means HEAD resolves and is left alone; exiting exactly 1 is confirmed unborn only if symbolic-ref -q HEAD names a branch and show-ref --verify -q <ref> exits exactly 1; any other status, or a branch ref that exists but is unreadable, is returned immediately with git's own text and nothing is written (a corrupt refs/heads/master on an old git used to be re-pointed and reported as success, where modern git refuses loudly)
  • an existing repository keeps its branch, as with modern git, so the later branch check renames the adjusted branch built on it to main with its history intact instead of stranding the history on master
  • every failure after the -b rejection says so (git init -b main was rejected (exit 129); plain git init failed: ..., could not point HEAD at main: ...) and silent failures report exit N
  • both calls pass -- before the path, so a path starting with - is a path (a path like --bare used to create a bare repository in the current directory)
  • initGitRepoOnMain is module-private and isInitialBranchUnsupported is gone, so the tests drive the real entry point initDataset and ensureLocalMainBranch; the export-surface test needs no change
  • tests use a PATH git shim expressed as a list of answers keyed on the call (it logs every call, runs the real git for everything else, and answers init -b with a usage error and exit 129 in English, German and with empty stderr); real git and real git-annex do the rest; the seed repositories are built under the test's own git config with every step asserted; separate tests ask the real git for exit codes (129 for an unknown switch under C, German and French locales, 128 for a path that cannot be created)
  • GIT_CONFIG_GLOBAL needs git 2.32, so the file skips with a loud message outside CI when git is older and throws in CI, which can never skip silently

Part of epic #1671. Supersedes #1650 (contributor PR retargeted to the epic branch). Issue: #1645.

Behavior to know about

  • An existing but unborn repository on master is named main immediately by the fallback. Modern git leaves it on master until the later rename prompt and ends on refs/heads/main (the adjusted branch renamed) with a stray master; the fallback ends on adjusted/main(unlocked) with no stray branch, which is the healthier result.
  • A SIGHUP-killed git init -b also reads as 129 in Bun; the cost is a redundant fallback attempt. A fix needs runCommand to expose the signal.
  • Error strings after a rejected -b begin with git init -b main was rejected (exit 129); , which the callers print under "Failed to initialize git-annex dataset".

Test plan

  • bun run typecheck (root and backend), biome check and bun run lint
  • test/git-init-branch-fallback.unit.test.ts 30 pass with CI unset and with CI=true; test/git-annex-export-surface.unit.test.ts 11; eight neighbor files that import init.ts: all pass, 0 fail
  • the CI-tier grep (testRequest|TEST_API_URL|runCli\() finds nothing in the test file
  • 21 mutations plus one extra probe applied one at a time and restored byte-identical, each caught: stderr-text match fails 5, unborn guard dropped fails 2, -b never tried or fallback first fails 14 and 21, fall back on any non-zero exit fails 3, a non-129 failure swallowed fails 3, HEAD never re-pointed fails 7, -- dropped from either call fails 14 and 9, the previous any-non-zero-is-unborn guard fails 5, each of the new probe statuses fails 1 to 2, fallback prefix dropped fails 9, the pinned empty-answer guard fails 1
  • the skip logic checked with a stub git that ignores GIT_CONFIG_GLOBAL: outside CI all tests skip with a warning (exit 0), under CI=true the file throws (exit 1)
  • A real git older than 2.28 was not run here (Docker was not available); the 129 status is checked against real git 2.56 and its documented usage-error path. Someone can run docker run ubuntu:20.04 with apt git and git init -b main /tmp/x; echo $?, expecting 129, and check symbolic-ref -q and show-ref on a corrupt ref on that git.

Follow-ups (not in this PR)

  • expose signalCode from runCommand so a SIGHUP is not read as a usage error
  • for a repository with history, ensureLocalMainBranch renames adjusted/master(unlocked) to main; that is the existing behavior on modern git and is left alone

bruAristimunha and others added 4 commits October 7, 2026 17:42
initDataset ran 'git init -b main', which git 2.25 (Ubuntu 20.04's system
git, common on HPC images) rejects with "unknown switch `b'"; uploads failed
at 'Initializing git-annex dataset' unless a newer git was shimmed onto PATH.
initGitRepoOnMain now retries with a plain 'git init' and points the unborn
HEAD at refs/heads/main, which is what -b does. Other init failures are
reported as before.
git localizes its usage errors (German 'Unbekannter Schalter', French 'bascule inconnue'), so matching the English text left a non-English git 2.25 failing as in #1645. Fall back on exit status 129 instead and report every other failure as before.

The helper is module-private now; the tests drive initDataset under a PATH stand-in for an older git binary, with English, German, French and empty stderr.
Modern git ignores -b for a repository that already exists, so HEAD stays on its branch and the upload's branch check renames it to main with its history. The fallback re-pointed HEAD unconditionally, which stranded the history on master and made main a one-commit root.

Re-point only an unborn HEAD. The tests seed a two-commit master repository, run the upload's init and branch check under the older-git stand-in, and compare the end state with a modern git's. Tested.
A path under a file also fails downstream when the init error is swallowed, so a bare success flag did not catch a swallowed failure. Compare the reported error with what the real git prints for the same path. Tested.
@neuromechanist neuromechanist changed the title Phase: git init falls back when -b is unsupported (#1645) Phase: git init falls back when -b is unsupported Oct 8, 2026
The git stand-in refused three hard-coded calls through one field each. A list of answers keyed on the call's arguments lets later tests make any other call fail. No behavior change; the 17 tests still pass.
Seed the two-commit repository under the test's own git config with gpg signing off, and stop on any failing seed step. Add a canary that fails when GIT_CONFIG_GLOBAL is ignored (git < 2.32), which makes the version skips dead, so remove them. Ask the real git for its usage-error (129) and fatal (128) statuses in three locales, assert the modern control kept c1 and c2, fail clearly without git on PATH, and make the temp tree removable before cleanup. Tested.
A path starting with a dash was parsed as a switch: --bare silently created a bare repository in the working directory, and any other dash word was a usage error (exit 129) that the fallback then misread. Pass the path after -- in both git init calls. The test runs initDataset on a path named --bare in a scratch working directory under both a modern git and the older-git stand-in. Tested.
rev-parse exits 1 quietly both for an unborn branch and for a branch ref that exists but is corrupt, and 128 for an unreadable repository. The fallback read every non-zero status as unborn, so it re-pointed HEAD at main over a corrupt master and reported success while modern git refuses loudly.

Resolving HEAD leaves it alone. Exit 1 is then confirmed unborn only when symbolic-ref names a branch whose ref show-ref reports absent; any other status is returned at once with git's own stderr and no write. An existing unborn master is named main immediately; a populated develop beside it is untouched. Statuses only, no text. Tested.
Every failure after git init -b is rejected now starts with 'git init -b main was rejected (exit 129);' and names the step that failed: plain git init, reading HEAD, or pointing HEAD at main. A silent failing step reports its exit status instead of a generic text that was identical for the first and second attempts. The caller prints these under 'Failed to initialize git-annex dataset', where a bare git error read like a git-annex problem. Tested, including the empty-stderr variants of both fallback steps.
initDataset built the env options after git init and initGitRepoOnMain rebuilt them from the raw env. Build them once, before the first call, and pass them in. No behavior change; the fallback and add-targets tests pass.
Say 129 means git rejected -b apart from the path, which now follows --, drop history and unverifiable claims about which systems ship an old git, describe the adjusted-branch rename accurately, document that an existing unborn branch is named main at once, and fix the test comments that misdescribed the stand-in, the caller of the branch check and a leftover count assertion. Comment and title changes only; the 32 tests still pass.
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1676

Version: 0.10.15-PR1676.ff2bcc3
Commit: ff2bcc3


🤖 Updated on each push.

The expression 'stderr.trim() || exit N' was spelled five ways. One helper, said(), returns git's words or the exit status, and the unreadable-HEAD reasons use early returns instead of a nested ternary. No behavior change; the 32 fallback tests pass.
runCommand only spreads options.env, so an empty env needs no conditional and no EnvOptions type. initGitRepoOnMain and readHeadState take the env record, and initDataset's own env options line is back where it was, which removes that hunk from the diff. No behavior change; the fallback and add-targets tests pass.
Answer.when is now the start of the call's arguments instead of a quoted shell pattern, which removes quoting at five call sites. The never-answering logging wrapper is makeShim(), and the words/no-words cases are explicit triples instead of a string special case. Every assertion keeps its meaning; the 32 tests pass.
Three near-identical seeders become seed(dir, branch, ...messages). Commits use -c user.name and user.email only: the gpgsign flag duplicated the test's GIT_CONFIG_GLOBAL file, and gitOk throwing on any failing step replaces the rev-list sanity check. The 32 tests pass.
The German, French and empty-text fallback cases killed the identical set of mutants, so the stand-in keeps English, German and empty text; the real-git locale test still asks git itself for 129 under C, German and French. The fatal-128 check folds into the path-cannot-be-created test. The parity-with-modern-git and dash-path tests stay: they are the only oracles for modern behavior and for --. 29 tests pass.
The throwing beforeAll canary failed the whole file on a developer's git older than 2.32. A plain init that lands on trunk proves both init.defaultBranch and GIT_CONFIG_GLOBAL, so one probe at module level replaces it: outside CI the suite skips with a warning, under CI the file throws, following requireOnCi in the Neurobagel deploy tests. Checked with a git stub that drops GIT_CONFIG_GLOBAL: 29 skipped and exit 0 locally, exit 1 with CI=true; 29 pass on the host git.
Four tests repeated success false, the fallback note, the mentioned text and no re-point. expectRefusal(shim, dir, mention) runs initDataset and asserts all four; the corrupt-ref test now reads git's own probe words before calling it. The 29 tests pass.
Dropping the ref === '' check left every test green. git show-ref --verify -q '' exits 1, the same status as an absent branch, so an empty branch name would read as unborn and HEAD would be re-pointed. A stand-in symbolic-ref that succeeds with no output now has to be refused; with the guard removed that test fails.
@neuromechanist

Copy link
Copy Markdown
Contributor Author

Review record

One pass of the PR review toolkit (Sonnet agents): code review, test analysis, silent-failure hunt, comment analysis, then a propose-only simplifier. No critical findings. Every finding below was fixed unless it is listed under "Not done". Tip at the time of writing: ff2bcc39.

Important

  • The fallback read any non-zero rev-parse as "unborn" (silent-failure hunt; ran on real git): a corrupt refs/heads/master on a git that answers 129 was re-pointed at main and reported as success, stranding the corrupt branch, where modern git refuses loudly; a rev-parse exit of 128 (not a repository, dubious ownership, a garbage HEAD file) was swallowed and surfaced only by luck from the next command, with a misleading message. Unborn is now confirmed by exit statuses only (rev-parse exactly 1, symbolic-ref -q HEAD naming a branch, show-ref --verify -q <ref> exactly 1); anything else is returned immediately with git's own text and nothing is written. Parity with modern git is kept for an unborn master over a populated develop.
  • Test seed ran outside the shim and ignored exit codes (test analysis; ran with a failing signing config): the seed repositories now use the test's own git config with every step asserted.
  • The GIT_CONFIG_GLOBAL trick needs git 2.32 (test analysis): on older hosts the "never re-point HEAD" mutant would pass silently. A module-level probe now skips the file with a loud message outside CI and throws in CI.
  • A usage error was only hand-written by the tests (test analysis): new tests ask the real git for exit codes (129 for an unknown switch under C, German and French locales; 128 for a path that cannot be created).
  • Comments that were false (comment analysis, run against git): "the arguments are fixed, so a usage error can only mean the switch" (the path is an argument), "fails exactly as the unfixed code did" (refers to a revision that is not in the tree), the adjusted-branch rename (it is adjusted/master(unlocked) that is renamed), and unverifiable folklore and history ("common on HPC", "previously depositors had to put a shim on PATH").

Suggestions and nice-to-have (all applied)

  • Failure text names the fallback step and reports exit N for silent failures; -- before the path in both calls (a path like --bare used to create a bare repository in the current directory); empty-stderr variants of the failing plain init and re-point; an existing but unborn repository test; env options passed straight to runCommand; a clear error when git is missing; cleanup that handles read-only annex objects.
  • Simplifier: one said() helper for stderr-or-exit text, three seeders merged into one, shared refusal assertions, the stand-in's answers keyed on a plain call prefix, overlapping cases dropped (the French stand-in case killed exactly the same mutants as the German one; the real-git locale test keeps French), and the ref === "" guard pinned with its own test after a probe showed it was unprotected.

Verification

  • 21 mutations plus one probe applied one at a time and restored byte-identical, each caught; typecheck, Biome, 30 tests in the file with CI unset and with CI=true, eight neighbor files, tier grep clean.
  • The skip logic was checked with a stub git that ignores GIT_CONFIG_GLOBAL: skip with a warning outside CI, throw in CI.

Not done (tracked follow-ups)

  • expose signalCode from runCommand
  • run the fallback against a real git 2.25 (Docker was not available)
  • the existing ensureLocalMainBranch rename behavior for a repository with history

@neuromechanist
neuromechanist merged commit bde6a22 into feature/issue-1671-epic-bru-upload-fixes Oct 8, 2026
14 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1645-git-init-fallback branch October 8, 2026 02:21

This branch was previously deployed

1 inactive deployment
npm — ff2bcc39 Deployed Oct 8, 2026 by neuromechanist via publish #3241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants