Repository navigation
fix(diff): re-admit files under a directory re-included by !dir/ - #1795
Batianxieshen1 wants to merge 1 commit into
Conversation
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
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). |
NanaseInori
left a comment
There was a problem hiding this comment.
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.
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 undersrc/. When nothing else changed, OCR reportsNo files changed.with exit code 0 — the same output as a clean tree.Root cause:
isPathExcludedskipped negated directory patterns outright: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:
build/andbuildapply to directories, so*+!*/re-admits every directory while files still match*./, with last-match-wins.!/src/therefore re-admits the directory and everything below it, while!*/re-admits only directories.Fixes #1793
Type of Change
How Has This Been Tested?
Ground truth was taken from git itself, not from reading the spec: a scratch repo with
.gitignoreholding/*/and!/src/, thengit check-ignore -q <path>:src/main.godocs/r.mdThe 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 withgit check-ignoreon the same pattern set.make testpasses locally — nomakein this environment, ran the equivalentgo test ./internal/...: every packageokgofmt -lclean,go vet ./internal/diff/clean,go test ./internal/diff/okChecklist
go fmt,go vet)deepseek-v4.1-flash). I reproduced the bug, cross-checked every case againstgit 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.