Skip to content

fix: only expand attribute macros when they are set#2640

Closed
Nils Andre (NilsIrl) wants to merge 1 commit into
GitoxideLabs:mainfrom
NilsIrl:fix-attr-macro-expansion
Closed

fix: only expand attribute macros when they are set#2640
Nils Andre (NilsIrl) wants to merge 1 commit into
GitoxideLabs:mainfrom
NilsIrl:fix-attr-macro-expansion

Conversation

@NilsIrl

Copy link
Copy Markdown

Fixes #2639.

What

Outcome::fill_attributes() expanded a macro's sub-assignments whenever a pattern referencing the macro matched, regardless of the assignment's state. Git expands a macro only when the matching rule sets it — attr.c's macroexpand_one() acts only if (item->macro && item->value == ATTR__TRUE) — so -macro, !macro, and macro=value must not apply the expansion.

The fix gates the expansion on State::Set while still recording the macro's own match (git also reports e.g. mymacro: unset for a -mymacro rule; only the expansion is suppressed).

Tests

A new macro-states repository in make_attributes_baseline.sh records git check-attr -a output for one path per assignment state (macro, -macro, !macro, macro=value), and a new macros_are_only_expanded_when_set test asserts parity, following the existing baseline-test pattern. Before the fix it fails with the full expansion showing up for the three non-set states; the existing lookup-order macro-cycle/recursion tests still pass since those macros are matched as set.

Ran cargo test -p gix-attributes, GIX_TEST_IGNORE_ARCHIVES=1 cargo test -p gix-attributes --test attributes, cargo test -p gix-worktree, cargo clippy -p gix-attributes --all-targets, and cargo fmt --check.

AI disclosure

This change was researched, written, and tested by an AI agent (Claude Fable 5, via Claude Code) directed and reviewed by Nils Andre (@NilsIrl); the commit is authored accordingly. The underlying issue (#2639) includes the standalone reproduction and the attr.c comparison.

Git expands a macro's sub-assignments only when the matching rule sets
the macro - `attr.c`'s `macroexpand_one()` acts only
`if (item->macro && item->value == ATTR__TRUE)`. We expanded macros in
any matched state, so `-macro`, `!macro`, and `macro=value` also
applied the expansion, making attributes like `filter` appear set
where `git check-attr` reports them as unspecified.

The new `macro-states` baseline fixture records git's answers for all
four assignment states of a macro and verifies parity.
Copilot AI review requested due to automatic review settings June 10, 2026 19:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds coverage and implementation for Git-compatible macro expansion behavior in attribute search outcomes (macros expand only when explicitly set).

Changes:

  • Add a regression test and new fixture baseline for macro state handling (set/unset/unspecified/value).
  • Update attribute outcome processing to expand macros only when the macro attribute is in the Set state.
  • Extend fixture generation script to produce baselines for the new macro-state scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
gix-attributes/tests/search/mod.rs Adds a new test asserting macros expand only when set.
gix-attributes/tests/fixtures/make_attributes_baseline.sh Generates a new fixture repo and baseline outputs for macro state cases.
gix-attributes/src/search/outcome.rs Changes macro expansion condition to match Git behavior (expand only on Set).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +147 to +150
for (rela_path, expected) in (baseline::Expectations { lines: input.lines() }) {
actual.reset();
group.pattern_matching_relative_path(rela_path, Case::Sensitive, None, &mut actual);
assert_references(&actual);
Comment on lines +102 to +103
git init
: > user.attributes
@Byron

Copy link
Copy Markdown
Member

Thanks a lot. This PR was superseded by an alternative implementation that also compares this behavior against a git baseline.

@NilsIrl
Nils Andre (NilsIrl) deleted the fix-attr-macro-expansion branch June 26, 2026 22:53
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.

gix-attributes: macros expand even when the matching rule unsets them, diverging from git check-attr

4 participants