Validate patches in cloud Mac CI with an isolated Git index
A CI repository on a long-running cloud Mac is building a release branch, while another job needs to validate a patch. Running git apply directly would mix unapproved changes into the working tree. Temporarily setting changes aside and restoring them afterward could also overwrite another job’s state. A better approach is to apply the patch to a temporary index, produce a candidate tree, and then decide whether it should proceed to the build stage.
Here, “side-effect-free” means only that the original staging area and working tree remain unchanged. It does not mean the repository’s object database stays untouched. The workflow below handles ordinary Git diff patches; it does not process the author information, commit messages, or signatures in email patches.
Pin the base commit instead of following a branch name
Validation requires three inputs: a full base commit hash, a patch file, and the directories the patch is allowed to modify. Branch names can move, so the report must record the resolved commit hash—not just “the release branch.”
The upstream job must deliver the patch to a separate input directory and stop modifying it once validation begins. If concurrent writes are possible, complete an atomic handoff before copying the file into this job’s directory. Copying alone does not guarantee a consistent snapshot. A file digest helps track the content, but it does not establish that the source is trustworthy.
Patch applicability, change scope, and code correctness are three separate gates. Passing the first does not replace the other two.
Using the current working directory as the baseline is especially risky on a long-running runner: it may contain files left over from a previous build. This workflow reads the tree directly from the specified commit, keeping those files out of the baseline.
Generate a candidate tree with a temporary index
Save the following script as validate-patch.sh and run it from the repository root. Git must be installed locally, and the base commit object must already be available. The optional third argument specifies the report path; store the report outside the working tree.
#!/bin/bash
set -euo pipefail
base=$(git rev-parse --verify "${1:?base commit required}^{commit}")
patch_input=${2:?patch file required}
report=${3:-"$HOME/patch-validation.txt"}
task_dir=$(mktemp -d "${TMPDIR:-/tmp}/patch-check.XXXXXX")
cleanup() {
rm -rf "$task_dir"
}
trap cleanup EXIT
cp "$patch_input" "$task_dir/input.patch"
export GIT_INDEX_FILE="$task_dir/index"
git read-tree "$base"
git apply --cached --check --whitespace=error-all \
"$task_dir/input.patch"
git apply --cached --whitespace=error-all \
"$task_dir/input.patch"
git diff --cached --check "$base"
tree=$(git write-tree)
{
printf 'base=%s\n' "$base"
printf 'tree=%s\n' "$tree"
shasum -a 256 "$task_dir/input.patch"
git diff --cached --stat "$base"
} > "$report"
Invoke it as follows. HEAD is resolved to a fixed commit when the script starts:
bash validate-patch.sh HEAD "$HOME/incoming/change.patch"
read-tree initializes the temporary index, and --cached explicitly limits the operation to that index. The first invocation checks the patch; the second actually produces the candidate content. A failure at any step stops the script without writing a success report. --whitespace=error-all is the strict policy used in this example. If your team permits particular whitespace formats, adjust the rules explicitly rather than silently fixing whitespace and treating the result as the original patch.
Review paths and file modes before the script exits
When integrating this into a production workflow, place the path gate before write-tree. The temporary index is deleted when the script exits. Any additional checks must inherit the same GIT_INDEX_FILE; do not run them in a separate step that lacks this variable.
git diff --cached --name-status "$base"
git diff --cached --summary "$base"
git diff --cached --name-only -z "$base" > "$task_dir/paths.bin"
The first two commands are for human review; the last is for programmatic processing. Automated checks must split paths on null bytes, not spaces or newlines. Otherwise, filenames containing spaces or tabs can break the checks.
Build an allowlist around the repository’s conventions. For example, application patches should be limited to source and test directories, while build scripts, pipeline configuration, and dependency lockfiles require separate approval. Checks must cover deletions and renames as well as newly added files.
File mode changes matter too. A regular file becoming a symbolic link, or a script gaining an executable bit, belongs in the report. Do not add --unsafe-paths to bypass path protection, and do not mistake path validation for a sandbox in which to execute untrusted code.
Turn application failures into diagnosable results
If Git reports that a patch cannot be applied, first compare the commit hash and patch digest, then inspect the failing paths. Common causes include a shifted baseline, changed context, or a patch that has already been partially merged. Do not immediately reach for whitespace-ignoring options just to make the check pass.
This workflow does not enable three-way merging or use --reject to leave partial results behind. Patches that require a three-way merge should go through a separate review process that explicitly records the base used and the outcome of conflict resolution.
Prepare three validation fixtures as well: a patch that applies cleanly, a patch with a context conflict, and a patch that violates the path allowlist. Verify candidate tree creation, a nonzero exit status, and rejection by the scope gate, respectively. This prevents “the script finished” from becoming the only definition of success.
Bind subsequent builds to the same candidate version
A candidate tree has no parent commit or author information, so its tree hash alone does not make it a release commit. A trusted job can turn it into a temporary commit whose parent is the pinned base, then check that commit out in a separate working tree. Do not update shared branches before review is complete.
The build stage must consume this candidate commit rather than reread a patch file that may have changed. For untrusted input, use an isolated job without release credentials. Do not run candidate build scripts directly in a user session that is currently publishing a release.
The final report should retain the base commit, patch digest, candidate tree, candidate commit, and test results. After deleting the temporary index, do not immediately perform aggressive cleanup of the shared object database. If candidate objects need to be retained long term, keep them reachable through dedicated refs. This makes validation reproducible for review without disrupting an active working tree just to check a patch.
Frequently asked questions
Does a temporary index change the existing working tree?
With GIT_INDEX_FILE set, read-tree and apply --cached operate on the specified index without changing the original index or working tree. Applying a patch and running write-tree can still add repository objects.
Does passing git apply --check make a patch safe to merge?
No. It only confirms applicability to the selected base. Review changed paths and run builds, tests, and security checks against the candidate commit in an isolated environment.
Rent a dedicated Mac mini for your self-hosted runner
The entire machine is dedicated to one customer, with prepaid daily, weekly, monthly, or quarterly plans. After payment, find the delivery details in the console. Your team manages system updates, Xcode versions, and runner processes.
Choose a Mac mini server