test_index.py 26 KB

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