Audit Git attribute filters before checkout on cloud Mac CI
If the same commit builds in the office but reads a different resource file on a cloud Mac, caching is not necessarily to blame. Git can apply filters, line-ending conversions, and encoding conversions between its object database and the working tree. If the build account inherits different configuration, the checked-out bytes may differ too. Audit input transformations before cleaning the build directory, or you risk deleting the evidence of the root cause.
1. Distinguish committed content from working-tree content
.gitattributes can assign a filter to a path, but it does not define the command to run. The actual clean, smudge, or process commands come from Git configuration. The first typically processes content being written to the object database; the latter two may take part in checkout transformations.
An identical commit hash therefore does not guarantee that the compiler reads identical files. A filter with the same name may be available on one machine but missing on another. If a filter is not marked as required, its failure may also leave content without the expected transformation.
An attributes file is not a command script, but it can select execution rules already configured on the machine. An audit must cover both repository attributes and the build account’s configuration.
working-tree-encoding, text, and eol also affect file bytes. This guide starts by rejecting filters and encoding conversions. Line-ending rules need a separate policy; not every text difference belongs in the same category.
2. Inspect attributes before the first checkout
Have Git and Python 3 available, and run the checks under the system account that actually handles build jobs. A successful check in your personal terminal does not mean that a background runner has the same configuration.
For a new workspace, clone the repository without checking out its files. The pipeline supplies REPO_URL:
git clone --no-checkout "$REPO_URL" audit-work
cd audit-work
git read-tree HEAD
git config --show-origin --name-only --get-regexp \
'^filter\..*\.(clean|smudge|process|required)$'
The last command returns a nonzero status when there are no matches. The pipeline must distinguish “no configuration found” from an actual execution error rather than applying the same failure handling to both.
read-tree populates only the index; it does not run checkout transformations. This lets you use --cached to inspect attributes in the target commit without first running a filter and only then discovering that it was not approved. The audit script must come from a trusted pipeline. Do not directly execute a script with the same name from the commit being audited.
Also inspect the global attributes file and .git/info/attributes. They are not synchronized with the commit, but they can change the effective attributes. The check below keeps these sources in scope and examines the rules that actually apply to the current account.
3. Fail on unapproved transformations
Save the following code as audit-attributes.py in a trusted location and run it from the repository root. It checks every indexed path, using NUL delimiters to handle filenames containing spaces or newlines without relying on line-by-line text parsing.
import subprocess
import sys
def git(*args, data=None):
return subprocess.run(
["git", *args],
input=data,
stdout=subprocess.PIPE,
check=True,
).stdout
paths = git("ls-files", "-z")
raw = git(
"check-attr", "--cached", "-z", "--stdin",
"filter", "working-tree-encoding",
data=paths,
)
fields = raw.split(b"\0")
if fields[-1] != b"":
raise SystemExit("Invalid attribute output")
fields.pop()
if len(fields) % 3:
raise SystemExit("Invalid attribute output")
blocked = False
for index in range(0, len(fields), 3):
path, attribute, value = fields[index:index + 3]
if value not in (b"unspecified", b"unset"):
blocked = True
print(repr((path, attribute, value)))
sys.exit(1 if blocked else 0)
This is a fail-closed baseline, not a complete sandbox. It blocks all declared filters, including legitimate rules for handling large files. Repositories that need these capabilities should use a precise, reviewed allowlist instead. Do not simply ignore the exit code.
Run git checkout --force HEAD only after the audit passes. This command is intended solely for a newly created, disposable, dedicated workspace—not a development directory containing uncommitted changes.
4. What to verify when approving transformations
If a filter is genuinely needed, pin down at least four things: its name, the permitted paths, the tool version, and the input/output contract. Approving a name alone must not give the filter permission to process arbitrary files.
Test both directions with small fixtures
Prepare plain text, an empty file, a file containing non-ASCII characters, and malformed input. Check the checkout results for valid samples. For invalid samples, verify that a conversion failure aborts the job. Then add the working-tree content back to a temporary index and check that the resulting objects match expectations.
Where a filter failure must stop the job, set required=true for that filter—but still inject an actual failure as a test. External downloads, service responses, and tool upgrades can all change the output. Marking a filter as required does not pin those conditions for you.
Do not treat a clean git status as proof of byte-for-byte equality: Git may apply the reverse transformation before comparing. When an exact comparison is needed, compute digests for the target files and compare them with the reviewed fixture results.
5. Preserve transformation evidence in failed jobs
On failure, preserve the commit hash, Git version, effective attribute triples, configuration sources, and target-file digests. Filter commands may contain tokens or private paths, so do not upload the complete configuration as a log attachment.
Keep the troubleshooting order consistent: confirm the effective attributes first, locate their configuration sources next, then check whether the tools are available, and finally compare content before and after transformation. A pre-checkout audit is not a permanent guarantee either. After configuration changes, switching commits, or initializing submodules, audit the relevant repository again.
Acceptance requires three things: unapproved transformations are rejected before checkout, approved transformations have fixed fixtures, and failures never silently fall back to the original content. With these safeguards in place, build inputs on a cloud Mac have a boundary you can review and verify.
Frequently asked questions
Can .gitattributes execute arbitrary commands by itself?
No. It assigns a filter name to paths, while Git configuration supplies the command. Execution becomes relevant when the runner already has a matching filter or CI injects one.
Does marking a filter as required make builds reproducible?
No. It makes failures of a configured filter fatal, but does not pin its implementation, external inputs or output. Audit attributes, pin the tool and validate transformed files.
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.