2
0

test_ignore.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. # test_ignore.py -- Tests for ignore files.
  2. # Copyright (C) 2017 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 published 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. """Tests for ignore files."""
  22. import os
  23. import re
  24. import shutil
  25. import tempfile
  26. from io import BytesIO
  27. from dulwich.ignore import (
  28. IgnoreFilter,
  29. IgnoreFilterManager,
  30. IgnoreFilterStack,
  31. Pattern,
  32. match_pattern,
  33. read_ignore_patterns,
  34. translate,
  35. )
  36. from dulwich.porcelain import _quote_path
  37. from dulwich.repo import Repo
  38. from . import TestCase
  39. POSITIVE_MATCH_TESTS = [
  40. (b"foo.c", b"*.c"),
  41. (b".c", b"*.c"),
  42. (b"foo/foo.c", b"*.c"),
  43. (b"foo/foo.c", b"foo.c"),
  44. (b"foo.c", b"/*.c"),
  45. (b"foo.c", b"/foo.c"),
  46. (b"foo.c", b"foo.c"),
  47. (b"foo.c", b"foo.[ch]"),
  48. (b"foo/bar/bla.c", b"foo/**"),
  49. (b"foo/bar/bla/blie.c", b"foo/**/blie.c"),
  50. (b"foo/bar/bla.c", b"**/bla.c"),
  51. (b"bla.c", b"**/bla.c"),
  52. (b"foo/bar", b"foo/**/bar"),
  53. (b"foo/bla/bar", b"foo/**/bar"),
  54. (b"foo/bar/", b"bar/"),
  55. (b"foo/bar/", b"bar"),
  56. (b"foo/bar/something", b"foo/bar/*"),
  57. ]
  58. NEGATIVE_MATCH_TESTS = [
  59. (b"foo.c", b"foo.[dh]"),
  60. (b"foo/foo.c", b"/foo.c"),
  61. (b"foo/foo.c", b"/*.c"),
  62. (b"foo/bar/", b"/bar/"),
  63. (b"foo/bar/", b"foo/bar/*"),
  64. (b"foo/bar", b"foo?bar"),
  65. ]
  66. TRANSLATE_TESTS = [
  67. (b"*.c", b"(?ms)(.*/)?[^/]*\\.c/?\\Z"),
  68. (b"foo.c", b"(?ms)(.*/)?foo\\.c/?\\Z"),
  69. (b"/*.c", b"(?ms)[^/]*\\.c/?\\Z"),
  70. (b"/foo.c", b"(?ms)foo\\.c/?\\Z"),
  71. (b"foo.c", b"(?ms)(.*/)?foo\\.c/?\\Z"),
  72. (b"foo.[ch]", b"(?ms)(.*/)?foo\\.[ch]/?\\Z"),
  73. (b"bar/", b"(?ms)(.*/)?bar\\/\\Z"),
  74. (b"foo/**", b"(?ms)foo/.*/?\\Z"),
  75. (b"foo/**/blie.c", b"(?ms)foo/(?:[^/]+/)*blie\\.c/?\\Z"),
  76. (b"**/bla.c", b"(?ms)(.*/)?bla\\.c/?\\Z"),
  77. (b"foo/**/bar", b"(?ms)foo/(?:[^/]+/)*bar/?\\Z"),
  78. (b"foo/bar/*", b"(?ms)foo\\/bar\\/[^/]+/?\\Z"),
  79. (b"/foo\\[bar\\]", b"(?ms)foo\\[bar\\]/?\\Z"),
  80. (b"/foo[bar]", b"(?ms)foo[bar]/?\\Z"),
  81. (b"/foo[0-9]", b"(?ms)foo[0-9]/?\\Z"),
  82. ]
  83. class TranslateTests(TestCase):
  84. def test_translate(self) -> None:
  85. for pattern, regex in TRANSLATE_TESTS:
  86. if re.escape(b"/") == b"/":
  87. # Slash is no longer escaped in Python3.7, so undo the escaping
  88. # in the expected return value..
  89. regex = regex.replace(b"\\/", b"/")
  90. self.assertEqual(
  91. regex,
  92. translate(pattern),
  93. f"orig pattern: {pattern!r}, regex: {translate(pattern)!r}, expected: {regex!r}",
  94. )
  95. class ReadIgnorePatterns(TestCase):
  96. def test_read_file(self) -> None:
  97. f = BytesIO(
  98. b"""
  99. # a comment
  100. \x20\x20
  101. # and an empty line:
  102. \\#not a comment
  103. !negative
  104. with trailing whitespace
  105. with escaped trailing whitespace\\
  106. """
  107. )
  108. self.assertEqual(
  109. list(read_ignore_patterns(f)),
  110. [
  111. b"\\#not a comment",
  112. b"!negative",
  113. b"with trailing whitespace",
  114. b"with escaped trailing whitespace ",
  115. ],
  116. )
  117. class MatchPatternTests(TestCase):
  118. def test_matches(self) -> None:
  119. for path, pattern in POSITIVE_MATCH_TESTS:
  120. self.assertTrue(
  121. match_pattern(path, pattern),
  122. f"path: {path!r}, pattern: {pattern!r}",
  123. )
  124. def test_no_matches(self) -> None:
  125. for path, pattern in NEGATIVE_MATCH_TESTS:
  126. self.assertFalse(
  127. match_pattern(path, pattern),
  128. f"path: {path!r}, pattern: {pattern!r}",
  129. )
  130. class ParentExclusionTests(TestCase):
  131. """Tests for parent directory exclusion helper functions."""
  132. def test_check_parent_exclusion_direct_directory(self) -> None:
  133. """Test _check_parent_exclusion with direct directory exclusion."""
  134. from dulwich.ignore import Pattern, _check_parent_exclusion
  135. # Pattern: dir/, !dir/file.txt
  136. patterns = [Pattern(b"dir/"), Pattern(b"!dir/file.txt")]
  137. # dir/file.txt has parent 'dir' excluded
  138. self.assertTrue(_check_parent_exclusion("dir/file.txt", patterns))
  139. # dir/subdir/file.txt also has parent 'dir' excluded
  140. self.assertTrue(_check_parent_exclusion("dir/subdir/file.txt", patterns))
  141. # other/file.txt has no parent excluded
  142. self.assertFalse(_check_parent_exclusion("other/file.txt", patterns))
  143. def test_check_parent_exclusion_no_negation(self) -> None:
  144. """Test _check_parent_exclusion when there's no negation pattern."""
  145. from dulwich.ignore import Pattern, _check_parent_exclusion
  146. # Only exclusion patterns
  147. patterns = [Pattern(b"*.log"), Pattern(b"build/")]
  148. # No negation pattern, so no parent exclusion check needed
  149. self.assertFalse(_check_parent_exclusion("build/file.txt", patterns))
  150. def test_pattern_excludes_parent_directory_slash(self) -> None:
  151. """Test _pattern_excludes_parent for patterns ending with /."""
  152. from dulwich.ignore import _pattern_excludes_parent
  153. # Pattern: parent/
  154. self.assertTrue(
  155. _pattern_excludes_parent("parent/", "parent/file.txt", "!parent/file.txt")
  156. )
  157. self.assertTrue(
  158. _pattern_excludes_parent(
  159. "parent/", "parent/sub/file.txt", "!parent/sub/file.txt"
  160. )
  161. )
  162. self.assertFalse(
  163. _pattern_excludes_parent("parent/", "other/file.txt", "!other/file.txt")
  164. )
  165. self.assertFalse(
  166. _pattern_excludes_parent("parent/", "parent", "!parent")
  167. ) # No / in path
  168. def test_pattern_excludes_parent_double_asterisk(self) -> None:
  169. """Test _pattern_excludes_parent for **/ patterns."""
  170. from dulwich.ignore import _pattern_excludes_parent
  171. # Pattern: **/node_modules/**
  172. self.assertTrue(
  173. _pattern_excludes_parent(
  174. "**/node_modules/**",
  175. "foo/node_modules/bar/file.txt",
  176. "!foo/node_modules/bar/file.txt",
  177. )
  178. )
  179. self.assertTrue(
  180. _pattern_excludes_parent(
  181. "**/node_modules/**", "node_modules/file.txt", "!node_modules/file.txt"
  182. )
  183. )
  184. self.assertFalse(
  185. _pattern_excludes_parent(
  186. "**/node_modules/**", "foo/bar/file.txt", "!foo/bar/file.txt"
  187. )
  188. )
  189. def test_pattern_excludes_parent_glob(self) -> None:
  190. """Test _pattern_excludes_parent for dir/** patterns."""
  191. from dulwich.ignore import _pattern_excludes_parent
  192. # Pattern: logs/** - allows exact file negations for immediate children
  193. self.assertFalse(
  194. _pattern_excludes_parent("logs/**", "logs/file.txt", "!logs/file.txt")
  195. )
  196. # Directory negations still have parent exclusion
  197. self.assertTrue(
  198. _pattern_excludes_parent("logs/**", "logs/keep/", "!logs/keep/")
  199. )
  200. # Non-exact negations have parent exclusion
  201. self.assertTrue(
  202. _pattern_excludes_parent("logs/**", "logs/keep/", "!logs/keep/file.txt")
  203. )
  204. # Nested paths have parent exclusion
  205. self.assertTrue(
  206. _pattern_excludes_parent("logs/**", "logs/sub/file.txt", "!logs/sub/")
  207. )
  208. self.assertTrue(
  209. _pattern_excludes_parent(
  210. "logs/**", "logs/sub/file.txt", "!logs/sub/file.txt"
  211. )
  212. )
  213. class IgnoreFilterTests(TestCase):
  214. def test_included(self) -> None:
  215. filter = IgnoreFilter([b"a.c", b"b.c"])
  216. self.assertTrue(filter.is_ignored(b"a.c"))
  217. self.assertIs(None, filter.is_ignored(b"c.c"))
  218. self.assertEqual([Pattern(b"a.c")], list(filter.find_matching(b"a.c")))
  219. self.assertEqual([], list(filter.find_matching(b"c.c")))
  220. def test_included_ignorecase(self) -> None:
  221. filter = IgnoreFilter([b"a.c", b"b.c"], ignorecase=False)
  222. self.assertTrue(filter.is_ignored(b"a.c"))
  223. self.assertFalse(filter.is_ignored(b"A.c"))
  224. filter = IgnoreFilter([b"a.c", b"b.c"], ignorecase=True)
  225. self.assertTrue(filter.is_ignored(b"a.c"))
  226. self.assertTrue(filter.is_ignored(b"A.c"))
  227. self.assertTrue(filter.is_ignored(b"A.C"))
  228. def test_excluded(self) -> None:
  229. filter = IgnoreFilter([b"a.c", b"b.c", b"!c.c"])
  230. self.assertFalse(filter.is_ignored(b"c.c"))
  231. self.assertIs(None, filter.is_ignored(b"d.c"))
  232. self.assertEqual([Pattern(b"!c.c")], list(filter.find_matching(b"c.c")))
  233. self.assertEqual([], list(filter.find_matching(b"d.c")))
  234. def test_include_exclude_include(self) -> None:
  235. filter = IgnoreFilter([b"a.c", b"!a.c", b"a.c"])
  236. self.assertTrue(filter.is_ignored(b"a.c"))
  237. self.assertEqual(
  238. [Pattern(b"a.c"), Pattern(b"!a.c"), Pattern(b"a.c")],
  239. list(filter.find_matching(b"a.c")),
  240. )
  241. def test_manpage(self) -> None:
  242. # A specific example from the gitignore manpage
  243. filter = IgnoreFilter([b"/*", b"!/foo", b"/foo/*", b"!/foo/bar"])
  244. self.assertTrue(filter.is_ignored(b"a.c"))
  245. self.assertTrue(filter.is_ignored(b"foo/blie"))
  246. self.assertFalse(filter.is_ignored(b"foo"))
  247. self.assertFalse(filter.is_ignored(b"foo/bar"))
  248. self.assertFalse(filter.is_ignored(b"foo/bar/"))
  249. self.assertFalse(filter.is_ignored(b"foo/bar/bloe"))
  250. def test_regex_special(self) -> None:
  251. # See https://github.com/dulwich/dulwich/issues/930#issuecomment-1026166429
  252. filter = IgnoreFilter([b"/foo\\[bar\\]", b"/foo"])
  253. self.assertTrue(filter.is_ignored("foo"))
  254. self.assertTrue(filter.is_ignored("foo[bar]"))
  255. def test_from_path_pathlib(self) -> None:
  256. import tempfile
  257. from pathlib import Path
  258. # Create a temporary .gitignore file
  259. with tempfile.NamedTemporaryFile(
  260. mode="w", suffix=".gitignore", delete=False
  261. ) as f:
  262. f.write("*.pyc\n__pycache__/\n")
  263. temp_path = f.name
  264. self.addCleanup(os.unlink, temp_path)
  265. # Test with pathlib.Path
  266. path_obj = Path(temp_path)
  267. ignore_filter = IgnoreFilter.from_path(path_obj)
  268. # Test that it loaded the patterns correctly
  269. self.assertTrue(ignore_filter.is_ignored("test.pyc"))
  270. self.assertTrue(ignore_filter.is_ignored("__pycache__/"))
  271. self.assertFalse(ignore_filter.is_ignored("test.py"))
  272. class IgnoreFilterStackTests(TestCase):
  273. def test_stack_first(self) -> None:
  274. filter1 = IgnoreFilter([b"[a].c", b"[b].c", b"![d].c"])
  275. filter2 = IgnoreFilter([b"[a].c", b"![b],c", b"[c].c", b"[d].c"])
  276. stack = IgnoreFilterStack([filter1, filter2])
  277. self.assertIs(True, stack.is_ignored(b"a.c"))
  278. self.assertIs(True, stack.is_ignored(b"b.c"))
  279. self.assertIs(True, stack.is_ignored(b"c.c"))
  280. self.assertIs(False, stack.is_ignored(b"d.c"))
  281. self.assertIs(None, stack.is_ignored(b"e.c"))
  282. class IgnoreFilterManagerTests(TestCase):
  283. def test_load_ignore(self) -> None:
  284. tmp_dir = tempfile.mkdtemp()
  285. self.addCleanup(shutil.rmtree, tmp_dir)
  286. repo = Repo.init(tmp_dir)
  287. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  288. f.write(b"/foo/bar\n")
  289. f.write(b"/dir2\n")
  290. f.write(b"/dir3/\n")
  291. os.mkdir(os.path.join(repo.path, "dir"))
  292. with open(os.path.join(repo.path, "dir", ".gitignore"), "wb") as f:
  293. f.write(b"/blie\n")
  294. with open(os.path.join(repo.path, "dir", "blie"), "wb") as f:
  295. f.write(b"IGNORED")
  296. p = os.path.join(repo.controldir(), "info", "exclude")
  297. with open(p, "wb") as f:
  298. f.write(b"/excluded\n")
  299. m = IgnoreFilterManager.from_repo(repo)
  300. self.assertTrue(m.is_ignored("dir/blie"))
  301. self.assertIs(None, m.is_ignored(os.path.join("dir", "bloe")))
  302. self.assertIs(None, m.is_ignored("dir"))
  303. self.assertTrue(m.is_ignored(os.path.join("foo", "bar")))
  304. self.assertTrue(m.is_ignored(os.path.join("excluded")))
  305. self.assertTrue(m.is_ignored(os.path.join("dir2", "fileinignoreddir")))
  306. self.assertFalse(m.is_ignored("dir3"))
  307. self.assertTrue(m.is_ignored("dir3/"))
  308. self.assertTrue(m.is_ignored("dir3/bla"))
  309. def test_nested_gitignores(self) -> None:
  310. tmp_dir = tempfile.mkdtemp()
  311. self.addCleanup(shutil.rmtree, tmp_dir)
  312. repo = Repo.init(tmp_dir)
  313. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  314. f.write(b"/*\n")
  315. f.write(b"!/foo\n")
  316. os.mkdir(os.path.join(repo.path, "foo"))
  317. with open(os.path.join(repo.path, "foo", ".gitignore"), "wb") as f:
  318. f.write(b"/bar\n")
  319. with open(os.path.join(repo.path, "foo", "bar"), "wb") as f:
  320. f.write(b"IGNORED")
  321. m = IgnoreFilterManager.from_repo(repo)
  322. self.assertTrue(m.is_ignored("foo/bar"))
  323. def test_load_ignore_ignorecase(self) -> None:
  324. tmp_dir = tempfile.mkdtemp()
  325. self.addCleanup(shutil.rmtree, tmp_dir)
  326. repo = Repo.init(tmp_dir)
  327. config = repo.get_config()
  328. config.set(b"core", b"ignorecase", True)
  329. config.write_to_path()
  330. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  331. f.write(b"/foo/bar\n")
  332. f.write(b"/dir\n")
  333. m = IgnoreFilterManager.from_repo(repo)
  334. self.assertTrue(m.is_ignored(os.path.join("dir", "blie")))
  335. self.assertTrue(m.is_ignored(os.path.join("DIR", "blie")))
  336. def test_ignored_contents(self) -> None:
  337. tmp_dir = tempfile.mkdtemp()
  338. self.addCleanup(shutil.rmtree, tmp_dir)
  339. repo = Repo.init(tmp_dir)
  340. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  341. f.write(b"a/*\n")
  342. f.write(b"!a/*.txt\n")
  343. m = IgnoreFilterManager.from_repo(repo)
  344. os.mkdir(os.path.join(repo.path, "a"))
  345. self.assertIs(None, m.is_ignored("a"))
  346. self.assertIs(None, m.is_ignored("a/"))
  347. self.assertFalse(m.is_ignored("a/b.txt"))
  348. self.assertTrue(m.is_ignored("a/c.dat"))
  349. def test_issue_1203_directory_negation(self) -> None:
  350. """Test for issue #1203: gitignore patterns with directory negation."""
  351. tmp_dir = tempfile.mkdtemp()
  352. self.addCleanup(shutil.rmtree, tmp_dir)
  353. repo = Repo.init(tmp_dir)
  354. # Create .gitignore with the patterns from the issue
  355. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  356. f.write(b"data/**\n")
  357. f.write(b"!data/*/\n")
  358. # Create directory structure
  359. os.makedirs(os.path.join(repo.path, "data", "subdir"))
  360. m = IgnoreFilterManager.from_repo(repo)
  361. # Test the expected behavior
  362. self.assertTrue(
  363. m.is_ignored("data/test.dvc")
  364. ) # File in data/ should be ignored
  365. self.assertFalse(m.is_ignored("data/")) # data/ directory should not be ignored
  366. self.assertTrue(
  367. m.is_ignored("data/subdir/")
  368. ) # Subdirectory should be ignored (matches Git behavior)
  369. def test_issue_1721_directory_negation_with_double_asterisk(self) -> None:
  370. """Test for issue #1721: regression with negated subdirectory patterns using **."""
  371. tmp_dir = tempfile.mkdtemp()
  372. self.addCleanup(shutil.rmtree, tmp_dir)
  373. repo = Repo.init(tmp_dir)
  374. # Create .gitignore with the patterns from issue #1721
  375. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  376. f.write(b"data/**\n")
  377. f.write(b"!data/**/\n")
  378. f.write(b"!data/**/*.csv\n")
  379. # Create directory structure
  380. os.makedirs(os.path.join(repo.path, "data", "subdir"))
  381. m = IgnoreFilterManager.from_repo(repo)
  382. # Test the expected behavior - issue #1721 was that data/myfile was not ignored
  383. self.assertTrue(
  384. m.is_ignored("data/myfile")
  385. ) # File should be ignored (fixes issue #1721)
  386. self.assertFalse(m.is_ignored("data/")) # data/ is matched by !data/**/
  387. self.assertFalse(
  388. m.is_ignored("data/subdir/")
  389. ) # Subdirectory is matched by !data/**/
  390. # With data/** pattern, Git allows CSV files to be re-included via !data/**/*.csv
  391. self.assertFalse(m.is_ignored("data/test.csv")) # CSV files are not ignored
  392. self.assertFalse(
  393. m.is_ignored("data/subdir/test.csv")
  394. ) # CSV files in subdirs are not ignored
  395. self.assertTrue(
  396. m.is_ignored("data/subdir/other.txt")
  397. ) # Non-CSV files in subdirs are ignored
  398. def test_parent_directory_exclusion(self) -> None:
  399. """Test Git's parent directory exclusion rule.
  400. Git rule: "It is not possible to re-include a file if a parent directory of that file is excluded."
  401. """
  402. tmp_dir = tempfile.mkdtemp()
  403. self.addCleanup(shutil.rmtree, tmp_dir)
  404. repo = Repo.init(tmp_dir)
  405. # Test case 1: Direct parent directory exclusion
  406. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  407. f.write(b"parent/\n")
  408. f.write(b"!parent/file.txt\n")
  409. f.write(b"!parent/child/\n")
  410. m = IgnoreFilterManager.from_repo(repo)
  411. # parent/ is excluded, so files inside cannot be re-included
  412. self.assertTrue(m.is_ignored("parent/"))
  413. self.assertTrue(m.is_ignored("parent/file.txt")) # Cannot re-include
  414. self.assertTrue(m.is_ignored("parent/child/")) # Cannot re-include
  415. self.assertTrue(m.is_ignored("parent/child/file.txt"))
  416. def test_parent_exclusion_with_wildcards(self) -> None:
  417. """Test parent directory exclusion with wildcard patterns."""
  418. tmp_dir = tempfile.mkdtemp()
  419. self.addCleanup(shutil.rmtree, tmp_dir)
  420. repo = Repo.init(tmp_dir)
  421. # Test case 2: Parent excluded by wildcard
  422. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  423. f.write(b"*/build/\n")
  424. f.write(b"!*/build/important.txt\n")
  425. m = IgnoreFilterManager.from_repo(repo)
  426. self.assertTrue(m.is_ignored("src/build/"))
  427. self.assertTrue(m.is_ignored("src/build/important.txt")) # Cannot re-include
  428. self.assertTrue(m.is_ignored("test/build/"))
  429. self.assertTrue(m.is_ignored("test/build/important.txt")) # Cannot re-include
  430. def test_parent_exclusion_with_double_asterisk(self) -> None:
  431. """Test parent directory exclusion with ** patterns."""
  432. tmp_dir = tempfile.mkdtemp()
  433. self.addCleanup(shutil.rmtree, tmp_dir)
  434. repo = Repo.init(tmp_dir)
  435. # Test case 3: Complex ** pattern with parent exclusion
  436. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  437. f.write(b"**/node_modules/\n")
  438. f.write(b"!**/node_modules/keep.txt\n")
  439. m = IgnoreFilterManager.from_repo(repo)
  440. self.assertTrue(m.is_ignored("node_modules/"))
  441. self.assertTrue(m.is_ignored("node_modules/keep.txt")) # Cannot re-include
  442. self.assertTrue(m.is_ignored("src/node_modules/"))
  443. self.assertTrue(m.is_ignored("src/node_modules/keep.txt")) # Cannot re-include
  444. self.assertTrue(m.is_ignored("deep/nested/node_modules/"))
  445. self.assertTrue(
  446. m.is_ignored("deep/nested/node_modules/keep.txt")
  447. ) # Cannot re-include
  448. def test_no_parent_exclusion_with_glob_contents(self) -> None:
  449. """Test that dir/** allows specific file negations for immediate children."""
  450. tmp_dir = tempfile.mkdtemp()
  451. self.addCleanup(shutil.rmtree, tmp_dir)
  452. repo = Repo.init(tmp_dir)
  453. # Test: dir/** allows specific file negations (unlike dir/ which doesn't)
  454. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  455. f.write(b"logs/**\n")
  456. f.write(b"!logs/important.log\n")
  457. f.write(b"!logs/keep/\n")
  458. m = IgnoreFilterManager.from_repo(repo)
  459. # logs/ itself is excluded by logs/**
  460. self.assertTrue(m.is_ignored("logs/"))
  461. # Specific file negation works with dir/** patterns
  462. self.assertFalse(m.is_ignored("logs/important.log"))
  463. # Directory negations still don't work (parent exclusion)
  464. self.assertTrue(m.is_ignored("logs/keep/"))
  465. # Nested paths are ignored
  466. self.assertTrue(m.is_ignored("logs/subdir/"))
  467. self.assertTrue(m.is_ignored("logs/subdir/file.txt"))
  468. def test_parent_exclusion_ordering(self) -> None:
  469. """Test that parent exclusion depends on pattern ordering."""
  470. tmp_dir = tempfile.mkdtemp()
  471. self.addCleanup(shutil.rmtree, tmp_dir)
  472. repo = Repo.init(tmp_dir)
  473. # Test case 5: Order matters for parent exclusion
  474. with open(os.path.join(repo.path, ".gitignore"), "wb") as f:
  475. f.write(b"!data/important/\n") # This comes first but won't work
  476. f.write(b"data/\n") # This excludes the parent
  477. m = IgnoreFilterManager.from_repo(repo)
  478. self.assertTrue(m.is_ignored("data/"))
  479. self.assertTrue(m.is_ignored("data/important/")) # Cannot re-include
  480. self.assertTrue(m.is_ignored("data/important/file.txt"))
  481. class QuotePathTests(TestCase):
  482. """Tests for _quote_path function."""
  483. def test_ascii_paths(self) -> None:
  484. """Test that ASCII paths are not quoted."""
  485. self.assertEqual(_quote_path("file.txt"), "file.txt")
  486. self.assertEqual(_quote_path("dir/file.txt"), "dir/file.txt")
  487. self.assertEqual(_quote_path("path with spaces.txt"), "path with spaces.txt")
  488. def test_unicode_paths(self) -> None:
  489. """Test that unicode paths are quoted with C-style escapes."""
  490. # Russian characters
  491. self.assertEqual(
  492. _quote_path("тест.txt"), '"\\321\\202\\320\\265\\321\\201\\321\\202.txt"'
  493. )
  494. # Chinese characters
  495. self.assertEqual(
  496. _quote_path("файл.测试"),
  497. '"\\321\\204\\320\\260\\320\\271\\320\\273.\\346\\265\\213\\350\\257\\225"',
  498. )
  499. # Mixed ASCII and unicode
  500. self.assertEqual(
  501. _quote_path("test-тест.txt"),
  502. '"test-\\321\\202\\320\\265\\321\\201\\321\\202.txt"',
  503. )
  504. def test_special_characters(self) -> None:
  505. """Test that special characters are properly escaped."""
  506. # Quotes in filename
  507. self.assertEqual(
  508. _quote_path('file"with"quotes.txt'), '"file\\"with\\"quotes.txt"'
  509. )
  510. # Backslashes in filename
  511. self.assertEqual(
  512. _quote_path("file\\with\\backslashes.txt"),
  513. '"file\\\\with\\\\backslashes.txt"',
  514. )
  515. # Mixed special chars and unicode
  516. self.assertEqual(
  517. _quote_path('тест"файл.txt'),
  518. '"\\321\\202\\320\\265\\321\\201\\321\\202\\"\\321\\204\\320\\260\\320\\271\\320\\273.txt"',
  519. )
  520. def test_empty_and_edge_cases(self) -> None:
  521. """Test edge cases."""
  522. self.assertEqual(_quote_path(""), "")
  523. self.assertEqual(_quote_path("a"), "a") # Single ASCII char
  524. self.assertEqual(_quote_path("я"), '"\\321\\217"') # Single unicode char
  525. class CheckIgnoreQuotePathTests(TestCase):
  526. """Integration tests for check_ignore with quote_path parameter."""
  527. def setUp(self) -> None:
  528. self.test_dir = tempfile.mkdtemp()
  529. self.addCleanup(shutil.rmtree, self.test_dir)
  530. def test_quote_path_true_unicode_filenames(self) -> None:
  531. """Test that quote_path=True returns quoted unicode filenames."""
  532. from dulwich import porcelain
  533. # Create a repository
  534. repo = Repo.init(self.test_dir)
  535. self.addCleanup(repo.close)
  536. # Create .gitignore with unicode patterns
  537. gitignore_path = os.path.join(self.test_dir, ".gitignore")
  538. with open(gitignore_path, "w", encoding="utf-8") as f:
  539. f.write("тест*\n")
  540. f.write("*.测试\n")
  541. # Create unicode files
  542. test_files = ["тест.txt", "файл.测试", "normal.txt"]
  543. for filename in test_files:
  544. filepath = os.path.join(self.test_dir, filename)
  545. with open(filepath, "w", encoding="utf-8") as f:
  546. f.write("test content")
  547. # Test with quote_path=True (default)
  548. abs_paths = [os.path.join(self.test_dir, f) for f in test_files]
  549. ignored_quoted = set(
  550. porcelain.check_ignore(self.test_dir, abs_paths, quote_path=True)
  551. )
  552. # Test with quote_path=False
  553. ignored_unquoted = set(
  554. porcelain.check_ignore(self.test_dir, abs_paths, quote_path=False)
  555. )
  556. # Verify quoted results
  557. expected_quoted = {
  558. '"\\321\\202\\320\\265\\321\\201\\321\\202.txt"', # тест.txt
  559. '"\\321\\204\\320\\260\\320\\271\\320\\273.\\346\\265\\213\\350\\257\\225"', # файл.测试
  560. }
  561. self.assertEqual(ignored_quoted, expected_quoted)
  562. # Verify unquoted results
  563. expected_unquoted = {"тест.txt", "файл.测试"}
  564. self.assertEqual(ignored_unquoted, expected_unquoted)
  565. def test_quote_path_ascii_filenames(self) -> None:
  566. """Test that ASCII filenames are unaffected by quote_path setting."""
  567. from dulwich import porcelain
  568. # Create a repository
  569. repo = Repo.init(self.test_dir)
  570. self.addCleanup(repo.close)
  571. # Create .gitignore
  572. gitignore_path = os.path.join(self.test_dir, ".gitignore")
  573. with open(gitignore_path, "w") as f:
  574. f.write("*.tmp\n")
  575. f.write("test*\n")
  576. # Create ASCII files
  577. test_files = ["test.txt", "file.tmp", "normal.txt"]
  578. for filename in test_files:
  579. filepath = os.path.join(self.test_dir, filename)
  580. with open(filepath, "w") as f:
  581. f.write("test content")
  582. # Test both settings
  583. abs_paths = [os.path.join(self.test_dir, f) for f in test_files]
  584. ignored_quoted = set(
  585. porcelain.check_ignore(self.test_dir, abs_paths, quote_path=True)
  586. )
  587. ignored_unquoted = set(
  588. porcelain.check_ignore(self.test_dir, abs_paths, quote_path=False)
  589. )
  590. # Both should return the same results for ASCII filenames
  591. expected = {"test.txt", "file.tmp"}
  592. self.assertEqual(ignored_quoted, expected)
  593. self.assertEqual(ignored_unquoted, expected)