test_porcelain.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. # test_porcelain.py -- porcelain tests
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) a later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for dulwich.porcelain."""
  19. from io import BytesIO
  20. import os
  21. import shutil
  22. import tarfile
  23. import tempfile
  24. from dulwich import porcelain
  25. from dulwich.diff_tree import tree_changes
  26. from dulwich.objects import (
  27. Blob,
  28. Tag,
  29. Tree,
  30. )
  31. from dulwich.repo import Repo
  32. from dulwich.tests import (
  33. TestCase,
  34. )
  35. from dulwich.tests.compat.utils import require_git_version
  36. from dulwich.tests.utils import (
  37. build_commit_graph,
  38. make_object,
  39. )
  40. class PorcelainTestCase(TestCase):
  41. def setUp(self):
  42. super(TestCase, self).setUp()
  43. repo_dir = tempfile.mkdtemp()
  44. self.addCleanup(shutil.rmtree, repo_dir)
  45. self.repo = Repo.init(repo_dir)
  46. class ArchiveTests(PorcelainTestCase):
  47. """Tests for the archive command."""
  48. def test_simple(self):
  49. # TODO(jelmer): Remove this once dulwich has its own implementation of archive.
  50. require_git_version((1, 5, 0))
  51. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  52. self.repo.refs[b"refs/heads/master"] = c3.id
  53. out = BytesIO()
  54. err = BytesIO()
  55. porcelain.archive(self.repo.path, b"refs/heads/master", outstream=out,
  56. errstream=err)
  57. self.assertEqual(b"", err.getvalue())
  58. tf = tarfile.TarFile(fileobj=out)
  59. self.addCleanup(tf.close)
  60. self.assertEqual([], tf.getnames())
  61. class UpdateServerInfoTests(PorcelainTestCase):
  62. def test_simple(self):
  63. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  64. [3, 1, 2]])
  65. self.repo.refs[b"refs/heads/foo"] = c3.id
  66. porcelain.update_server_info(self.repo.path)
  67. self.assertTrue(os.path.exists(os.path.join(self.repo.controldir(),
  68. b'info', b'refs')))
  69. class CommitTests(PorcelainTestCase):
  70. def test_custom_author(self):
  71. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  72. [3, 1, 2]])
  73. self.repo.refs[b"refs/heads/foo"] = c3.id
  74. sha = porcelain.commit(self.repo.path, message=b"Some message",
  75. author=b"Joe <joe@example.com>", committer=b"Bob <bob@example.com>")
  76. self.assertTrue(isinstance(sha, bytes))
  77. self.assertEqual(len(sha), 40)
  78. class CloneTests(PorcelainTestCase):
  79. def test_simple_local(self):
  80. f1_1 = make_object(Blob, data=b'f1')
  81. commit_spec = [[1], [2, 1], [3, 1, 2]]
  82. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  83. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  84. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  85. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  86. commit_spec, trees)
  87. self.repo.refs[b"refs/heads/master"] = c3.id
  88. target_path = tempfile.mkdtemp()
  89. errstream = BytesIO()
  90. self.addCleanup(shutil.rmtree, target_path)
  91. r = porcelain.clone(self.repo.path, target_path,
  92. checkout=False, errstream=errstream)
  93. self.assertEqual(r.path, target_path)
  94. self.assertEqual(Repo(target_path).head(), c3.id)
  95. self.assertTrue(b'f1' not in os.listdir(target_path))
  96. self.assertTrue(b'f2' not in os.listdir(target_path))
  97. def test_simple_local_with_checkout(self):
  98. f1_1 = make_object(Blob, data=b'f1')
  99. commit_spec = [[1], [2, 1], [3, 1, 2]]
  100. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  101. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  102. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  103. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  104. commit_spec, trees)
  105. self.repo.refs[b"refs/heads/master"] = c3.id
  106. target_path = tempfile.mkdtemp()
  107. errstream = BytesIO()
  108. self.addCleanup(shutil.rmtree, target_path)
  109. r = porcelain.clone(self.repo.path, target_path,
  110. checkout=True, errstream=errstream)
  111. self.assertEqual(r.path, target_path)
  112. self.assertEqual(Repo(target_path).head(), c3.id)
  113. self.assertTrue('f1' in os.listdir(target_path))
  114. self.assertTrue('f2' in os.listdir(target_path))
  115. def test_bare_local_with_checkout(self):
  116. f1_1 = make_object(Blob, data=b'f1')
  117. commit_spec = [[1], [2, 1], [3, 1, 2]]
  118. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  119. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  120. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  121. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  122. commit_spec, trees)
  123. self.repo.refs[b"refs/heads/master"] = c3.id
  124. target_path = tempfile.mkdtemp()
  125. errstream = BytesIO()
  126. self.addCleanup(shutil.rmtree, target_path)
  127. r = porcelain.clone(self.repo.path, target_path,
  128. bare=True, errstream=errstream)
  129. self.assertEqual(r.path, target_path)
  130. self.assertEqual(Repo(target_path).head(), c3.id)
  131. self.assertFalse(b'f1' in os.listdir(target_path))
  132. self.assertFalse(b'f2' in os.listdir(target_path))
  133. def test_no_checkout_with_bare(self):
  134. f1_1 = make_object(Blob, data=b'f1')
  135. commit_spec = [[1]]
  136. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]}
  137. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  138. self.repo.refs[b"refs/heads/master"] = c1.id
  139. target_path = tempfile.mkdtemp()
  140. errstream = BytesIO()
  141. self.addCleanup(shutil.rmtree, target_path)
  142. self.assertRaises(ValueError, porcelain.clone, self.repo.path,
  143. target_path, checkout=True, bare=True, errstream=errstream)
  144. class InitTests(TestCase):
  145. def test_non_bare(self):
  146. repo_dir = tempfile.mkdtemp()
  147. self.addCleanup(shutil.rmtree, repo_dir)
  148. porcelain.init(repo_dir)
  149. def test_bare(self):
  150. repo_dir = tempfile.mkdtemp()
  151. self.addCleanup(shutil.rmtree, repo_dir)
  152. porcelain.init(repo_dir, bare=True)
  153. class AddTests(PorcelainTestCase):
  154. def test_add_default_paths(self):
  155. # create a file for initial commit
  156. with open(os.path.join(self.repo.path, 'blah'), 'w') as f:
  157. f.write("\n")
  158. porcelain.add(repo=self.repo.path, paths=['blah'])
  159. porcelain.commit(repo=self.repo.path, message=b'test',
  160. author=b'test', committer=b'test')
  161. # Add a second test file and a file in a directory
  162. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  163. f.write("\n")
  164. os.mkdir(os.path.join(self.repo.path, 'adir'))
  165. with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f:
  166. f.write("\n")
  167. porcelain.add(self.repo.path)
  168. # Check that foo was added and nothing in .git was modified
  169. index = self.repo.open_index()
  170. self.assertEqual(sorted(index), [b'adir/afile', b'blah', b'foo'])
  171. def test_add_file(self):
  172. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  173. f.write("BAR")
  174. porcelain.add(self.repo.path, paths=["foo"])
  175. class RemoveTests(PorcelainTestCase):
  176. def test_remove_file(self):
  177. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  178. f.write("BAR")
  179. porcelain.add(self.repo.path, paths=["foo"])
  180. porcelain.rm(self.repo.path, paths=["foo"])
  181. class LogTests(PorcelainTestCase):
  182. def test_simple(self):
  183. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  184. [3, 1, 2]])
  185. self.repo.refs[b"HEAD"] = c3.id
  186. outstream = BytesIO()
  187. porcelain.log(self.repo.path, outstream=outstream)
  188. self.assertEqual(3, outstream.getvalue().count(b"-" * 50))
  189. def test_max_entries(self):
  190. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  191. [3, 1, 2]])
  192. self.repo.refs[b"HEAD"] = c3.id
  193. outstream = BytesIO()
  194. porcelain.log(self.repo.path, outstream=outstream, max_entries=1)
  195. self.assertEqual(1, outstream.getvalue().count(b"-" * 50))
  196. class ShowTests(PorcelainTestCase):
  197. def test_nolist(self):
  198. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  199. [3, 1, 2]])
  200. self.repo.refs[b"HEAD"] = c3.id
  201. outstream = BytesIO()
  202. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  203. self.assertTrue(outstream.getvalue().startswith(b"-" * 50))
  204. def test_simple(self):
  205. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  206. [3, 1, 2]])
  207. self.repo.refs[b"HEAD"] = c3.id
  208. outstream = BytesIO()
  209. porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
  210. self.assertTrue(outstream.getvalue().startswith(b"-" * 50))
  211. def test_blob(self):
  212. b = Blob.from_string(b"The Foo\n")
  213. self.repo.object_store.add_object(b)
  214. outstream = BytesIO()
  215. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  216. self.assertEqual(outstream.getvalue(), b"The Foo\n")
  217. class SymbolicRefTests(PorcelainTestCase):
  218. def test_set_wrong_symbolic_ref(self):
  219. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  220. [3, 1, 2]])
  221. self.repo.refs[b"HEAD"] = c3.id
  222. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path, b'foobar')
  223. def test_set_force_wrong_symbolic_ref(self):
  224. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  225. [3, 1, 2]])
  226. self.repo.refs[b"HEAD"] = c3.id
  227. porcelain.symbolic_ref(self.repo.path, b'force_foobar', force=True)
  228. #test if we actually changed the file
  229. with self.repo.get_named_file(b'HEAD') as f:
  230. new_ref = f.read()
  231. self.assertEqual(new_ref, b'ref: refs/heads/force_foobar\n')
  232. def test_set_symbolic_ref(self):
  233. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  234. [3, 1, 2]])
  235. self.repo.refs[b"HEAD"] = c3.id
  236. porcelain.symbolic_ref(self.repo.path, b'master')
  237. def test_set_symbolic_ref_other_than_master(self):
  238. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  239. [3, 1, 2]], attrs=dict(refs='develop'))
  240. self.repo.refs[b"HEAD"] = c3.id
  241. self.repo.refs[b"refs/heads/develop"] = c3.id
  242. porcelain.symbolic_ref(self.repo.path, b'develop')
  243. #test if we actually changed the file
  244. with self.repo.get_named_file(b'HEAD') as f:
  245. new_ref = f.read()
  246. self.assertEqual(new_ref, b'ref: refs/heads/develop\n')
  247. class DiffTreeTests(PorcelainTestCase):
  248. def test_empty(self):
  249. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  250. [3, 1, 2]])
  251. self.repo.refs[b"HEAD"] = c3.id
  252. outstream = BytesIO()
  253. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree, outstream=outstream)
  254. self.assertEqual(outstream.getvalue(), b"")
  255. class CommitTreeTests(PorcelainTestCase):
  256. def test_simple(self):
  257. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  258. [3, 1, 2]])
  259. b = Blob()
  260. b.data = b"foo the bar"
  261. t = Tree()
  262. t.add(b"somename", 0o100644, b.id)
  263. self.repo.object_store.add_object(t)
  264. self.repo.object_store.add_object(b)
  265. sha = porcelain.commit_tree(
  266. self.repo.path, t.id, message=b"Withcommit.",
  267. author=b"Joe <joe@example.com>",
  268. committer=b"Jane <jane@example.com>")
  269. self.assertTrue(isinstance(sha, bytes))
  270. self.assertEqual(len(sha), 40)
  271. class RevListTests(PorcelainTestCase):
  272. def test_simple(self):
  273. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  274. [3, 1, 2]])
  275. outstream = BytesIO()
  276. porcelain.rev_list(
  277. self.repo.path, [c3.id], outstream=outstream)
  278. self.assertEqual(
  279. c3.id + b"\n" +
  280. c2.id + b"\n" +
  281. c1.id + b"\n",
  282. outstream.getvalue())
  283. class TagCreateTests(PorcelainTestCase):
  284. def test_annotated(self):
  285. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  286. [3, 1, 2]])
  287. self.repo.refs[b"HEAD"] = c3.id
  288. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  289. b'bar', annotated=True)
  290. tags = self.repo.refs.as_dict(b"refs/tags")
  291. self.assertEqual(list(tags.keys()), [b"tryme"])
  292. tag = self.repo[b'refs/tags/tryme']
  293. self.assertTrue(isinstance(tag, Tag))
  294. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  295. self.assertEqual(b"bar", tag.message)
  296. def test_unannotated(self):
  297. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  298. [3, 1, 2]])
  299. self.repo.refs[b"HEAD"] = c3.id
  300. porcelain.tag_create(self.repo.path, b"tryme", annotated=False)
  301. tags = self.repo.refs.as_dict(b"refs/tags")
  302. self.assertEqual(list(tags.keys()), [b"tryme"])
  303. self.repo[b'refs/tags/tryme']
  304. self.assertEqual(list(tags.values()), [self.repo.head()])
  305. class TagListTests(PorcelainTestCase):
  306. def test_empty(self):
  307. tags = porcelain.tag_list(self.repo.path)
  308. self.assertEqual([], tags)
  309. def test_simple(self):
  310. self.repo.refs[b"refs/tags/foo"] = b"aa" * 20
  311. self.repo.refs[b"refs/tags/bar/bla"] = b"bb" * 20
  312. tags = porcelain.tag_list(self.repo.path)
  313. self.assertEqual([b"bar/bla", b"foo"], tags)
  314. class TagDeleteTests(PorcelainTestCase):
  315. def test_simple(self):
  316. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  317. self.repo[b"HEAD"] = c1.id
  318. porcelain.tag_create(self.repo, b'foo')
  319. self.assertTrue(b"foo" in porcelain.tag_list(self.repo))
  320. porcelain.tag_delete(self.repo, b'foo')
  321. self.assertFalse(b"foo" in porcelain.tag_list(self.repo))
  322. class ResetTests(PorcelainTestCase):
  323. def test_hard_head(self):
  324. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  325. f.write("BAR")
  326. porcelain.add(self.repo.path, paths=["foo"])
  327. porcelain.commit(self.repo.path, message=b"Some message",
  328. committer=b"Jane <jane@example.com>",
  329. author=b"John <john@example.com>")
  330. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  331. f.write(b"OOH")
  332. porcelain.reset(self.repo, "hard", b"HEAD")
  333. index = self.repo.open_index()
  334. changes = list(tree_changes(self.repo,
  335. index.commit(self.repo.object_store),
  336. self.repo[b'HEAD'].tree))
  337. self.assertEqual([], changes)
  338. class PushTests(PorcelainTestCase):
  339. def test_simple(self):
  340. """
  341. Basic test of porcelain push where self.repo is the remote. First
  342. clone the remote, commit a file to the clone, then push the changes
  343. back to the remote.
  344. """
  345. outstream = BytesIO()
  346. errstream = BytesIO()
  347. porcelain.commit(repo=self.repo.path, message=b'init',
  348. author=b'', committer=b'')
  349. # Setup target repo cloned from temp test repo
  350. clone_path = tempfile.mkdtemp()
  351. porcelain.clone(self.repo.path, target=clone_path, errstream=errstream)
  352. # create a second file to be pushed back to origin
  353. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  354. porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)])
  355. porcelain.commit(repo=clone_path, message=b'push',
  356. author=b'', committer=b'')
  357. # Setup a non-checked out branch in the remote
  358. refs_path = b"refs/heads/foo"
  359. self.repo[refs_path] = self.repo[b'HEAD']
  360. # Push to the remote
  361. porcelain.push(clone_path, self.repo.path, refs_path, outstream=outstream,
  362. errstream=errstream)
  363. # Check that the target and source
  364. r_clone = Repo(clone_path)
  365. # Get the change in the target repo corresponding to the add
  366. # this will be in the foo branch.
  367. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree,
  368. self.repo[b'refs/heads/foo'].tree))[0]
  369. self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id)
  370. self.assertEqual(os.path.basename(fullpath), change.new.path.decode('ascii'))
  371. class PullTests(PorcelainTestCase):
  372. def test_simple(self):
  373. outstream = BytesIO()
  374. errstream = BytesIO()
  375. # create a file for initial commit
  376. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  377. filename = os.path.basename(fullpath)
  378. porcelain.add(repo=self.repo.path, paths=filename)
  379. porcelain.commit(repo=self.repo.path, message=b'test',
  380. author=b'test', committer=b'test')
  381. # Setup target repo
  382. target_path = tempfile.mkdtemp()
  383. porcelain.clone(self.repo.path, target=target_path, errstream=errstream)
  384. # create a second file to be pushed
  385. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  386. filename = os.path.basename(fullpath)
  387. porcelain.add(repo=self.repo.path, paths=filename)
  388. porcelain.commit(repo=self.repo.path, message=b'test2',
  389. author=b'test2', committer=b'test2')
  390. # Pull changes into the cloned repo
  391. porcelain.pull(target_path, self.repo.path, b'refs/heads/master',
  392. outstream=outstream, errstream=errstream)
  393. # Check the target repo for pushed changes
  394. r = Repo(target_path)
  395. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  396. class StatusTests(PorcelainTestCase):
  397. def test_status(self):
  398. """Integration test for `status` functionality."""
  399. # Commit a dummy file then modify it
  400. fullpath = os.path.join(self.repo.path, 'foo')
  401. with open(fullpath, 'w') as f:
  402. f.write('origstuff')
  403. porcelain.add(repo=self.repo.path, paths=['foo'])
  404. porcelain.commit(repo=self.repo.path, message=b'test status',
  405. author=b'', committer=b'')
  406. # modify access and modify time of path
  407. os.utime(fullpath, (0, 0))
  408. with open(fullpath, 'wb') as f:
  409. f.write(b'stuff')
  410. # Make a dummy file and stage it
  411. filename_add = 'bar'
  412. fullpath = os.path.join(self.repo.path, filename_add)
  413. with open(fullpath, 'w') as f:
  414. f.write('stuff')
  415. porcelain.add(repo=self.repo.path, paths=filename_add)
  416. results = porcelain.status(self.repo)
  417. self.assertEqual(results.staged['add'][0], filename_add.encode('ascii'))
  418. self.assertEqual(results.unstaged, [b'foo'])
  419. def test_get_tree_changes_add(self):
  420. """Unit test for get_tree_changes add."""
  421. # Make a dummy file, stage
  422. filename = 'bar'
  423. with open(os.path.join(self.repo.path, filename), 'w') as f:
  424. f.write('stuff')
  425. porcelain.add(repo=self.repo.path, paths=filename)
  426. porcelain.commit(repo=self.repo.path, message=b'test status',
  427. author=b'', committer=b'')
  428. filename = 'foo'
  429. with open(os.path.join(self.repo.path, filename), 'w') as f:
  430. f.write('stuff')
  431. porcelain.add(repo=self.repo.path, paths=filename)
  432. changes = porcelain.get_tree_changes(self.repo.path)
  433. self.assertEqual(changes['add'][0], filename.encode('ascii'))
  434. self.assertEqual(len(changes['add']), 1)
  435. self.assertEqual(len(changes['modify']), 0)
  436. self.assertEqual(len(changes['delete']), 0)
  437. def test_get_tree_changes_modify(self):
  438. """Unit test for get_tree_changes modify."""
  439. # Make a dummy file, stage, commit, modify
  440. filename = 'foo'
  441. fullpath = os.path.join(self.repo.path, filename)
  442. with open(fullpath, 'w') as f:
  443. f.write('stuff')
  444. porcelain.add(repo=self.repo.path, paths=filename)
  445. porcelain.commit(repo=self.repo.path, message=b'test status',
  446. author=b'', committer=b'')
  447. with open(fullpath, 'w') as f:
  448. f.write('otherstuff')
  449. porcelain.add(repo=self.repo.path, paths=filename)
  450. changes = porcelain.get_tree_changes(self.repo.path)
  451. self.assertEqual(changes['modify'][0], filename.encode('ascii'))
  452. self.assertEqual(len(changes['add']), 0)
  453. self.assertEqual(len(changes['modify']), 1)
  454. self.assertEqual(len(changes['delete']), 0)
  455. def test_get_tree_changes_delete(self):
  456. """Unit test for get_tree_changes delete."""
  457. # Make a dummy file, stage, commit, remove
  458. filename = 'foo'
  459. with open(os.path.join(self.repo.path, filename), 'w') as f:
  460. f.write('stuff')
  461. porcelain.add(repo=self.repo.path, paths=filename)
  462. porcelain.commit(repo=self.repo.path, message=b'test status',
  463. author=b'', committer=b'')
  464. porcelain.rm(repo=self.repo.path, paths=[filename])
  465. changes = porcelain.get_tree_changes(self.repo.path)
  466. self.assertEqual(changes['delete'][0], filename.encode('ascii'))
  467. self.assertEqual(len(changes['add']), 0)
  468. self.assertEqual(len(changes['modify']), 0)
  469. self.assertEqual(len(changes['delete']), 1)
  470. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  471. class UploadPackTests(PorcelainTestCase):
  472. """Tests for upload_pack."""
  473. def test_upload_pack(self):
  474. outf = BytesIO()
  475. exitcode = porcelain.upload_pack(self.repo.path, BytesIO(b"0000"), outf)
  476. outlines = outf.getvalue().splitlines()
  477. self.assertEqual([b"0000"], outlines)
  478. self.assertEqual(0, exitcode)
  479. class ReceivePackTests(PorcelainTestCase):
  480. """Tests for receive_pack."""
  481. def test_receive_pack(self):
  482. filename = 'foo'
  483. with open(os.path.join(self.repo.path, filename), 'w') as f:
  484. f.write('stuff')
  485. porcelain.add(repo=self.repo.path, paths=filename)
  486. self.repo.do_commit(message=b'test status',
  487. author=b'', committer=b'', author_timestamp=1402354300,
  488. commit_timestamp=1402354300, author_timezone=0, commit_timezone=0)
  489. outf = BytesIO()
  490. exitcode = porcelain.receive_pack(self.repo.path, BytesIO(b"0000"), outf)
  491. outlines = outf.getvalue().splitlines()
  492. self.assertEqual([
  493. b'005a9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00report-status '
  494. b'delete-refs side-band-64k',
  495. b'003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master',
  496. b'0000'], outlines)
  497. self.assertEqual(0, exitcode)
  498. class BranchListTests(PorcelainTestCase):
  499. def test_standard(self):
  500. self.assertEqual(set([]), set(porcelain.branch_list(self.repo)))
  501. def test_new_branch(self):
  502. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  503. self.repo[b"HEAD"] = c1.id
  504. porcelain.branch_create(self.repo, b"foo")
  505. self.assertEqual(
  506. set([b"master", b"foo"]),
  507. set(porcelain.branch_list(self.repo)))
  508. class BranchCreateTests(PorcelainTestCase):
  509. def test_branch_exists(self):
  510. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  511. self.repo[b"HEAD"] = c1.id
  512. porcelain.branch_create(self.repo, b"foo")
  513. self.assertRaises(KeyError, porcelain.branch_create, self.repo, b"foo")
  514. porcelain.branch_create(self.repo, b"foo", force=True)
  515. def test_new_branch(self):
  516. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  517. self.repo[b"HEAD"] = c1.id
  518. porcelain.branch_create(self.repo, b"foo")
  519. self.assertEqual(
  520. set([b"master", b"foo"]),
  521. set(porcelain.branch_list(self.repo)))
  522. class BranchDeleteTests(PorcelainTestCase):
  523. def test_simple(self):
  524. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  525. self.repo[b"HEAD"] = c1.id
  526. porcelain.branch_create(self.repo, b'foo')
  527. self.assertTrue(b"foo" in porcelain.branch_list(self.repo))
  528. porcelain.branch_delete(self.repo, b'foo')
  529. self.assertFalse(b"foo" in porcelain.branch_list(self.repo))
  530. class FetchTests(PorcelainTestCase):
  531. def test_simple(self):
  532. outstream = BytesIO()
  533. errstream = BytesIO()
  534. # create a file for initial commit
  535. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  536. filename = os.path.basename(fullpath)
  537. porcelain.add(repo=self.repo.path, paths=filename)
  538. porcelain.commit(repo=self.repo.path, message=b'test',
  539. author=b'test', committer=b'test')
  540. # Setup target repo
  541. target_path = tempfile.mkdtemp()
  542. target_repo = porcelain.clone(self.repo.path, target=target_path,
  543. errstream=errstream)
  544. # create a second file to be pushed
  545. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  546. filename = os.path.basename(fullpath)
  547. porcelain.add(repo=self.repo.path, paths=filename)
  548. porcelain.commit(repo=self.repo.path, message=b'test2',
  549. author=b'test2', committer=b'test2')
  550. self.assertFalse(self.repo[b'HEAD'].id in target_repo)
  551. # Fetch changes into the cloned repo
  552. porcelain.fetch(target_path, self.repo.path, outstream=outstream,
  553. errstream=errstream)
  554. # Check the target repo for pushed changes
  555. r = Repo(target_path)
  556. self.assertTrue(self.repo[b'HEAD'].id in r)