← Back to Pull requests

fix(build-script): rerun commit-embedding scripts when git cannot open the repository #1074

Closed
unknown closed this pull request without merging on Dec 03, 2026 22:12 UTC
View on GitHub
unknown unknown commented on Dec 03, 2026 21:22 UTC
Author

A binary that embeds the commit hash could keep reporting the previous commit after HEAD moved, when git could not open the repository the build ran in. mbx now runs such a build script again instead of replaying its earlier result.

When it happened

All three of these had to hold:

1. mbx asks git for the checked-out commit instead of reading git's files itself. It does that when GIT_DIR, GIT_COMMON_DIR, GIT_CEILING_DIRECTORIES, GIT_DISCOVERY_ACROSS_FILESYSTEM or GIT_WORK_TREE is set, when the repository stores its references as reftable, or when a .git file names no directory.
2. git fails there although a checkout with a commit is on disk. The common case is fatal: detected dubious ownership in repository, in a container that mounts a checkout made by another user without a safe.directory entry. A repository format or configuration that git rejects does the same, and so does a GIT_DIR that names a directory that is not a repository.
3. The build script reads the commit without running git: through gix or git2, or by reading .git/HEAD. Those are not stopped by what stops the git command.

Before and after

Before, mbx took every failure of git rev-parse to mean that no commit is checked out. It stored the script's result under a key without the commit, and replayed it on the next commit, so the binary reported a stale hash.

Now only two answers from git mean "no commit": it finds no repository, or it finds one whose HEAD names no commit (a new branch before its first commit). After any other failure mbx treats the commit as unknown and does not store the script's result. The script then runs on every build, which shows in the statistics as a build-script-unreadable-head bypass. Adding the checkout to safe.directory lets git answer, and the results are cached again.

GIT_DIR gets one more check, because it keeps git from searching: when git finds no commit where GIT_DIR points and a .git is on disk above the package, the commit also counts as unknown.

Builds outside a repository are unchanged: a source tarball built with GIT_CEILING_DIRECTORIES set still has its build scripts cached.

How the cases are told apart

git exits 128 for every fatal error, so the exit status alone cannot separate them. mbx runs git rev-parse --verify --quiet HEAD with LC_ALL=C and reads its error. This is what git 2.53.0 answers:

| Case | Exit | Message | mbx reads it as |
| --- | --- | --- | --- |
| Branch with no commit yet | 1 | none | no commit |
| No repository | 128 | fatal: not a git repository (or any of the parent directories): .git | no commit |
| Search stopped at a filesystem boundary | 128 | fatal: not a git repository (or any parent up to mount point /) | no commit |
| A .git file naming a directory that is not a repository | 128 | fatal: not a git repository: <path> | no commit |
| GIT_DIR naming a directory that is not a repository | 128 | fatal: not a git repository: '<path>' | unknown if a .git is on disk above the package, otherwise no commit |
| Checkout owned by another user | 128 | fatal: detected dubious ownership in repository at '<path>' | unknown |
| Bare repository with safe.bareRepository=explicit | 128 | fatal: cannot use bare repository '<path>' … | unknown |
| Unsupported repository format | 128 | fatal: Expected git repo version <= 1, found 99 | unknown |
| Unparseable repository configuration | 128 | fatal: bad config line 1 in file .git/config | unknown |

A line starting with fatal: not a git repository is the only message read as "no repository". Git's source has every form of that error starting this way since at least 1.7.0, with a capital N before git 2.17, which is matched too. A message mbx does not recognize counts as unknown, so a wording change in a future git costs cache hits rather than correctness.

Validation

  • Unit tests run the real git against a directory with no repository, a GIT_DIR that is not a repository, a branch with no commit, a repository with a commit, a repository git treats as another user's, an unsupported format, an unparseable configuration, and a git that cannot be started. Two more check the message matching against the wordings above, including the older unsafe repository and capitalized forms, and the GIT_DIR check against a checkout on disk.

  • Two new end-to-end tests build two commits of a project whose build script reads .git/HEAD, one while git refuses the repository and one with GIT_DIR naming a missing directory. Without the matching change the second build's binary printed the first commit in each; with it both print the second.

  • cargo fmt --check, cargo clippy -p mbx --all-targets -- -D warnings, cargo test -p mbx --lib build_script and the a_build_script tests in cargo test -p mbx --test build pass.
  • Limits: the tests that make git refuse a repository use GIT_TEST_ASSUME_DIFFERENT_OWNER, which git has had since 2.35.3. Older git wordings were checked in git's source, not by running old versions. No translated git was installed where this was tested, so LC_ALL=C overriding a translated locale was not exercised. A GIT_DIR that names a different repository with a commit of its own is not covered: mbx keys on the commit git reports there.

    AI-assisted — Tool: Claude Code; model: anthropic/claude-fable-5-2; version: 2.1.332.

    🤖 Generated with Claude Code

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    Bug Fixes
    Build information now distinguishes repositories with no commit from cases where Git cannot determine the commit.
    Checkout commit information is preserved when Git cannot read repository metadata, including when repository settings point to an unavailable location.
    These changes make reported build information more accurate across detached checkouts and other Git access failures.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: jdx Review duration: 49m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1074"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%