test_check_ignore.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. # test_check_ignore.py -- Compatibility tests for git check-ignore
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Compatibility tests for git check-ignore functionality."""
  22. import os
  23. import tempfile
  24. from dulwich import porcelain
  25. from dulwich.repo import Repo
  26. from .utils import CompatTestCase, run_git_or_fail
  27. class CheckIgnoreCompatTestCase(CompatTestCase):
  28. """Test git check-ignore compatibility between dulwich and git."""
  29. min_git_version = (1, 8, 5) # git check-ignore was added in 1.8.5
  30. def setUp(self) -> None:
  31. super().setUp()
  32. self.test_dir = tempfile.mkdtemp()
  33. self.addCleanup(self._cleanup_test_dir)
  34. self.repo = Repo.init(self.test_dir)
  35. self.addCleanup(self.repo.close)
  36. def _cleanup_test_dir(self) -> None:
  37. import shutil
  38. shutil.rmtree(self.test_dir)
  39. def _write_gitignore(self, content: str) -> None:
  40. """Write .gitignore file with given content."""
  41. gitignore_path = os.path.join(self.test_dir, ".gitignore")
  42. with open(gitignore_path, "w") as f:
  43. f.write(content)
  44. def _create_file(self, path: str, content: str = "") -> None:
  45. """Create a file with given content."""
  46. full_path = os.path.join(self.test_dir, path)
  47. os.makedirs(os.path.dirname(full_path), exist_ok=True)
  48. with open(full_path, "w") as f:
  49. f.write(content)
  50. def _create_dir(self, path: str) -> None:
  51. """Create a directory."""
  52. full_path = os.path.join(self.test_dir, path)
  53. os.makedirs(full_path, exist_ok=True)
  54. def _git_check_ignore(self, paths: list[str]) -> set[str]:
  55. """Run git check-ignore and return set of ignored paths."""
  56. try:
  57. output = run_git_or_fail(
  58. ["-c", "core.quotePath=false", "check-ignore", *paths],
  59. cwd=self.test_dir,
  60. )
  61. # git check-ignore returns paths separated by newlines
  62. return set(
  63. line.decode("utf-8") for line in output.strip().split(b"\n") if line
  64. )
  65. except AssertionError:
  66. # git check-ignore returns non-zero when no paths are ignored
  67. return set()
  68. def _dulwich_check_ignore(self, paths: list[str]) -> set[str]:
  69. """Run dulwich check_ignore and return set of ignored paths."""
  70. # Convert to absolute paths relative to the test directory
  71. abs_paths = [os.path.join(self.test_dir, path) for path in paths]
  72. ignored = set(
  73. porcelain.check_ignore(self.test_dir, abs_paths, quote_path=False)
  74. )
  75. # Convert back to relative paths and preserve original path format
  76. result = set()
  77. path_mapping = {}
  78. for orig_path, abs_path in zip(paths, abs_paths):
  79. path_mapping[abs_path] = orig_path
  80. for path in ignored:
  81. if path.startswith(self.test_dir + "/"):
  82. rel_path = path[len(self.test_dir) + 1 :]
  83. # Find the original path format that was requested
  84. orig_path = None
  85. for requested_path in paths:
  86. if requested_path.rstrip("/") == rel_path.rstrip("/"):
  87. orig_path = requested_path
  88. break
  89. result.add(orig_path if orig_path else rel_path)
  90. else:
  91. result.add(path)
  92. return result
  93. def _assert_ignore_match(self, paths: list[str]) -> None:
  94. """Assert that dulwich and git return the same ignored paths."""
  95. git_ignored = self._git_check_ignore(paths)
  96. dulwich_ignored = self._dulwich_check_ignore(paths)
  97. self.assertEqual(
  98. git_ignored,
  99. dulwich_ignored,
  100. f"Mismatch for paths {paths}: git={git_ignored}, dulwich={dulwich_ignored}",
  101. )
  102. def test_issue_1203_directory_negation(self) -> None:
  103. """Test issue #1203: directory negation patterns with data/**,!data/*/."""
  104. self._write_gitignore("data/**\n!data/*/\n")
  105. self._create_file("data/test.dvc", "content")
  106. self._create_dir("data/subdir")
  107. # Based on dulwich's own test for issue #1203, the expected behavior is:
  108. # data/test.dvc: ignored, data/: not ignored, data/subdir/: not ignored
  109. # But git check-ignore might behave differently...
  110. # Test the core case that issue #1203 was about
  111. self._assert_ignore_match(["data/test.dvc"])
  112. def test_basic_patterns(self) -> None:
  113. """Test basic gitignore patterns."""
  114. self._write_gitignore("*.tmp\n*.log\n")
  115. self._create_file("test.tmp")
  116. self._create_file("debug.log")
  117. self._create_file("readme.txt")
  118. paths = ["test.tmp", "debug.log", "readme.txt"]
  119. self._assert_ignore_match(paths)
  120. def test_directory_patterns(self) -> None:
  121. """Test directory-specific patterns."""
  122. self._write_gitignore("build/\nnode_modules/\n")
  123. self._create_dir("build")
  124. self._create_dir("node_modules")
  125. self._create_file("build.txt")
  126. paths = ["build/", "node_modules/", "build.txt"]
  127. self._assert_ignore_match(paths)
  128. def test_issue_972_directory_pattern_with_slash(self) -> None:
  129. """Test issue #972: /data/ pattern should match both 'data' and 'data/'."""
  130. self._write_gitignore("/data/\n")
  131. self._create_dir("data")
  132. self._create_file("data/file.txt")
  133. # Both 'data' and 'data/' should be matched by /data/ pattern
  134. paths = ["data", "data/", "data/file.txt"]
  135. self._assert_ignore_match(paths)
  136. def test_wildcard_patterns(self) -> None:
  137. """Test wildcard patterns."""
  138. self._write_gitignore("*.py[cod]\n__pycache__/\n*.so\n")
  139. self._create_file("test.pyc")
  140. self._create_file("test.pyo")
  141. self._create_file("test.pyd")
  142. self._create_file("test.py")
  143. self._create_dir("__pycache__")
  144. paths = ["test.pyc", "test.pyo", "test.pyd", "test.py", "__pycache__/"]
  145. self._assert_ignore_match(paths)
  146. def test_negation_patterns(self) -> None:
  147. """Test negation patterns with !."""
  148. self._write_gitignore("*.log\n!important.log\n")
  149. self._create_file("debug.log")
  150. self._create_file("error.log")
  151. self._create_file("important.log")
  152. paths = ["debug.log", "error.log", "important.log"]
  153. self._assert_ignore_match(paths)
  154. def test_double_asterisk_patterns(self) -> None:
  155. """Test double asterisk ** patterns."""
  156. self._write_gitignore("**/temp\nvendor/**/cache\n")
  157. self._create_file("temp")
  158. self._create_file("src/temp")
  159. self._create_file("deep/nested/temp")
  160. self._create_file("vendor/lib/cache")
  161. self._create_file("vendor/gem/deep/cache")
  162. paths = [
  163. "temp",
  164. "src/temp",
  165. "deep/nested/temp",
  166. "vendor/lib/cache",
  167. "vendor/gem/deep/cache",
  168. ]
  169. self._assert_ignore_match(paths)
  170. def test_subdirectory_gitignore(self) -> None:
  171. """Test .gitignore files in subdirectories."""
  172. # Root .gitignore
  173. self._write_gitignore("*.tmp\n")
  174. # Subdirectory .gitignore
  175. self._create_dir("subdir")
  176. subdir_gitignore = os.path.join(self.test_dir, "subdir", ".gitignore")
  177. with open(subdir_gitignore, "w") as f:
  178. f.write("*.local\n!important.local\n")
  179. self._create_file("test.tmp")
  180. self._create_file("subdir/test.tmp")
  181. self._create_file("subdir/config.local")
  182. self._create_file("subdir/important.local")
  183. paths = [
  184. "test.tmp",
  185. "subdir/test.tmp",
  186. "subdir/config.local",
  187. "subdir/important.local",
  188. ]
  189. self._assert_ignore_match(paths)
  190. def test_complex_directory_negation(self) -> None:
  191. """Test complex directory negation patterns."""
  192. self._write_gitignore("dist/\n!dist/assets/\ndist/assets/*.tmp\n")
  193. self._create_dir("dist/assets")
  194. self._create_file("dist/main.js")
  195. self._create_file("dist/assets/style.css")
  196. self._create_file("dist/assets/temp.tmp")
  197. paths = [
  198. "dist/",
  199. "dist/main.js",
  200. "dist/assets/",
  201. "dist/assets/style.css",
  202. "dist/assets/temp.tmp",
  203. ]
  204. self._assert_ignore_match(paths)
  205. def test_leading_slash_patterns(self) -> None:
  206. """Test patterns with leading slash."""
  207. self._write_gitignore("/root-only.txt\nsubdir/specific.txt\n")
  208. self._create_file("root-only.txt")
  209. self._create_file("deep/root-only.txt") # Should not be ignored
  210. self._create_file("subdir/specific.txt")
  211. self._create_file("deep/subdir/specific.txt") # Should also be ignored
  212. paths = [
  213. "root-only.txt",
  214. "deep/root-only.txt",
  215. "subdir/specific.txt",
  216. "deep/subdir/specific.txt",
  217. ]
  218. self._assert_ignore_match(paths)
  219. def test_empty_directory_edge_case(self) -> None:
  220. """Test edge case with empty directories."""
  221. self._write_gitignore("empty/\n!empty/keep\n")
  222. self._create_dir("empty")
  223. self._create_file("empty/keep", "keep this")
  224. paths = ["empty/", "empty/keep"]
  225. self._assert_ignore_match(paths)
  226. def test_nested_wildcard_negation(self) -> None:
  227. """Test nested wildcard patterns with negation."""
  228. self._write_gitignore("docs/**\n!docs/*/\n!docs/**/*.md\n")
  229. self._create_file("docs/readme.txt") # Should be ignored
  230. self._create_file("docs/guide.md") # Should not be ignored
  231. self._create_dir("docs/api") # Should not be ignored
  232. self._create_file("docs/api/index.md") # Should not be ignored
  233. self._create_file("docs/api/temp.txt") # Should be ignored
  234. paths = [
  235. "docs/readme.txt",
  236. "docs/guide.md",
  237. "docs/api/",
  238. "docs/api/index.md",
  239. "docs/api/temp.txt",
  240. ]
  241. self._assert_ignore_match(paths)
  242. def test_case_sensitivity(self) -> None:
  243. """Test case sensitivity in patterns."""
  244. self._write_gitignore("*.TMP\nREADME\n")
  245. self._create_file("test.tmp") # Lowercase
  246. self._create_file("test.TMP") # Uppercase
  247. self._create_file("readme") # Lowercase
  248. self._create_file("README") # Uppercase
  249. paths = ["test.tmp", "test.TMP", "readme", "README"]
  250. self._assert_ignore_match(paths)
  251. def test_unicode_filenames(self) -> None:
  252. """Test unicode filenames in patterns."""
  253. try:
  254. self._write_gitignore("тест*\n*.测试\n")
  255. self._create_file("тест.txt")
  256. self._create_file("файл.测试")
  257. self._create_file("normal.txt")
  258. paths = ["тест.txt", "файл.测试", "normal.txt"]
  259. self._assert_ignore_match(paths)
  260. except (UnicodeEncodeError, OSError):
  261. # Skip test if filesystem doesn't support unicode
  262. self.skipTest("Filesystem doesn't support unicode filenames")
  263. def test_double_asterisk_edge_cases(self) -> None:
  264. """Test edge cases with ** patterns."""
  265. self._write_gitignore("**/afile\ndir1/**/b\n**/*.tmp\n")
  266. # Test **/afile pattern
  267. self._create_file("afile") # Root level
  268. self._create_file("dir/afile") # One level deep
  269. self._create_file("deep/nested/afile") # Multiple levels deep
  270. # Test dir1/**/b pattern
  271. self._create_file("dir1/b") # Direct child
  272. self._create_file("dir1/subdir/b") # One level deep in dir1/
  273. self._create_file("dir1/deep/nested/b") # Multiple levels deep in dir1/
  274. self._create_file("other/dir1/b") # Should not match (dir1/ not at start)
  275. # Test **/*.tmp pattern
  276. self._create_file("test.tmp") # Root level
  277. self._create_file("dir/test.tmp") # One level deep
  278. self._create_file("deep/nested/test.tmp") # Multiple levels deep
  279. paths = [
  280. "afile",
  281. "dir/afile",
  282. "deep/nested/afile",
  283. "dir1/b",
  284. "dir1/subdir/b",
  285. "dir1/deep/nested/b",
  286. "other/dir1/b",
  287. "test.tmp",
  288. "dir/test.tmp",
  289. "deep/nested/test.tmp",
  290. ]
  291. self._assert_ignore_match(paths)
  292. def test_double_asterisk_with_negation(self) -> None:
  293. """Test ** patterns combined with negation."""
  294. self._write_gitignore(
  295. "**/build/**\n!**/build/assets/**\n**/build/assets/*.tmp\n"
  296. )
  297. # Create build directories at different levels
  298. self._create_file("build/main.js")
  299. self._create_file("build/assets/style.css")
  300. self._create_file("build/assets/temp.tmp")
  301. self._create_file("src/build/app.js")
  302. self._create_file("src/build/assets/logo.png")
  303. self._create_file("src/build/assets/cache.tmp")
  304. self._create_file("deep/nested/build/lib.js")
  305. self._create_file("deep/nested/build/assets/icon.svg")
  306. self._create_file("deep/nested/build/assets/debug.tmp")
  307. paths = [
  308. "build/main.js",
  309. "build/assets/style.css",
  310. "build/assets/temp.tmp",
  311. "src/build/app.js",
  312. "src/build/assets/logo.png",
  313. "src/build/assets/cache.tmp",
  314. "deep/nested/build/lib.js",
  315. "deep/nested/build/assets/icon.svg",
  316. "deep/nested/build/assets/debug.tmp",
  317. ]
  318. self._assert_ignore_match(paths)
  319. def test_double_asterisk_middle_patterns(self) -> None:
  320. """Test ** patterns in the middle of paths."""
  321. self._write_gitignore("src/**/test/**\nlib/**/node_modules\n**/cache/**/temp\n")
  322. # Test src/**/test/** pattern
  323. self._create_file("src/test/unit.js")
  324. self._create_file("src/components/test/unit.js")
  325. self._create_file("src/deep/nested/test/integration.js")
  326. self._create_file("other/src/test/unit.js") # Should not match
  327. # Test lib/**/node_modules pattern
  328. self._create_file("lib/node_modules/package.json")
  329. self._create_file("lib/vendor/node_modules/package.json")
  330. self._create_file("lib/deep/path/node_modules/package.json")
  331. self._create_file("other/lib/node_modules/package.json") # Should not match
  332. # Test **/cache/**/temp pattern
  333. self._create_file("cache/temp")
  334. self._create_file("cache/data/temp")
  335. self._create_file("app/cache/temp")
  336. self._create_file("app/cache/nested/temp")
  337. self._create_file("deep/cache/very/nested/temp")
  338. paths = [
  339. "src/test/unit.js",
  340. "src/components/test/unit.js",
  341. "src/deep/nested/test/integration.js",
  342. "other/src/test/unit.js",
  343. "lib/node_modules/package.json",
  344. "lib/vendor/node_modules/package.json",
  345. "lib/deep/path/node_modules/package.json",
  346. "other/lib/node_modules/package.json",
  347. "cache/temp",
  348. "cache/data/temp",
  349. "app/cache/temp",
  350. "app/cache/nested/temp",
  351. "deep/cache/very/nested/temp",
  352. ]
  353. self._assert_ignore_match(paths)
  354. def test_multiple_double_asterisks(self) -> None:
  355. """Test patterns with multiple ** segments."""
  356. self._write_gitignore("**/**/test/**/*.js\n**/src/**/build/**/dist\n")
  357. # Test **/**/test/**/*.js pattern (multiple ** in one pattern)
  358. self._create_file("test/file.js")
  359. self._create_file("a/test/file.js")
  360. self._create_file("a/b/test/file.js")
  361. self._create_file("test/c/file.js")
  362. self._create_file("test/c/d/file.js")
  363. self._create_file("a/b/test/c/d/file.js")
  364. self._create_file("a/b/test/c/d/file.txt") # Different extension
  365. # Test **/src/**/build/**/dist pattern
  366. self._create_file("src/build/dist")
  367. self._create_file("app/src/build/dist")
  368. self._create_file("src/lib/build/dist")
  369. self._create_file("src/build/prod/dist")
  370. self._create_file("app/src/lib/build/prod/dist")
  371. paths = [
  372. "test/file.js",
  373. "a/test/file.js",
  374. "a/b/test/file.js",
  375. "test/c/file.js",
  376. "test/c/d/file.js",
  377. "a/b/test/c/d/file.js",
  378. "a/b/test/c/d/file.txt",
  379. "src/build/dist",
  380. "app/src/build/dist",
  381. "src/lib/build/dist",
  382. "src/build/prod/dist",
  383. "app/src/lib/build/prod/dist",
  384. ]
  385. self._assert_ignore_match(paths)
  386. def test_double_asterisk_directory_traversal(self) -> None:
  387. """Test ** patterns with directory traversal edge cases."""
  388. self._write_gitignore("**/.*\n!**/.gitkeep\n**/.git/**\n")
  389. # Test **/.* pattern (hidden files at any level)
  390. self._create_file(".hidden")
  391. self._create_file("dir/.hidden")
  392. self._create_file("deep/nested/.hidden")
  393. self._create_file(".gitkeep") # Should be negated
  394. self._create_file("dir/.gitkeep") # Should be negated
  395. # Test **/.git/** pattern
  396. self._create_file(".git/config")
  397. self._create_file(".git/objects/abc123")
  398. self._create_file("submodule/.git/config")
  399. self._create_file("deep/submodule/.git/refs/heads/master")
  400. paths = [
  401. ".hidden",
  402. "dir/.hidden",
  403. "deep/nested/.hidden",
  404. ".gitkeep",
  405. "dir/.gitkeep",
  406. ".git/config",
  407. ".git/objects/abc123",
  408. "submodule/.git/config",
  409. "deep/submodule/.git/refs/heads/master",
  410. ]
  411. self._assert_ignore_match(paths)
  412. def test_double_asterisk_empty_segments(self) -> None:
  413. """Test ** patterns with edge cases around empty path segments."""
  414. self._write_gitignore("a/**//b\n**//**/test\nc/**/**/\n")
  415. # These patterns test edge cases with path separator handling
  416. self._create_file("a/b")
  417. self._create_file("a/x/b")
  418. self._create_file("a/x/y/b")
  419. self._create_file("test")
  420. self._create_file("dir/test")
  421. self._create_file("dir/nested/test")
  422. self._create_file("c/file")
  423. self._create_file("c/dir/file")
  424. self._create_file("c/deep/nested/file")
  425. paths = [
  426. "a/b",
  427. "a/x/b",
  428. "a/x/y/b",
  429. "test",
  430. "dir/test",
  431. "dir/nested/test",
  432. "c/file",
  433. "c/dir/file",
  434. "c/deep/nested/file",
  435. ]
  436. self._assert_ignore_match(paths)
  437. def test_double_asterisk_root_patterns(self) -> None:
  438. """Test ** patterns at repository root with complex negations."""
  439. self._write_gitignore("/**\n!/**/\n!/**/*.md\n/**/*.tmp\n")
  440. # Pattern explanation:
  441. # /** - Ignore everything at any depth
  442. # !/**/ - But don't ignore directories
  443. # !/**/*.md - And don't ignore .md files
  444. # /**/*.tmp - But do ignore .tmp files (overrides .md negation for .tmp.md files)
  445. self._create_file("file.txt")
  446. self._create_file("readme.md")
  447. self._create_file("temp.tmp")
  448. self._create_file("backup.tmp.md") # Edge case: both .tmp and .md
  449. self._create_dir("dir")
  450. self._create_file("dir/file.txt")
  451. self._create_file("dir/guide.md")
  452. self._create_file("dir/cache.tmp")
  453. self._create_file("deep/nested/doc.md")
  454. self._create_file("deep/nested/log.tmp")
  455. paths = [
  456. "file.txt",
  457. "readme.md",
  458. "temp.tmp",
  459. "backup.tmp.md",
  460. "dir/",
  461. "dir/file.txt",
  462. "dir/guide.md",
  463. "dir/cache.tmp",
  464. "deep/nested/doc.md",
  465. "deep/nested/log.tmp",
  466. ]
  467. self._assert_ignore_match(paths)
  468. def test_single_asterisk_patterns(self) -> None:
  469. """Test single asterisk * patterns in various positions."""
  470. self._write_gitignore("src/*/build\n*.log\ntest*/\n*_backup\nlib/*\n*/temp/*\n")
  471. # Test src/*/build pattern
  472. self._create_file("src/app/build")
  473. self._create_file("src/lib/build")
  474. self._create_file("src/nested/deep/build") # Should not match (only one level)
  475. self._create_file("other/src/app/build") # Should not match
  476. # Test *.log pattern
  477. self._create_file("app.log")
  478. self._create_file("error.log")
  479. self._create_file("logs/debug.log") # Should match
  480. self._create_file("app.log.old") # Should not match
  481. # Test test*/ pattern (directories starting with test)
  482. self._create_dir("test")
  483. self._create_dir("testing")
  484. self._create_dir("test_data")
  485. self._create_file("test_file") # Should not match (not a directory)
  486. # Test *_backup pattern
  487. self._create_file("db_backup")
  488. self._create_file("config_backup")
  489. self._create_file("old_backup_file") # Should not match (backup not at end)
  490. # Test lib/* pattern
  491. self._create_file("lib/module.js")
  492. self._create_file("lib/utils.py")
  493. self._create_file("lib/nested/deep.js") # Should not match (only one level)
  494. # Test */temp/* pattern
  495. self._create_file("app/temp/cache")
  496. self._create_file("src/temp/logs")
  497. self._create_file("deep/nested/temp/file") # Should not match (nested too deep)
  498. self._create_file("temp/file") # Should not match (temp at root)
  499. paths = [
  500. "src/app/build",
  501. "src/lib/build",
  502. "src/nested/deep/build",
  503. "other/src/app/build",
  504. "app.log",
  505. "error.log",
  506. "logs/debug.log",
  507. "app.log.old",
  508. "test/",
  509. "testing/",
  510. "test_data/",
  511. "test_file",
  512. "db_backup",
  513. "config_backup",
  514. "old_backup_file",
  515. "lib/module.js",
  516. "lib/utils.py",
  517. "lib/nested/deep.js",
  518. "app/temp/cache",
  519. "src/temp/logs",
  520. "deep/nested/temp/file",
  521. "temp/file",
  522. ]
  523. self._assert_ignore_match(paths)
  524. def test_single_asterisk_edge_cases(self) -> None:
  525. """Test edge cases with single asterisk patterns."""
  526. self._write_gitignore("*\n!*/\n!*.txt\n*.*.*\n")
  527. # Pattern explanation:
  528. # * - Ignore everything
  529. # !*/ - But don't ignore directories
  530. # !*.txt - And don't ignore .txt files
  531. # *.*.* - But ignore files with multiple dots
  532. self._create_file("file")
  533. self._create_file("readme.txt")
  534. self._create_file("config.json")
  535. self._create_file("archive.tar.gz") # Multiple dots
  536. self._create_file("backup.sql.old") # Multiple dots
  537. self._create_dir("folder")
  538. self._create_file("folder/nested.txt")
  539. self._create_file("folder/data.json")
  540. paths = [
  541. "file",
  542. "readme.txt",
  543. "config.json",
  544. "archive.tar.gz",
  545. "backup.sql.old",
  546. "folder/",
  547. "folder/nested.txt",
  548. "folder/data.json",
  549. ]
  550. self._assert_ignore_match(paths)
  551. def test_single_asterisk_with_character_classes(self) -> None:
  552. """Test single asterisk with character classes and special patterns."""
  553. self._write_gitignore("*.[oa]\n*~\n.*\n!.gitignore\n[Tt]emp*\n")
  554. # Test *.[oa] pattern (object and archive files)
  555. self._create_file("main.o")
  556. self._create_file("lib.a")
  557. self._create_file("app.so") # Should not match
  558. self._create_file("test.c") # Should not match
  559. # Test *~ pattern (backup files)
  560. self._create_file("file~")
  561. self._create_file("config~")
  562. self._create_file("~file") # Should not match (~ at start)
  563. # Test .* pattern with negation
  564. self._create_file(".hidden")
  565. self._create_file(".secret")
  566. self._create_file(".gitignore") # Should be negated
  567. # Test [Tt]emp* pattern (case variations)
  568. self._create_file("temp_file")
  569. self._create_file("Temp_data")
  570. self._create_file("TEMP_LOG") # Should not match (not T or t)
  571. self._create_file("temporary")
  572. paths = [
  573. "main.o",
  574. "lib.a",
  575. "app.so",
  576. "test.c",
  577. "file~",
  578. "config~",
  579. "~file",
  580. ".hidden",
  581. ".secret",
  582. ".gitignore",
  583. "temp_file",
  584. "Temp_data",
  585. "TEMP_LOG",
  586. "temporary",
  587. ]
  588. self._assert_ignore_match(paths)
  589. def test_mixed_single_double_asterisk_patterns(self) -> None:
  590. """Test patterns that mix single (*) and double (**) asterisks."""
  591. self._write_gitignore(
  592. "src/**/test/*.js\n**/build/*\n*/cache/**\nlib/*/vendor/**/*.min.*\n"
  593. )
  594. # Test src/**/test/*.js - double asterisk in middle, single at end
  595. self._create_file("src/test/unit.js")
  596. self._create_file("src/components/test/spec.js")
  597. self._create_file("src/deep/nested/test/integration.js")
  598. self._create_file(
  599. "src/test/nested/unit.js"
  600. ) # Should not match (nested after test)
  601. self._create_file(
  602. "src/components/test/unit.ts"
  603. ) # Should not match (wrong extension)
  604. # Test **/build/* - double asterisk at start, single at end
  605. self._create_file("build/app.js")
  606. self._create_file("src/build/main.js")
  607. self._create_file("deep/nested/build/lib.js")
  608. self._create_file("build/dist/app.js") # Should not match (nested after build)
  609. # Test */cache/** - single at start, double at end
  610. self._create_file("app/cache/temp")
  611. self._create_file("src/cache/data/file")
  612. self._create_file("lib/cache/deep/nested/item")
  613. self._create_file(
  614. "nested/deep/cache/file"
  615. ) # Should not match (cache not at second level)
  616. self._create_file("cache/file") # Should not match (cache at root)
  617. # Test lib/*/vendor/**/*.min.* - complex mixed pattern
  618. self._create_file("lib/app/vendor/jquery.min.js")
  619. self._create_file("lib/ui/vendor/bootstrap.min.css")
  620. self._create_file("lib/core/vendor/deep/nested/lib.min.map")
  621. self._create_file("lib/app/vendor/jquery.js") # Should not match (not .min.)
  622. self._create_file(
  623. "lib/nested/deep/vendor/lib.min.js"
  624. ) # Should not match (too deep before vendor)
  625. paths = [
  626. "src/test/unit.js",
  627. "src/components/test/spec.js",
  628. "src/deep/nested/test/integration.js",
  629. "src/test/nested/unit.js",
  630. "src/components/test/unit.ts",
  631. "build/app.js",
  632. "src/build/main.js",
  633. "deep/nested/build/lib.js",
  634. "build/dist/app.js",
  635. "app/cache/temp",
  636. "src/cache/data/file",
  637. "lib/cache/deep/nested/item",
  638. "nested/deep/cache/file",
  639. "cache/file",
  640. "lib/app/vendor/jquery.min.js",
  641. "lib/ui/vendor/bootstrap.min.css",
  642. "lib/core/vendor/deep/nested/lib.min.map",
  643. "lib/app/vendor/jquery.js",
  644. "lib/nested/deep/vendor/lib.min.js",
  645. ]
  646. self._assert_ignore_match(paths)
  647. def test_asterisk_pattern_overlaps(self) -> None:
  648. """Test overlapping single and double asterisk patterns with negations."""
  649. self._write_gitignore(
  650. "**/*.tmp\n!src/**/*.tmp\nsrc/*/cache/*.tmp\n**/test/*\n!**/test/*.spec.*\n"
  651. )
  652. # Pattern explanation:
  653. # **/*.tmp - Ignore all .tmp files anywhere
  654. # !src/**/*.tmp - But don't ignore .tmp files under src/
  655. # src/*/cache/*.tmp - But do ignore .tmp files in src/*/cache/ (overrides negation)
  656. # **/test/* - Ignore everything directly in test directories
  657. # !**/test/*.spec.* - But don't ignore spec files in test directories
  658. # Test tmp file patterns with src/ negation
  659. self._create_file("temp.tmp") # Should be ignored
  660. self._create_file("build/cache.tmp") # Should be ignored
  661. self._create_file("src/app.tmp") # Should not be ignored (src negation)
  662. self._create_file("src/lib/utils.tmp") # Should not be ignored (src negation)
  663. self._create_file(
  664. "src/app/cache/data.tmp"
  665. ) # Should be ignored (cache override)
  666. self._create_file(
  667. "src/lib/cache/temp.tmp"
  668. ) # Should be ignored (cache override)
  669. # Test test directory patterns with spec negation
  670. self._create_file("test/unit.js") # Should be ignored
  671. self._create_file("src/test/helper.js") # Should be ignored
  672. self._create_file("test/app.spec.js") # Should not be ignored (spec negation)
  673. self._create_file(
  674. "src/test/lib.spec.ts"
  675. ) # Should not be ignored (spec negation)
  676. self._create_file(
  677. "test/nested/file.js"
  678. ) # Should not be ignored (not direct child)
  679. paths = [
  680. "temp.tmp",
  681. "build/cache.tmp",
  682. "src/app.tmp",
  683. "src/lib/utils.tmp",
  684. "src/app/cache/data.tmp",
  685. "src/lib/cache/temp.tmp",
  686. "test/unit.js",
  687. "src/test/helper.js",
  688. "test/app.spec.js",
  689. "src/test/lib.spec.ts",
  690. "test/nested/file.js",
  691. ]
  692. self._assert_ignore_match(paths)
  693. def test_asterisk_boundary_conditions(self) -> None:
  694. """Test boundary conditions between single and double asterisk patterns."""
  695. self._write_gitignore("a/**/b/*\nc/**/**/d\n*/e/**/*\nf/*/g/**\n")
  696. # Test a/**/b/* - ** in middle, * at end
  697. self._create_file("a/b/file") # Direct path
  698. self._create_file("a/x/b/file") # One level between a and b
  699. self._create_file("a/x/y/b/file") # Multiple levels between a and b
  700. self._create_file("a/b/nested/file") # Should not match (nested after b)
  701. # Test c/**/**/d - multiple ** separated by single level
  702. self._create_file("c/d") # Minimal match
  703. self._create_file("c/x/d") # One level before d
  704. self._create_file("c/x/y/d") # Multiple levels before d
  705. self._create_file("c/x/y/z/d") # Even more levels
  706. # Test */e/**/* - * at start, ** in middle, * at end
  707. self._create_file("a/e/file") # Minimal match
  708. self._create_file("x/e/nested/file") # Nested after e
  709. self._create_file("y/e/deep/nested/file") # Deep nesting after e
  710. self._create_file(
  711. "nested/path/e/file"
  712. ) # Should not match (path before e too deep)
  713. # Test f/*/g/** - * in middle, ** at end
  714. self._create_file("f/x/g/file") # Basic match
  715. self._create_file("f/y/g/nested/file") # Nested after g
  716. self._create_file("f/z/g/deep/nested/file") # Deep nesting after g
  717. self._create_file(
  718. "f/nested/path/g/file"
  719. ) # Should not match (path between f and g too deep)
  720. paths = [
  721. "a/b/file",
  722. "a/x/b/file",
  723. "a/x/y/b/file",
  724. "a/b/nested/file",
  725. "c/d",
  726. "c/x/d",
  727. "c/x/y/d",
  728. "c/x/y/z/d",
  729. "a/e/file",
  730. "x/e/nested/file",
  731. "y/e/deep/nested/file",
  732. "nested/path/e/file",
  733. "f/x/g/file",
  734. "f/y/g/nested/file",
  735. "f/z/g/deep/nested/file",
  736. "f/nested/path/g/file",
  737. ]
  738. self._assert_ignore_match(paths)
  739. def test_asterisk_edge_case_combinations(self) -> None:
  740. """Test really tricky edge cases with asterisk combinations."""
  741. self._write_gitignore("***\n**/*\n*/**\n*/*/\n**/*/*\n*/*/**\n")
  742. # Test *** pattern (should behave like **)
  743. self._create_file("file1")
  744. self._create_file("dir/file2")
  745. self._create_file("deep/nested/file3")
  746. # Test **/* pattern (anything with at least one path segment)
  747. self._create_file("path1/item1")
  748. self._create_file("path2/sub/item2")
  749. # Test */** pattern (anything under a single-level directory)
  750. self._create_file("single/file4")
  751. self._create_file("single/nested/deep")
  752. # Test */*/ pattern (directories exactly two levels deep)
  753. self._create_dir("level1/level2")
  754. self._create_dir("dir1/dir2")
  755. self._create_dir("path3/sub1/sub2") # Should not match (too deep)
  756. # Test **/*/* pattern (at least two path segments after any prefix)
  757. self._create_file("test1/test2/test3")
  758. self._create_file("deep/nested/item3/item4")
  759. self._create_file(
  760. "simple/item"
  761. ) # Should not match (only one segment after any prefix at root)
  762. # Test */*/** pattern (single/single/anything)
  763. self._create_file("part1/part2/anything")
  764. self._create_file("seg1/seg2/deep/nested")
  765. paths = [
  766. "file1",
  767. "dir/file2",
  768. "deep/nested/file3",
  769. "path1/item1",
  770. "path2/sub/item2",
  771. "single/file4",
  772. "single/nested/deep",
  773. "level1/level2/",
  774. "dir1/dir2/",
  775. "path3/sub1/sub2/",
  776. "test1/test2/test3",
  777. "deep/nested/item3/item4",
  778. "simple/item",
  779. "part1/part2/anything",
  780. "seg1/seg2/deep/nested",
  781. ]
  782. self._assert_ignore_match(paths)
  783. def test_asterisk_consecutive_patterns(self) -> None:
  784. """Test patterns with consecutive asterisks and weird spacing."""
  785. self._write_gitignore("a*/b*\n*x*y*\n**z**\n**/.*/**\n*.*./*\n")
  786. # Test a*/b* pattern
  787. self._create_file("a/b") # Minimal match
  788. self._create_file("app/build") # Both have suffixes
  789. self._create_file("api/backup") # Both have suffixes
  790. self._create_file("a/build") # a exact, b with suffix
  791. self._create_file("app/b") # a with suffix, b exact
  792. self._create_file("x/a/b") # Should not match (a not at start)
  793. # Test *x*y* pattern
  794. self._create_file("xy") # Minimal
  795. self._create_file("axby") # x and y in middle
  796. self._create_file("prefixsuffyend") # x and y with text around
  797. self._create_file("xyz") # Should not match (no y after x)
  798. self._create_file("axy") # x and y consecutive
  799. # Test **z** pattern
  800. self._create_file("z") # Just z
  801. self._create_file("az") # z at end
  802. self._create_file("za") # z at start
  803. self._create_file("aza") # z in middle
  804. self._create_file("dir/z") # z at any depth
  805. self._create_file("deep/nested/prefix_z_suffix") # z anywhere in name
  806. # Test **/.*/** pattern (hidden files in any directory structure)
  807. self._create_file("dir/.hidden/file")
  808. self._create_file("deep/nested/.secret/data")
  809. self._create_file(".visible/file") # At root level
  810. self._create_file("other/.config") # Should not match (no trailing path)
  811. # Test *.*./* pattern (files with dots in specific structure)
  812. self._create_file("app.min.js/file") # Two dots, then directory
  813. self._create_file("lib.bundle.css/asset") # Two dots, then directory
  814. self._create_file("simple.js") # Should not match (only one dot, no directory)
  815. self._create_file("no.dots.here") # Should not match (no trailing directory)
  816. paths = [
  817. "a/b",
  818. "app/build",
  819. "api/backup",
  820. "a/build",
  821. "app/b",
  822. "x/a/b",
  823. "xy",
  824. "axby",
  825. "prefixsuffyend",
  826. "xyz",
  827. "axy",
  828. "z",
  829. "az",
  830. "za",
  831. "aza",
  832. "dir/z",
  833. "deep/nested/prefix_z_suffix",
  834. "dir/.hidden/file",
  835. "deep/nested/.secret/data",
  836. ".visible/file",
  837. "other/.config",
  838. "app.min.js/file",
  839. "lib.bundle.css/asset",
  840. "simple.js",
  841. "no.dots.here",
  842. ]
  843. self._assert_ignore_match(paths)
  844. def test_asterisk_escaping_and_special_chars(self) -> None:
  845. """Test asterisk patterns with special characters and potential escaping."""
  846. self._write_gitignore(
  847. "\\*literal\n**/*.\\*\n[*]bracket\n*\\[escape\\]\n*.{tmp,log}\n"
  848. )
  849. # Test \*literal pattern (literal asterisk)
  850. self._create_file("*literal") # Literal asterisk at start
  851. self._create_file("xliteral") # Should not match (no literal asterisk)
  852. self._create_file("prefix*literal") # Literal asterisk in middle
  853. # Test **/*.* pattern (files with .* extension)
  854. self._create_file("file.*") # Literal .* extension
  855. self._create_file("dir/test.*") # At any depth
  856. self._create_file("file.txt") # Should not match (not .* extension)
  857. # Test [*]bracket pattern (bracket containing asterisk)
  858. self._create_file("*bracket") # Literal asterisk from bracket
  859. self._create_file("xbracket") # Should not match
  860. self._create_file("abracket") # Should not match
  861. # Test *\[escape\] pattern (literal brackets)
  862. self._create_file("test[escape]") # Literal brackets
  863. self._create_file("prefix[escape]") # With prefix
  864. self._create_file("test[other]") # Should not match (wrong brackets)
  865. # Test *.{tmp,log} pattern (brace expansion - may not work in gitignore)
  866. self._create_file("file.{tmp,log}") # Literal braces
  867. self._create_file("test.tmp") # Might match if braces are expanded
  868. self._create_file("test.log") # Might match if braces are expanded
  869. self._create_file("test.{other}") # Should not match
  870. paths = [
  871. "*literal",
  872. "xliteral",
  873. "prefix*literal",
  874. "file.*",
  875. "dir/test.*",
  876. "file.txt",
  877. "*bracket",
  878. "xbracket",
  879. "abracket",
  880. "test[escape]",
  881. "prefix[escape]",
  882. "test[other]",
  883. "file.{tmp,log}",
  884. "test.tmp",
  885. "test.log",
  886. "test.{other}",
  887. ]
  888. self._assert_ignore_match(paths)
  889. def test_quote_path_true_unicode_filenames(self) -> None:
  890. """Test quote_path=True functionality with unicode filenames."""
  891. try:
  892. self._write_gitignore("тест*\n*.测试\n")
  893. self._create_file("тест.txt")
  894. self._create_file("файл.测试")
  895. self._create_file("normal.txt")
  896. paths = ["тест.txt", "файл.测试", "normal.txt"]
  897. # Test that dulwich with quote_path=True matches git's quoted output
  898. git_ignored = self._git_check_ignore_quoted(paths)
  899. dulwich_ignored = self._dulwich_check_ignore_quoted(paths)
  900. self.assertEqual(
  901. git_ignored,
  902. dulwich_ignored,
  903. f"Mismatch for quoted paths {paths}: git={git_ignored}, dulwich={dulwich_ignored}",
  904. )
  905. except (UnicodeEncodeError, OSError):
  906. # Skip test if filesystem doesn't support unicode
  907. self.skipTest("Filesystem doesn't support unicode filenames")
  908. def test_quote_path_consistency(self) -> None:
  909. """Test that quote_path=True and quote_path=False are consistent."""
  910. try:
  911. self._write_gitignore("тест*\n*.测试\nmixed_тест*\n")
  912. self._create_file("тест.txt")
  913. self._create_file("файл.测试")
  914. self._create_file("normal.txt")
  915. self._create_file("mixed_тест.log")
  916. paths = ["тест.txt", "файл.测试", "normal.txt", "mixed_тест.log"]
  917. # Get both quoted and unquoted results from dulwich
  918. quoted_ignored = self._dulwich_check_ignore_quoted(paths)
  919. unquoted_ignored = self._dulwich_check_ignore(paths)
  920. # Verify that the number of ignored files is the same
  921. self.assertEqual(
  922. len(quoted_ignored),
  923. len(unquoted_ignored),
  924. "Quote path setting should not change which files are ignored",
  925. )
  926. # Verify quoted paths contain the expected files
  927. expected_quoted = {
  928. '"\\321\\202\\320\\265\\321\\201\\321\\202.txt"',
  929. '"\\321\\204\\320\\260\\320\\271\\320\\273.\\346\\265\\213\\350\\257\\225"',
  930. '"mixed_\\321\\202\\320\\265\\321\\201\\321\\202.log"',
  931. }
  932. self.assertEqual(quoted_ignored, expected_quoted)
  933. # Verify unquoted paths contain the expected files
  934. expected_unquoted = {"тест.txt", "файл.测试", "mixed_тест.log"}
  935. self.assertEqual(unquoted_ignored, expected_unquoted)
  936. except (UnicodeEncodeError, OSError):
  937. # Skip test if filesystem doesn't support unicode
  938. self.skipTest("Filesystem doesn't support unicode filenames")
  939. def _git_check_ignore_quoted(self, paths: list[str]) -> set[str]:
  940. """Run git check-ignore with default quoting and return set of ignored paths."""
  941. try:
  942. # Use default git settings (core.quotePath=true by default)
  943. output = run_git_or_fail(
  944. ["check-ignore", *paths],
  945. cwd=self.test_dir,
  946. )
  947. # git check-ignore returns paths separated by newlines
  948. return set(
  949. line.decode("utf-8") for line in output.strip().split(b"\n") if line
  950. )
  951. except AssertionError:
  952. # git check-ignore returns non-zero when no paths are ignored
  953. return set()
  954. def _dulwich_check_ignore_quoted(self, paths: list[str]) -> set[str]:
  955. """Run dulwich check_ignore with quote_path=True and return set of ignored paths."""
  956. # Convert to absolute paths relative to the test directory
  957. abs_paths = [os.path.join(self.test_dir, path) for path in paths]
  958. ignored = set(porcelain.check_ignore(self.test_dir, abs_paths, quote_path=True))
  959. # Convert back to relative paths and preserve original path format
  960. result = set()
  961. path_mapping = {}
  962. for orig_path, abs_path in zip(paths, abs_paths):
  963. path_mapping[abs_path] = orig_path
  964. for path in ignored:
  965. if path.startswith(self.test_dir + "/"):
  966. rel_path = path[len(self.test_dir) + 1 :]
  967. # Find the original path format that was requested
  968. orig_path = None
  969. for requested_path in paths:
  970. if requested_path.rstrip("/") == rel_path.rstrip("/"):
  971. orig_path = requested_path
  972. break
  973. result.add(orig_path if orig_path else rel_path)
  974. else:
  975. result.add(path)
  976. return result