test_porcelain.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. # test_porcelain.py -- porcelain tests
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Tests for dulwich.porcelain."""
  21. from io import BytesIO
  22. try:
  23. from StringIO import StringIO
  24. except ImportError:
  25. from io import StringIO
  26. import os
  27. import shutil
  28. import tarfile
  29. import tempfile
  30. import time
  31. from dulwich import porcelain
  32. from dulwich.diff_tree import tree_changes
  33. from dulwich.objects import (
  34. Blob,
  35. Tag,
  36. Tree,
  37. ZERO_SHA,
  38. )
  39. from dulwich.repo import Repo
  40. from dulwich.tests import (
  41. TestCase,
  42. )
  43. from dulwich.tests.utils import (
  44. build_commit_graph,
  45. make_object,
  46. )
  47. class PorcelainTestCase(TestCase):
  48. def setUp(self):
  49. super(PorcelainTestCase, self).setUp()
  50. repo_dir = tempfile.mkdtemp()
  51. self.addCleanup(shutil.rmtree, repo_dir)
  52. self.repo = Repo.init(repo_dir)
  53. def tearDown(self):
  54. super(PorcelainTestCase, self).tearDown()
  55. self.repo.close()
  56. class ArchiveTests(PorcelainTestCase):
  57. """Tests for the archive command."""
  58. def test_simple(self):
  59. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  60. self.repo.refs[b"refs/heads/master"] = c3.id
  61. out = BytesIO()
  62. err = BytesIO()
  63. porcelain.archive(self.repo.path, b"refs/heads/master", outstream=out,
  64. errstream=err)
  65. self.assertEqual(b"", err.getvalue())
  66. tf = tarfile.TarFile(fileobj=out)
  67. self.addCleanup(tf.close)
  68. self.assertEqual([], tf.getnames())
  69. class UpdateServerInfoTests(PorcelainTestCase):
  70. def test_simple(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. porcelain.update_server_info(self.repo.path)
  75. self.assertTrue(os.path.exists(os.path.join(self.repo.controldir(),
  76. 'info', 'refs')))
  77. class CommitTests(PorcelainTestCase):
  78. def test_custom_author(self):
  79. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  80. [3, 1, 2]])
  81. self.repo.refs[b"refs/heads/foo"] = c3.id
  82. sha = porcelain.commit(self.repo.path, message=b"Some message",
  83. author=b"Joe <joe@example.com>", committer=b"Bob <bob@example.com>")
  84. self.assertTrue(isinstance(sha, bytes))
  85. self.assertEqual(len(sha), 40)
  86. class CloneTests(PorcelainTestCase):
  87. def test_simple_local(self):
  88. f1_1 = make_object(Blob, data=b'f1')
  89. commit_spec = [[1], [2, 1], [3, 1, 2]]
  90. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  91. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  92. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  93. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  94. commit_spec, trees)
  95. self.repo.refs[b"refs/heads/master"] = c3.id
  96. self.repo.refs[b"refs/tags/foo"] = c3.id
  97. target_path = tempfile.mkdtemp()
  98. errstream = BytesIO()
  99. self.addCleanup(shutil.rmtree, target_path)
  100. r = porcelain.clone(self.repo.path, target_path,
  101. checkout=False, errstream=errstream)
  102. self.assertEqual(r.path, target_path)
  103. target_repo = Repo(target_path)
  104. self.assertEqual(target_repo.head(), c3.id)
  105. self.assertEqual(c3.id, target_repo.refs[b'refs/tags/foo'])
  106. self.assertTrue(b'f1' not in os.listdir(target_path))
  107. self.assertTrue(b'f2' not in os.listdir(target_path))
  108. c = r.get_config()
  109. encoded_path = self.repo.path
  110. if not isinstance(encoded_path, bytes):
  111. encoded_path = encoded_path.encode('utf-8')
  112. self.assertEqual(encoded_path, c.get((b'remote', b'origin'), b'url'))
  113. self.assertEqual(
  114. b'+refs/heads/*:refs/remotes/origin/*',
  115. c.get((b'remote', b'origin'), b'fetch'))
  116. def test_simple_local_with_checkout(self):
  117. f1_1 = make_object(Blob, data=b'f1')
  118. commit_spec = [[1], [2, 1], [3, 1, 2]]
  119. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  120. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  121. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  122. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  123. commit_spec, trees)
  124. self.repo.refs[b"refs/heads/master"] = c3.id
  125. target_path = tempfile.mkdtemp()
  126. errstream = BytesIO()
  127. self.addCleanup(shutil.rmtree, target_path)
  128. with porcelain.clone(self.repo.path, target_path,
  129. checkout=True,
  130. errstream=errstream) as r:
  131. self.assertEqual(r.path, target_path)
  132. with Repo(target_path) as r:
  133. self.assertEqual(r.head(), c3.id)
  134. self.assertTrue('f1' in os.listdir(target_path))
  135. self.assertTrue('f2' in os.listdir(target_path))
  136. def test_bare_local_with_checkout(self):
  137. f1_1 = make_object(Blob, data=b'f1')
  138. commit_spec = [[1], [2, 1], [3, 1, 2]]
  139. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  140. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  141. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  142. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  143. commit_spec, trees)
  144. self.repo.refs[b"refs/heads/master"] = c3.id
  145. target_path = tempfile.mkdtemp()
  146. errstream = BytesIO()
  147. self.addCleanup(shutil.rmtree, target_path)
  148. r = porcelain.clone(self.repo.path, target_path,
  149. bare=True, errstream=errstream)
  150. self.assertEqual(r.path, target_path)
  151. self.assertEqual(Repo(target_path).head(), c3.id)
  152. self.assertFalse(b'f1' in os.listdir(target_path))
  153. self.assertFalse(b'f2' in os.listdir(target_path))
  154. def test_no_checkout_with_bare(self):
  155. f1_1 = make_object(Blob, data=b'f1')
  156. commit_spec = [[1]]
  157. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]}
  158. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  159. self.repo.refs[b"refs/heads/master"] = c1.id
  160. target_path = tempfile.mkdtemp()
  161. errstream = BytesIO()
  162. self.addCleanup(shutil.rmtree, target_path)
  163. self.assertRaises(ValueError, porcelain.clone, self.repo.path,
  164. target_path, checkout=True, bare=True, errstream=errstream)
  165. class InitTests(TestCase):
  166. def test_non_bare(self):
  167. repo_dir = tempfile.mkdtemp()
  168. self.addCleanup(shutil.rmtree, repo_dir)
  169. porcelain.init(repo_dir)
  170. def test_bare(self):
  171. repo_dir = tempfile.mkdtemp()
  172. self.addCleanup(shutil.rmtree, repo_dir)
  173. porcelain.init(repo_dir, bare=True)
  174. class AddTests(PorcelainTestCase):
  175. def test_add_default_paths(self):
  176. # create a file for initial commit
  177. with open(os.path.join(self.repo.path, 'blah'), 'w') as f:
  178. f.write("\n")
  179. porcelain.add(repo=self.repo.path, paths=['blah'])
  180. porcelain.commit(repo=self.repo.path, message=b'test',
  181. author=b'test', committer=b'test')
  182. # Add a second test file and a file in a directory
  183. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  184. f.write("\n")
  185. os.mkdir(os.path.join(self.repo.path, 'adir'))
  186. with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f:
  187. f.write("\n")
  188. porcelain.add(self.repo.path)
  189. # Check that foo was added and nothing in .git was modified
  190. index = self.repo.open_index()
  191. self.assertEqual(sorted(index), [b'adir/afile', b'blah', b'foo'])
  192. def test_add_file(self):
  193. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  194. f.write("BAR")
  195. porcelain.add(self.repo.path, paths=["foo"])
  196. self.assertIn("foo", self.repo.open_index())
  197. def test_add_file_absolute_path(self):
  198. # Absolute paths are (not yet) supported
  199. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  200. f.write("BAR")
  201. porcelain.add(self.repo, paths=[os.path.join(self.repo.path, "foo")])
  202. self.assertIn("foo", self.repo.open_index())
  203. class RemoveTests(PorcelainTestCase):
  204. def test_remove_file(self):
  205. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  206. f.write("BAR")
  207. porcelain.add(self.repo.path, paths=["foo"])
  208. porcelain.rm(self.repo.path, paths=["foo"])
  209. class LogTests(PorcelainTestCase):
  210. def test_simple(self):
  211. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  212. [3, 1, 2]])
  213. self.repo.refs[b"HEAD"] = c3.id
  214. outstream = StringIO()
  215. porcelain.log(self.repo.path, outstream=outstream)
  216. self.assertEqual(3, outstream.getvalue().count("-" * 50))
  217. def test_max_entries(self):
  218. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  219. [3, 1, 2]])
  220. self.repo.refs[b"HEAD"] = c3.id
  221. outstream = StringIO()
  222. porcelain.log(self.repo.path, outstream=outstream, max_entries=1)
  223. self.assertEqual(1, outstream.getvalue().count("-" * 50))
  224. class ShowTests(PorcelainTestCase):
  225. def test_nolist(self):
  226. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  227. [3, 1, 2]])
  228. self.repo.refs[b"HEAD"] = c3.id
  229. outstream = StringIO()
  230. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  231. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  232. def test_simple(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. outstream = StringIO()
  237. porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
  238. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  239. def test_blob(self):
  240. b = Blob.from_string(b"The Foo\n")
  241. self.repo.object_store.add_object(b)
  242. outstream = StringIO()
  243. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  244. self.assertEqual(outstream.getvalue(), "The Foo\n")
  245. class SymbolicRefTests(PorcelainTestCase):
  246. def test_set_wrong_symbolic_ref(self):
  247. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  248. [3, 1, 2]])
  249. self.repo.refs[b"HEAD"] = c3.id
  250. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path, b'foobar')
  251. def test_set_force_wrong_symbolic_ref(self):
  252. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  253. [3, 1, 2]])
  254. self.repo.refs[b"HEAD"] = c3.id
  255. porcelain.symbolic_ref(self.repo.path, b'force_foobar', force=True)
  256. #test if we actually changed the file
  257. with self.repo.get_named_file('HEAD') as f:
  258. new_ref = f.read()
  259. self.assertEqual(new_ref, b'ref: refs/heads/force_foobar\n')
  260. def test_set_symbolic_ref(self):
  261. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  262. [3, 1, 2]])
  263. self.repo.refs[b"HEAD"] = c3.id
  264. porcelain.symbolic_ref(self.repo.path, b'master')
  265. def test_set_symbolic_ref_other_than_master(self):
  266. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  267. [3, 1, 2]], attrs=dict(refs='develop'))
  268. self.repo.refs[b"HEAD"] = c3.id
  269. self.repo.refs[b"refs/heads/develop"] = c3.id
  270. porcelain.symbolic_ref(self.repo.path, b'develop')
  271. #test if we actually changed the file
  272. with self.repo.get_named_file('HEAD') as f:
  273. new_ref = f.read()
  274. self.assertEqual(new_ref, b'ref: refs/heads/develop\n')
  275. class DiffTreeTests(PorcelainTestCase):
  276. def test_empty(self):
  277. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  278. [3, 1, 2]])
  279. self.repo.refs[b"HEAD"] = c3.id
  280. outstream = BytesIO()
  281. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree, outstream=outstream)
  282. self.assertEqual(outstream.getvalue(), b"")
  283. class CommitTreeTests(PorcelainTestCase):
  284. def test_simple(self):
  285. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  286. [3, 1, 2]])
  287. b = Blob()
  288. b.data = b"foo the bar"
  289. t = Tree()
  290. t.add(b"somename", 0o100644, b.id)
  291. self.repo.object_store.add_object(t)
  292. self.repo.object_store.add_object(b)
  293. sha = porcelain.commit_tree(
  294. self.repo.path, t.id, message=b"Withcommit.",
  295. author=b"Joe <joe@example.com>",
  296. committer=b"Jane <jane@example.com>")
  297. self.assertTrue(isinstance(sha, bytes))
  298. self.assertEqual(len(sha), 40)
  299. class RevListTests(PorcelainTestCase):
  300. def test_simple(self):
  301. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  302. [3, 1, 2]])
  303. outstream = BytesIO()
  304. porcelain.rev_list(
  305. self.repo.path, [c3.id], outstream=outstream)
  306. self.assertEqual(
  307. c3.id + b"\n" +
  308. c2.id + b"\n" +
  309. c1.id + b"\n",
  310. outstream.getvalue())
  311. class TagCreateTests(PorcelainTestCase):
  312. def test_annotated(self):
  313. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  314. [3, 1, 2]])
  315. self.repo.refs[b"HEAD"] = c3.id
  316. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  317. b'bar', annotated=True)
  318. tags = self.repo.refs.as_dict(b"refs/tags")
  319. self.assertEqual(list(tags.keys()), [b"tryme"])
  320. tag = self.repo[b'refs/tags/tryme']
  321. self.assertTrue(isinstance(tag, Tag))
  322. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  323. self.assertEqual(b"bar", tag.message)
  324. self.assertLess(time.time() - tag.tag_time, 5)
  325. def test_unannotated(self):
  326. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  327. [3, 1, 2]])
  328. self.repo.refs[b"HEAD"] = c3.id
  329. porcelain.tag_create(self.repo.path, b"tryme", annotated=False)
  330. tags = self.repo.refs.as_dict(b"refs/tags")
  331. self.assertEqual(list(tags.keys()), [b"tryme"])
  332. self.repo[b'refs/tags/tryme']
  333. self.assertEqual(list(tags.values()), [self.repo.head()])
  334. class TagListTests(PorcelainTestCase):
  335. def test_empty(self):
  336. tags = porcelain.tag_list(self.repo.path)
  337. self.assertEqual([], tags)
  338. def test_simple(self):
  339. self.repo.refs[b"refs/tags/foo"] = b"aa" * 20
  340. self.repo.refs[b"refs/tags/bar/bla"] = b"bb" * 20
  341. tags = porcelain.tag_list(self.repo.path)
  342. self.assertEqual([b"bar/bla", b"foo"], tags)
  343. class TagDeleteTests(PorcelainTestCase):
  344. def test_simple(self):
  345. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  346. self.repo[b"HEAD"] = c1.id
  347. porcelain.tag_create(self.repo, b'foo')
  348. self.assertTrue(b"foo" in porcelain.tag_list(self.repo))
  349. porcelain.tag_delete(self.repo, b'foo')
  350. self.assertFalse(b"foo" in porcelain.tag_list(self.repo))
  351. class ResetTests(PorcelainTestCase):
  352. def test_hard_head(self):
  353. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  354. f.write("BAR")
  355. porcelain.add(self.repo.path, paths=["foo"])
  356. porcelain.commit(self.repo.path, message=b"Some message",
  357. committer=b"Jane <jane@example.com>",
  358. author=b"John <john@example.com>")
  359. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  360. f.write(b"OOH")
  361. porcelain.reset(self.repo, "hard", b"HEAD")
  362. index = self.repo.open_index()
  363. changes = list(tree_changes(self.repo,
  364. index.commit(self.repo.object_store),
  365. self.repo[b'HEAD'].tree))
  366. self.assertEqual([], changes)
  367. def test_hard_commit(self):
  368. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  369. f.write("BAR")
  370. porcelain.add(self.repo.path, paths=["foo"])
  371. sha = porcelain.commit(self.repo.path, message=b"Some message",
  372. committer=b"Jane <jane@example.com>",
  373. author=b"John <john@example.com>")
  374. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  375. f.write(b"BAZ")
  376. porcelain.add(self.repo.path, paths=["foo"])
  377. porcelain.commit(self.repo.path, message=b"Some other message",
  378. committer=b"Jane <jane@example.com>",
  379. author=b"John <john@example.com>")
  380. porcelain.reset(self.repo, "hard", sha)
  381. index = self.repo.open_index()
  382. changes = list(tree_changes(self.repo,
  383. index.commit(self.repo.object_store),
  384. self.repo[sha].tree))
  385. self.assertEqual([], changes)
  386. class PushTests(PorcelainTestCase):
  387. def test_simple(self):
  388. """
  389. Basic test of porcelain push where self.repo is the remote. First
  390. clone the remote, commit a file to the clone, then push the changes
  391. back to the remote.
  392. """
  393. outstream = BytesIO()
  394. errstream = BytesIO()
  395. porcelain.commit(repo=self.repo.path, message=b'init',
  396. author=b'', committer=b'')
  397. # Setup target repo cloned from temp test repo
  398. clone_path = tempfile.mkdtemp()
  399. self.addCleanup(shutil.rmtree, clone_path)
  400. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  401. errstream=errstream)
  402. try:
  403. self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD'])
  404. finally:
  405. target_repo.close()
  406. # create a second file to be pushed back to origin
  407. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  408. os.close(handle)
  409. porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)])
  410. porcelain.commit(repo=clone_path, message=b'push',
  411. author=b'', committer=b'')
  412. # Setup a non-checked out branch in the remote
  413. refs_path = b"refs/heads/foo"
  414. new_id = self.repo[b'HEAD'].id
  415. self.assertNotEqual(new_id, ZERO_SHA)
  416. self.repo.refs[refs_path] = new_id
  417. # Push to the remote
  418. porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path, outstream=outstream,
  419. errstream=errstream)
  420. # Check that the target and source
  421. with Repo(clone_path) as r_clone:
  422. self.assertEqual({
  423. b'HEAD': new_id,
  424. b'refs/heads/foo': r_clone[b'HEAD'].id,
  425. b'refs/heads/master': new_id,
  426. }, self.repo.get_refs())
  427. self.assertEqual(r_clone[b'HEAD'].id, self.repo.refs[refs_path])
  428. # Get the change in the target repo corresponding to the add
  429. # this will be in the foo branch.
  430. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree,
  431. self.repo[b'refs/heads/foo'].tree))[0]
  432. self.assertEqual(os.path.basename(fullpath),
  433. change.new.path.decode('ascii'))
  434. def test_delete(self):
  435. """Basic test of porcelain push, removing a branch.
  436. """
  437. outstream = BytesIO()
  438. errstream = BytesIO()
  439. porcelain.commit(repo=self.repo.path, message=b'init',
  440. author=b'', committer=b'')
  441. # Setup target repo cloned from temp test repo
  442. clone_path = tempfile.mkdtemp()
  443. self.addCleanup(shutil.rmtree, clone_path)
  444. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  445. errstream=errstream)
  446. target_repo.close()
  447. # Setup a non-checked out branch in the remote
  448. refs_path = b"refs/heads/foo"
  449. new_id = self.repo[b'HEAD'].id
  450. self.assertNotEqual(new_id, ZERO_SHA)
  451. self.repo.refs[refs_path] = new_id
  452. # Push to the remote
  453. porcelain.push(clone_path, self.repo.path, b":" + refs_path, outstream=outstream,
  454. errstream=errstream)
  455. self.assertEqual({
  456. b'HEAD': new_id,
  457. b'refs/heads/master': new_id,
  458. }, self.repo.get_refs())
  459. class PullTests(PorcelainTestCase):
  460. def setUp(self):
  461. super(PullTests, self).setUp()
  462. # create a file for initial commit
  463. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  464. os.close(handle)
  465. filename = os.path.basename(fullpath)
  466. porcelain.add(repo=self.repo.path, paths=filename)
  467. porcelain.commit(repo=self.repo.path, message=b'test',
  468. author=b'test', committer=b'test')
  469. # Setup target repo
  470. self.target_path = tempfile.mkdtemp()
  471. self.addCleanup(shutil.rmtree, self.target_path)
  472. target_repo = porcelain.clone(self.repo.path, target=self.target_path,
  473. errstream=BytesIO())
  474. target_repo.close()
  475. # create a second file to be pushed
  476. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  477. os.close(handle)
  478. filename = os.path.basename(fullpath)
  479. porcelain.add(repo=self.repo.path, paths=filename)
  480. porcelain.commit(repo=self.repo.path, message=b'test2',
  481. author=b'test2', committer=b'test2')
  482. self.assertTrue(b'refs/heads/master' in self.repo.refs)
  483. self.assertTrue(b'refs/heads/master' in target_repo.refs)
  484. def test_simple(self):
  485. outstream = BytesIO()
  486. errstream = BytesIO()
  487. # Pull changes into the cloned repo
  488. porcelain.pull(self.target_path, self.repo.path, b'refs/heads/master',
  489. outstream=outstream, errstream=errstream)
  490. # Check the target repo for pushed changes
  491. with Repo(self.target_path) as r:
  492. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  493. def test_no_refspec(self):
  494. outstream = BytesIO()
  495. errstream = BytesIO()
  496. # Pull changes into the cloned repo
  497. porcelain.pull(self.target_path, self.repo.path, outstream=outstream,
  498. errstream=errstream)
  499. # Check the target repo for pushed changes
  500. with Repo(self.target_path) as r:
  501. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  502. class StatusTests(PorcelainTestCase):
  503. def test_empty(self):
  504. results = porcelain.status(self.repo)
  505. self.assertEqual(
  506. {'add': [], 'delete': [], 'modify': []},
  507. results.staged)
  508. self.assertEqual([], results.unstaged)
  509. def test_status(self):
  510. """Integration test for `status` functionality."""
  511. # Commit a dummy file then modify it
  512. fullpath = os.path.join(self.repo.path, 'foo')
  513. with open(fullpath, 'w') as f:
  514. f.write('origstuff')
  515. porcelain.add(repo=self.repo.path, paths=['foo'])
  516. porcelain.commit(repo=self.repo.path, message=b'test status',
  517. author=b'', committer=b'')
  518. # modify access and modify time of path
  519. os.utime(fullpath, (0, 0))
  520. with open(fullpath, 'wb') as f:
  521. f.write(b'stuff')
  522. # Make a dummy file and stage it
  523. filename_add = 'bar'
  524. fullpath = os.path.join(self.repo.path, filename_add)
  525. with open(fullpath, 'w') as f:
  526. f.write('stuff')
  527. porcelain.add(repo=self.repo.path, paths=filename_add)
  528. results = porcelain.status(self.repo)
  529. self.assertEqual(results.staged['add'][0], filename_add.encode('ascii'))
  530. self.assertEqual(results.unstaged, [b'foo'])
  531. def test_get_tree_changes_add(self):
  532. """Unit test for get_tree_changes add."""
  533. # Make a dummy file, stage
  534. filename = 'bar'
  535. with open(os.path.join(self.repo.path, filename), 'w') as f:
  536. f.write('stuff')
  537. porcelain.add(repo=self.repo.path, paths=filename)
  538. porcelain.commit(repo=self.repo.path, message=b'test status',
  539. author=b'', committer=b'')
  540. filename = 'foo'
  541. with open(os.path.join(self.repo.path, filename), 'w') as f:
  542. f.write('stuff')
  543. porcelain.add(repo=self.repo.path, paths=filename)
  544. changes = porcelain.get_tree_changes(self.repo.path)
  545. self.assertEqual(changes['add'][0], filename.encode('ascii'))
  546. self.assertEqual(len(changes['add']), 1)
  547. self.assertEqual(len(changes['modify']), 0)
  548. self.assertEqual(len(changes['delete']), 0)
  549. def test_get_tree_changes_modify(self):
  550. """Unit test for get_tree_changes modify."""
  551. # Make a dummy file, stage, commit, modify
  552. filename = 'foo'
  553. fullpath = os.path.join(self.repo.path, filename)
  554. with open(fullpath, 'w') as f:
  555. f.write('stuff')
  556. porcelain.add(repo=self.repo.path, paths=filename)
  557. porcelain.commit(repo=self.repo.path, message=b'test status',
  558. author=b'', committer=b'')
  559. with open(fullpath, 'w') as f:
  560. f.write('otherstuff')
  561. porcelain.add(repo=self.repo.path, paths=filename)
  562. changes = porcelain.get_tree_changes(self.repo.path)
  563. self.assertEqual(changes['modify'][0], filename.encode('ascii'))
  564. self.assertEqual(len(changes['add']), 0)
  565. self.assertEqual(len(changes['modify']), 1)
  566. self.assertEqual(len(changes['delete']), 0)
  567. def test_get_tree_changes_delete(self):
  568. """Unit test for get_tree_changes delete."""
  569. # Make a dummy file, stage, commit, remove
  570. filename = 'foo'
  571. with open(os.path.join(self.repo.path, filename), 'w') as f:
  572. f.write('stuff')
  573. porcelain.add(repo=self.repo.path, paths=filename)
  574. porcelain.commit(repo=self.repo.path, message=b'test status',
  575. author=b'', committer=b'')
  576. porcelain.rm(repo=self.repo.path, paths=[filename])
  577. changes = porcelain.get_tree_changes(self.repo.path)
  578. self.assertEqual(changes['delete'][0], filename.encode('ascii'))
  579. self.assertEqual(len(changes['add']), 0)
  580. self.assertEqual(len(changes['modify']), 0)
  581. self.assertEqual(len(changes['delete']), 1)
  582. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  583. class UploadPackTests(PorcelainTestCase):
  584. """Tests for upload_pack."""
  585. def test_upload_pack(self):
  586. outf = BytesIO()
  587. exitcode = porcelain.upload_pack(self.repo.path, BytesIO(b"0000"), outf)
  588. outlines = outf.getvalue().splitlines()
  589. self.assertEqual([b"0000"], outlines)
  590. self.assertEqual(0, exitcode)
  591. class ReceivePackTests(PorcelainTestCase):
  592. """Tests for receive_pack."""
  593. def test_receive_pack(self):
  594. filename = 'foo'
  595. with open(os.path.join(self.repo.path, filename), 'w') as f:
  596. f.write('stuff')
  597. porcelain.add(repo=self.repo.path, paths=filename)
  598. self.repo.do_commit(message=b'test status',
  599. author=b'', committer=b'', author_timestamp=1402354300,
  600. commit_timestamp=1402354300, author_timezone=0, commit_timezone=0)
  601. outf = BytesIO()
  602. exitcode = porcelain.receive_pack(self.repo.path, BytesIO(b"0000"), outf)
  603. outlines = outf.getvalue().splitlines()
  604. self.assertEqual([
  605. b'00739e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00 report-status '
  606. b'delete-refs quiet ofs-delta side-band-64k no-done',
  607. b'003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master',
  608. b'0000'], outlines)
  609. self.assertEqual(0, exitcode)
  610. class BranchListTests(PorcelainTestCase):
  611. def test_standard(self):
  612. self.assertEqual(set([]), set(porcelain.branch_list(self.repo)))
  613. def test_new_branch(self):
  614. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  615. self.repo[b"HEAD"] = c1.id
  616. porcelain.branch_create(self.repo, b"foo")
  617. self.assertEqual(
  618. set([b"master", b"foo"]),
  619. set(porcelain.branch_list(self.repo)))
  620. class BranchCreateTests(PorcelainTestCase):
  621. def test_branch_exists(self):
  622. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  623. self.repo[b"HEAD"] = c1.id
  624. porcelain.branch_create(self.repo, b"foo")
  625. self.assertRaises(KeyError, porcelain.branch_create, self.repo, b"foo")
  626. porcelain.branch_create(self.repo, b"foo", force=True)
  627. def test_new_branch(self):
  628. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  629. self.repo[b"HEAD"] = c1.id
  630. porcelain.branch_create(self.repo, b"foo")
  631. self.assertEqual(
  632. set([b"master", b"foo"]),
  633. set(porcelain.branch_list(self.repo)))
  634. class BranchDeleteTests(PorcelainTestCase):
  635. def test_simple(self):
  636. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  637. self.repo[b"HEAD"] = c1.id
  638. porcelain.branch_create(self.repo, b'foo')
  639. self.assertTrue(b"foo" in porcelain.branch_list(self.repo))
  640. porcelain.branch_delete(self.repo, b'foo')
  641. self.assertFalse(b"foo" in porcelain.branch_list(self.repo))
  642. class FetchTests(PorcelainTestCase):
  643. def test_simple(self):
  644. outstream = BytesIO()
  645. errstream = BytesIO()
  646. # create a file for initial commit
  647. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  648. os.close(handle)
  649. filename = os.path.basename(fullpath)
  650. porcelain.add(repo=self.repo.path, paths=filename)
  651. porcelain.commit(repo=self.repo.path, message=b'test',
  652. author=b'test', committer=b'test')
  653. # Setup target repo
  654. target_path = tempfile.mkdtemp()
  655. self.addCleanup(shutil.rmtree, target_path)
  656. target_repo = porcelain.clone(self.repo.path, target=target_path,
  657. errstream=errstream)
  658. # create a second file to be pushed
  659. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  660. os.close(handle)
  661. filename = os.path.basename(fullpath)
  662. porcelain.add(repo=self.repo.path, paths=filename)
  663. porcelain.commit(repo=self.repo.path, message=b'test2',
  664. author=b'test2', committer=b'test2')
  665. self.assertFalse(self.repo[b'HEAD'].id in target_repo)
  666. target_repo.close()
  667. # Fetch changes into the cloned repo
  668. porcelain.fetch(target_path, self.repo.path, outstream=outstream,
  669. errstream=errstream)
  670. # Check the target repo for pushed changes
  671. with Repo(target_path) as r:
  672. self.assertTrue(self.repo[b'HEAD'].id in r)
  673. class RepackTests(PorcelainTestCase):
  674. def test_empty(self):
  675. porcelain.repack(self.repo)
  676. def test_simple(self):
  677. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  678. os.close(handle)
  679. filename = os.path.basename(fullpath)
  680. porcelain.add(repo=self.repo.path, paths=filename)
  681. porcelain.repack(self.repo)
  682. class LsTreeTests(PorcelainTestCase):
  683. def test_empty(self):
  684. porcelain.commit(repo=self.repo.path, message=b'test status',
  685. author=b'', committer=b'')
  686. f = StringIO()
  687. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  688. self.assertEqual(f.getvalue(), "")
  689. def test_simple(self):
  690. # Commit a dummy file then modify it
  691. fullpath = os.path.join(self.repo.path, 'foo')
  692. with open(fullpath, 'w') as f:
  693. f.write('origstuff')
  694. porcelain.add(repo=self.repo.path, paths=['foo'])
  695. porcelain.commit(repo=self.repo.path, message=b'test status',
  696. author=b'', committer=b'')
  697. f = StringIO()
  698. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  699. self.assertEqual(
  700. f.getvalue(),
  701. '100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n')
  702. class LsRemoteTests(PorcelainTestCase):
  703. def test_empty(self):
  704. self.assertEqual({}, porcelain.ls_remote(self.repo.path))
  705. def test_some(self):
  706. cid = porcelain.commit(repo=self.repo.path, message=b'test status',
  707. author=b'', committer=b'')
  708. self.assertEqual({
  709. b'refs/heads/master': cid,
  710. b'HEAD': cid},
  711. porcelain.ls_remote(self.repo.path))