test_index.py 26 KB

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