test_porcelain.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. class RemoveTests(PorcelainTestCase):
  197. def test_remove_file(self):
  198. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  199. f.write("BAR")
  200. porcelain.add(self.repo.path, paths=["foo"])
  201. porcelain.rm(self.repo.path, paths=["foo"])
  202. class LogTests(PorcelainTestCase):
  203. def test_simple(self):
  204. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  205. [3, 1, 2]])
  206. self.repo.refs[b"HEAD"] = c3.id
  207. outstream = StringIO()
  208. porcelain.log(self.repo.path, outstream=outstream)
  209. self.assertEqual(3, outstream.getvalue().count("-" * 50))
  210. def test_max_entries(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, max_entries=1)
  216. self.assertEqual(1, outstream.getvalue().count("-" * 50))
  217. class ShowTests(PorcelainTestCase):
  218. def test_nolist(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. outstream = StringIO()
  223. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  224. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  225. def test_simple(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_blob(self):
  233. b = Blob.from_string(b"The Foo\n")
  234. self.repo.object_store.add_object(b)
  235. outstream = StringIO()
  236. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  237. self.assertEqual(outstream.getvalue(), "The Foo\n")
  238. class SymbolicRefTests(PorcelainTestCase):
  239. def test_set_wrong_symbolic_ref(self):
  240. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  241. [3, 1, 2]])
  242. self.repo.refs[b"HEAD"] = c3.id
  243. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path, b'foobar')
  244. def test_set_force_wrong_symbolic_ref(self):
  245. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  246. [3, 1, 2]])
  247. self.repo.refs[b"HEAD"] = c3.id
  248. porcelain.symbolic_ref(self.repo.path, b'force_foobar', force=True)
  249. #test if we actually changed the file
  250. with self.repo.get_named_file('HEAD') as f:
  251. new_ref = f.read()
  252. self.assertEqual(new_ref, b'ref: refs/heads/force_foobar\n')
  253. def test_set_symbolic_ref(self):
  254. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  255. [3, 1, 2]])
  256. self.repo.refs[b"HEAD"] = c3.id
  257. porcelain.symbolic_ref(self.repo.path, b'master')
  258. def test_set_symbolic_ref_other_than_master(self):
  259. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  260. [3, 1, 2]], attrs=dict(refs='develop'))
  261. self.repo.refs[b"HEAD"] = c3.id
  262. self.repo.refs[b"refs/heads/develop"] = c3.id
  263. porcelain.symbolic_ref(self.repo.path, b'develop')
  264. #test if we actually changed the file
  265. with self.repo.get_named_file('HEAD') as f:
  266. new_ref = f.read()
  267. self.assertEqual(new_ref, b'ref: refs/heads/develop\n')
  268. class DiffTreeTests(PorcelainTestCase):
  269. def test_empty(self):
  270. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  271. [3, 1, 2]])
  272. self.repo.refs[b"HEAD"] = c3.id
  273. outstream = BytesIO()
  274. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree, outstream=outstream)
  275. self.assertEqual(outstream.getvalue(), b"")
  276. class CommitTreeTests(PorcelainTestCase):
  277. def test_simple(self):
  278. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  279. [3, 1, 2]])
  280. b = Blob()
  281. b.data = b"foo the bar"
  282. t = Tree()
  283. t.add(b"somename", 0o100644, b.id)
  284. self.repo.object_store.add_object(t)
  285. self.repo.object_store.add_object(b)
  286. sha = porcelain.commit_tree(
  287. self.repo.path, t.id, message=b"Withcommit.",
  288. author=b"Joe <joe@example.com>",
  289. committer=b"Jane <jane@example.com>")
  290. self.assertTrue(isinstance(sha, bytes))
  291. self.assertEqual(len(sha), 40)
  292. class RevListTests(PorcelainTestCase):
  293. def test_simple(self):
  294. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  295. [3, 1, 2]])
  296. outstream = BytesIO()
  297. porcelain.rev_list(
  298. self.repo.path, [c3.id], outstream=outstream)
  299. self.assertEqual(
  300. c3.id + b"\n" +
  301. c2.id + b"\n" +
  302. c1.id + b"\n",
  303. outstream.getvalue())
  304. class TagCreateTests(PorcelainTestCase):
  305. def test_annotated(self):
  306. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  307. [3, 1, 2]])
  308. self.repo.refs[b"HEAD"] = c3.id
  309. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  310. b'bar', annotated=True)
  311. tags = self.repo.refs.as_dict(b"refs/tags")
  312. self.assertEqual(list(tags.keys()), [b"tryme"])
  313. tag = self.repo[b'refs/tags/tryme']
  314. self.assertTrue(isinstance(tag, Tag))
  315. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  316. self.assertEqual(b"bar", tag.message)
  317. self.assertLess(time.time() - tag.tag_time, 5)
  318. def test_unannotated(self):
  319. c1, c2, c3 = build_commit_graph(self.repo.object_store, [[1], [2, 1],
  320. [3, 1, 2]])
  321. self.repo.refs[b"HEAD"] = c3.id
  322. porcelain.tag_create(self.repo.path, b"tryme", annotated=False)
  323. tags = self.repo.refs.as_dict(b"refs/tags")
  324. self.assertEqual(list(tags.keys()), [b"tryme"])
  325. self.repo[b'refs/tags/tryme']
  326. self.assertEqual(list(tags.values()), [self.repo.head()])
  327. class TagListTests(PorcelainTestCase):
  328. def test_empty(self):
  329. tags = porcelain.tag_list(self.repo.path)
  330. self.assertEqual([], tags)
  331. def test_simple(self):
  332. self.repo.refs[b"refs/tags/foo"] = b"aa" * 20
  333. self.repo.refs[b"refs/tags/bar/bla"] = b"bb" * 20
  334. tags = porcelain.tag_list(self.repo.path)
  335. self.assertEqual([b"bar/bla", b"foo"], tags)
  336. class TagDeleteTests(PorcelainTestCase):
  337. def test_simple(self):
  338. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  339. self.repo[b"HEAD"] = c1.id
  340. porcelain.tag_create(self.repo, b'foo')
  341. self.assertTrue(b"foo" in porcelain.tag_list(self.repo))
  342. porcelain.tag_delete(self.repo, b'foo')
  343. self.assertFalse(b"foo" in porcelain.tag_list(self.repo))
  344. class ResetTests(PorcelainTestCase):
  345. def test_hard_head(self):
  346. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  347. f.write("BAR")
  348. porcelain.add(self.repo.path, paths=["foo"])
  349. porcelain.commit(self.repo.path, message=b"Some message",
  350. committer=b"Jane <jane@example.com>",
  351. author=b"John <john@example.com>")
  352. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  353. f.write(b"OOH")
  354. porcelain.reset(self.repo, "hard", b"HEAD")
  355. index = self.repo.open_index()
  356. changes = list(tree_changes(self.repo,
  357. index.commit(self.repo.object_store),
  358. self.repo[b'HEAD'].tree))
  359. self.assertEqual([], changes)
  360. def test_hard_commit(self):
  361. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  362. f.write("BAR")
  363. porcelain.add(self.repo.path, paths=["foo"])
  364. sha = porcelain.commit(self.repo.path, message=b"Some message",
  365. committer=b"Jane <jane@example.com>",
  366. author=b"John <john@example.com>")
  367. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  368. f.write(b"BAZ")
  369. porcelain.add(self.repo.path, paths=["foo"])
  370. porcelain.commit(self.repo.path, message=b"Some other message",
  371. committer=b"Jane <jane@example.com>",
  372. author=b"John <john@example.com>")
  373. porcelain.reset(self.repo, "hard", sha)
  374. index = self.repo.open_index()
  375. changes = list(tree_changes(self.repo,
  376. index.commit(self.repo.object_store),
  377. self.repo[sha].tree))
  378. self.assertEqual([], changes)
  379. class PushTests(PorcelainTestCase):
  380. def test_simple(self):
  381. """
  382. Basic test of porcelain push where self.repo is the remote. First
  383. clone the remote, commit a file to the clone, then push the changes
  384. back to the remote.
  385. """
  386. outstream = BytesIO()
  387. errstream = BytesIO()
  388. porcelain.commit(repo=self.repo.path, message=b'init',
  389. author=b'', committer=b'')
  390. # Setup target repo cloned from temp test repo
  391. clone_path = tempfile.mkdtemp()
  392. self.addCleanup(shutil.rmtree, clone_path)
  393. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  394. errstream=errstream)
  395. try:
  396. self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD'])
  397. finally:
  398. target_repo.close()
  399. # create a second file to be pushed back to origin
  400. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  401. os.close(handle)
  402. porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)])
  403. porcelain.commit(repo=clone_path, message=b'push',
  404. author=b'', committer=b'')
  405. # Setup a non-checked out branch in the remote
  406. refs_path = b"refs/heads/foo"
  407. new_id = self.repo[b'HEAD'].id
  408. self.assertNotEqual(new_id, ZERO_SHA)
  409. self.repo.refs[refs_path] = new_id
  410. # Push to the remote
  411. porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path, outstream=outstream,
  412. errstream=errstream)
  413. # Check that the target and source
  414. with Repo(clone_path) as r_clone:
  415. self.assertEqual({
  416. b'HEAD': new_id,
  417. b'refs/heads/foo': r_clone[b'HEAD'].id,
  418. b'refs/heads/master': new_id,
  419. }, self.repo.get_refs())
  420. self.assertEqual(r_clone[b'HEAD'].id, self.repo.refs[refs_path])
  421. # Get the change in the target repo corresponding to the add
  422. # this will be in the foo branch.
  423. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree,
  424. self.repo[b'refs/heads/foo'].tree))[0]
  425. self.assertEqual(os.path.basename(fullpath),
  426. change.new.path.decode('ascii'))
  427. def test_delete(self):
  428. """Basic test of porcelain push, removing a branch.
  429. """
  430. outstream = BytesIO()
  431. errstream = BytesIO()
  432. porcelain.commit(repo=self.repo.path, message=b'init',
  433. author=b'', committer=b'')
  434. # Setup target repo cloned from temp test repo
  435. clone_path = tempfile.mkdtemp()
  436. self.addCleanup(shutil.rmtree, clone_path)
  437. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  438. errstream=errstream)
  439. target_repo.close()
  440. # Setup a non-checked out branch in the remote
  441. refs_path = b"refs/heads/foo"
  442. new_id = self.repo[b'HEAD'].id
  443. self.assertNotEqual(new_id, ZERO_SHA)
  444. self.repo.refs[refs_path] = new_id
  445. # Push to the remote
  446. porcelain.push(clone_path, self.repo.path, b":" + refs_path, outstream=outstream,
  447. errstream=errstream)
  448. self.assertEqual({
  449. b'HEAD': new_id,
  450. b'refs/heads/master': new_id,
  451. }, self.repo.get_refs())
  452. class PullTests(PorcelainTestCase):
  453. def setUp(self):
  454. super(PullTests, self).setUp()
  455. # create a file for initial commit
  456. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  457. os.close(handle)
  458. filename = os.path.basename(fullpath)
  459. porcelain.add(repo=self.repo.path, paths=filename)
  460. porcelain.commit(repo=self.repo.path, message=b'test',
  461. author=b'test', committer=b'test')
  462. # Setup target repo
  463. self.target_path = tempfile.mkdtemp()
  464. self.addCleanup(shutil.rmtree, self.target_path)
  465. target_repo = porcelain.clone(self.repo.path, target=self.target_path,
  466. errstream=BytesIO())
  467. target_repo.close()
  468. # create a second file to be pushed
  469. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  470. os.close(handle)
  471. filename = os.path.basename(fullpath)
  472. porcelain.add(repo=self.repo.path, paths=filename)
  473. porcelain.commit(repo=self.repo.path, message=b'test2',
  474. author=b'test2', committer=b'test2')
  475. self.assertTrue(b'refs/heads/master' in self.repo.refs)
  476. self.assertTrue(b'refs/heads/master' in target_repo.refs)
  477. def test_simple(self):
  478. outstream = BytesIO()
  479. errstream = BytesIO()
  480. # Pull changes into the cloned repo
  481. porcelain.pull(self.target_path, self.repo.path, b'refs/heads/master',
  482. outstream=outstream, errstream=errstream)
  483. # Check the target repo for pushed changes
  484. with Repo(self.target_path) as r:
  485. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  486. def test_no_refspec(self):
  487. outstream = BytesIO()
  488. errstream = BytesIO()
  489. # Pull changes into the cloned repo
  490. porcelain.pull(self.target_path, self.repo.path, outstream=outstream,
  491. errstream=errstream)
  492. # Check the target repo for pushed changes
  493. with Repo(self.target_path) as r:
  494. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  495. class StatusTests(PorcelainTestCase):
  496. def test_empty(self):
  497. results = porcelain.status(self.repo)
  498. self.assertEqual(
  499. {'add': [], 'delete': [], 'modify': []},
  500. results.staged)
  501. self.assertEqual([], results.unstaged)
  502. def test_status(self):
  503. """Integration test for `status` functionality."""
  504. # Commit a dummy file then modify it
  505. fullpath = os.path.join(self.repo.path, 'foo')
  506. with open(fullpath, 'w') as f:
  507. f.write('origstuff')
  508. porcelain.add(repo=self.repo.path, paths=['foo'])
  509. porcelain.commit(repo=self.repo.path, message=b'test status',
  510. author=b'', committer=b'')
  511. # modify access and modify time of path
  512. os.utime(fullpath, (0, 0))
  513. with open(fullpath, 'wb') as f:
  514. f.write(b'stuff')
  515. # Make a dummy file and stage it
  516. filename_add = 'bar'
  517. fullpath = os.path.join(self.repo.path, filename_add)
  518. with open(fullpath, 'w') as f:
  519. f.write('stuff')
  520. porcelain.add(repo=self.repo.path, paths=filename_add)
  521. results = porcelain.status(self.repo)
  522. self.assertEqual(results.staged['add'][0], filename_add.encode('ascii'))
  523. self.assertEqual(results.unstaged, [b'foo'])
  524. def test_get_tree_changes_add(self):
  525. """Unit test for get_tree_changes add."""
  526. # Make a dummy file, stage
  527. filename = 'bar'
  528. with open(os.path.join(self.repo.path, filename), 'w') as f:
  529. f.write('stuff')
  530. porcelain.add(repo=self.repo.path, paths=filename)
  531. porcelain.commit(repo=self.repo.path, message=b'test status',
  532. author=b'', committer=b'')
  533. filename = 'foo'
  534. with open(os.path.join(self.repo.path, filename), 'w') as f:
  535. f.write('stuff')
  536. porcelain.add(repo=self.repo.path, paths=filename)
  537. changes = porcelain.get_tree_changes(self.repo.path)
  538. self.assertEqual(changes['add'][0], filename.encode('ascii'))
  539. self.assertEqual(len(changes['add']), 1)
  540. self.assertEqual(len(changes['modify']), 0)
  541. self.assertEqual(len(changes['delete']), 0)
  542. def test_get_tree_changes_modify(self):
  543. """Unit test for get_tree_changes modify."""
  544. # Make a dummy file, stage, commit, modify
  545. filename = 'foo'
  546. fullpath = os.path.join(self.repo.path, filename)
  547. with open(fullpath, 'w') as f:
  548. f.write('stuff')
  549. porcelain.add(repo=self.repo.path, paths=filename)
  550. porcelain.commit(repo=self.repo.path, message=b'test status',
  551. author=b'', committer=b'')
  552. with open(fullpath, 'w') as f:
  553. f.write('otherstuff')
  554. porcelain.add(repo=self.repo.path, paths=filename)
  555. changes = porcelain.get_tree_changes(self.repo.path)
  556. self.assertEqual(changes['modify'][0], filename.encode('ascii'))
  557. self.assertEqual(len(changes['add']), 0)
  558. self.assertEqual(len(changes['modify']), 1)
  559. self.assertEqual(len(changes['delete']), 0)
  560. def test_get_tree_changes_delete(self):
  561. """Unit test for get_tree_changes delete."""
  562. # Make a dummy file, stage, commit, remove
  563. filename = 'foo'
  564. with open(os.path.join(self.repo.path, filename), 'w') as f:
  565. f.write('stuff')
  566. porcelain.add(repo=self.repo.path, paths=filename)
  567. porcelain.commit(repo=self.repo.path, message=b'test status',
  568. author=b'', committer=b'')
  569. porcelain.rm(repo=self.repo.path, paths=[filename])
  570. changes = porcelain.get_tree_changes(self.repo.path)
  571. self.assertEqual(changes['delete'][0], filename.encode('ascii'))
  572. self.assertEqual(len(changes['add']), 0)
  573. self.assertEqual(len(changes['modify']), 0)
  574. self.assertEqual(len(changes['delete']), 1)
  575. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  576. class UploadPackTests(PorcelainTestCase):
  577. """Tests for upload_pack."""
  578. def test_upload_pack(self):
  579. outf = BytesIO()
  580. exitcode = porcelain.upload_pack(self.repo.path, BytesIO(b"0000"), outf)
  581. outlines = outf.getvalue().splitlines()
  582. self.assertEqual([b"0000"], outlines)
  583. self.assertEqual(0, exitcode)
  584. class ReceivePackTests(PorcelainTestCase):
  585. """Tests for receive_pack."""
  586. def test_receive_pack(self):
  587. filename = 'foo'
  588. with open(os.path.join(self.repo.path, filename), 'w') as f:
  589. f.write('stuff')
  590. porcelain.add(repo=self.repo.path, paths=filename)
  591. self.repo.do_commit(message=b'test status',
  592. author=b'', committer=b'', author_timestamp=1402354300,
  593. commit_timestamp=1402354300, author_timezone=0, commit_timezone=0)
  594. outf = BytesIO()
  595. exitcode = porcelain.receive_pack(self.repo.path, BytesIO(b"0000"), outf)
  596. outlines = outf.getvalue().splitlines()
  597. self.assertEqual([
  598. b'00739e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00 report-status '
  599. b'delete-refs quiet ofs-delta side-band-64k no-done',
  600. b'003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master',
  601. b'0000'], outlines)
  602. self.assertEqual(0, exitcode)
  603. class BranchListTests(PorcelainTestCase):
  604. def test_standard(self):
  605. self.assertEqual(set([]), set(porcelain.branch_list(self.repo)))
  606. def test_new_branch(self):
  607. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  608. self.repo[b"HEAD"] = c1.id
  609. porcelain.branch_create(self.repo, b"foo")
  610. self.assertEqual(
  611. set([b"master", b"foo"]),
  612. set(porcelain.branch_list(self.repo)))
  613. class BranchCreateTests(PorcelainTestCase):
  614. def test_branch_exists(self):
  615. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  616. self.repo[b"HEAD"] = c1.id
  617. porcelain.branch_create(self.repo, b"foo")
  618. self.assertRaises(KeyError, porcelain.branch_create, self.repo, b"foo")
  619. porcelain.branch_create(self.repo, b"foo", force=True)
  620. def test_new_branch(self):
  621. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  622. self.repo[b"HEAD"] = c1.id
  623. porcelain.branch_create(self.repo, b"foo")
  624. self.assertEqual(
  625. set([b"master", b"foo"]),
  626. set(porcelain.branch_list(self.repo)))
  627. class BranchDeleteTests(PorcelainTestCase):
  628. def test_simple(self):
  629. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  630. self.repo[b"HEAD"] = c1.id
  631. porcelain.branch_create(self.repo, b'foo')
  632. self.assertTrue(b"foo" in porcelain.branch_list(self.repo))
  633. porcelain.branch_delete(self.repo, b'foo')
  634. self.assertFalse(b"foo" in porcelain.branch_list(self.repo))
  635. class FetchTests(PorcelainTestCase):
  636. def test_simple(self):
  637. outstream = BytesIO()
  638. errstream = BytesIO()
  639. # create a file for initial commit
  640. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  641. os.close(handle)
  642. filename = os.path.basename(fullpath)
  643. porcelain.add(repo=self.repo.path, paths=filename)
  644. porcelain.commit(repo=self.repo.path, message=b'test',
  645. author=b'test', committer=b'test')
  646. # Setup target repo
  647. target_path = tempfile.mkdtemp()
  648. self.addCleanup(shutil.rmtree, target_path)
  649. target_repo = porcelain.clone(self.repo.path, target=target_path,
  650. errstream=errstream)
  651. # create a second file to be pushed
  652. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  653. os.close(handle)
  654. filename = os.path.basename(fullpath)
  655. porcelain.add(repo=self.repo.path, paths=filename)
  656. porcelain.commit(repo=self.repo.path, message=b'test2',
  657. author=b'test2', committer=b'test2')
  658. self.assertFalse(self.repo[b'HEAD'].id in target_repo)
  659. target_repo.close()
  660. # Fetch changes into the cloned repo
  661. porcelain.fetch(target_path, self.repo.path, outstream=outstream,
  662. errstream=errstream)
  663. # Check the target repo for pushed changes
  664. with Repo(target_path) as r:
  665. self.assertTrue(self.repo[b'HEAD'].id in r)
  666. class RepackTests(PorcelainTestCase):
  667. def test_empty(self):
  668. porcelain.repack(self.repo)
  669. def test_simple(self):
  670. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  671. os.close(handle)
  672. filename = os.path.basename(fullpath)
  673. porcelain.add(repo=self.repo.path, paths=filename)
  674. porcelain.repack(self.repo)
  675. class LsTreeTests(PorcelainTestCase):
  676. def test_empty(self):
  677. porcelain.commit(repo=self.repo.path, message=b'test status',
  678. author=b'', committer=b'')
  679. f = StringIO()
  680. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  681. self.assertEqual(f.getvalue(), "")
  682. def test_simple(self):
  683. # Commit a dummy file then modify it
  684. fullpath = os.path.join(self.repo.path, 'foo')
  685. with open(fullpath, 'w') as f:
  686. f.write('origstuff')
  687. porcelain.add(repo=self.repo.path, paths=['foo'])
  688. porcelain.commit(repo=self.repo.path, message=b'test status',
  689. author=b'', committer=b'')
  690. f = StringIO()
  691. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  692. self.assertEqual(
  693. f.getvalue(),
  694. '100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n')
  695. class LsRemoteTests(PorcelainTestCase):
  696. def test_empty(self):
  697. self.assertEqual({}, porcelain.ls_remote(self.repo.path))
  698. def test_some(self):
  699. cid = porcelain.commit(repo=self.repo.path, message=b'test status',
  700. author=b'', committer=b'')
  701. self.assertEqual({
  702. b'refs/heads/master': cid,
  703. b'HEAD': cid},
  704. porcelain.ls_remote(self.repo.path))