Validate Git history before incremental checks on a cloud Mac

Validate Git history before incremental checks on a cloud Mac

The same commit shows more than a dozen changed files on a developer’s machine, but only two in the incremental pipeline on a cloud Mac. Before clearing the build cache, check the history: shallow clones, replacement refs, and legacy graft files on a long-lived runner can all change the commit relationships Git sees. This article adds a history gate before source checks so that a passing result covers the intended range—not an unexpectedly reduced diff.

Define the history range to compare first

Assume the pipeline provides two full commit IDs: CI_BASE_SHA, the baseline from the trusted target branch, and CI_HEAD_SHA, the commit to check. Both should be injected by trusted pipeline orchestration configuration. Do not take them directly from the body of a change request or resolve a moving branch name halfway through the run.

This article compares the common ancestor with the commit under review, which is suitable for checking file changes introduced by a branch. If the checked-out commit is a merge result, use that as the target and define the comparison starting point separately. Do not mix the two sets of semantics.

A Git history gate validates the comparison range; it does not authenticate the code’s origin. Disabling local replacements does not establish that a commit is trusted, nor does it replace code review.

Record whether the commit actually built matches the commit being checked. If you inspect branch source but build a merge result, calling both the “current commit” makes it impossible to correlate the evidence.

Inspect shallow clones and local commit-graph rewrites

Start by running these commands in the repository:

git --version
git rev-parse --is-shallow-repository
git for-each-ref --format='%(refname)' refs/replace/
git rev-parse --git-path info/grafts

The first command records the tool version. A result of true from the second means the history is truncated. The third should produce no output. The fourth only returns a path; you still need to inspect the file’s contents.

Replacement refs let another object stand in for the original commit, affecting logs, ancestry checks, and diff calculations. Grafts rewrite parent relationships through a local file. These may be leftovers from troubleshooting rather than an attack, but neither should silently affect release checks.

Use --git-path rather than hard-coding .git/info/grafts: a worktree’s .git may be a text file pointing to the actual Git directory. If you find an anomaly, preserve the file and ref evidence before preparing a fresh workspace. Do not simply delete them in the script that picks up jobs.

Make the preflight script fail closed

Run the following script in the repository directory using modern Git and Bash. Both environment variables must already contain the commit IDs pinned by the pipeline orchestrator:

set -euo pipefail

: "${CI_BASE_SHA:?CI_BASE_SHA is required}"
: "${CI_HEAD_SHA:?CI_HEAD_SHA is required}"

export GIT_NO_REPLACE_OBJECTS=1

if [ "$(git rev-parse --is-shallow-repository)" != false ]; then
  printf '%s\n' 'Shallow history is not accepted' >&2
  exit 2
fi

grafts=$(git rev-parse --git-path info/grafts)
if [ -s "$grafts" ]; then
  printf '%s\n' 'Local grafts are not accepted' >&2
  exit 2
fi

replacements=$(git for-each-ref --format='%(refname)' refs/replace/)
if [ -n "$replacements" ]; then
  printf '%s\n' 'Replacement refs are not accepted' >&2
  exit 2
fi

base=$(git rev-parse --verify --end-of-options "${CI_BASE_SHA}^{commit}")
head=$(git rev-parse --verify --end-of-options "${CI_HEAD_SHA}^{commit}")
merge_base=$(git merge-base --all "$base" "$head")

if [ -z "$merge_base" ] || [[ "$merge_base" == *$'\n'* ]]; then
  printf '%s\n' 'A single merge base is required' >&2
  exit 2
fi

printf 'base=%s\nhead=%s\nmerge_base=%s\n' \
  "$base" "$head" "$merge_base"

git diff --no-ext-diff --no-textconv --name-only -z \
  "$merge_base" "$head" -- > changed-paths.z

Object replacement is disabled before commit resolution, and refs in the default replacement namespace are also rejected so that leftover state is not simply ignored. If no common ancestor can be found, the command fails and stops the script. Multiple best common ancestors also block the run, rather than allowing an arbitrary choice.

The output paths are NUL-delimited, and downstream consumers must read them that way. Do not split filenames on spaces or turn a failed command into an empty file and then declare “no changes.”

Fetch missing history without moving the baseline

Once you have verified that the remote is trusted and that network access and credentials work, you can run this in a shallow repository:

git fetch --unshallow --no-tags origin

This is not a universal fix: the remote itself may be incomplete, and the required commit may not belong to any ref covered by the current fetch rules. After fetching, check the shallow status again and verify the two originally pinned commits. Do not substitute the freshly updated remote branch tip just to make the check pass.

This article conservatively rejects all shallow repositories. That does not mean every incremental check requires the entire history. If your team chooses to deepen history on demand, explicitly verify the history needed to establish the common ancestor and set a limit on fetch attempts or elapsed time. Block the run when that budget is exhausted; do not fall back to zero changes.

For branches with no shared history, have someone determine whether the cause is the wrong repository, the wrong baseline, or an intentionally independent history. Do not automatically force a merge to hide the problem.

Test the gate and its evidence with failure cases

In a disposable test repository, create a shallow clone, replacement refs, and a nonempty graft file as separate test cases. Verify that all three fail before the incremental tool starts. Also supply a nonexistent commit ID and confirm that a wrapper script does not swallow the failure.

Passing test cases should cover at least an ordinary branch, a target branch that has advanced, no file changes, and filenames containing spaces or newlines. For complex merge graphs, verify that multiple common ancestors block the run rather than produce an unstable file list.

For every run, retain the Git version, baseline commit, target commit, common ancestor, and raw path list. Log only the identifiers you need; do not print remote URLs containing credentials. Have the incremental check consume that same fixed list, and keep replacements disabled in the environment used by subsequent scripts.

The acceptance criterion is not that every repository passes. It is that incomplete history or an ambiguous baseline causes an explicit failure. Only then does a green result represent a check range you can explain.

Frequently asked questions

Does every incremental check require a full clone?

No. Limited history can work when both commits and their required ancestry are available. This conservative gate rejects shallow repositories. A relaxed policy must fetch from a trusted source, verify ancestry completeness, and fail rather than treat a missing merge base as an empty diff.

Should graft files be checked when replacement objects are disabled?

Yes. Grafts are a separate legacy mechanism for rewriting the local commit graph. The gate rejects a nonempty info/grafts file and default replacement refs, while GIT_NO_REPLACE_OBJECTS disables object replacement.

Choose a dedicated cloud Mac for your build workload

Match the chip, memory, SSD, and rental term to your workload. Each physical machine is assigned to one customer; check the console for real-time availability.

Check the requirements before connecting

Find connection methods, runner registration, and toolchain setup in the Help Center. See rental plans for configurations and billing periods.