2
0

test_index.py 26 KB

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