test_index.py 25 KB

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