2
0

test_index.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. # test_index.py -- Tests for the git index
  2. # Copyright (C) 2008-2009 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. """Tests for the index."""
  22. import os
  23. import shutil
  24. import stat
  25. import struct
  26. import sys
  27. import tempfile
  28. from io import BytesIO
  29. from dulwich.index import (
  30. Index,
  31. IndexEntry,
  32. SerializedIndexEntry,
  33. _fs_to_tree_path,
  34. _tree_to_fs_path,
  35. build_index_from_tree,
  36. cleanup_mode,
  37. commit_tree,
  38. get_unstaged_changes,
  39. index_entry_from_stat,
  40. read_index,
  41. read_index_dict,
  42. validate_path_element_default,
  43. validate_path_element_ntfs,
  44. write_cache_time,
  45. write_index,
  46. write_index_dict,
  47. )
  48. from dulwich.object_store import MemoryObjectStore
  49. from dulwich.objects import S_IFGITLINK, Blob, Commit, Tree
  50. from dulwich.repo import Repo
  51. from . import TestCase, skipIf
  52. def can_symlink() -> bool:
  53. """Return whether running process can create symlinks."""
  54. if sys.platform != "win32":
  55. # Platforms other than Windows should allow symlinks without issues.
  56. return True
  57. test_source = tempfile.mkdtemp()
  58. test_target = test_source + "can_symlink"
  59. try:
  60. os.symlink(test_source, test_target)
  61. except (NotImplementedError, OSError):
  62. return False
  63. return True
  64. class IndexTestCase(TestCase):
  65. datadir = os.path.join(os.path.dirname(__file__), "../testdata/indexes")
  66. def get_simple_index(self, name):
  67. return Index(os.path.join(self.datadir, name))
  68. class SimpleIndexTestCase(IndexTestCase):
  69. def test_len(self) -> None:
  70. self.assertEqual(1, len(self.get_simple_index("index")))
  71. def test_iter(self) -> None:
  72. self.assertEqual([b"bla"], list(self.get_simple_index("index")))
  73. def test_iterobjects(self) -> None:
  74. self.assertEqual(
  75. [(b"bla", b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 33188)],
  76. list(self.get_simple_index("index").iterobjects()),
  77. )
  78. def test_getitem(self) -> None:
  79. self.assertEqual(
  80. IndexEntry(
  81. (1230680220, 0),
  82. (1230680220, 0),
  83. 2050,
  84. 3761020,
  85. 33188,
  86. 1000,
  87. 1000,
  88. 0,
  89. b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
  90. ),
  91. self.get_simple_index("index")[b"bla"],
  92. )
  93. def test_empty(self) -> None:
  94. i = self.get_simple_index("notanindex")
  95. self.assertEqual(0, len(i))
  96. self.assertFalse(os.path.exists(i._filename))
  97. def test_against_empty_tree(self) -> None:
  98. i = self.get_simple_index("index")
  99. changes = list(i.changes_from_tree(MemoryObjectStore(), None))
  100. self.assertEqual(1, len(changes))
  101. (oldname, newname), (oldmode, newmode), (oldsha, newsha) = changes[0]
  102. self.assertEqual(b"bla", newname)
  103. self.assertEqual(b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", newsha)
  104. class SimpleIndexWriterTestCase(IndexTestCase):
  105. def setUp(self) -> None:
  106. IndexTestCase.setUp(self)
  107. self.tempdir = tempfile.mkdtemp()
  108. def tearDown(self) -> None:
  109. IndexTestCase.tearDown(self)
  110. shutil.rmtree(self.tempdir)
  111. def test_simple_write(self) -> None:
  112. entries = [
  113. (
  114. SerializedIndexEntry(
  115. b"barbla",
  116. (1230680220, 0),
  117. (1230680220, 0),
  118. 2050,
  119. 3761020,
  120. 33188,
  121. 1000,
  122. 1000,
  123. 0,
  124. b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
  125. 0,
  126. 0,
  127. )
  128. )
  129. ]
  130. filename = os.path.join(self.tempdir, "test-simple-write-index")
  131. with open(filename, "wb+") as x:
  132. write_index(x, entries)
  133. with open(filename, "rb") as x:
  134. self.assertEqual(entries, list(read_index(x)))
  135. class ReadIndexDictTests(IndexTestCase):
  136. def setUp(self) -> None:
  137. IndexTestCase.setUp(self)
  138. self.tempdir = tempfile.mkdtemp()
  139. def tearDown(self) -> None:
  140. IndexTestCase.tearDown(self)
  141. shutil.rmtree(self.tempdir)
  142. def test_simple_write(self) -> None:
  143. entries = {
  144. b"barbla": IndexEntry(
  145. (1230680220, 0),
  146. (1230680220, 0),
  147. 2050,
  148. 3761020,
  149. 33188,
  150. 1000,
  151. 1000,
  152. 0,
  153. b"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
  154. )
  155. }
  156. filename = os.path.join(self.tempdir, "test-simple-write-index")
  157. with open(filename, "wb+") as x:
  158. write_index_dict(x, entries)
  159. with open(filename, "rb") as x:
  160. self.assertEqual(entries, read_index_dict(x))
  161. class CommitTreeTests(TestCase):
  162. def setUp(self) -> None:
  163. super().setUp()
  164. self.store = MemoryObjectStore()
  165. def test_single_blob(self) -> None:
  166. blob = Blob()
  167. blob.data = b"foo"
  168. self.store.add_object(blob)
  169. blobs = [(b"bla", blob.id, stat.S_IFREG)]
  170. rootid = commit_tree(self.store, blobs)
  171. self.assertEqual(rootid, b"1a1e80437220f9312e855c37ac4398b68e5c1d50")
  172. self.assertEqual((stat.S_IFREG, blob.id), self.store[rootid][b"bla"])
  173. self.assertEqual({rootid, blob.id}, set(self.store._data.keys()))
  174. def test_nested(self) -> None:
  175. blob = Blob()
  176. blob.data = b"foo"
  177. self.store.add_object(blob)
  178. blobs = [(b"bla/bar", blob.id, stat.S_IFREG)]
  179. rootid = commit_tree(self.store, blobs)
  180. self.assertEqual(rootid, b"d92b959b216ad0d044671981196781b3258fa537")
  181. dirid = self.store[rootid][b"bla"][1]
  182. self.assertEqual(dirid, b"c1a1deb9788150829579a8b4efa6311e7b638650")
  183. self.assertEqual((stat.S_IFDIR, dirid), self.store[rootid][b"bla"])
  184. self.assertEqual((stat.S_IFREG, blob.id), self.store[dirid][b"bar"])
  185. self.assertEqual({rootid, dirid, blob.id}, set(self.store._data.keys()))
  186. class CleanupModeTests(TestCase):
  187. def assertModeEqual(self, expected, got) -> None:
  188. self.assertEqual(expected, got, f"{expected:o} != {got:o}")
  189. def test_file(self) -> None:
  190. self.assertModeEqual(0o100644, cleanup_mode(0o100000))
  191. def test_executable(self) -> None:
  192. self.assertModeEqual(0o100755, cleanup_mode(0o100711))
  193. self.assertModeEqual(0o100755, cleanup_mode(0o100700))
  194. def test_symlink(self) -> None:
  195. self.assertModeEqual(0o120000, cleanup_mode(0o120711))
  196. def test_dir(self) -> None:
  197. self.assertModeEqual(0o040000, cleanup_mode(0o40531))
  198. def test_submodule(self) -> None:
  199. self.assertModeEqual(0o160000, cleanup_mode(0o160744))
  200. class WriteCacheTimeTests(TestCase):
  201. def test_write_string(self) -> None:
  202. f = BytesIO()
  203. self.assertRaises(TypeError, write_cache_time, f, "foo")
  204. def test_write_int(self) -> None:
  205. f = BytesIO()
  206. write_cache_time(f, 434343)
  207. self.assertEqual(struct.pack(">LL", 434343, 0), f.getvalue())
  208. def test_write_tuple(self) -> None:
  209. f = BytesIO()
  210. write_cache_time(f, (434343, 21))
  211. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  212. def test_write_float(self) -> None:
  213. f = BytesIO()
  214. write_cache_time(f, 434343.000000021)
  215. self.assertEqual(struct.pack(">LL", 434343, 21), f.getvalue())
  216. class IndexEntryFromStatTests(TestCase):
  217. def test_simple(self) -> None:
  218. st = os.stat_result(
  219. (
  220. 16877,
  221. 131078,
  222. 64769,
  223. 154,
  224. 1000,
  225. 1000,
  226. 12288,
  227. 1323629595,
  228. 1324180496,
  229. 1324180496,
  230. )
  231. )
  232. entry = index_entry_from_stat(st, b"22" * 20)
  233. self.assertEqual(
  234. entry,
  235. IndexEntry(
  236. 1324180496,
  237. 1324180496,
  238. 64769,
  239. 131078,
  240. 16384,
  241. 1000,
  242. 1000,
  243. 12288,
  244. b"2222222222222222222222222222222222222222",
  245. ),
  246. )
  247. def test_override_mode(self) -> None:
  248. st = os.stat_result(
  249. (
  250. stat.S_IFREG + 0o644,
  251. 131078,
  252. 64769,
  253. 154,
  254. 1000,
  255. 1000,
  256. 12288,
  257. 1323629595,
  258. 1324180496,
  259. 1324180496,
  260. )
  261. )
  262. entry = index_entry_from_stat(st, b"22" * 20, mode=stat.S_IFREG + 0o755)
  263. self.assertEqual(
  264. entry,
  265. IndexEntry(
  266. 1324180496,
  267. 1324180496,
  268. 64769,
  269. 131078,
  270. 33261,
  271. 1000,
  272. 1000,
  273. 12288,
  274. b"2222222222222222222222222222222222222222",
  275. ),
  276. )
  277. class BuildIndexTests(TestCase):
  278. def assertReasonableIndexEntry(self, index_entry, mode, filesize, sha) -> None:
  279. self.assertEqual(index_entry.mode, mode) # mode
  280. self.assertEqual(index_entry.size, filesize) # filesize
  281. self.assertEqual(index_entry.sha, sha) # sha
  282. def assertFileContents(self, path, contents, symlink=False) -> None:
  283. if symlink:
  284. self.assertEqual(os.readlink(path), contents)
  285. else:
  286. with open(path, "rb") as f:
  287. self.assertEqual(f.read(), contents)
  288. def test_empty(self) -> None:
  289. repo_dir = tempfile.mkdtemp()
  290. self.addCleanup(shutil.rmtree, repo_dir)
  291. with Repo.init(repo_dir) as repo:
  292. tree = Tree()
  293. repo.object_store.add_object(tree)
  294. build_index_from_tree(
  295. repo.path, repo.index_path(), repo.object_store, tree.id
  296. )
  297. # Verify index entries
  298. index = repo.open_index()
  299. self.assertEqual(len(index), 0)
  300. # Verify no files
  301. self.assertEqual([".git"], os.listdir(repo.path))
  302. def test_git_dir(self) -> None:
  303. repo_dir = tempfile.mkdtemp()
  304. self.addCleanup(shutil.rmtree, repo_dir)
  305. with Repo.init(repo_dir) as repo:
  306. # Populate repo
  307. filea = Blob.from_string(b"file a")
  308. filee = Blob.from_string(b"d")
  309. tree = Tree()
  310. tree[b".git/a"] = (stat.S_IFREG | 0o644, filea.id)
  311. tree[b"c/e"] = (stat.S_IFREG | 0o644, filee.id)
  312. repo.object_store.add_objects([(o, None) for o in [filea, filee, tree]])
  313. build_index_from_tree(
  314. repo.path, repo.index_path(), repo.object_store, tree.id
  315. )
  316. # Verify index entries
  317. index = repo.open_index()
  318. self.assertEqual(len(index), 1)
  319. # filea
  320. apath = os.path.join(repo.path, ".git", "a")
  321. self.assertFalse(os.path.exists(apath))
  322. # filee
  323. epath = os.path.join(repo.path, "c", "e")
  324. self.assertTrue(os.path.exists(epath))
  325. self.assertReasonableIndexEntry(
  326. index[b"c/e"], stat.S_IFREG | 0o644, 1, filee.id
  327. )
  328. self.assertFileContents(epath, b"d")
  329. def test_nonempty(self) -> None:
  330. repo_dir = tempfile.mkdtemp()
  331. self.addCleanup(shutil.rmtree, repo_dir)
  332. with Repo.init(repo_dir) as repo:
  333. # Populate repo
  334. filea = Blob.from_string(b"file a")
  335. fileb = Blob.from_string(b"file b")
  336. filed = Blob.from_string(b"file d")
  337. tree = Tree()
  338. tree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
  339. tree[b"b"] = (stat.S_IFREG | 0o644, fileb.id)
  340. tree[b"c/d"] = (stat.S_IFREG | 0o644, filed.id)
  341. repo.object_store.add_objects(
  342. [(o, None) for o in [filea, fileb, filed, tree]]
  343. )
  344. build_index_from_tree(
  345. repo.path, repo.index_path(), repo.object_store, tree.id
  346. )
  347. # Verify index entries
  348. index = repo.open_index()
  349. self.assertEqual(len(index), 3)
  350. # filea
  351. apath = os.path.join(repo.path, "a")
  352. self.assertTrue(os.path.exists(apath))
  353. self.assertReasonableIndexEntry(
  354. index[b"a"], stat.S_IFREG | 0o644, 6, filea.id
  355. )
  356. self.assertFileContents(apath, b"file a")
  357. # fileb
  358. bpath = os.path.join(repo.path, "b")
  359. self.assertTrue(os.path.exists(bpath))
  360. self.assertReasonableIndexEntry(
  361. index[b"b"], stat.S_IFREG | 0o644, 6, fileb.id
  362. )
  363. self.assertFileContents(bpath, b"file b")
  364. # filed
  365. dpath = os.path.join(repo.path, "c", "d")
  366. self.assertTrue(os.path.exists(dpath))
  367. self.assertReasonableIndexEntry(
  368. index[b"c/d"], stat.S_IFREG | 0o644, 6, filed.id
  369. )
  370. self.assertFileContents(dpath, b"file d")
  371. # Verify no extra files
  372. self.assertEqual([".git", "a", "b", "c"], sorted(os.listdir(repo.path)))
  373. self.assertEqual(["d"], sorted(os.listdir(os.path.join(repo.path, "c"))))
  374. @skipIf(not getattr(os, "sync", None), "Requires sync support")
  375. def test_norewrite(self) -> None:
  376. repo_dir = tempfile.mkdtemp()
  377. self.addCleanup(shutil.rmtree, repo_dir)
  378. with Repo.init(repo_dir) as repo:
  379. # Populate repo
  380. filea = Blob.from_string(b"file a")
  381. filea_path = os.path.join(repo_dir, "a")
  382. tree = Tree()
  383. tree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
  384. repo.object_store.add_objects([(o, None) for o in [filea, tree]])
  385. # First Write
  386. build_index_from_tree(
  387. repo.path, repo.index_path(), repo.object_store, tree.id
  388. )
  389. # Use sync as metadata can be cached on some FS
  390. os.sync()
  391. mtime = os.stat(filea_path).st_mtime
  392. # Test Rewrite
  393. build_index_from_tree(
  394. repo.path, repo.index_path(), repo.object_store, tree.id
  395. )
  396. os.sync()
  397. self.assertEqual(mtime, os.stat(filea_path).st_mtime)
  398. # Modify content
  399. with open(filea_path, "wb") as fh:
  400. fh.write(b"test a")
  401. os.sync()
  402. mtime = os.stat(filea_path).st_mtime
  403. # Test rewrite
  404. build_index_from_tree(
  405. repo.path, repo.index_path(), repo.object_store, tree.id
  406. )
  407. os.sync()
  408. with open(filea_path, "rb") as fh:
  409. self.assertEqual(b"file a", fh.read())
  410. @skipIf(not can_symlink(), "Requires symlink support")
  411. def test_symlink(self) -> None:
  412. repo_dir = tempfile.mkdtemp()
  413. self.addCleanup(shutil.rmtree, repo_dir)
  414. with Repo.init(repo_dir) as repo:
  415. # Populate repo
  416. filed = Blob.from_string(b"file d")
  417. filee = Blob.from_string(b"d")
  418. tree = Tree()
  419. tree[b"c/d"] = (stat.S_IFREG | 0o644, filed.id)
  420. tree[b"c/e"] = (stat.S_IFLNK, filee.id) # symlink
  421. repo.object_store.add_objects([(o, None) for o in [filed, filee, tree]])
  422. build_index_from_tree(
  423. repo.path, repo.index_path(), repo.object_store, tree.id
  424. )
  425. # Verify index entries
  426. index = repo.open_index()
  427. # symlink to d
  428. epath = os.path.join(repo.path, "c", "e")
  429. self.assertTrue(os.path.exists(epath))
  430. self.assertReasonableIndexEntry(
  431. index[b"c/e"],
  432. stat.S_IFLNK,
  433. 0 if sys.platform == "win32" else 1,
  434. filee.id,
  435. )
  436. self.assertFileContents(epath, "d", symlink=True)
  437. def test_no_decode_encode(self) -> None:
  438. repo_dir = tempfile.mkdtemp()
  439. repo_dir_bytes = os.fsencode(repo_dir)
  440. self.addCleanup(shutil.rmtree, repo_dir)
  441. with Repo.init(repo_dir) as repo:
  442. # Populate repo
  443. file = Blob.from_string(b"foo")
  444. tree = Tree()
  445. latin1_name = "À".encode("latin1")
  446. try:
  447. latin1_path = os.path.join(repo_dir_bytes, latin1_name)
  448. except UnicodeDecodeError:
  449. self.skipTest("can not decode as latin1")
  450. utf8_name = "À".encode()
  451. utf8_path = os.path.join(repo_dir_bytes, utf8_name)
  452. tree[latin1_name] = (stat.S_IFREG | 0o644, file.id)
  453. tree[utf8_name] = (stat.S_IFREG | 0o644, file.id)
  454. repo.object_store.add_objects([(o, None) for o in [file, tree]])
  455. try:
  456. build_index_from_tree(
  457. repo.path, repo.index_path(), repo.object_store, tree.id
  458. )
  459. except OSError as e:
  460. if e.errno == 92 and sys.platform == "darwin":
  461. # Our filename isn't supported by the platform :(
  462. self.skipTest(f"can not write filename {e.filename!r}")
  463. else:
  464. raise
  465. except UnicodeDecodeError:
  466. # This happens e.g. with python3.6 on Windows.
  467. # It implicitly decodes using utf8, which doesn't work.
  468. self.skipTest("can not implicitly convert as utf8")
  469. # Verify index entries
  470. index = repo.open_index()
  471. self.assertIn(latin1_name, index)
  472. self.assertIn(utf8_name, index)
  473. self.assertTrue(os.path.exists(latin1_path))
  474. self.assertTrue(os.path.exists(utf8_path))
  475. def test_git_submodule(self) -> None:
  476. repo_dir = tempfile.mkdtemp()
  477. self.addCleanup(shutil.rmtree, repo_dir)
  478. with Repo.init(repo_dir) as repo:
  479. filea = Blob.from_string(b"file alalala")
  480. subtree = Tree()
  481. subtree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
  482. c = Commit()
  483. c.tree = subtree.id
  484. c.committer = c.author = b"Somebody <somebody@example.com>"
  485. c.commit_time = c.author_time = 42342
  486. c.commit_timezone = c.author_timezone = 0
  487. c.parents = []
  488. c.message = b"Subcommit"
  489. tree = Tree()
  490. tree[b"c"] = (S_IFGITLINK, c.id)
  491. repo.object_store.add_objects([(o, None) for o in [tree]])
  492. build_index_from_tree(
  493. repo.path, repo.index_path(), repo.object_store, tree.id
  494. )
  495. # Verify index entries
  496. index = repo.open_index()
  497. self.assertEqual(len(index), 1)
  498. # filea
  499. apath = os.path.join(repo.path, "c/a")
  500. self.assertFalse(os.path.exists(apath))
  501. # dir c
  502. cpath = os.path.join(repo.path, "c")
  503. self.assertTrue(os.path.isdir(cpath))
  504. self.assertEqual(index[b"c"].mode, S_IFGITLINK) # mode
  505. self.assertEqual(index[b"c"].sha, c.id) # sha
  506. def test_git_submodule_exists(self) -> None:
  507. repo_dir = tempfile.mkdtemp()
  508. self.addCleanup(shutil.rmtree, repo_dir)
  509. with Repo.init(repo_dir) as repo:
  510. filea = Blob.from_string(b"file alalala")
  511. subtree = Tree()
  512. subtree[b"a"] = (stat.S_IFREG | 0o644, filea.id)
  513. c = Commit()
  514. c.tree = subtree.id
  515. c.committer = c.author = b"Somebody <somebody@example.com>"
  516. c.commit_time = c.author_time = 42342
  517. c.commit_timezone = c.author_timezone = 0
  518. c.parents = []
  519. c.message = b"Subcommit"
  520. tree = Tree()
  521. tree[b"c"] = (S_IFGITLINK, c.id)
  522. os.mkdir(os.path.join(repo_dir, "c"))
  523. repo.object_store.add_objects([(o, None) for o in [tree]])
  524. build_index_from_tree(
  525. repo.path, repo.index_path(), repo.object_store, tree.id
  526. )
  527. # Verify index entries
  528. index = repo.open_index()
  529. self.assertEqual(len(index), 1)
  530. # filea
  531. apath = os.path.join(repo.path, "c/a")
  532. self.assertFalse(os.path.exists(apath))
  533. # dir c
  534. cpath = os.path.join(repo.path, "c")
  535. self.assertTrue(os.path.isdir(cpath))
  536. self.assertEqual(index[b"c"].mode, S_IFGITLINK) # mode
  537. self.assertEqual(index[b"c"].sha, c.id) # sha
  538. class GetUnstagedChangesTests(TestCase):
  539. def test_get_unstaged_changes(self) -> None:
  540. """Unit test for get_unstaged_changes."""
  541. repo_dir = tempfile.mkdtemp()
  542. self.addCleanup(shutil.rmtree, repo_dir)
  543. with Repo.init(repo_dir) as repo:
  544. # Commit a dummy file then modify it
  545. foo1_fullpath = os.path.join(repo_dir, "foo1")
  546. with open(foo1_fullpath, "wb") as f:
  547. f.write(b"origstuff")
  548. foo2_fullpath = os.path.join(repo_dir, "foo2")
  549. with open(foo2_fullpath, "wb") as f:
  550. f.write(b"origstuff")
  551. repo.stage(["foo1", "foo2"])
  552. repo.do_commit(
  553. b"test status",
  554. author=b"author <email>",
  555. committer=b"committer <email>",
  556. )
  557. with open(foo1_fullpath, "wb") as f:
  558. f.write(b"newstuff")
  559. # modify access and modify time of path
  560. os.utime(foo1_fullpath, (0, 0))
  561. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  562. self.assertEqual(list(changes), [b"foo1"])
  563. def test_get_unstaged_deleted_changes(self) -> None:
  564. """Unit test for get_unstaged_changes."""
  565. repo_dir = tempfile.mkdtemp()
  566. self.addCleanup(shutil.rmtree, repo_dir)
  567. with Repo.init(repo_dir) as repo:
  568. # Commit a dummy file then remove it
  569. foo1_fullpath = os.path.join(repo_dir, "foo1")
  570. with open(foo1_fullpath, "wb") as f:
  571. f.write(b"origstuff")
  572. repo.stage(["foo1"])
  573. repo.do_commit(
  574. b"test status",
  575. author=b"author <email>",
  576. committer=b"committer <email>",
  577. )
  578. os.unlink(foo1_fullpath)
  579. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  580. self.assertEqual(list(changes), [b"foo1"])
  581. def test_get_unstaged_changes_removed_replaced_by_directory(self) -> None:
  582. """Unit test for get_unstaged_changes."""
  583. repo_dir = tempfile.mkdtemp()
  584. self.addCleanup(shutil.rmtree, repo_dir)
  585. with Repo.init(repo_dir) as repo:
  586. # Commit a dummy file then modify it
  587. foo1_fullpath = os.path.join(repo_dir, "foo1")
  588. with open(foo1_fullpath, "wb") as f:
  589. f.write(b"origstuff")
  590. repo.stage(["foo1"])
  591. repo.do_commit(
  592. b"test status",
  593. author=b"author <email>",
  594. committer=b"committer <email>",
  595. )
  596. os.remove(foo1_fullpath)
  597. os.mkdir(foo1_fullpath)
  598. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  599. self.assertEqual(list(changes), [b"foo1"])
  600. @skipIf(not can_symlink(), "Requires symlink support")
  601. def test_get_unstaged_changes_removed_replaced_by_link(self) -> None:
  602. """Unit test for get_unstaged_changes."""
  603. repo_dir = tempfile.mkdtemp()
  604. self.addCleanup(shutil.rmtree, repo_dir)
  605. with Repo.init(repo_dir) as repo:
  606. # Commit a dummy file then modify it
  607. foo1_fullpath = os.path.join(repo_dir, "foo1")
  608. with open(foo1_fullpath, "wb") as f:
  609. f.write(b"origstuff")
  610. repo.stage(["foo1"])
  611. repo.do_commit(
  612. b"test status",
  613. author=b"author <email>",
  614. committer=b"committer <email>",
  615. )
  616. os.remove(foo1_fullpath)
  617. os.symlink(os.path.dirname(foo1_fullpath), foo1_fullpath)
  618. changes = get_unstaged_changes(repo.open_index(), repo_dir)
  619. self.assertEqual(list(changes), [b"foo1"])
  620. class TestValidatePathElement(TestCase):
  621. def test_default(self) -> None:
  622. self.assertTrue(validate_path_element_default(b"bla"))
  623. self.assertTrue(validate_path_element_default(b".bla"))
  624. self.assertFalse(validate_path_element_default(b".git"))
  625. self.assertFalse(validate_path_element_default(b".giT"))
  626. self.assertFalse(validate_path_element_default(b".."))
  627. self.assertTrue(validate_path_element_default(b"git~1"))
  628. def test_ntfs(self) -> None:
  629. self.assertTrue(validate_path_element_ntfs(b"bla"))
  630. self.assertTrue(validate_path_element_ntfs(b".bla"))
  631. self.assertFalse(validate_path_element_ntfs(b".git"))
  632. self.assertFalse(validate_path_element_ntfs(b".giT"))
  633. self.assertFalse(validate_path_element_ntfs(b".."))
  634. self.assertFalse(validate_path_element_ntfs(b"git~1"))
  635. class TestTreeFSPathConversion(TestCase):
  636. def test_tree_to_fs_path(self) -> None:
  637. tree_path = "délwíçh/foo".encode()
  638. fs_path = _tree_to_fs_path(b"/prefix/path", tree_path)
  639. self.assertEqual(
  640. fs_path,
  641. os.fsencode(os.path.join("/prefix/path", "délwíçh", "foo")),
  642. )
  643. def test_fs_to_tree_path_str(self) -> None:
  644. fs_path = os.path.join(os.path.join("délwíçh", "foo"))
  645. tree_path = _fs_to_tree_path(fs_path)
  646. self.assertEqual(tree_path, "délwíçh/foo".encode())
  647. def test_fs_to_tree_path_bytes(self) -> None:
  648. fs_path = os.path.join(os.fsencode(os.path.join("délwíçh", "foo")))
  649. tree_path = _fs_to_tree_path(fs_path)
  650. self.assertEqual(tree_path, "délwíçh/foo".encode())