test_porcelain.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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(
  60. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  61. self.repo.refs[b"refs/heads/master"] = c3.id
  62. out = BytesIO()
  63. err = BytesIO()
  64. porcelain.archive(self.repo.path, b"refs/heads/master", outstream=out,
  65. errstream=err)
  66. self.assertEqual(b"", err.getvalue())
  67. tf = tarfile.TarFile(fileobj=out)
  68. self.addCleanup(tf.close)
  69. self.assertEqual([], tf.getnames())
  70. class UpdateServerInfoTests(PorcelainTestCase):
  71. def test_simple(self):
  72. c1, c2, c3 = build_commit_graph(
  73. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  74. self.repo.refs[b"refs/heads/foo"] = c3.id
  75. porcelain.update_server_info(self.repo.path)
  76. self.assertTrue(os.path.exists(
  77. os.path.join(self.repo.controldir(), 'info', 'refs')))
  78. class CommitTests(PorcelainTestCase):
  79. def test_custom_author(self):
  80. c1, c2, c3 = build_commit_graph(
  81. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  82. self.repo.refs[b"refs/heads/foo"] = c3.id
  83. sha = porcelain.commit(
  84. self.repo.path, message=b"Some message",
  85. author=b"Joe <joe@example.com>",
  86. committer=b"Bob <bob@example.com>")
  87. self.assertTrue(isinstance(sha, bytes))
  88. self.assertEqual(len(sha), 40)
  89. class CloneTests(PorcelainTestCase):
  90. def test_simple_local(self):
  91. f1_1 = make_object(Blob, data=b'f1')
  92. commit_spec = [[1], [2, 1], [3, 1, 2]]
  93. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  94. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  95. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  96. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  97. commit_spec, trees)
  98. self.repo.refs[b"refs/heads/master"] = c3.id
  99. self.repo.refs[b"refs/tags/foo"] = c3.id
  100. target_path = tempfile.mkdtemp()
  101. errstream = BytesIO()
  102. self.addCleanup(shutil.rmtree, target_path)
  103. r = porcelain.clone(self.repo.path, target_path,
  104. checkout=False, errstream=errstream)
  105. self.assertEqual(r.path, target_path)
  106. target_repo = Repo(target_path)
  107. self.assertEqual(target_repo.head(), c3.id)
  108. self.assertEqual(c3.id, target_repo.refs[b'refs/tags/foo'])
  109. self.assertTrue(b'f1' not in os.listdir(target_path))
  110. self.assertTrue(b'f2' not in os.listdir(target_path))
  111. c = r.get_config()
  112. encoded_path = self.repo.path
  113. if not isinstance(encoded_path, bytes):
  114. encoded_path = encoded_path.encode('utf-8')
  115. self.assertEqual(encoded_path, c.get((b'remote', b'origin'), b'url'))
  116. self.assertEqual(
  117. b'+refs/heads/*:refs/remotes/origin/*',
  118. c.get((b'remote', b'origin'), b'fetch'))
  119. def test_simple_local_with_checkout(self):
  120. f1_1 = make_object(Blob, data=b'f1')
  121. commit_spec = [[1], [2, 1], [3, 1, 2]]
  122. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  123. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  124. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  125. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  126. commit_spec, trees)
  127. self.repo.refs[b"refs/heads/master"] = c3.id
  128. target_path = tempfile.mkdtemp()
  129. errstream = BytesIO()
  130. self.addCleanup(shutil.rmtree, target_path)
  131. with porcelain.clone(self.repo.path, target_path,
  132. checkout=True,
  133. errstream=errstream) as r:
  134. self.assertEqual(r.path, target_path)
  135. with Repo(target_path) as r:
  136. self.assertEqual(r.head(), c3.id)
  137. self.assertTrue('f1' in os.listdir(target_path))
  138. self.assertTrue('f2' in os.listdir(target_path))
  139. def test_bare_local_with_checkout(self):
  140. f1_1 = make_object(Blob, data=b'f1')
  141. commit_spec = [[1], [2, 1], [3, 1, 2]]
  142. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  143. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  144. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  145. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  146. commit_spec, trees)
  147. self.repo.refs[b"refs/heads/master"] = c3.id
  148. target_path = tempfile.mkdtemp()
  149. errstream = BytesIO()
  150. self.addCleanup(shutil.rmtree, target_path)
  151. with porcelain.clone(
  152. self.repo.path, target_path, bare=True,
  153. errstream=errstream) as r:
  154. self.assertEqual(r.path, target_path)
  155. with Repo(target_path) as r:
  156. self.assertRaises(KeyError, r.head)
  157. self.assertFalse(b'f1' in os.listdir(target_path))
  158. self.assertFalse(b'f2' in os.listdir(target_path))
  159. def test_no_checkout_with_bare(self):
  160. f1_1 = make_object(Blob, data=b'f1')
  161. commit_spec = [[1]]
  162. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]}
  163. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  164. self.repo.refs[b"refs/heads/master"] = c1.id
  165. self.repo.refs[b"HEAD"] = c1.id
  166. target_path = tempfile.mkdtemp()
  167. errstream = BytesIO()
  168. self.addCleanup(shutil.rmtree, target_path)
  169. self.assertRaises(
  170. ValueError, porcelain.clone, self.repo.path,
  171. target_path, checkout=True, bare=True, errstream=errstream)
  172. def test_no_head_no_checkout(self):
  173. f1_1 = make_object(Blob, data=b'f1')
  174. commit_spec = [[1]]
  175. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]}
  176. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  177. self.repo.refs[b"refs/heads/master"] = c1.id
  178. target_path = tempfile.mkdtemp()
  179. self.addCleanup(shutil.rmtree, target_path)
  180. errstream = BytesIO()
  181. r = porcelain.clone(
  182. self.repo.path, target_path, checkout=True, errstream=errstream)
  183. r.close()
  184. class InitTests(TestCase):
  185. def test_non_bare(self):
  186. repo_dir = tempfile.mkdtemp()
  187. self.addCleanup(shutil.rmtree, repo_dir)
  188. porcelain.init(repo_dir)
  189. def test_bare(self):
  190. repo_dir = tempfile.mkdtemp()
  191. self.addCleanup(shutil.rmtree, repo_dir)
  192. porcelain.init(repo_dir, bare=True)
  193. class AddTests(PorcelainTestCase):
  194. def test_add_default_paths(self):
  195. # create a file for initial commit
  196. with open(os.path.join(self.repo.path, 'blah'), 'w') as f:
  197. f.write("\n")
  198. porcelain.add(repo=self.repo.path, paths=['blah'])
  199. porcelain.commit(repo=self.repo.path, message=b'test',
  200. author=b'test', committer=b'test')
  201. # Add a second test file and a file in a directory
  202. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  203. f.write("\n")
  204. os.mkdir(os.path.join(self.repo.path, 'adir'))
  205. with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f:
  206. f.write("\n")
  207. cwd = os.getcwd()
  208. try:
  209. os.chdir(self.repo.path)
  210. porcelain.add(self.repo.path)
  211. finally:
  212. os.chdir(cwd)
  213. # Check that foo was added and nothing in .git was modified
  214. index = self.repo.open_index()
  215. self.assertEqual(sorted(index), [b'adir/afile', b'blah', b'foo'])
  216. def test_add_default_paths_subdir(self):
  217. os.mkdir(os.path.join(self.repo.path, 'foo'))
  218. with open(os.path.join(self.repo.path, 'blah'), 'w') as f:
  219. f.write("\n")
  220. with open(os.path.join(self.repo.path, 'foo', 'blie'), 'w') as f:
  221. f.write("\n")
  222. cwd = os.getcwd()
  223. try:
  224. os.chdir(os.path.join(self.repo.path, 'foo'))
  225. porcelain.add(repo=self.repo.path)
  226. porcelain.commit(repo=self.repo.path, message=b'test',
  227. author=b'test', committer=b'test')
  228. finally:
  229. os.chdir(cwd)
  230. index = self.repo.open_index()
  231. self.assertEqual(sorted(index), [b'foo/blie'])
  232. def test_add_file(self):
  233. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  234. f.write("BAR")
  235. porcelain.add(self.repo.path, paths=["foo"])
  236. self.assertIn(b"foo", self.repo.open_index())
  237. def test_add_ignored(self):
  238. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  239. f.write("foo")
  240. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  241. f.write("BAR")
  242. with open(os.path.join(self.repo.path, 'bar'), 'w') as f:
  243. f.write("BAR")
  244. (added, ignored) = porcelain.add(self.repo.path, paths=["foo", "bar"])
  245. self.assertIn(b"bar", self.repo.open_index())
  246. self.assertEqual(set(['bar']), set(added))
  247. self.assertEqual(set(['foo']), ignored)
  248. def test_add_file_absolute_path(self):
  249. # Absolute paths are (not yet) supported
  250. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  251. f.write("BAR")
  252. porcelain.add(self.repo, paths=[os.path.join(self.repo.path, "foo")])
  253. self.assertIn(b"foo", self.repo.open_index())
  254. class RemoveTests(PorcelainTestCase):
  255. def test_remove_file(self):
  256. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  257. f.write("BAR")
  258. porcelain.add(self.repo.path, paths=["foo"])
  259. porcelain.rm(self.repo.path, paths=["foo"])
  260. class LogTests(PorcelainTestCase):
  261. def test_simple(self):
  262. c1, c2, c3 = build_commit_graph(
  263. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  264. self.repo.refs[b"HEAD"] = c3.id
  265. outstream = StringIO()
  266. porcelain.log(self.repo.path, outstream=outstream)
  267. self.assertEqual(3, outstream.getvalue().count("-" * 50))
  268. def test_max_entries(self):
  269. c1, c2, c3 = build_commit_graph(
  270. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  271. self.repo.refs[b"HEAD"] = c3.id
  272. outstream = StringIO()
  273. porcelain.log(self.repo.path, outstream=outstream, max_entries=1)
  274. self.assertEqual(1, outstream.getvalue().count("-" * 50))
  275. class ShowTests(PorcelainTestCase):
  276. def test_nolist(self):
  277. c1, c2, c3 = build_commit_graph(
  278. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  279. self.repo.refs[b"HEAD"] = c3.id
  280. outstream = StringIO()
  281. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  282. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  283. def test_simple(self):
  284. c1, c2, c3 = build_commit_graph(
  285. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  286. self.repo.refs[b"HEAD"] = c3.id
  287. outstream = StringIO()
  288. porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
  289. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  290. def test_blob(self):
  291. b = Blob.from_string(b"The Foo\n")
  292. self.repo.object_store.add_object(b)
  293. outstream = StringIO()
  294. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  295. self.assertEqual(outstream.getvalue(), "The Foo\n")
  296. class SymbolicRefTests(PorcelainTestCase):
  297. def test_set_wrong_symbolic_ref(self):
  298. c1, c2, c3 = build_commit_graph(
  299. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  300. self.repo.refs[b"HEAD"] = c3.id
  301. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path,
  302. b'foobar')
  303. def test_set_force_wrong_symbolic_ref(self):
  304. c1, c2, c3 = build_commit_graph(
  305. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  306. self.repo.refs[b"HEAD"] = c3.id
  307. porcelain.symbolic_ref(self.repo.path, b'force_foobar', force=True)
  308. # test if we actually changed the file
  309. with self.repo.get_named_file('HEAD') as f:
  310. new_ref = f.read()
  311. self.assertEqual(new_ref, b'ref: refs/heads/force_foobar\n')
  312. def test_set_symbolic_ref(self):
  313. c1, c2, c3 = build_commit_graph(
  314. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  315. self.repo.refs[b"HEAD"] = c3.id
  316. porcelain.symbolic_ref(self.repo.path, b'master')
  317. def test_set_symbolic_ref_other_than_master(self):
  318. c1, c2, c3 = build_commit_graph(
  319. self.repo.object_store, [[1], [2, 1], [3, 1, 2]],
  320. attrs=dict(refs='develop'))
  321. self.repo.refs[b"HEAD"] = c3.id
  322. self.repo.refs[b"refs/heads/develop"] = c3.id
  323. porcelain.symbolic_ref(self.repo.path, b'develop')
  324. # test if we actually changed the file
  325. with self.repo.get_named_file('HEAD') as f:
  326. new_ref = f.read()
  327. self.assertEqual(new_ref, b'ref: refs/heads/develop\n')
  328. class DiffTreeTests(PorcelainTestCase):
  329. def test_empty(self):
  330. c1, c2, c3 = build_commit_graph(
  331. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  332. self.repo.refs[b"HEAD"] = c3.id
  333. outstream = BytesIO()
  334. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree,
  335. outstream=outstream)
  336. self.assertEqual(outstream.getvalue(), b"")
  337. class CommitTreeTests(PorcelainTestCase):
  338. def test_simple(self):
  339. c1, c2, c3 = build_commit_graph(
  340. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  341. b = Blob()
  342. b.data = b"foo the bar"
  343. t = Tree()
  344. t.add(b"somename", 0o100644, b.id)
  345. self.repo.object_store.add_object(t)
  346. self.repo.object_store.add_object(b)
  347. sha = porcelain.commit_tree(
  348. self.repo.path, t.id, message=b"Withcommit.",
  349. author=b"Joe <joe@example.com>",
  350. committer=b"Jane <jane@example.com>")
  351. self.assertTrue(isinstance(sha, bytes))
  352. self.assertEqual(len(sha), 40)
  353. class RevListTests(PorcelainTestCase):
  354. def test_simple(self):
  355. c1, c2, c3 = build_commit_graph(
  356. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  357. outstream = BytesIO()
  358. porcelain.rev_list(
  359. self.repo.path, [c3.id], outstream=outstream)
  360. self.assertEqual(
  361. c3.id + b"\n" +
  362. c2.id + b"\n" +
  363. c1.id + b"\n",
  364. outstream.getvalue())
  365. class TagCreateTests(PorcelainTestCase):
  366. def test_annotated(self):
  367. c1, c2, c3 = build_commit_graph(
  368. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  369. self.repo.refs[b"HEAD"] = c3.id
  370. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  371. b'bar', annotated=True)
  372. tags = self.repo.refs.as_dict(b"refs/tags")
  373. self.assertEqual(list(tags.keys()), [b"tryme"])
  374. tag = self.repo[b'refs/tags/tryme']
  375. self.assertTrue(isinstance(tag, Tag))
  376. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  377. self.assertEqual(b"bar", tag.message)
  378. self.assertLess(time.time() - tag.tag_time, 5)
  379. def test_unannotated(self):
  380. c1, c2, c3 = build_commit_graph(
  381. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  382. self.repo.refs[b"HEAD"] = c3.id
  383. porcelain.tag_create(self.repo.path, b"tryme", annotated=False)
  384. tags = self.repo.refs.as_dict(b"refs/tags")
  385. self.assertEqual(list(tags.keys()), [b"tryme"])
  386. self.repo[b'refs/tags/tryme']
  387. self.assertEqual(list(tags.values()), [self.repo.head()])
  388. class TagListTests(PorcelainTestCase):
  389. def test_empty(self):
  390. tags = porcelain.tag_list(self.repo.path)
  391. self.assertEqual([], tags)
  392. def test_simple(self):
  393. self.repo.refs[b"refs/tags/foo"] = b"aa" * 20
  394. self.repo.refs[b"refs/tags/bar/bla"] = b"bb" * 20
  395. tags = porcelain.tag_list(self.repo.path)
  396. self.assertEqual([b"bar/bla", b"foo"], tags)
  397. class TagDeleteTests(PorcelainTestCase):
  398. def test_simple(self):
  399. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  400. self.repo[b"HEAD"] = c1.id
  401. porcelain.tag_create(self.repo, b'foo')
  402. self.assertTrue(b"foo" in porcelain.tag_list(self.repo))
  403. porcelain.tag_delete(self.repo, b'foo')
  404. self.assertFalse(b"foo" in porcelain.tag_list(self.repo))
  405. class ResetTests(PorcelainTestCase):
  406. def test_hard_head(self):
  407. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  408. f.write("BAR")
  409. porcelain.add(self.repo.path, paths=["foo"])
  410. porcelain.commit(self.repo.path, message=b"Some message",
  411. committer=b"Jane <jane@example.com>",
  412. author=b"John <john@example.com>")
  413. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  414. f.write(b"OOH")
  415. porcelain.reset(self.repo, "hard", b"HEAD")
  416. index = self.repo.open_index()
  417. changes = list(tree_changes(self.repo,
  418. index.commit(self.repo.object_store),
  419. self.repo[b'HEAD'].tree))
  420. self.assertEqual([], changes)
  421. def test_hard_commit(self):
  422. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  423. f.write("BAR")
  424. porcelain.add(self.repo.path, paths=["foo"])
  425. sha = porcelain.commit(self.repo.path, message=b"Some message",
  426. committer=b"Jane <jane@example.com>",
  427. author=b"John <john@example.com>")
  428. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  429. f.write(b"BAZ")
  430. porcelain.add(self.repo.path, paths=["foo"])
  431. porcelain.commit(self.repo.path, message=b"Some other message",
  432. committer=b"Jane <jane@example.com>",
  433. author=b"John <john@example.com>")
  434. porcelain.reset(self.repo, "hard", sha)
  435. index = self.repo.open_index()
  436. changes = list(tree_changes(self.repo,
  437. index.commit(self.repo.object_store),
  438. self.repo[sha].tree))
  439. self.assertEqual([], changes)
  440. class PushTests(PorcelainTestCase):
  441. def test_simple(self):
  442. """
  443. Basic test of porcelain push where self.repo is the remote. First
  444. clone the remote, commit a file to the clone, then push the changes
  445. back to the remote.
  446. """
  447. outstream = BytesIO()
  448. errstream = BytesIO()
  449. porcelain.commit(repo=self.repo.path, message=b'init',
  450. author=b'', committer=b'')
  451. # Setup target repo cloned from temp test repo
  452. clone_path = tempfile.mkdtemp()
  453. self.addCleanup(shutil.rmtree, clone_path)
  454. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  455. errstream=errstream)
  456. try:
  457. self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD'])
  458. finally:
  459. target_repo.close()
  460. # create a second file to be pushed back to origin
  461. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  462. os.close(handle)
  463. porcelain.add(repo=clone_path, paths=[os.path.basename(fullpath)])
  464. porcelain.commit(repo=clone_path, message=b'push',
  465. author=b'', committer=b'')
  466. # Setup a non-checked out branch in the remote
  467. refs_path = b"refs/heads/foo"
  468. new_id = self.repo[b'HEAD'].id
  469. self.assertNotEqual(new_id, ZERO_SHA)
  470. self.repo.refs[refs_path] = new_id
  471. # Push to the remote
  472. porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path,
  473. outstream=outstream, errstream=errstream)
  474. # Check that the target and source
  475. with Repo(clone_path) as r_clone:
  476. self.assertEqual({
  477. b'HEAD': new_id,
  478. b'refs/heads/foo': r_clone[b'HEAD'].id,
  479. b'refs/heads/master': new_id,
  480. }, self.repo.get_refs())
  481. self.assertEqual(r_clone[b'HEAD'].id, self.repo.refs[refs_path])
  482. # Get the change in the target repo corresponding to the add
  483. # this will be in the foo branch.
  484. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree,
  485. self.repo[b'refs/heads/foo'].tree))[0]
  486. self.assertEqual(os.path.basename(fullpath),
  487. change.new.path.decode('ascii'))
  488. def test_delete(self):
  489. """Basic test of porcelain push, removing a branch.
  490. """
  491. outstream = BytesIO()
  492. errstream = BytesIO()
  493. porcelain.commit(repo=self.repo.path, message=b'init',
  494. author=b'', committer=b'')
  495. # Setup target repo cloned from temp test repo
  496. clone_path = tempfile.mkdtemp()
  497. self.addCleanup(shutil.rmtree, clone_path)
  498. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  499. errstream=errstream)
  500. target_repo.close()
  501. # Setup a non-checked out branch in the remote
  502. refs_path = b"refs/heads/foo"
  503. new_id = self.repo[b'HEAD'].id
  504. self.assertNotEqual(new_id, ZERO_SHA)
  505. self.repo.refs[refs_path] = new_id
  506. # Push to the remote
  507. porcelain.push(clone_path, self.repo.path, b":" + refs_path,
  508. outstream=outstream, errstream=errstream)
  509. self.assertEqual({
  510. b'HEAD': new_id,
  511. b'refs/heads/master': new_id,
  512. }, self.repo.get_refs())
  513. class PullTests(PorcelainTestCase):
  514. def setUp(self):
  515. super(PullTests, self).setUp()
  516. # create a file for initial commit
  517. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  518. os.close(handle)
  519. filename = os.path.basename(fullpath)
  520. porcelain.add(repo=self.repo.path, paths=filename)
  521. porcelain.commit(repo=self.repo.path, message=b'test',
  522. author=b'test', committer=b'test')
  523. # Setup target repo
  524. self.target_path = tempfile.mkdtemp()
  525. self.addCleanup(shutil.rmtree, self.target_path)
  526. target_repo = porcelain.clone(self.repo.path, target=self.target_path,
  527. errstream=BytesIO())
  528. target_repo.close()
  529. # create a second file to be pushed
  530. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  531. os.close(handle)
  532. filename = os.path.basename(fullpath)
  533. porcelain.add(repo=self.repo.path, paths=filename)
  534. porcelain.commit(repo=self.repo.path, message=b'test2',
  535. author=b'test2', committer=b'test2')
  536. self.assertTrue(b'refs/heads/master' in self.repo.refs)
  537. self.assertTrue(b'refs/heads/master' in target_repo.refs)
  538. def test_simple(self):
  539. outstream = BytesIO()
  540. errstream = BytesIO()
  541. # Pull changes into the cloned repo
  542. porcelain.pull(self.target_path, self.repo.path, b'refs/heads/master',
  543. outstream=outstream, errstream=errstream)
  544. # Check the target repo for pushed changes
  545. with Repo(self.target_path) as r:
  546. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  547. def test_no_refspec(self):
  548. outstream = BytesIO()
  549. errstream = BytesIO()
  550. # Pull changes into the cloned repo
  551. porcelain.pull(self.target_path, self.repo.path, outstream=outstream,
  552. errstream=errstream)
  553. # Check the target repo for pushed changes
  554. with Repo(self.target_path) as r:
  555. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  556. class StatusTests(PorcelainTestCase):
  557. def test_empty(self):
  558. results = porcelain.status(self.repo)
  559. self.assertEqual(
  560. {'add': [], 'delete': [], 'modify': []},
  561. results.staged)
  562. self.assertEqual([], results.unstaged)
  563. def test_status(self):
  564. """Integration test for `status` functionality."""
  565. # Commit a dummy file then modify it
  566. fullpath = os.path.join(self.repo.path, 'foo')
  567. with open(fullpath, 'w') as f:
  568. f.write('origstuff')
  569. porcelain.add(repo=self.repo.path, paths=['foo'])
  570. porcelain.commit(repo=self.repo.path, message=b'test status',
  571. author=b'', committer=b'')
  572. # modify access and modify time of path
  573. os.utime(fullpath, (0, 0))
  574. with open(fullpath, 'wb') as f:
  575. f.write(b'stuff')
  576. # Make a dummy file and stage it
  577. filename_add = 'bar'
  578. fullpath = os.path.join(self.repo.path, filename_add)
  579. with open(fullpath, 'w') as f:
  580. f.write('stuff')
  581. porcelain.add(repo=self.repo.path, paths=filename_add)
  582. results = porcelain.status(self.repo)
  583. self.assertEqual(results.staged['add'][0],
  584. filename_add.encode('ascii'))
  585. self.assertEqual(results.unstaged, [b'foo'])
  586. def test_get_tree_changes_add(self):
  587. """Unit test for get_tree_changes add."""
  588. # Make a dummy file, stage
  589. filename = 'bar'
  590. with open(os.path.join(self.repo.path, filename), 'w') as f:
  591. f.write('stuff')
  592. porcelain.add(repo=self.repo.path, paths=filename)
  593. porcelain.commit(repo=self.repo.path, message=b'test status',
  594. author=b'', committer=b'')
  595. filename = 'foo'
  596. with open(os.path.join(self.repo.path, filename), 'w') as f:
  597. f.write('stuff')
  598. porcelain.add(repo=self.repo.path, paths=filename)
  599. changes = porcelain.get_tree_changes(self.repo.path)
  600. self.assertEqual(changes['add'][0], filename.encode('ascii'))
  601. self.assertEqual(len(changes['add']), 1)
  602. self.assertEqual(len(changes['modify']), 0)
  603. self.assertEqual(len(changes['delete']), 0)
  604. def test_get_tree_changes_modify(self):
  605. """Unit test for get_tree_changes modify."""
  606. # Make a dummy file, stage, commit, modify
  607. filename = 'foo'
  608. fullpath = os.path.join(self.repo.path, filename)
  609. with open(fullpath, 'w') as f:
  610. f.write('stuff')
  611. porcelain.add(repo=self.repo.path, paths=filename)
  612. porcelain.commit(repo=self.repo.path, message=b'test status',
  613. author=b'', committer=b'')
  614. with open(fullpath, 'w') as f:
  615. f.write('otherstuff')
  616. porcelain.add(repo=self.repo.path, paths=filename)
  617. changes = porcelain.get_tree_changes(self.repo.path)
  618. self.assertEqual(changes['modify'][0], filename.encode('ascii'))
  619. self.assertEqual(len(changes['add']), 0)
  620. self.assertEqual(len(changes['modify']), 1)
  621. self.assertEqual(len(changes['delete']), 0)
  622. def test_get_tree_changes_delete(self):
  623. """Unit test for get_tree_changes delete."""
  624. # Make a dummy file, stage, commit, remove
  625. filename = 'foo'
  626. with open(os.path.join(self.repo.path, filename), 'w') as f:
  627. f.write('stuff')
  628. porcelain.add(repo=self.repo.path, paths=filename)
  629. porcelain.commit(repo=self.repo.path, message=b'test status',
  630. author=b'', committer=b'')
  631. porcelain.rm(repo=self.repo.path, paths=[filename])
  632. changes = porcelain.get_tree_changes(self.repo.path)
  633. self.assertEqual(changes['delete'][0], filename.encode('ascii'))
  634. self.assertEqual(len(changes['add']), 0)
  635. self.assertEqual(len(changes['modify']), 0)
  636. self.assertEqual(len(changes['delete']), 1)
  637. def test_get_untracked_paths(self):
  638. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  639. f.write('ignored\n')
  640. with open(os.path.join(self.repo.path, 'ignored'), 'w') as f:
  641. f.write('blah\n')
  642. with open(os.path.join(self.repo.path, 'notignored'), 'w') as f:
  643. f.write('blah\n')
  644. self.assertEqual(
  645. set(['ignored', 'notignored', '.gitignore']),
  646. set(porcelain.get_untracked_paths(self.repo.path, self.repo.path,
  647. self.repo.open_index())))
  648. self.assertEqual(set(['.gitignore', 'notignored']),
  649. set(porcelain.status(self.repo).untracked))
  650. def test_get_untracked_paths_nested(self):
  651. with open(os.path.join(self.repo.path, 'notignored'), 'w') as f:
  652. f.write('blah\n')
  653. subrepo = Repo.init(os.path.join(self.repo.path, 'nested'), mkdir=True)
  654. with open(os.path.join(subrepo.path, 'another'), 'w') as f:
  655. f.write('foo\n')
  656. self.assertEqual(
  657. set(['notignored']),
  658. set(porcelain.get_untracked_paths(self.repo.path, self.repo.path,
  659. self.repo.open_index())))
  660. self.assertEqual(
  661. set(['another']),
  662. set(porcelain.get_untracked_paths(subrepo.path, subrepo.path,
  663. subrepo.open_index())))
  664. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  665. class UploadPackTests(PorcelainTestCase):
  666. """Tests for upload_pack."""
  667. def test_upload_pack(self):
  668. outf = BytesIO()
  669. exitcode = porcelain.upload_pack(
  670. self.repo.path, BytesIO(b"0000"), outf)
  671. outlines = outf.getvalue().splitlines()
  672. self.assertEqual([b"0000"], outlines)
  673. self.assertEqual(0, exitcode)
  674. class ReceivePackTests(PorcelainTestCase):
  675. """Tests for receive_pack."""
  676. def test_receive_pack(self):
  677. filename = 'foo'
  678. with open(os.path.join(self.repo.path, filename), 'w') as f:
  679. f.write('stuff')
  680. porcelain.add(repo=self.repo.path, paths=filename)
  681. self.repo.do_commit(message=b'test status',
  682. author=b'', committer=b'',
  683. author_timestamp=1402354300,
  684. commit_timestamp=1402354300, author_timezone=0,
  685. commit_timezone=0)
  686. outf = BytesIO()
  687. exitcode = porcelain.receive_pack(
  688. self.repo.path, BytesIO(b"0000"), outf)
  689. outlines = outf.getvalue().splitlines()
  690. self.assertEqual([
  691. b'00739e65bdcf4a22cdd4f3700604a275cd2aaf146b23 HEAD\x00 report-status ' # noqa: E501
  692. b'delete-refs quiet ofs-delta side-band-64k no-done',
  693. b'003f9e65bdcf4a22cdd4f3700604a275cd2aaf146b23 refs/heads/master',
  694. b'0000'], outlines)
  695. self.assertEqual(0, exitcode)
  696. class BranchListTests(PorcelainTestCase):
  697. def test_standard(self):
  698. self.assertEqual(set([]), set(porcelain.branch_list(self.repo)))
  699. def test_new_branch(self):
  700. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  701. self.repo[b"HEAD"] = c1.id
  702. porcelain.branch_create(self.repo, b"foo")
  703. self.assertEqual(
  704. set([b"master", b"foo"]),
  705. set(porcelain.branch_list(self.repo)))
  706. class BranchCreateTests(PorcelainTestCase):
  707. def test_branch_exists(self):
  708. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  709. self.repo[b"HEAD"] = c1.id
  710. porcelain.branch_create(self.repo, b"foo")
  711. self.assertRaises(KeyError, porcelain.branch_create, self.repo, b"foo")
  712. porcelain.branch_create(self.repo, b"foo", force=True)
  713. def test_new_branch(self):
  714. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  715. self.repo[b"HEAD"] = c1.id
  716. porcelain.branch_create(self.repo, b"foo")
  717. self.assertEqual(
  718. set([b"master", b"foo"]),
  719. set(porcelain.branch_list(self.repo)))
  720. class BranchDeleteTests(PorcelainTestCase):
  721. def test_simple(self):
  722. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  723. self.repo[b"HEAD"] = c1.id
  724. porcelain.branch_create(self.repo, b'foo')
  725. self.assertTrue(b"foo" in porcelain.branch_list(self.repo))
  726. porcelain.branch_delete(self.repo, b'foo')
  727. self.assertFalse(b"foo" in porcelain.branch_list(self.repo))
  728. class FetchTests(PorcelainTestCase):
  729. def test_simple(self):
  730. outstream = BytesIO()
  731. errstream = BytesIO()
  732. # create a file for initial commit
  733. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  734. os.close(handle)
  735. filename = os.path.basename(fullpath)
  736. porcelain.add(repo=self.repo.path, paths=filename)
  737. porcelain.commit(repo=self.repo.path, message=b'test',
  738. author=b'test', committer=b'test')
  739. # Setup target repo
  740. target_path = tempfile.mkdtemp()
  741. self.addCleanup(shutil.rmtree, target_path)
  742. target_repo = porcelain.clone(self.repo.path, target=target_path,
  743. errstream=errstream)
  744. # create a second file to be pushed
  745. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  746. os.close(handle)
  747. filename = os.path.basename(fullpath)
  748. porcelain.add(repo=self.repo.path, paths=filename)
  749. porcelain.commit(repo=self.repo.path, message=b'test2',
  750. author=b'test2', committer=b'test2')
  751. self.assertFalse(self.repo[b'HEAD'].id in target_repo)
  752. target_repo.close()
  753. # Fetch changes into the cloned repo
  754. porcelain.fetch(target_path, self.repo.path, outstream=outstream,
  755. errstream=errstream)
  756. # Check the target repo for pushed changes
  757. with Repo(target_path) as r:
  758. self.assertTrue(self.repo[b'HEAD'].id in r)
  759. class RepackTests(PorcelainTestCase):
  760. def test_empty(self):
  761. porcelain.repack(self.repo)
  762. def test_simple(self):
  763. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  764. os.close(handle)
  765. filename = os.path.basename(fullpath)
  766. porcelain.add(repo=self.repo.path, paths=filename)
  767. porcelain.repack(self.repo)
  768. class LsTreeTests(PorcelainTestCase):
  769. def test_empty(self):
  770. porcelain.commit(repo=self.repo.path, message=b'test status',
  771. author=b'', committer=b'')
  772. f = StringIO()
  773. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  774. self.assertEqual(f.getvalue(), "")
  775. def test_simple(self):
  776. # Commit a dummy file then modify it
  777. fullpath = os.path.join(self.repo.path, 'foo')
  778. with open(fullpath, 'w') as f:
  779. f.write('origstuff')
  780. porcelain.add(repo=self.repo.path, paths=['foo'])
  781. porcelain.commit(repo=self.repo.path, message=b'test status',
  782. author=b'', committer=b'')
  783. f = StringIO()
  784. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  785. self.assertEqual(
  786. f.getvalue(),
  787. '100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n')
  788. class LsRemoteTests(PorcelainTestCase):
  789. def test_empty(self):
  790. self.assertEqual({}, porcelain.ls_remote(self.repo.path))
  791. def test_some(self):
  792. cid = porcelain.commit(repo=self.repo.path, message=b'test status',
  793. author=b'', committer=b'')
  794. self.assertEqual({
  795. b'refs/heads/master': cid,
  796. b'HEAD': cid},
  797. porcelain.ls_remote(self.repo.path))
  798. class RemoteAddTests(PorcelainTestCase):
  799. def test_new(self):
  800. porcelain.remote_add(
  801. self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
  802. c = self.repo.get_config()
  803. self.assertEqual(
  804. c.get((b'remote', b'jelmer'), b'url'),
  805. b'git://jelmer.uk/code/dulwich')
  806. def test_exists(self):
  807. porcelain.remote_add(
  808. self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
  809. self.assertRaises(porcelain.RemoteExists, porcelain.remote_add,
  810. self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
  811. class CheckIgnoreTests(PorcelainTestCase):
  812. def test_check_ignored(self):
  813. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  814. f.write("foo")
  815. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  816. f.write("BAR")
  817. with open(os.path.join(self.repo.path, 'bar'), 'w') as f:
  818. f.write("BAR")
  819. self.assertEqual(
  820. ['foo'],
  821. list(porcelain.check_ignore(self.repo, ['foo'])))
  822. self.assertEqual([], list(porcelain.check_ignore(self.repo, ['bar'])))
  823. def test_check_added(self):
  824. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  825. f.write("BAR")
  826. self.repo.stage(['foo'])
  827. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  828. f.write("foo\n")
  829. self.assertEqual(
  830. [], list(porcelain.check_ignore(self.repo, ['foo'])))
  831. self.assertEqual(
  832. ['foo'],
  833. list(porcelain.check_ignore(self.repo, ['foo'], no_index=True)))