test_index.py 26 KB

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