test_porcelain.py 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. # test_porcelain.py -- porcelain tests
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@jelmer.uk>
  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 (
  40. NoIndexPresent,
  41. Repo,
  42. )
  43. from dulwich.tests import (
  44. TestCase,
  45. )
  46. from dulwich.tests.utils import (
  47. build_commit_graph,
  48. make_commit,
  49. make_object,
  50. )
  51. class PorcelainTestCase(TestCase):
  52. def setUp(self):
  53. super(PorcelainTestCase, self).setUp()
  54. self.test_dir = tempfile.mkdtemp()
  55. self.addCleanup(shutil.rmtree, self.test_dir)
  56. self.repo = Repo.init(os.path.join(self.test_dir, 'repo'), mkdir=True)
  57. self.addCleanup(self.repo.close)
  58. class ArchiveTests(PorcelainTestCase):
  59. """Tests for the archive command."""
  60. def test_simple(self):
  61. c1, c2, c3 = build_commit_graph(
  62. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  63. self.repo.refs[b"refs/heads/master"] = c3.id
  64. out = BytesIO()
  65. err = BytesIO()
  66. porcelain.archive(self.repo.path, b"refs/heads/master", outstream=out,
  67. errstream=err)
  68. self.assertEqual(b"", err.getvalue())
  69. tf = tarfile.TarFile(fileobj=out)
  70. self.addCleanup(tf.close)
  71. self.assertEqual([], tf.getnames())
  72. class UpdateServerInfoTests(PorcelainTestCase):
  73. def test_simple(self):
  74. c1, c2, c3 = build_commit_graph(
  75. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  76. self.repo.refs[b"refs/heads/foo"] = c3.id
  77. porcelain.update_server_info(self.repo.path)
  78. self.assertTrue(os.path.exists(
  79. os.path.join(self.repo.controldir(), 'info', 'refs')))
  80. class CommitTests(PorcelainTestCase):
  81. def test_custom_author(self):
  82. c1, c2, c3 = build_commit_graph(
  83. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  84. self.repo.refs[b"refs/heads/foo"] = c3.id
  85. sha = porcelain.commit(
  86. self.repo.path, message=b"Some message",
  87. author=b"Joe <joe@example.com>",
  88. committer=b"Bob <bob@example.com>")
  89. self.assertTrue(isinstance(sha, bytes))
  90. self.assertEqual(len(sha), 40)
  91. def test_unicode(self):
  92. c1, c2, c3 = build_commit_graph(
  93. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  94. self.repo.refs[b"refs/heads/foo"] = c3.id
  95. sha = porcelain.commit(
  96. self.repo.path, message="Some message",
  97. author="Joe <joe@example.com>",
  98. committer="Bob <bob@example.com>")
  99. self.assertTrue(isinstance(sha, bytes))
  100. self.assertEqual(len(sha), 40)
  101. class CloneTests(PorcelainTestCase):
  102. def test_simple_local(self):
  103. f1_1 = make_object(Blob, data=b'f1')
  104. commit_spec = [[1], [2, 1], [3, 1, 2]]
  105. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  106. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  107. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  108. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  109. commit_spec, trees)
  110. self.repo.refs[b"refs/heads/master"] = c3.id
  111. self.repo.refs[b"refs/tags/foo"] = c3.id
  112. target_path = tempfile.mkdtemp()
  113. errstream = BytesIO()
  114. self.addCleanup(shutil.rmtree, target_path)
  115. r = porcelain.clone(self.repo.path, target_path,
  116. checkout=False, errstream=errstream)
  117. self.addCleanup(r.close)
  118. self.assertEqual(r.path, target_path)
  119. target_repo = Repo(target_path)
  120. self.assertEqual(0, len(target_repo.open_index()))
  121. self.assertEqual(c3.id, target_repo.refs[b'refs/tags/foo'])
  122. self.assertTrue(b'f1' not in os.listdir(target_path))
  123. self.assertTrue(b'f2' not in os.listdir(target_path))
  124. c = r.get_config()
  125. encoded_path = self.repo.path
  126. if not isinstance(encoded_path, bytes):
  127. encoded_path = encoded_path.encode('utf-8')
  128. self.assertEqual(encoded_path, c.get((b'remote', b'origin'), b'url'))
  129. self.assertEqual(
  130. b'+refs/heads/*:refs/remotes/origin/*',
  131. c.get((b'remote', b'origin'), b'fetch'))
  132. def test_simple_local_with_checkout(self):
  133. f1_1 = make_object(Blob, data=b'f1')
  134. commit_spec = [[1], [2, 1], [3, 1, 2]]
  135. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  136. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  137. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  138. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  139. commit_spec, trees)
  140. self.repo.refs[b"refs/heads/master"] = c3.id
  141. target_path = tempfile.mkdtemp()
  142. errstream = BytesIO()
  143. self.addCleanup(shutil.rmtree, target_path)
  144. with porcelain.clone(self.repo.path, target_path,
  145. checkout=True,
  146. errstream=errstream) as r:
  147. self.assertEqual(r.path, target_path)
  148. with Repo(target_path) as r:
  149. self.assertEqual(r.head(), c3.id)
  150. self.assertTrue('f1' in os.listdir(target_path))
  151. self.assertTrue('f2' in os.listdir(target_path))
  152. def test_bare_local_with_checkout(self):
  153. f1_1 = make_object(Blob, data=b'f1')
  154. commit_spec = [[1], [2, 1], [3, 1, 2]]
  155. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)],
  156. 2: [(b'f1', f1_1), (b'f2', f1_1)],
  157. 3: [(b'f1', f1_1), (b'f2', f1_1)], }
  158. c1, c2, c3 = build_commit_graph(self.repo.object_store,
  159. commit_spec, trees)
  160. self.repo.refs[b"refs/heads/master"] = c3.id
  161. target_path = tempfile.mkdtemp()
  162. errstream = BytesIO()
  163. self.addCleanup(shutil.rmtree, target_path)
  164. with porcelain.clone(
  165. self.repo.path, target_path, bare=True,
  166. errstream=errstream) as r:
  167. self.assertEqual(r.path, target_path)
  168. with Repo(target_path) as r:
  169. r.head()
  170. self.assertRaises(NoIndexPresent, r.open_index)
  171. self.assertFalse(b'f1' in os.listdir(target_path))
  172. self.assertFalse(b'f2' in os.listdir(target_path))
  173. def test_no_checkout_with_bare(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. self.repo.refs[b"HEAD"] = c1.id
  180. target_path = tempfile.mkdtemp()
  181. errstream = BytesIO()
  182. self.addCleanup(shutil.rmtree, target_path)
  183. self.assertRaises(
  184. ValueError, porcelain.clone, self.repo.path,
  185. target_path, checkout=True, bare=True, errstream=errstream)
  186. def test_no_head_no_checkout(self):
  187. f1_1 = make_object(Blob, data=b'f1')
  188. commit_spec = [[1]]
  189. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]}
  190. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  191. self.repo.refs[b"refs/heads/master"] = c1.id
  192. target_path = tempfile.mkdtemp()
  193. self.addCleanup(shutil.rmtree, target_path)
  194. errstream = BytesIO()
  195. r = porcelain.clone(
  196. self.repo.path, target_path, checkout=True, errstream=errstream)
  197. r.close()
  198. def test_no_head_no_checkout_outstream_errstream_autofallback(self):
  199. f1_1 = make_object(Blob, data=b'f1')
  200. commit_spec = [[1]]
  201. trees = {1: [(b'f1', f1_1), (b'f2', f1_1)]}
  202. (c1, ) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  203. self.repo.refs[b"refs/heads/master"] = c1.id
  204. target_path = tempfile.mkdtemp()
  205. self.addCleanup(shutil.rmtree, target_path)
  206. errstream = porcelain.NoneStream()
  207. r = porcelain.clone(
  208. self.repo.path, target_path, checkout=True, errstream=errstream)
  209. r.close()
  210. class InitTests(TestCase):
  211. def test_non_bare(self):
  212. repo_dir = tempfile.mkdtemp()
  213. self.addCleanup(shutil.rmtree, repo_dir)
  214. porcelain.init(repo_dir)
  215. def test_bare(self):
  216. repo_dir = tempfile.mkdtemp()
  217. self.addCleanup(shutil.rmtree, repo_dir)
  218. porcelain.init(repo_dir, bare=True)
  219. class AddTests(PorcelainTestCase):
  220. def test_add_default_paths(self):
  221. # create a file for initial commit
  222. fullpath = os.path.join(self.repo.path, 'blah')
  223. with open(fullpath, 'w') as f:
  224. f.write("\n")
  225. porcelain.add(repo=self.repo.path, paths=[fullpath])
  226. porcelain.commit(repo=self.repo.path, message=b'test',
  227. author=b'test <email>', committer=b'test <email>')
  228. # Add a second test file and a file in a directory
  229. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  230. f.write("\n")
  231. os.mkdir(os.path.join(self.repo.path, 'adir'))
  232. with open(os.path.join(self.repo.path, 'adir', 'afile'), 'w') as f:
  233. f.write("\n")
  234. cwd = os.getcwd()
  235. try:
  236. os.chdir(self.repo.path)
  237. porcelain.add(self.repo.path)
  238. finally:
  239. os.chdir(cwd)
  240. # Check that foo was added and nothing in .git was modified
  241. index = self.repo.open_index()
  242. self.assertEqual(sorted(index), [b'adir/afile', b'blah', b'foo'])
  243. def test_add_default_paths_subdir(self):
  244. os.mkdir(os.path.join(self.repo.path, 'foo'))
  245. with open(os.path.join(self.repo.path, 'blah'), 'w') as f:
  246. f.write("\n")
  247. with open(os.path.join(self.repo.path, 'foo', 'blie'), 'w') as f:
  248. f.write("\n")
  249. cwd = os.getcwd()
  250. try:
  251. os.chdir(os.path.join(self.repo.path, 'foo'))
  252. porcelain.add(repo=self.repo.path)
  253. porcelain.commit(repo=self.repo.path, message=b'test',
  254. author=b'test <email>',
  255. committer=b'test <email>')
  256. finally:
  257. os.chdir(cwd)
  258. index = self.repo.open_index()
  259. self.assertEqual(sorted(index), [b'foo/blie'])
  260. def test_add_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. self.assertIn(b"foo", self.repo.open_index())
  266. def test_add_ignored(self):
  267. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  268. f.write("foo")
  269. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  270. f.write("BAR")
  271. with open(os.path.join(self.repo.path, 'bar'), 'w') as f:
  272. f.write("BAR")
  273. (added, ignored) = porcelain.add(self.repo.path, paths=[
  274. os.path.join(self.repo.path, "foo"),
  275. os.path.join(self.repo.path, "bar")])
  276. self.assertIn(b"bar", self.repo.open_index())
  277. self.assertEqual(set(['bar']), set(added))
  278. self.assertEqual(set(['foo']), ignored)
  279. def test_add_file_absolute_path(self):
  280. # Absolute paths are (not yet) supported
  281. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  282. f.write("BAR")
  283. porcelain.add(self.repo, paths=[os.path.join(self.repo.path, "foo")])
  284. self.assertIn(b"foo", self.repo.open_index())
  285. def test_add_not_in_repo(self):
  286. with open(os.path.join(self.test_dir, 'foo'), 'w') as f:
  287. f.write("BAR")
  288. self.assertRaises(
  289. ValueError,
  290. porcelain.add, self.repo,
  291. paths=[os.path.join(self.test_dir, "foo")])
  292. self.assertRaises(
  293. ValueError,
  294. porcelain.add, self.repo,
  295. paths=["../foo"])
  296. self.assertEqual([], list(self.repo.open_index()))
  297. class RemoveTests(PorcelainTestCase):
  298. def test_remove_file(self):
  299. fullpath = os.path.join(self.repo.path, 'foo')
  300. with open(fullpath, 'w') as f:
  301. f.write("BAR")
  302. porcelain.add(self.repo.path, paths=[fullpath])
  303. porcelain.commit(repo=self.repo, message=b'test',
  304. author=b'test <email>',
  305. committer=b'test <email>')
  306. self.assertTrue(os.path.exists(os.path.join(self.repo.path, 'foo')))
  307. cwd = os.getcwd()
  308. try:
  309. os.chdir(self.repo.path)
  310. porcelain.remove(self.repo.path, paths=["foo"])
  311. finally:
  312. os.chdir(cwd)
  313. self.assertFalse(os.path.exists(os.path.join(self.repo.path, 'foo')))
  314. def test_remove_file_staged(self):
  315. fullpath = os.path.join(self.repo.path, 'foo')
  316. with open(fullpath, 'w') as f:
  317. f.write("BAR")
  318. cwd = os.getcwd()
  319. try:
  320. os.chdir(self.repo.path)
  321. porcelain.add(self.repo.path, paths=[fullpath])
  322. self.assertRaises(Exception, porcelain.rm, self.repo.path,
  323. paths=["foo"])
  324. finally:
  325. os.chdir(cwd)
  326. class LogTests(PorcelainTestCase):
  327. def test_simple(self):
  328. c1, c2, c3 = build_commit_graph(
  329. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  330. self.repo.refs[b"HEAD"] = c3.id
  331. outstream = StringIO()
  332. porcelain.log(self.repo.path, outstream=outstream)
  333. self.assertEqual(3, outstream.getvalue().count("-" * 50))
  334. def test_max_entries(self):
  335. c1, c2, c3 = build_commit_graph(
  336. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  337. self.repo.refs[b"HEAD"] = c3.id
  338. outstream = StringIO()
  339. porcelain.log(self.repo.path, outstream=outstream, max_entries=1)
  340. self.assertEqual(1, outstream.getvalue().count("-" * 50))
  341. class ShowTests(PorcelainTestCase):
  342. def test_nolist(self):
  343. c1, c2, c3 = build_commit_graph(
  344. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  345. self.repo.refs[b"HEAD"] = c3.id
  346. outstream = StringIO()
  347. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  348. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  349. def test_simple(self):
  350. c1, c2, c3 = build_commit_graph(
  351. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  352. self.repo.refs[b"HEAD"] = c3.id
  353. outstream = StringIO()
  354. porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
  355. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  356. def test_blob(self):
  357. b = Blob.from_string(b"The Foo\n")
  358. self.repo.object_store.add_object(b)
  359. outstream = StringIO()
  360. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  361. self.assertEqual(outstream.getvalue(), "The Foo\n")
  362. def test_commit_no_parent(self):
  363. a = Blob.from_string(b"The Foo\n")
  364. ta = Tree()
  365. ta.add(b"somename", 0o100644, a.id)
  366. ca = make_commit(tree=ta.id)
  367. self.repo.object_store.add_objects([(a, None), (ta, None), (ca, None)])
  368. outstream = StringIO()
  369. porcelain.show(self.repo.path, objects=[ca.id], outstream=outstream)
  370. self.assertMultiLineEqual(outstream.getvalue(), """\
  371. --------------------------------------------------
  372. commit: 344da06c1bb85901270b3e8875c988a027ec087d
  373. Author: Test Author <test@nodomain.com>
  374. Committer: Test Committer <test@nodomain.com>
  375. Date: Fri Jan 01 2010 00:00:00 +0000
  376. Test message.
  377. diff --git /dev/null b/somename
  378. new mode 100644
  379. index 0000000..ea5c7bf 100644
  380. --- /dev/null
  381. +++ b/somename
  382. @@ -0,0 +1 @@
  383. +The Foo
  384. """)
  385. def test_commit_with_change(self):
  386. a = Blob.from_string(b"The Foo\n")
  387. ta = Tree()
  388. ta.add(b"somename", 0o100644, a.id)
  389. ca = make_commit(tree=ta.id)
  390. b = Blob.from_string(b"The Bar\n")
  391. tb = Tree()
  392. tb.add(b"somename", 0o100644, b.id)
  393. cb = make_commit(tree=tb.id, parents=[ca.id])
  394. self.repo.object_store.add_objects(
  395. [(a, None), (b, None), (ta, None), (tb, None),
  396. (ca, None), (cb, None)])
  397. outstream = StringIO()
  398. porcelain.show(self.repo.path, objects=[cb.id], outstream=outstream)
  399. self.assertMultiLineEqual(outstream.getvalue(), """\
  400. --------------------------------------------------
  401. commit: 2c6b6c9cb72c130956657e1fdae58e5b103744fa
  402. Author: Test Author <test@nodomain.com>
  403. Committer: Test Committer <test@nodomain.com>
  404. Date: Fri Jan 01 2010 00:00:00 +0000
  405. Test message.
  406. diff --git a/somename b/somename
  407. index ea5c7bf..fd38bcb 100644
  408. --- a/somename
  409. +++ b/somename
  410. @@ -1 +1 @@
  411. -The Foo
  412. +The Bar
  413. """)
  414. class SymbolicRefTests(PorcelainTestCase):
  415. def test_set_wrong_symbolic_ref(self):
  416. c1, c2, c3 = build_commit_graph(
  417. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  418. self.repo.refs[b"HEAD"] = c3.id
  419. self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path,
  420. b'foobar')
  421. def test_set_force_wrong_symbolic_ref(self):
  422. c1, c2, c3 = build_commit_graph(
  423. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  424. self.repo.refs[b"HEAD"] = c3.id
  425. porcelain.symbolic_ref(self.repo.path, b'force_foobar', force=True)
  426. # test if we actually changed the file
  427. with self.repo.get_named_file('HEAD') as f:
  428. new_ref = f.read()
  429. self.assertEqual(new_ref, b'ref: refs/heads/force_foobar\n')
  430. def test_set_symbolic_ref(self):
  431. c1, c2, c3 = build_commit_graph(
  432. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  433. self.repo.refs[b"HEAD"] = c3.id
  434. porcelain.symbolic_ref(self.repo.path, b'master')
  435. def test_set_symbolic_ref_other_than_master(self):
  436. c1, c2, c3 = build_commit_graph(
  437. self.repo.object_store, [[1], [2, 1], [3, 1, 2]],
  438. attrs=dict(refs='develop'))
  439. self.repo.refs[b"HEAD"] = c3.id
  440. self.repo.refs[b"refs/heads/develop"] = c3.id
  441. porcelain.symbolic_ref(self.repo.path, b'develop')
  442. # test if we actually changed the file
  443. with self.repo.get_named_file('HEAD') as f:
  444. new_ref = f.read()
  445. self.assertEqual(new_ref, b'ref: refs/heads/develop\n')
  446. class DiffTreeTests(PorcelainTestCase):
  447. def test_empty(self):
  448. c1, c2, c3 = build_commit_graph(
  449. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  450. self.repo.refs[b"HEAD"] = c3.id
  451. outstream = BytesIO()
  452. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree,
  453. outstream=outstream)
  454. self.assertEqual(outstream.getvalue(), b"")
  455. class CommitTreeTests(PorcelainTestCase):
  456. def test_simple(self):
  457. c1, c2, c3 = build_commit_graph(
  458. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  459. b = Blob()
  460. b.data = b"foo the bar"
  461. t = Tree()
  462. t.add(b"somename", 0o100644, b.id)
  463. self.repo.object_store.add_object(t)
  464. self.repo.object_store.add_object(b)
  465. sha = porcelain.commit_tree(
  466. self.repo.path, t.id, message=b"Withcommit.",
  467. author=b"Joe <joe@example.com>",
  468. committer=b"Jane <jane@example.com>")
  469. self.assertTrue(isinstance(sha, bytes))
  470. self.assertEqual(len(sha), 40)
  471. class RevListTests(PorcelainTestCase):
  472. def test_simple(self):
  473. c1, c2, c3 = build_commit_graph(
  474. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  475. outstream = BytesIO()
  476. porcelain.rev_list(
  477. self.repo.path, [c3.id], outstream=outstream)
  478. self.assertEqual(
  479. c3.id + b"\n" +
  480. c2.id + b"\n" +
  481. c1.id + b"\n",
  482. outstream.getvalue())
  483. class TagCreateTests(PorcelainTestCase):
  484. def test_annotated(self):
  485. c1, c2, c3 = build_commit_graph(
  486. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  487. self.repo.refs[b"HEAD"] = c3.id
  488. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  489. b'bar', annotated=True)
  490. tags = self.repo.refs.as_dict(b"refs/tags")
  491. self.assertEqual(list(tags.keys()), [b"tryme"])
  492. tag = self.repo[b'refs/tags/tryme']
  493. self.assertTrue(isinstance(tag, Tag))
  494. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  495. self.assertEqual(b"bar", tag.message)
  496. self.assertLess(time.time() - tag.tag_time, 5)
  497. def test_unannotated(self):
  498. c1, c2, c3 = build_commit_graph(
  499. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  500. self.repo.refs[b"HEAD"] = c3.id
  501. porcelain.tag_create(self.repo.path, b"tryme", annotated=False)
  502. tags = self.repo.refs.as_dict(b"refs/tags")
  503. self.assertEqual(list(tags.keys()), [b"tryme"])
  504. self.repo[b'refs/tags/tryme']
  505. self.assertEqual(list(tags.values()), [self.repo.head()])
  506. def test_unannotated_unicode(self):
  507. c1, c2, c3 = build_commit_graph(
  508. self.repo.object_store, [[1], [2, 1], [3, 1, 2]])
  509. self.repo.refs[b"HEAD"] = c3.id
  510. porcelain.tag_create(self.repo.path, "tryme", annotated=False)
  511. tags = self.repo.refs.as_dict(b"refs/tags")
  512. self.assertEqual(list(tags.keys()), [b"tryme"])
  513. self.repo[b'refs/tags/tryme']
  514. self.assertEqual(list(tags.values()), [self.repo.head()])
  515. class TagListTests(PorcelainTestCase):
  516. def test_empty(self):
  517. tags = porcelain.tag_list(self.repo.path)
  518. self.assertEqual([], tags)
  519. def test_simple(self):
  520. self.repo.refs[b"refs/tags/foo"] = b"aa" * 20
  521. self.repo.refs[b"refs/tags/bar/bla"] = b"bb" * 20
  522. tags = porcelain.tag_list(self.repo.path)
  523. self.assertEqual([b"bar/bla", b"foo"], tags)
  524. class TagDeleteTests(PorcelainTestCase):
  525. def test_simple(self):
  526. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  527. self.repo[b"HEAD"] = c1.id
  528. porcelain.tag_create(self.repo, b'foo')
  529. self.assertTrue(b"foo" in porcelain.tag_list(self.repo))
  530. porcelain.tag_delete(self.repo, b'foo')
  531. self.assertFalse(b"foo" in porcelain.tag_list(self.repo))
  532. class ResetTests(PorcelainTestCase):
  533. def test_hard_head(self):
  534. fullpath = os.path.join(self.repo.path, 'foo')
  535. with open(fullpath, 'w') as f:
  536. f.write("BAR")
  537. porcelain.add(self.repo.path, paths=[fullpath])
  538. porcelain.commit(self.repo.path, message=b"Some message",
  539. committer=b"Jane <jane@example.com>",
  540. author=b"John <john@example.com>")
  541. with open(os.path.join(self.repo.path, 'foo'), 'wb') as f:
  542. f.write(b"OOH")
  543. porcelain.reset(self.repo, "hard", b"HEAD")
  544. index = self.repo.open_index()
  545. changes = list(tree_changes(self.repo,
  546. index.commit(self.repo.object_store),
  547. self.repo[b'HEAD'].tree))
  548. self.assertEqual([], changes)
  549. def test_hard_commit(self):
  550. fullpath = os.path.join(self.repo.path, 'foo')
  551. with open(fullpath, 'w') as f:
  552. f.write("BAR")
  553. porcelain.add(self.repo.path, paths=[fullpath])
  554. sha = porcelain.commit(self.repo.path, message=b"Some message",
  555. committer=b"Jane <jane@example.com>",
  556. author=b"John <john@example.com>")
  557. with open(fullpath, 'wb') as f:
  558. f.write(b"BAZ")
  559. porcelain.add(self.repo.path, paths=[fullpath])
  560. porcelain.commit(self.repo.path, message=b"Some other message",
  561. committer=b"Jane <jane@example.com>",
  562. author=b"John <john@example.com>")
  563. porcelain.reset(self.repo, "hard", sha)
  564. index = self.repo.open_index()
  565. changes = list(tree_changes(self.repo,
  566. index.commit(self.repo.object_store),
  567. self.repo[sha].tree))
  568. self.assertEqual([], changes)
  569. class PushTests(PorcelainTestCase):
  570. def test_simple(self):
  571. """
  572. Basic test of porcelain push where self.repo is the remote. First
  573. clone the remote, commit a file to the clone, then push the changes
  574. back to the remote.
  575. """
  576. outstream = BytesIO()
  577. errstream = BytesIO()
  578. porcelain.commit(repo=self.repo.path, message=b'init',
  579. author=b'author <email>',
  580. committer=b'committer <email>')
  581. # Setup target repo cloned from temp test repo
  582. clone_path = tempfile.mkdtemp()
  583. self.addCleanup(shutil.rmtree, clone_path)
  584. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  585. errstream=errstream)
  586. try:
  587. self.assertEqual(target_repo[b'HEAD'], self.repo[b'HEAD'])
  588. finally:
  589. target_repo.close()
  590. # create a second file to be pushed back to origin
  591. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  592. os.close(handle)
  593. porcelain.add(repo=clone_path, paths=[fullpath])
  594. porcelain.commit(repo=clone_path, message=b'push',
  595. author=b'author <email>',
  596. committer=b'committer <email>')
  597. # Setup a non-checked out branch in the remote
  598. refs_path = b"refs/heads/foo"
  599. new_id = self.repo[b'HEAD'].id
  600. self.assertNotEqual(new_id, ZERO_SHA)
  601. self.repo.refs[refs_path] = new_id
  602. # Push to the remote
  603. porcelain.push(clone_path, self.repo.path, b"HEAD:" + refs_path,
  604. outstream=outstream, errstream=errstream)
  605. # Check that the target and source
  606. with Repo(clone_path) as r_clone:
  607. self.assertEqual({
  608. b'HEAD': new_id,
  609. b'refs/heads/foo': r_clone[b'HEAD'].id,
  610. b'refs/heads/master': new_id,
  611. }, self.repo.get_refs())
  612. self.assertEqual(r_clone[b'HEAD'].id, self.repo[refs_path].id)
  613. # Get the change in the target repo corresponding to the add
  614. # this will be in the foo branch.
  615. change = list(tree_changes(self.repo, self.repo[b'HEAD'].tree,
  616. self.repo[b'refs/heads/foo'].tree))[0]
  617. self.assertEqual(os.path.basename(fullpath),
  618. change.new.path.decode('ascii'))
  619. def test_delete(self):
  620. """Basic test of porcelain push, removing a branch.
  621. """
  622. outstream = BytesIO()
  623. errstream = BytesIO()
  624. porcelain.commit(repo=self.repo.path, message=b'init',
  625. author=b'author <email>',
  626. committer=b'committer <email>')
  627. # Setup target repo cloned from temp test repo
  628. clone_path = tempfile.mkdtemp()
  629. self.addCleanup(shutil.rmtree, clone_path)
  630. target_repo = porcelain.clone(self.repo.path, target=clone_path,
  631. errstream=errstream)
  632. target_repo.close()
  633. # Setup a non-checked out branch in the remote
  634. refs_path = b"refs/heads/foo"
  635. new_id = self.repo[b'HEAD'].id
  636. self.assertNotEqual(new_id, ZERO_SHA)
  637. self.repo.refs[refs_path] = new_id
  638. # Push to the remote
  639. porcelain.push(clone_path, self.repo.path, b":" + refs_path,
  640. outstream=outstream, errstream=errstream)
  641. self.assertEqual({
  642. b'HEAD': new_id,
  643. b'refs/heads/master': new_id,
  644. }, self.repo.get_refs())
  645. class PullTests(PorcelainTestCase):
  646. def setUp(self):
  647. super(PullTests, self).setUp()
  648. # create a file for initial commit
  649. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  650. os.close(handle)
  651. porcelain.add(repo=self.repo.path, paths=fullpath)
  652. porcelain.commit(repo=self.repo.path, message=b'test',
  653. author=b'test <email>',
  654. committer=b'test <email>')
  655. # Setup target repo
  656. self.target_path = tempfile.mkdtemp()
  657. self.addCleanup(shutil.rmtree, self.target_path)
  658. target_repo = porcelain.clone(self.repo.path, target=self.target_path,
  659. errstream=BytesIO())
  660. target_repo.close()
  661. # create a second file to be pushed
  662. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  663. os.close(handle)
  664. porcelain.add(repo=self.repo.path, paths=fullpath)
  665. porcelain.commit(repo=self.repo.path, message=b'test2',
  666. author=b'test2 <email>',
  667. committer=b'test2 <email>')
  668. self.assertTrue(b'refs/heads/master' in self.repo.refs)
  669. self.assertTrue(b'refs/heads/master' in target_repo.refs)
  670. def test_simple(self):
  671. outstream = BytesIO()
  672. errstream = BytesIO()
  673. # Pull changes into the cloned repo
  674. porcelain.pull(self.target_path, self.repo.path, b'refs/heads/master',
  675. outstream=outstream, errstream=errstream)
  676. # Check the target repo for pushed changes
  677. with Repo(self.target_path) as r:
  678. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  679. def test_no_refspec(self):
  680. outstream = BytesIO()
  681. errstream = BytesIO()
  682. # Pull changes into the cloned repo
  683. porcelain.pull(self.target_path, self.repo.path, outstream=outstream,
  684. errstream=errstream)
  685. # Check the target repo for pushed changes
  686. with Repo(self.target_path) as r:
  687. self.assertEqual(r[b'HEAD'].id, self.repo[b'HEAD'].id)
  688. class StatusTests(PorcelainTestCase):
  689. def test_empty(self):
  690. results = porcelain.status(self.repo)
  691. self.assertEqual(
  692. {'add': [], 'delete': [], 'modify': []},
  693. results.staged)
  694. self.assertEqual([], results.unstaged)
  695. def test_status_base(self):
  696. """Integration test for `status` functionality."""
  697. # Commit a dummy file then modify it
  698. fullpath = os.path.join(self.repo.path, 'foo')
  699. with open(fullpath, 'w') as f:
  700. f.write('origstuff')
  701. porcelain.add(repo=self.repo.path, paths=[fullpath])
  702. porcelain.commit(repo=self.repo.path, message=b'test status',
  703. author=b'author <email>',
  704. committer=b'committer <email>')
  705. # modify access and modify time of path
  706. os.utime(fullpath, (0, 0))
  707. with open(fullpath, 'wb') as f:
  708. f.write(b'stuff')
  709. # Make a dummy file and stage it
  710. filename_add = 'bar'
  711. fullpath = os.path.join(self.repo.path, filename_add)
  712. with open(fullpath, 'w') as f:
  713. f.write('stuff')
  714. porcelain.add(repo=self.repo.path, paths=fullpath)
  715. results = porcelain.status(self.repo)
  716. self.assertEqual(results.staged['add'][0],
  717. filename_add.encode('ascii'))
  718. self.assertEqual(results.unstaged, [b'foo'])
  719. def test_status_all(self):
  720. del_path = os.path.join(self.repo.path, 'foo')
  721. mod_path = os.path.join(self.repo.path, 'bar')
  722. add_path = os.path.join(self.repo.path, 'baz')
  723. us_path = os.path.join(self.repo.path, 'blye')
  724. ut_path = os.path.join(self.repo.path, 'blyat')
  725. with open(del_path, 'w') as f:
  726. f.write('origstuff')
  727. with open(mod_path, 'w') as f:
  728. f.write('origstuff')
  729. with open(us_path, 'w') as f:
  730. f.write('origstuff')
  731. porcelain.add(repo=self.repo.path, paths=[del_path, mod_path, us_path])
  732. porcelain.commit(repo=self.repo.path, message=b'test status',
  733. author=b'author <email>',
  734. committer=b'committer <email>')
  735. porcelain.remove(self.repo.path, [del_path])
  736. with open(add_path, 'w') as f:
  737. f.write('origstuff')
  738. with open(mod_path, 'w') as f:
  739. f.write('more_origstuff')
  740. with open(us_path, 'w') as f:
  741. f.write('more_origstuff')
  742. porcelain.add(repo=self.repo.path, paths=[add_path, mod_path])
  743. with open(us_path, 'w') as f:
  744. f.write('\norigstuff')
  745. with open(ut_path, 'w') as f:
  746. f.write('origstuff')
  747. results = porcelain.status(self.repo.path)
  748. self.assertDictEqual(
  749. {'add': [b'baz'], 'delete': [b'foo'], 'modify': [b'bar']},
  750. results.staged)
  751. self.assertListEqual(results.unstaged, [b'blye'])
  752. self.assertListEqual(results.untracked, ['blyat'])
  753. def test_get_tree_changes_add(self):
  754. """Unit test for get_tree_changes add."""
  755. # Make a dummy file, stage
  756. filename = 'bar'
  757. fullpath = os.path.join(self.repo.path, filename)
  758. with open(fullpath, 'w') as f:
  759. f.write('stuff')
  760. porcelain.add(repo=self.repo.path, paths=fullpath)
  761. porcelain.commit(repo=self.repo.path, message=b'test status',
  762. author=b'author <email>',
  763. committer=b'committer <email>')
  764. filename = 'foo'
  765. fullpath = os.path.join(self.repo.path, filename)
  766. with open(fullpath, 'w') as f:
  767. f.write('stuff')
  768. porcelain.add(repo=self.repo.path, paths=fullpath)
  769. changes = porcelain.get_tree_changes(self.repo.path)
  770. self.assertEqual(changes['add'][0], filename.encode('ascii'))
  771. self.assertEqual(len(changes['add']), 1)
  772. self.assertEqual(len(changes['modify']), 0)
  773. self.assertEqual(len(changes['delete']), 0)
  774. def test_get_tree_changes_modify(self):
  775. """Unit test for get_tree_changes modify."""
  776. # Make a dummy file, stage, commit, modify
  777. filename = 'foo'
  778. fullpath = os.path.join(self.repo.path, filename)
  779. with open(fullpath, 'w') as f:
  780. f.write('stuff')
  781. porcelain.add(repo=self.repo.path, paths=fullpath)
  782. porcelain.commit(repo=self.repo.path, message=b'test status',
  783. author=b'author <email>',
  784. committer=b'committer <email>')
  785. with open(fullpath, 'w') as f:
  786. f.write('otherstuff')
  787. porcelain.add(repo=self.repo.path, paths=fullpath)
  788. changes = porcelain.get_tree_changes(self.repo.path)
  789. self.assertEqual(changes['modify'][0], filename.encode('ascii'))
  790. self.assertEqual(len(changes['add']), 0)
  791. self.assertEqual(len(changes['modify']), 1)
  792. self.assertEqual(len(changes['delete']), 0)
  793. def test_get_tree_changes_delete(self):
  794. """Unit test for get_tree_changes delete."""
  795. # Make a dummy file, stage, commit, remove
  796. filename = 'foo'
  797. fullpath = os.path.join(self.repo.path, filename)
  798. with open(fullpath, 'w') as f:
  799. f.write('stuff')
  800. porcelain.add(repo=self.repo.path, paths=fullpath)
  801. porcelain.commit(repo=self.repo.path, message=b'test status',
  802. author=b'author <email>',
  803. committer=b'committer <email>')
  804. cwd = os.getcwd()
  805. try:
  806. os.chdir(self.repo.path)
  807. porcelain.remove(repo=self.repo.path, paths=[filename])
  808. finally:
  809. os.chdir(cwd)
  810. changes = porcelain.get_tree_changes(self.repo.path)
  811. self.assertEqual(changes['delete'][0], filename.encode('ascii'))
  812. self.assertEqual(len(changes['add']), 0)
  813. self.assertEqual(len(changes['modify']), 0)
  814. self.assertEqual(len(changes['delete']), 1)
  815. def test_get_untracked_paths(self):
  816. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  817. f.write('ignored\n')
  818. with open(os.path.join(self.repo.path, 'ignored'), 'w') as f:
  819. f.write('blah\n')
  820. with open(os.path.join(self.repo.path, 'notignored'), 'w') as f:
  821. f.write('blah\n')
  822. self.assertEqual(
  823. set(['ignored', 'notignored', '.gitignore']),
  824. set(porcelain.get_untracked_paths(self.repo.path, self.repo.path,
  825. self.repo.open_index())))
  826. self.assertEqual(set(['.gitignore', 'notignored']),
  827. set(porcelain.status(self.repo).untracked))
  828. self.assertEqual(set(['.gitignore', 'notignored', 'ignored']),
  829. set(porcelain.status(self.repo, ignored=True)
  830. .untracked))
  831. def test_get_untracked_paths_nested(self):
  832. with open(os.path.join(self.repo.path, 'notignored'), 'w') as f:
  833. f.write('blah\n')
  834. subrepo = Repo.init(os.path.join(self.repo.path, 'nested'), mkdir=True)
  835. with open(os.path.join(subrepo.path, 'another'), 'w') as f:
  836. f.write('foo\n')
  837. self.assertEqual(
  838. set(['notignored']),
  839. set(porcelain.get_untracked_paths(self.repo.path, self.repo.path,
  840. self.repo.open_index())))
  841. self.assertEqual(
  842. set(['another']),
  843. set(porcelain.get_untracked_paths(subrepo.path, subrepo.path,
  844. subrepo.open_index())))
  845. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  846. class UploadPackTests(PorcelainTestCase):
  847. """Tests for upload_pack."""
  848. def test_upload_pack(self):
  849. outf = BytesIO()
  850. exitcode = porcelain.upload_pack(
  851. self.repo.path, BytesIO(b"0000"), outf)
  852. outlines = outf.getvalue().splitlines()
  853. self.assertEqual([b"0000"], outlines)
  854. self.assertEqual(0, exitcode)
  855. class ReceivePackTests(PorcelainTestCase):
  856. """Tests for receive_pack."""
  857. def test_receive_pack(self):
  858. filename = 'foo'
  859. fullpath = os.path.join(self.repo.path, filename)
  860. with open(fullpath, 'w') as f:
  861. f.write('stuff')
  862. porcelain.add(repo=self.repo.path, paths=fullpath)
  863. self.repo.do_commit(message=b'test status',
  864. author=b'author <email>',
  865. committer=b'committer <email>',
  866. author_timestamp=1402354300,
  867. commit_timestamp=1402354300, author_timezone=0,
  868. commit_timezone=0)
  869. outf = BytesIO()
  870. exitcode = porcelain.receive_pack(
  871. self.repo.path, BytesIO(b"0000"), outf)
  872. outlines = outf.getvalue().splitlines()
  873. self.assertEqual([
  874. b'0091319b56ce3aee2d489f759736a79cc552c9bb86d9 HEAD\x00 report-status ' # noqa: E501
  875. b'delete-refs quiet ofs-delta side-band-64k '
  876. b'no-done symref=HEAD:refs/heads/master',
  877. b'003f319b56ce3aee2d489f759736a79cc552c9bb86d9 refs/heads/master',
  878. b'0000'], outlines)
  879. self.assertEqual(0, exitcode)
  880. class BranchListTests(PorcelainTestCase):
  881. def test_standard(self):
  882. self.assertEqual(set([]), set(porcelain.branch_list(self.repo)))
  883. def test_new_branch(self):
  884. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  885. self.repo[b"HEAD"] = c1.id
  886. porcelain.branch_create(self.repo, b"foo")
  887. self.assertEqual(
  888. set([b"master", b"foo"]),
  889. set(porcelain.branch_list(self.repo)))
  890. class BranchCreateTests(PorcelainTestCase):
  891. def test_branch_exists(self):
  892. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  893. self.repo[b"HEAD"] = c1.id
  894. porcelain.branch_create(self.repo, b"foo")
  895. self.assertRaises(KeyError, porcelain.branch_create, self.repo, b"foo")
  896. porcelain.branch_create(self.repo, b"foo", force=True)
  897. def test_new_branch(self):
  898. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  899. self.repo[b"HEAD"] = c1.id
  900. porcelain.branch_create(self.repo, b"foo")
  901. self.assertEqual(
  902. set([b"master", b"foo"]),
  903. set(porcelain.branch_list(self.repo)))
  904. class BranchDeleteTests(PorcelainTestCase):
  905. def test_simple(self):
  906. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  907. self.repo[b"HEAD"] = c1.id
  908. porcelain.branch_create(self.repo, b'foo')
  909. self.assertTrue(b"foo" in porcelain.branch_list(self.repo))
  910. porcelain.branch_delete(self.repo, b'foo')
  911. self.assertFalse(b"foo" in porcelain.branch_list(self.repo))
  912. def test_simple_unicode(self):
  913. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  914. self.repo[b"HEAD"] = c1.id
  915. porcelain.branch_create(self.repo, 'foo')
  916. self.assertTrue(b"foo" in porcelain.branch_list(self.repo))
  917. porcelain.branch_delete(self.repo, 'foo')
  918. self.assertFalse(b"foo" in porcelain.branch_list(self.repo))
  919. class FetchTests(PorcelainTestCase):
  920. def test_simple(self):
  921. outstream = BytesIO()
  922. errstream = BytesIO()
  923. # create a file for initial commit
  924. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  925. os.close(handle)
  926. porcelain.add(repo=self.repo.path, paths=fullpath)
  927. porcelain.commit(repo=self.repo.path, message=b'test',
  928. author=b'test <email>',
  929. committer=b'test <email>')
  930. # Setup target repo
  931. target_path = tempfile.mkdtemp()
  932. self.addCleanup(shutil.rmtree, target_path)
  933. target_repo = porcelain.clone(self.repo.path, target=target_path,
  934. errstream=errstream)
  935. # create a second file to be pushed
  936. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  937. os.close(handle)
  938. porcelain.add(repo=self.repo.path, paths=fullpath)
  939. porcelain.commit(repo=self.repo.path, message=b'test2',
  940. author=b'test2 <email>',
  941. committer=b'test2 <email>')
  942. self.assertFalse(self.repo[b'HEAD'].id in target_repo)
  943. target_repo.close()
  944. # Fetch changes into the cloned repo
  945. porcelain.fetch(target_path, self.repo.path,
  946. outstream=outstream, errstream=errstream)
  947. # Assert that fetch updated the local image of the remote
  948. self.assert_correct_remote_refs(
  949. target_repo.get_refs(), self.repo.get_refs())
  950. # Check the target repo for pushed changes
  951. with Repo(target_path) as r:
  952. self.assertTrue(self.repo[b'HEAD'].id in r)
  953. def test_with_remote_name(self):
  954. remote_name = b'origin'
  955. outstream = BytesIO()
  956. errstream = BytesIO()
  957. # create a file for initial commit
  958. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  959. os.close(handle)
  960. porcelain.add(repo=self.repo.path, paths=fullpath)
  961. porcelain.commit(repo=self.repo.path, message=b'test',
  962. author=b'test <email>',
  963. committer=b'test <email>')
  964. # Setup target repo
  965. target_path = tempfile.mkdtemp()
  966. self.addCleanup(shutil.rmtree, target_path)
  967. target_repo = porcelain.clone(self.repo.path, target=target_path,
  968. errstream=errstream)
  969. # Capture current refs
  970. target_refs = target_repo.get_refs()
  971. # create a second file to be pushed
  972. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  973. os.close(handle)
  974. porcelain.add(repo=self.repo.path, paths=fullpath)
  975. porcelain.commit(repo=self.repo.path, message=b'test2',
  976. author=b'test2 <email>',
  977. committer=b'test2 <email>')
  978. self.assertFalse(self.repo[b'HEAD'].id in target_repo)
  979. target_repo.close()
  980. # Fetch changes into the cloned repo
  981. porcelain.fetch(target_path, self.repo.path, remote_name=remote_name,
  982. outstream=outstream, errstream=errstream)
  983. # Assert that fetch updated the local image of the remote
  984. self.assert_correct_remote_refs(
  985. target_repo.get_refs(), self.repo.get_refs())
  986. # Check the target repo for pushed changes, as well as updates
  987. # for the refs
  988. with Repo(target_path) as r:
  989. self.assertTrue(self.repo[b'HEAD'].id in r)
  990. self.assertNotEqual(self.repo.get_refs(), target_refs)
  991. def assert_correct_remote_refs(
  992. self, local_refs, remote_refs, remote_name=b'origin'):
  993. """Assert that known remote refs corresponds to actual remote refs."""
  994. local_ref_prefix = b'refs/heads'
  995. remote_ref_prefix = b'refs/remotes/' + remote_name
  996. locally_known_remote_refs = {
  997. k[len(remote_ref_prefix) + 1:]: v for k, v in local_refs.items()
  998. if k.startswith(remote_ref_prefix)}
  999. normalized_remote_refs = {
  1000. k[len(local_ref_prefix) + 1:]: v for k, v in remote_refs.items()
  1001. if k.startswith(local_ref_prefix)}
  1002. self.assertEqual(locally_known_remote_refs, normalized_remote_refs)
  1003. class RepackTests(PorcelainTestCase):
  1004. def test_empty(self):
  1005. porcelain.repack(self.repo)
  1006. def test_simple(self):
  1007. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  1008. os.close(handle)
  1009. porcelain.add(repo=self.repo.path, paths=fullpath)
  1010. porcelain.repack(self.repo)
  1011. class LsTreeTests(PorcelainTestCase):
  1012. def test_empty(self):
  1013. porcelain.commit(repo=self.repo.path, message=b'test status',
  1014. author=b'author <email>',
  1015. committer=b'committer <email>')
  1016. f = StringIO()
  1017. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  1018. self.assertEqual(f.getvalue(), "")
  1019. def test_simple(self):
  1020. # Commit a dummy file then modify it
  1021. fullpath = os.path.join(self.repo.path, 'foo')
  1022. with open(fullpath, 'w') as f:
  1023. f.write('origstuff')
  1024. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1025. porcelain.commit(repo=self.repo.path, message=b'test status',
  1026. author=b'author <email>',
  1027. committer=b'committer <email>')
  1028. f = StringIO()
  1029. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  1030. self.assertEqual(
  1031. f.getvalue(),
  1032. '100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n')
  1033. def test_recursive(self):
  1034. # Create a directory then write a dummy file in it
  1035. dirpath = os.path.join(self.repo.path, 'adir')
  1036. filepath = os.path.join(dirpath, 'afile')
  1037. os.mkdir(dirpath)
  1038. with open(filepath, 'w') as f:
  1039. f.write('origstuff')
  1040. porcelain.add(repo=self.repo.path, paths=[filepath])
  1041. porcelain.commit(repo=self.repo.path, message=b'test status',
  1042. author=b'author <email>',
  1043. committer=b'committer <email>')
  1044. f = StringIO()
  1045. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  1046. self.assertEqual(
  1047. f.getvalue(),
  1048. '40000 tree b145cc69a5e17693e24d8a7be0016ed8075de66d\tadir\n')
  1049. f = StringIO()
  1050. porcelain.ls_tree(self.repo, b"HEAD", outstream=f, recursive=True)
  1051. self.assertEqual(
  1052. f.getvalue(),
  1053. '40000 tree b145cc69a5e17693e24d8a7be0016ed8075de66d\tadir\n'
  1054. '100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tadir'
  1055. '/afile\n')
  1056. class LsRemoteTests(PorcelainTestCase):
  1057. def test_empty(self):
  1058. self.assertEqual({}, porcelain.ls_remote(self.repo.path))
  1059. def test_some(self):
  1060. cid = porcelain.commit(repo=self.repo.path, message=b'test status',
  1061. author=b'author <email>',
  1062. committer=b'committer <email>')
  1063. self.assertEqual({
  1064. b'refs/heads/master': cid,
  1065. b'HEAD': cid},
  1066. porcelain.ls_remote(self.repo.path))
  1067. class LsFilesTests(PorcelainTestCase):
  1068. def test_empty(self):
  1069. self.assertEqual([], list(porcelain.ls_files(self.repo)))
  1070. def test_simple(self):
  1071. # Commit a dummy file then modify it
  1072. fullpath = os.path.join(self.repo.path, 'foo')
  1073. with open(fullpath, 'w') as f:
  1074. f.write('origstuff')
  1075. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1076. self.assertEqual([b'foo'], list(porcelain.ls_files(self.repo)))
  1077. class RemoteAddTests(PorcelainTestCase):
  1078. def test_new(self):
  1079. porcelain.remote_add(
  1080. self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
  1081. c = self.repo.get_config()
  1082. self.assertEqual(
  1083. c.get((b'remote', b'jelmer'), b'url'),
  1084. b'git://jelmer.uk/code/dulwich')
  1085. def test_exists(self):
  1086. porcelain.remote_add(
  1087. self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
  1088. self.assertRaises(porcelain.RemoteExists, porcelain.remote_add,
  1089. self.repo, 'jelmer', 'git://jelmer.uk/code/dulwich')
  1090. class CheckIgnoreTests(PorcelainTestCase):
  1091. def test_check_ignored(self):
  1092. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  1093. f.write('foo')
  1094. foo_path = os.path.join(self.repo.path, 'foo')
  1095. with open(foo_path, 'w') as f:
  1096. f.write('BAR')
  1097. bar_path = os.path.join(self.repo.path, 'bar')
  1098. with open(bar_path, 'w') as f:
  1099. f.write('BAR')
  1100. self.assertEqual(
  1101. ['foo'],
  1102. list(porcelain.check_ignore(self.repo, [foo_path])))
  1103. self.assertEqual(
  1104. [], list(porcelain.check_ignore(self.repo, [bar_path])))
  1105. def test_check_added_abs(self):
  1106. path = os.path.join(self.repo.path, 'foo')
  1107. with open(path, 'w') as f:
  1108. f.write('BAR')
  1109. self.repo.stage(['foo'])
  1110. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  1111. f.write('foo\n')
  1112. self.assertEqual(
  1113. [], list(porcelain.check_ignore(self.repo, [path])))
  1114. self.assertEqual(
  1115. ['foo'],
  1116. list(porcelain.check_ignore(self.repo, [path], no_index=True)))
  1117. def test_check_added_rel(self):
  1118. with open(os.path.join(self.repo.path, 'foo'), 'w') as f:
  1119. f.write('BAR')
  1120. self.repo.stage(['foo'])
  1121. with open(os.path.join(self.repo.path, '.gitignore'), 'w') as f:
  1122. f.write('foo\n')
  1123. cwd = os.getcwd()
  1124. os.mkdir(os.path.join(self.repo.path, 'bar'))
  1125. os.chdir(os.path.join(self.repo.path, 'bar'))
  1126. try:
  1127. self.assertEqual(
  1128. list(porcelain.check_ignore(self.repo, ['../foo'])), [])
  1129. self.assertEqual(['../foo'], list(
  1130. porcelain.check_ignore(self.repo, ['../foo'], no_index=True)))
  1131. finally:
  1132. os.chdir(cwd)
  1133. class UpdateHeadTests(PorcelainTestCase):
  1134. def test_set_to_branch(self):
  1135. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  1136. self.repo.refs[b"refs/heads/blah"] = c1.id
  1137. porcelain.update_head(self.repo, "blah")
  1138. self.assertEqual(c1.id, self.repo.head())
  1139. self.assertEqual(b'ref: refs/heads/blah',
  1140. self.repo.refs.read_ref(b'HEAD'))
  1141. def test_set_to_branch_detached(self):
  1142. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  1143. self.repo.refs[b"refs/heads/blah"] = c1.id
  1144. porcelain.update_head(self.repo, "blah", detached=True)
  1145. self.assertEqual(c1.id, self.repo.head())
  1146. self.assertEqual(c1.id, self.repo.refs.read_ref(b'HEAD'))
  1147. def test_set_to_commit_detached(self):
  1148. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  1149. self.repo.refs[b"refs/heads/blah"] = c1.id
  1150. porcelain.update_head(self.repo, c1.id, detached=True)
  1151. self.assertEqual(c1.id, self.repo.head())
  1152. self.assertEqual(c1.id, self.repo.refs.read_ref(b'HEAD'))
  1153. def test_set_new_branch(self):
  1154. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  1155. self.repo.refs[b"refs/heads/blah"] = c1.id
  1156. porcelain.update_head(self.repo, "blah", new_branch="bar")
  1157. self.assertEqual(c1.id, self.repo.head())
  1158. self.assertEqual(b'ref: refs/heads/bar',
  1159. self.repo.refs.read_ref(b'HEAD'))
  1160. class MailmapTests(PorcelainTestCase):
  1161. def test_no_mailmap(self):
  1162. self.assertEqual(
  1163. b'Jelmer Vernooij <jelmer@samba.org>',
  1164. porcelain.check_mailmap(
  1165. self.repo, b'Jelmer Vernooij <jelmer@samba.org>'))
  1166. def test_mailmap_lookup(self):
  1167. with open(os.path.join(self.repo.path, '.mailmap'), 'wb') as f:
  1168. f.write(b"""\
  1169. Jelmer Vernooij <jelmer@debian.org>
  1170. """)
  1171. self.assertEqual(
  1172. b'Jelmer Vernooij <jelmer@debian.org>',
  1173. porcelain.check_mailmap(
  1174. self.repo, b'Jelmer Vernooij <jelmer@samba.org>'))
  1175. class FsckTests(PorcelainTestCase):
  1176. def test_none(self):
  1177. self.assertEqual(
  1178. [],
  1179. list(porcelain.fsck(self.repo)))
  1180. def test_git_dir(self):
  1181. obj = Tree()
  1182. a = Blob()
  1183. a.data = b"foo"
  1184. obj.add(b".git", 0o100644, a.id)
  1185. self.repo.object_store.add_objects(
  1186. [(a, None), (obj, None)])
  1187. self.assertEqual(
  1188. [(obj.id, 'invalid name .git')],
  1189. [(sha, str(e)) for (sha, e) in porcelain.fsck(self.repo)])
  1190. class DescribeTests(PorcelainTestCase):
  1191. def test_no_commits(self):
  1192. self.assertRaises(KeyError, porcelain.describe, self.repo.path)
  1193. def test_single_commit(self):
  1194. fullpath = os.path.join(self.repo.path, 'foo')
  1195. with open(fullpath, 'w') as f:
  1196. f.write("BAR")
  1197. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1198. sha = porcelain.commit(
  1199. self.repo.path, message=b"Some message",
  1200. author=b"Joe <joe@example.com>",
  1201. committer=b"Bob <bob@example.com>")
  1202. self.assertEqual(
  1203. 'g{}'.format(sha[:7].decode('ascii')),
  1204. porcelain.describe(self.repo.path))
  1205. def test_tag(self):
  1206. fullpath = os.path.join(self.repo.path, 'foo')
  1207. with open(fullpath, 'w') as f:
  1208. f.write("BAR")
  1209. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1210. porcelain.commit(
  1211. self.repo.path, message=b"Some message",
  1212. author=b"Joe <joe@example.com>",
  1213. committer=b"Bob <bob@example.com>")
  1214. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  1215. b'bar', annotated=True)
  1216. self.assertEqual(
  1217. "tryme",
  1218. porcelain.describe(self.repo.path))
  1219. def test_tag_and_commit(self):
  1220. fullpath = os.path.join(self.repo.path, 'foo')
  1221. with open(fullpath, 'w') as f:
  1222. f.write("BAR")
  1223. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1224. porcelain.commit(
  1225. self.repo.path, message=b"Some message",
  1226. author=b"Joe <joe@example.com>",
  1227. committer=b"Bob <bob@example.com>")
  1228. porcelain.tag_create(self.repo.path, b"tryme", b'foo <foo@bar.com>',
  1229. b'bar', annotated=True)
  1230. with open(fullpath, 'w') as f:
  1231. f.write("BAR2")
  1232. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1233. sha = porcelain.commit(
  1234. self.repo.path, message=b"Some message",
  1235. author=b"Joe <joe@example.com>",
  1236. committer=b"Bob <bob@example.com>")
  1237. self.assertEqual(
  1238. 'tryme-1-g{}'.format(sha[:7].decode('ascii')),
  1239. porcelain.describe(self.repo.path))
  1240. class HelperTests(PorcelainTestCase):
  1241. def test_path_to_tree_path_base(self):
  1242. self.assertEqual(
  1243. b'bar', porcelain.path_to_tree_path('/home/foo', '/home/foo/bar'))
  1244. self.assertEqual(b'bar', porcelain.path_to_tree_path('.', './bar'))
  1245. self.assertEqual(b'bar', porcelain.path_to_tree_path('.', 'bar'))
  1246. cwd = os.getcwd()
  1247. self.assertEqual(
  1248. b'bar', porcelain.path_to_tree_path('.', os.path.join(cwd, 'bar')))
  1249. self.assertEqual(b'bar', porcelain.path_to_tree_path(cwd, 'bar'))
  1250. def test_path_to_tree_path_syntax(self):
  1251. self.assertEqual(b'bar', porcelain.path_to_tree_path(b'.', './bar'))
  1252. self.assertEqual(b'bar', porcelain.path_to_tree_path('.', b'./bar'))
  1253. self.assertEqual(b'bar', porcelain.path_to_tree_path(b'.', b'./bar'))
  1254. def test_path_to_tree_path_error(self):
  1255. with self.assertRaises(ValueError):
  1256. porcelain.path_to_tree_path('/home/foo/', '/home/bar/baz')
  1257. def test_path_to_tree_path_rel(self):
  1258. cwd = os.getcwd()
  1259. os.mkdir(os.path.join(self.repo.path, 'foo'))
  1260. os.mkdir(os.path.join(self.repo.path, 'foo/bar'))
  1261. try:
  1262. os.chdir(os.path.join(self.repo.path, 'foo/bar'))
  1263. self.assertEqual(b'bar/baz', porcelain.path_to_tree_path(
  1264. '..', 'baz'))
  1265. self.assertEqual(b'bar/baz', porcelain.path_to_tree_path(
  1266. os.path.join(os.getcwd(), '..'),
  1267. os.path.join(os.getcwd(), 'baz')))
  1268. self.assertEqual(b'bar/baz', porcelain.path_to_tree_path(
  1269. '..', os.path.join(os.getcwd(), 'baz')))
  1270. self.assertEqual(b'bar/baz', porcelain.path_to_tree_path(
  1271. os.path.join(os.getcwd(), '..'), 'baz'))
  1272. finally:
  1273. os.chdir(cwd)
  1274. class GetObjectBypathTests(PorcelainTestCase):
  1275. def test_simple(self):
  1276. fullpath = os.path.join(self.repo.path, 'foo')
  1277. with open(fullpath, 'w') as f:
  1278. f.write("BAR")
  1279. porcelain.add(repo=self.repo.path, paths=[fullpath])
  1280. porcelain.commit(
  1281. self.repo.path, message=b"Some message",
  1282. author=b"Joe <joe@example.com>",
  1283. committer=b"Bob <bob@example.com>")
  1284. self.assertEqual(
  1285. b"BAR",
  1286. porcelain.get_object_by_path(self.repo, 'foo').data)
  1287. def test_missing(self):
  1288. self.assertRaises(
  1289. KeyError,
  1290. porcelain.get_object_by_path, self.repo, 'foo')