Skip to content

fix(diff): re-admit files under a directory re-included by !dir/ - #1795

Open
Batianxieshen1 wants to merge 1 commit into
alibaba:mainfrom
Batianxieshen1:fix-gitignore-negated-directory
Open

Batianxieshen1 wants to merge 1 commit into
alibaba:mainfrom
Batianxieshen1:fix-gitignore-negated-directory

Conversation

@Batianxieshen1

Copy link
Copy Markdown
Contributor

Description

A negated directory pattern (!dir/) never re-includes anything, so a repository that ignores every top-level directory with /*/ and re-admits its own with !/src/ loses every changed file under src/. When nothing else changed, OCR reports No files changed. with exit code 0 — the same output as a clean tree.

Root cause: isPathExcluded skipped negated directory patterns outright:

if negated && strings.HasSuffix(body, "/") {
    continue
}

Skipping them was necessary to keep !*/ from re-admitting every file below the root, but it also disabled the specific-directory form git does honour.

This resolves the two layers separately, as git does:

  • Directory layer — a path is excluded when an ancestor directory is excluded. Ancestors are walked root-down and the first excluded one decides, mirroring git's refusal to descend past it. Both build/ and build apply to directories, so * + !*/ re-admits every directory while files still match *.
  • File layer — the path itself is matched by patterns without a trailing /, with last-match-wins.

!/src/ therefore re-admits the directory and everything below it, while !*/ re-admits only directories.

Fixes #1793

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

Ground truth was taken from git itself, not from reading the spec: a scratch repo with .gitignore holding /*/ and !/src/, then git check-ignore -q <path>:

path git before after
src/main.go tracked excluded ❌ tracked ✅
docs/r.md ignored ignored ✅ ignored ✅

The added test also pins nested re-exclusion (/*/ + !/src/ + /src/internal/), last-match-wins, and the allow-list asymmetry (* + !*/ must still exclude files). Each case was cross-checked with git check-ignore on the same pattern set.

  • make test passes locally — no make in this environment, ran the equivalent go test ./internal/...: every package ok
  • Manual testing: gofmt -l clean, go vet ./internal/diff/ clean, go test ./internal/diff/ ok

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable) — no user-facing docs describe the simplified matching rules
  • I have signed the CLA — will sign via the bot on this PR
  • AI disclosure: this change was prepared with AI assistance (ZCode agent, model deepseek-v4.1-flash). I reproduced the bug, cross-checked every case against git check-ignore, and reviewed the final diff. No commits are attributed to AI, and I will answer maintainer questions and review comments directly. Nothing in the diff claims AI authorship.

A negated directory pattern was skipped outright, so `/*/` followed by
`!/src/` still dropped every file under `src/`. Git re-admits that directory and
descends into it, while OCR saw no changes and reported "No files changed."
with exit code 0 — indistinguishable from a clean tree.

Resolve the two layers separately, the way git does: a path is excluded when an
ancestor directory is excluded or when the path itself matches an excluding
pattern. Ancestors are walked root-down and the first excluded one decides, so
`!*/` still cannot re-admit files that `*` already matched.

Checked against git's own `check-ignore` for the `/*/` + `!/src/` shape, a
nested re-exclusion, and the allow-list idiom; `go test ./internal/...` passes.

Fixes alibaba#1793
@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@NanaseInori NanaseInori 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.

Reviewed both changed files, the surrounding matchers, #1793 and the competing earlier #1794 implementation/discussion. Separating file and directory state addresses the original case, but two gaps remain.

[P2] Preserve trailing / semantics for ancestors.** With rules src/** then !src/*.go, matchGitignoreDir("src", "src/") delegates to doublestar, whose zero-length suffix handling returns true. isBelowExcludedDirectory therefore drops src/a.go before its file exception is evaluated. Git includes that file: trailing / matches a directory's contents, not the directory itself. This is also one of the outstanding cases on #1794; the separate helper here does not avoid it.

[P2] Match slash-normalized directory paths independently of the host separator. The new helper uses filepath.Match for ordinary patterns while dir is explicitly slash-separated. On Windows, its '*' excludes backslashes, not forward slashes. For /*/ then !/src/, the src ancestor is re-admitted, but * also matches src/inner; the exact src negation cannot clear that child, so src/inner/a.go remains excluded.

I executed Go's filepath.Match helpers with their Windows branches and separator selected in an isolated harness: src was included and src/inner excluded. A real git check-ignore --no-index fixture included src/inner/a.go, and also included src/a.go under the trailing-/** case. This was not a native Windows or full OCR test run.

Use the slash-based matching contract consistently, handle trailing-/** ancestor semantics explicitly, and add nested-directory regressions on Windows as well as Linux. Only one of #1794/#1795 should land; duplicate status is separate from these defects. Primary references: Go matcher semantics, Git ignore semantics, doublestar matcher.

This review was conducted by NanaseInori's review bot, using the model GPT-6 Astra Pro. If you need a human review, please manually @NanaseInori.

This branch has not been deployed

No deployments
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.

.gitignore !dir/ never re-includes a directory: /*/ + !/src/ drops every file under src/

3 participants