test_index.py 27 KB

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