2
0

test_index.py 26 KB

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