fix(web): match .gitattributes patterns in subdirectories
Fixes #1691
resolveLanguageFromGitAttributes matched every pattern against the full repo-relative path with micromatch.isMatch. So *.h linguist-language=C only applied to headers at the repo root, and an anchored pattern like /config.in never matched. The file viewer and file-source API ignored the override for nearly every file.
It now follows git's rule: a pattern with no slash is matched against the file name (any directory); any other pattern is matched from the repo root, with a leading / stripped. dot: true is set so dotfiles match too. The last matching rule still wins.
How I verified it
New packages/web/src/lib/gitattributes.test.ts (4 tests):
- On
main(b4931510), 3 fail. For examplesrc/include/foo.hwith*.h linguist-language=Cresolves toundefinedinstead ofC. - With the fix, all 4 pass. The whole web suite passes (148 files, 1507 tests), and eslint is clean on both files.
A reviewer can confirm it by adding *.h linguist-language=C to a repo's .gitattributes and opening a header in a subdirectory.
AI disclosure: an AI coding agent working for the breken-ai account found, fixed and tested this.
🤖 Generated with Claude Code
[!NOTE]
Low Risk
Localized change to file-viewer language resolution with new unit tests; behavior aligns with gitattributes semantics and mainly fixes incorrect highlighting for nested paths.
Overview
Fixes .gitattributes linguist-language overrides so they apply to files in subdirectories (and match git’s pattern rules), not only at the repo root.
resolveLanguageFromGitAttributes no longer matches every pattern against the full path with micromatch alone. It uses a new matchesGitAttributesPattern: slashless patterns (e.g. *.h) match the basename at any depth; patterns with / match the normalized repo-relative path (leading / stripped, . segments collapsed). Matching uses dot: true for dotfiles, and false / unspecified linguist-language values clear a prior override while last matching rule still wins.
Adds gitattributes.test.ts for depth, root anchoring, path rules, dotfiles, and reset behavior, plus a changelog entry.
Reviewed by Cursor Bugbot for commit 85f13ee0162471ffd1059da132c0bebbd8afc35b. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by cubic
Fixes #1691 so .gitattributes linguist-language overrides apply to files anywhere in the repo instead of only at the root. resolveLanguageFromGitAttributes previously matched each pattern against the full repo-relative path, so *.h linguist-language=C missed src/foo.h and anchored patterns like /config.in never matched.
It now follows git's rules: a pattern with no slash matches the file name at any depth, and any other pattern is matched from the repo root with a leading / stripped. Paths with . segments are normalized before matching, dot: true ensures dotfiles match, and negated rules or false/unspecified values clear a prior override. The last matching rule still wins. Adds tests covering depth, anchoring, dotfiles, and reset behavior, plus a changelog entry.
Summary by CodeRabbit
- Bug Fixes
.gitattributeslanguage overrides now apply correctly to files in subdirectories and dotfiles. Patterns without slashes match filenames at any directory depth, while patterns with slashes match repository-relative paths; leading-slash patterns apply only at the repository root. Paths prefixed with./are handled correctly. The last matching rule determines the language, and a rule can clear or replace an earlier override.