test_index.py 26 KB

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