test_index.py 26 KB

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