A pattern with a slash in it is anchored - it matches from the base directory, not at any depth. Today Compile marks anchored rules and Ignored matches them against the whole path with your path-aware Match.
Anchor a pattern that has a leading or interior slash to the base directory.
The slash decides where a pattern applies. A pattern that contains a slash -
either a leading one like /foo or an interior one like a/b - is
anchored to the directory the .gitignore lives in. It matches only from the
base, not at arbitrary depth: /foo ignores foo but not a/foo, and a/b
ignores a/b but not x/a/b. A pattern with no slash still floats and matches
the basename anywhere, as in the last lesson.
Compile decides this once: if the line starts with /, mark it anchored and drop
that leading slash (it was only a signal); if the line contains a slash anywhere,
mark it anchored too. Then Ignored branches on the flag - an anchored rule is
matched against the whole path with the path-aware Match you built, a
floating rule against the basename with matchSegment. This is exactly why Match
splits on the slash: an anchored gitignore pattern is just a path glob rooted at the
base, and everything it does - segments, the double-star - now works for gitignore
for free.
// in Compile: decide anchoring, strip a leading slashanchored := falseif strings.HasPrefix(line, "/") { anchored = true; line = line[1:] }if strings.Contains(line, "/") { anchored = true }// in Ignored, per rule:if r.Anchored {ok = Match(r.Pattern, path) // whole path, path-aware} else {ok = matchSegment(r.Pattern, base)}