test_porcelain.py 42 KB

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