test_index.py 26 KB

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