test_porcelain.py 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667
  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. import contextlib
  22. import os
  23. import platform
  24. import re
  25. import shutil
  26. import stat
  27. import subprocess
  28. import sys
  29. import tarfile
  30. import tempfile
  31. import threading
  32. import time
  33. from io import BytesIO, StringIO
  34. from unittest import skipIf
  35. from dulwich import porcelain
  36. from dulwich.diff_tree import tree_changes
  37. from dulwich.errors import CommitError
  38. from dulwich.objects import ZERO_SHA, Blob, Tag, Tree
  39. from dulwich.porcelain import CheckoutError
  40. from dulwich.repo import NoIndexPresent, Repo
  41. from dulwich.server import DictBackend
  42. from dulwich.tests.utils import build_commit_graph, make_commit, make_object
  43. from dulwich.web import make_server, make_wsgi_chain
  44. from . import TestCase
  45. try:
  46. import gpg
  47. except ImportError:
  48. gpg = None
  49. def flat_walk_dir(dir_to_walk):
  50. for dirpath, _, filenames in os.walk(dir_to_walk):
  51. rel_dirpath = os.path.relpath(dirpath, dir_to_walk)
  52. if not dirpath == dir_to_walk:
  53. yield rel_dirpath
  54. for filename in filenames:
  55. if dirpath == dir_to_walk:
  56. yield filename
  57. else:
  58. yield os.path.join(rel_dirpath, filename)
  59. class PorcelainTestCase(TestCase):
  60. def setUp(self):
  61. super().setUp()
  62. self.test_dir = tempfile.mkdtemp()
  63. self.addCleanup(shutil.rmtree, self.test_dir)
  64. self.repo_path = os.path.join(self.test_dir, "repo")
  65. self.repo = Repo.init(self.repo_path, mkdir=True)
  66. self.addCleanup(self.repo.close)
  67. def assertRecentTimestamp(self, ts):
  68. # On some slow CIs it does actually take more than 5 seconds to go from
  69. # creating the tag to here.
  70. self.assertLess(time.time() - ts, 50)
  71. @skipIf(gpg is None, "gpg is not available")
  72. class PorcelainGpgTestCase(PorcelainTestCase):
  73. DEFAULT_KEY = """
  74. -----BEGIN PGP PRIVATE KEY BLOCK-----
  75. lQVYBGBjIyIBDADAwydvMPQqeEiK54FG1DHwT5sQejAaJOb+PsOhVa4fLcKsrO3F
  76. g5CxO+/9BHCXAr8xQAtp/gOhDN05fyK3MFyGlL9s+Cd8xf34S3R4rN/qbF0oZmaa
  77. FW0MuGnniq54HINs8KshadVn1Dhi/GYSJ588qNFRl/qxFTYAk+zaGsgX/QgFfy0f
  78. djWXJLypZXu9D6DlyJ0cPSzUlfBkI2Ytx6grzIquRjY0FbkjK3l+iGsQ+ebRMdcP
  79. Sqd5iTN9XuzIUVoBFAZBRjibKV3N2wxlnCbfLlzCyDp7rktzSThzjJ2pVDuLrMAx
  80. 6/L9hIhwmFwdtY4FBFGvMR0b0Ugh3kCsRWr8sgj9I7dUoLHid6ObYhJFhnD3GzRc
  81. U+xX1uy3iTCqJDsG334aQIhC5Giuxln4SUZna2MNbq65ksh38N1aM/t3+Dc/TKVB
  82. rb5KWicRPCQ4DIQkHMDCSPyj+dvRLCPzIaPvHD7IrCfHYHOWuvvPGCpwjo0As3iP
  83. IecoMeguPLVaqgcAEQEAAQAL/i5/pQaUd4G7LDydpbixPS6r9UrfPrU/y5zvBP/p
  84. DCynPDutJ1oq539pZvXQ2VwEJJy7x0UVKkjyMndJLNWly9wHC7o8jkHx/NalVP47
  85. LXR+GWbCdOOcYYbdAWcCNB3zOtzPnWhdAEagkc2G9xRQDIB0dLHLCIUpCbLP/CWM
  86. qlHnDsVMrVTWjgzcpsnyGgw8NeLYJtYGB8dsN+XgCCjo7a9LEvUBKNgdmWBbf14/
  87. iBw7PCugazFcH9QYfZwzhsi3nqRRagTXHbxFRG0LD9Ro9qCEutHYGP2PJ59Nj8+M
  88. zaVkJj/OxWxVOGvn2q16mQBCjKpbWfqXZVVl+G5DGOmiSTZqXy+3j6JCKdOMy6Qd
  89. JBHOHhFZXYmWYaaPzoc33T/C3QhMfY5sOtUDLJmV05Wi4dyBeNBEslYgUuTk/jXb
  90. 5ZAie25eDdrsoqkcnSs2ZguMF7AXhe6il2zVhUUMs/6UZgd6I7I4Is0HXT/pnxEp
  91. uiTRFu4v8E+u+5a8O3pffe5boQYA3TsIxceen20qY+kRaTOkURHMZLn/y6KLW8bZ
  92. rNJyXWS9hBAcbbSGhfOwYfzbDCM17yPQO3E2zo8lcGdRklUdIIaCxQwtu36N5dfx
  93. OLCCQc5LmYdl/EAm91iAhrr7dNntZ18MU09gdzUu+ONZwu4CP3cJT83+qYZULso8
  94. 4Fvd/X8IEfGZ7kM+ylrdqBwtlrn8yYXtom+ows2M2UuNR53B+BUOd73kVLTkTCjE
  95. JH63+nE8BqG7tDLCMws+23SAA3xxBgDfDrr0x7zCozQKVQEqBzQr9Uoo/c/ZjAfi
  96. syzNSrDz+g5gqJYtuL9XpPJVWf6V1GXVyJlSbxR9CjTkBxmlPxpvV25IsbVSsh0o
  97. aqkf2eWpbCL6Qb2E0jd1rvf8sGeTTohzYfiSVVsC2t9ngRO/CmetizwQBvRzLGMZ
  98. 4mtAPiy7ZEDc2dFrPp7zlKISYmJZUx/DJVuZWuOrVMpBP+bSgJXoMTlICxZUqUnE
  99. 2VKVStb/L+Tl8XCwIWdrZb9BaDnHqfcGAM2B4HNPxP88Yj1tEDly/vqeb3vVMhj+
  100. S1lunnLdgxp46YyuTMYAzj88eCGurRtzBsdxxlGAsioEnZGebEqAHQbieKq/DO6I
  101. MOMZHMSVBDqyyIx3assGlxSX8BSFW0lhKyT7i0XqnAgCJ9f/5oq0SbFGq+01VQb7
  102. jIx9PbcYJORxsE0JG/CXXPv27bRtQXsudkWGSYvC0NLOgk4z8+kQpQtyFh16lujq
  103. WRwMeriu0qNDjCa1/eHIKDovhAZ3GyO5/9m1tBlUZXN0IFVzZXIgPHRlc3RAdGVz
  104. dC5jb20+iQHOBBMBCAA4AhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAFiEEjrR8
  105. MQ4fJK44PYMvfN2AClLmXiYFAmDcEZEACgkQfN2AClLmXibZzgv/ZfeTpTuqQE1W
  106. C1jT5KpQExnt0BizTX0U7BvSn8Fr6VXTyol6kYc3u71GLUuJyawCLtIzOXqOXJvz
  107. bjcZqymcMADuftKcfMy513FhbF6MhdVd6QoeBP6+7/xXOFJCi+QVYF7SQ2h7K1Qm
  108. +yXOiAMgSxhCZQGPBNJLlDUOd47nSIMANvlumFtmLY/1FD7RpG7WQWjeX1mnxNTw
  109. hUU+Yv7GuFc/JprXCIYqHbhWfvXyVtae2ZK4xuVi5eqwA2RfggOVM7drb+CgPhG0
  110. +9aEDDLOZqVi65wK7J73Puo3rFTbPQMljxw5s27rWqF+vB6hhVdJOPNomWy3naPi
  111. k5MW0mhsacASz1WYndpZz+XaQTq/wJF5HUyyeUWJ0vlOEdwx021PHcqSTyfNnkjD
  112. KncrE21t2sxWRsgGDETxIwkd2b2HNGAvveUD0ffFK/oJHGSXjAERFGc3wuiDj3mQ
  113. BvKm4wt4QF9ZMrCdhMAA6ax5kfEUqQR4ntmrJk/khp/mV7TILaI4nQVYBGBjIyIB
  114. DADghIo9wXnRxzfdDTvwnP8dHpLAIaPokgdpyLswqUCixJWiW2xcV6weUjEWwH6n
  115. eN/t1uZYVehbrotxVPla+MPvzhxp6/cmG+2lhzEBOp6zRwnL1wIB6HoKJfpREhyM
  116. c8rLR0zMso1L1bJTyydvnu07a7BWo3VWKjilb0rEZZUSD/2hidx5HxMOJSoidLWe
  117. d/PPuv6yht3NtA4UThlcfldm9G6PbqCdm1kMEKAkq0wVJvhPJ6gEFRNJimgygfUw
  118. MDFXEIhQtxjgdV5Uoz3O5452VLoRsDlgpi3E0WDGj7WXDaO5uSU0T5aJgVgHCP/f
  119. xZhHuQFk2YYIl5nCBpOZyWWI0IKmscTuEwzpkhICQDQFvcMZ5ibsl7wA2P7YTrQf
  120. FDMjjzuaK80GYPfxDFlyKUyLqFt8w/QzsZLDLX7+jxIEpbRAaMw/JsWqm5BMxxbS
  121. 3CIQiS5S3oSKDsNINelqWFfwvLhvlQra8gIxyNTlek25OdgG66BiiX+seH8A/ql+
  122. F+MAEQEAAQAL/1jrNSLjMt9pwo6qFKClVQZP2vf7+sH7v7LeHIDXr3EnYUnVYnOq
  123. B1FU5PspTp/+J9W25DB9CZLx7Gj8qeslFdiuLSOoIBB4RCToB3kAoeTH0DHqW/Gs
  124. hFTrmJkuDp9zpo/ek6SIXJx5rHAyR9KVw0fizQprH2f6PcgLbTWeM61dJuqowmg3
  125. 7eCOyIKv7VQvFqEhYokLD+JNmrvg+Htg0DXGvdjRjAwPf/NezEXpj67a6cHTp1/C
  126. hwp7pevG+3fTxaCJFesl5/TxxtnaBLE8m2uo/S6Hxgn9l0edonroe1QlTjEqGLy2
  127. 7qi2z5Rem+v6GWNDRgvAWur13v8FNdyduHlioG/NgRsU9mE2MYeFsfi3cfNpJQp/
  128. wC9PSCIXrb/45mkS8KyjZpCrIPB9RV/m0MREq01TPom7rstZc4A1pD0Ot7AtUYS3
  129. e95zLyEmeLziPJ9fV4fgPmEudDr1uItnmV0LOskKlpg5sc0hhdrwYoobfkKt2dx6
  130. DqfMlcM1ZkUbLQYA4jwfpFJG4HmYvjL2xCJxM0ycjvMbqFN+4UjgYWVlRfOrm1V4
  131. Op86FjbRbV6OOCNhznotAg7mul4xtzrrTkK8o3YLBeJseDgl4AWuzXtNa9hE0XpK
  132. 9gJoEHUuBOOsamVh2HpXESFyE5CclOV7JSh541TlZKfnqfZYCg4JSbp0UijkawCL
  133. 5bJJUiGGMD9rZUxIAKQO1DvUEzptS7Jl6S3y5sbIIhilp4KfYWbSk3PPu9CnZD5b
  134. LhEQp0elxnb/IL8PBgD+DpTeC8unkGKXUpbe9x0ISI6V1D6FmJq/FxNg7fMa3QCh
  135. fGiAyoTm80ZETynj+blRaDO3gY4lTLa3Opubof1EqK2QmwXmpyvXEZNYcQfQ2CCS
  136. GOWUCK8jEQamUPf1PWndZXJUmROI1WukhlL71V/ir6zQeVCv1wcwPwclJPnAe87u
  137. pEklnCYpvsEldwHUX9u0BWzoULIEsi+ddtHmT0KTeF/DHRy0W15jIHbjFqhqckj1
  138. /6fmr7l7kIi/kN4vWe0F/0Q8IXX+cVMgbl3aIuaGcvENLGcoAsAtPGx88SfRgmfu
  139. HK64Y7hx1m+Bo215rxJzZRjqHTBPp0BmCi+JKkaavIBrYRbsx20gveI4dzhLcUhB
  140. kiT4Q7oz0/VbGHS1CEf9KFeS/YOGj57s4yHauSVI0XdP9kBRTWmXvBkzsooB2cKH
  141. hwhUN7iiT1k717CiTNUT6Q/pcPFCyNuMoBBGQTU206JEgIjQvI3f8xMUMGmGVVQz
  142. 9/k716ycnhb2JZ/Q/AyQIeHJiQG2BBgBCAAgAhsMFiEEjrR8MQ4fJK44PYMvfN2A
  143. ClLmXiYFAmDcEa4ACgkQfN2AClLmXiZxxQv/XaMN0hPCygtrQMbCsTNb34JbvJzh
  144. hngPuUAfTbRHrR3YeATyQofNbL0DD3fvfzeFF8qESqvzCSZxS6dYsXPd4MCJTzlp
  145. zYBZ2X0sOrgDqZvqCZKN72RKgdk0KvthdzAxsIm2dfcQOxxowXMxhJEXZmsFpusx
  146. jKJxOcrfVRjXJnh9isY0NpCoqMQ+3k3wDJ3VGEHV7G+A+vFkWfbLJF5huQ96uaH9
  147. Uc+jUsREUH9G82ZBqpoioEN8Ith4VXpYnKdTMonK/+ZcyeraJZhXrvbjnEomKdzU
  148. 0pu4bt1HlLR3dcnpjN7b009MBf2xLgEfQk2nPZ4zzY+tDkxygtPllaB4dldFjBpT
  149. j7Q+t49sWMjmlJUbLlHfuJ7nUUK5+cGjBsWVObAEcyfemHWCTVFnEa2BJslGC08X
  150. rFcjRRcMEr9ct4551QFBHsv3O/Wp3/wqczYgE9itSnGT05w+4vLt4smG+dnEHjRJ
  151. brMb2upTHa+kjktjdO96/BgSnKYqmNmPB/qB
  152. =ivA/
  153. -----END PGP PRIVATE KEY BLOCK-----
  154. """
  155. DEFAULT_KEY_ID = "8EB47C310E1F24AE383D832F7CDD800A52E65E26"
  156. NON_DEFAULT_KEY = """
  157. -----BEGIN PGP PRIVATE KEY BLOCK-----
  158. lQVYBGBjI0ABDADGWBRp+t02emfzUlhrc1psqIhhecFm6Em0Kv33cfDpnfoMF1tK
  159. Yy/4eLYIR7FmpdbFPcDThFNHbXJzBi00L1mp0XQE2l50h/2bDAAgREdZ+NVo5a7/
  160. RSZjauNU1PxW6pnXMehEh1tyIQmV78jAukaakwaicrpIenMiFUN3fAKHnLuFffA6
  161. t0f3LqJvTDhUw/o2vPgw5e6UDQhA1C+KTv1KXVrhJNo88a3hZqCZ76z3drKR411Q
  162. zYgT4DUb8lfnbN+z2wfqT9oM5cegh2k86/mxAA3BYOeQrhmQo/7uhezcgbxtdGZr
  163. YlbuaNDTSBrn10ZoaxLPo2dJe2zWxgD6MpvsGU1w3tcRW508qo/+xoWp2/pDzmok
  164. +uhOh1NAj9zB05VWBz1r7oBgCOIKpkD/LD4VKq59etsZ/UnrYDwKdXWZp7uhshkU
  165. M7N35lUJcR76a852dlMdrgpmY18+BP7+o7M+5ElHTiqQbMuE1nHTg8RgVpdV+tUx
  166. dg6GWY/XHf5asm8AEQEAAQAL/A85epOp+GnymmEQfI3+5D178D//Lwu9n86vECB6
  167. xAHCqQtdjZnXpDp/1YUsL59P8nzgYRk7SoMskQDoQ/cB/XFuDOhEdMSgHaTVlnrj
  168. ktCCq6rqGnUosyolbb64vIfVaSqd/5SnCStpAsnaBoBYrAu4ZmV4xfjDQWwn0q5s
  169. u+r56mD0SkjPgbwk/b3qTVagVmf2OFzUgWwm1e/X+bA1oPag1NV8VS4hZPXswT4f
  170. qhiyqUFOgP6vUBcqehkjkIDIl/54xII7/P5tp3LIZawvIXqHKNTqYPCqaCqCj+SL
  171. vMYDIb6acjescfZoM71eAeHAANeFZzr/rwfBT+dEP6qKmPXNcvgE11X44ZCr04nT
  172. zOV/uDUifEvKT5qgtyJpSFEVr7EXubJPKoNNhoYqq9z1pYU7IedX5BloiVXKOKTY
  173. 0pk7JkLqf3g5fYtXh/wol1owemITJy5V5PgaqZvk491LkI6S+kWC7ANYUg+TDPIW
  174. afxW3E5N1CYV6XDAl0ZihbLcoQYAy0Ky/p/wayWKePyuPBLwx9O89GSONK2pQljZ
  175. yaAgxPQ5/i1vx6LIMg7k/722bXR9W3zOjWOin4eatPM3d2hkG96HFvnBqXSmXOPV
  176. 03Xqy1/B5Tj8E9naLKUHE/OBQEc363DgLLG9db5HfPlpAngeppYPdyWkhzXyzkgS
  177. PylaE5eW3zkdjEbYJ6RBTecTZEgBaMvJNPdWbn//frpP7kGvyiCg5Es+WjLInUZ6
  178. 0sdifcNTCewzLXK80v/y5mVOdJhPBgD5zs9cYdyiQJayqAuOr+He1eMHMVUbm9as
  179. qBmPrst398eBW9ZYF7eBfTSlUf6B+WnvyLKEGsUf/7IK0EWDlzoBuWzWiHjUAY1g
  180. m9eTV2MnvCCCefqCErWwfFo2nWOasAZA9sKD+ICIBY4tbtvSl4yfLBzTMwSvs9ZS
  181. K1ocPSYUnhm2miSWZ8RLZPH7roHQasNHpyq/AX7DahFf2S/bJ+46ZGZ8Pigr7hA+
  182. MjmpQ4qVdb5SaViPmZhAKO+PjuCHm+EF/2H0Y3Sl4eXgxZWoQVOUeXdWg9eMfYrj
  183. XDtUMIFppV/QxbeztZKvJdfk64vt/crvLsOp0hOky9cKwY89r4QaHfexU3qR+qDq
  184. UlMvR1rHk7dS5HZAtw0xKsFJNkuDxvBkMqv8Los8zp3nUl+U99dfZOArzNkW38wx
  185. FPa0ixkC9za2BkDrWEA8vTnxw0A2upIFegDUhwOByrSyfPPnG3tKGeqt3Izb/kDk
  186. Q9vmo+HgxBOguMIvlzbBfQZwtbd/gXzlvPqCtCJBbm90aGVyIFRlc3QgVXNlciA8
  187. dGVzdDJAdGVzdC5jb20+iQHOBBMBCAA4AhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4B
  188. AheAFiEEapM5P1DF5qzT1vtFuTYhLttOFMAFAmDcEeEACgkQuTYhLttOFMDe0Qv/
  189. Qx/bzXztJ3BCc+CYAVDx7Kr37S68etwwLgcWzhG+CDeMB5F/QE+upKgxy2iaqQFR
  190. mxfOMgf/TIQkUfkbaASzK1LpnesYO85pk7XYjoN1bYEHiXTkeW+bgB6aJIxrRmO2
  191. SrWasdBC/DsI3Mrya8YMt/TiHC6VpRJVxCe5vv7/kZC4CXrgTBnZocXx/YXimbke
  192. poPMVdbvhYh6N0aGeS38jRKgyN10KXmhDTAQDwseVFavBWAjVfx3DEwjtK2Z2GbA
  193. aL8JvAwRtqiPFkDMIKPL4UwxtXFws8SpMt6juroUkNyf6+BxNWYqmwXHPy8zCJAb
  194. xkxIJMlEc+s7qQsP3fILOo8Xn+dVzJ5sa5AoARoXm1GMjsdqaKAzq99Dic/dHnaQ
  195. Civev1PQsdwlYW2C2wNXNeIrxMndbDMFfNuZ6BnGHWJ/wjcp/pFs4YkyyZN8JH7L
  196. hP2FO4Jgham3AuP13kC3Ivea7V6hR8QNcDZRwFPOMIX4tXwQv1T72+7DZGaA25O7
  197. nQVXBGBjI0ABDADJMBYIcG0Yil9YxFs7aYzNbd7alUAr89VbY8eIGPHP3INFPM1w
  198. lBQCu+4j6xdEbhMpppLBZ9A5TEylP4C6qLtPa+oLtPeuSw8gHDE10XE4lbgPs376
  199. rL60XdImSOHhiduACUefYjqpcmFH9Bim1CC+koArYrSQJQx1Jri+OpnTaL/8UID0
  200. KzD/kEgMVGlHIVj9oJmb4+j9pW8I/g0wDSnIaEKFMxqu6SIVJ1GWj+MUMvZigjLC
  201. sNCZd7PnbOC5VeU3SsXj6he74Jx0AmGMPWIHi9M0DjHO5d1cCbXTnud8xxM1bOh4
  202. 7aCTnMK5cVyIr+adihgJpVVhrndSM8aklBPRgtozrGNCgF2CkYU2P1blxfloNr/8
  203. UZpM83o+s1aObBszzRNLxnpNORqoLqjfPtLEPQnagxE+4EapCq0NZ/x6yO5VTwwp
  204. NljdFAEk40uGuKyn1QA3uNMHy5DlpLl+tU7t1KEovdZ+OVYsYKZhVzw0MTpKogk9
  205. JI7AN0q62ronPskAEQEAAQAL+O8BUSt1ZCVjPSIXIsrR+ZOSkszZwgJ1CWIoh0IH
  206. YD2vmcMHGIhFYgBdgerpvhptKhaw7GcXDScEnYkyh5s4GE2hxclik1tbj/x1gYCN
  207. 8BNoyeDdPFxQG73qN12D99QYEctpOsz9xPLIDwmL0j1ehAfhwqHIAPm9Ca+i8JYM
  208. x/F+35S/jnKDXRI+NVlwbiEyXKXxxIqNlpy9i8sDBGexO5H5Sg0zSN/B1duLekGD
  209. biDw6gLc6bCgnS+0JOUpU07Z2fccMOY9ncjKGD2uIb/ePPUaek92GCQyq0eorCIV
  210. brcQsRc5sSsNtnRKQTQtxioROeDg7kf2oWySeHTswlXW/219ihrSXgteHJd+rPm7
  211. DYLEeGLRny8bRKv8rQdAtApHaJE4dAATXeY4RYo4NlXHYaztGYtU6kiM/3zCfWAe
  212. 9Nn+Wh9jMTZrjefUCagS5r6ZqAh7veNo/vgIGaCLh0a1Ypa0Yk9KFrn3LYEM3zgk
  213. 3m3bn+7qgy5cUYXoJ3DGJJEhBgDPonpW0WElqLs5ZMem1ha85SC38F0IkAaSuzuz
  214. v3eORiKWuyJGF32Q2XHa1RHQs1JtUKd8rxFer3b8Oq71zLz6JtVc9dmRudvgcJYX
  215. 0PC11F6WGjZFSSp39dajFp0A5DKUs39F3w7J1yuDM56TDIN810ywufGAHARY1pZb
  216. UJAy/dTqjFnCbNjpAakor3hVzqxcmUG+7Y2X9c2AGncT1MqAQC3M8JZcuZvkK8A9
  217. cMk8B914ryYE7VsZMdMhyTwHmykGAPgNLLa3RDETeGeGCKWI+ZPOoU0ib5JtJZ1d
  218. P3tNwfZKuZBZXKW9gqYqyBa/qhMip84SP30pr/TvulcdAFC759HK8sQZyJ6Vw24P
  219. c+5ssRxrQUEw1rvJPWhmQCmCOZHBMQl5T6eaTOpR5u3aUKTMlxPKhK9eC1dCSTnI
  220. /nyL8An3VKnLy+K/LI42YGphBVLLJmBewuTVDIJviWRdntiG8dElyEJMOywUltk3
  221. 2CEmqgsD9tPO8rXZjnMrMn3gfsiaoQYA6/6/e2utkHr7gAoWBgrBBdqVHsvqh5Ro
  222. 2DjLAOpZItO/EdCJfDAmbTYOa04535sBDP2tcH/vipPOPpbr1Y9Y/mNsKCulNxed
  223. yqAmEkKOcerLUP5UHju0AB6VBjHJFdU2mqT+UjPyBk7WeKXgFomyoYMv3KpNOFWR
  224. xi0Xji4kKHbttA6Hy3UcGPr9acyUAlDYeKmxbSUYIPhw32bbGrX9+F5YriTufRsG
  225. 3jftQVo9zqdcQSD/5pUTMn3EYbEcohYB2YWJAbYEGAEIACACGwwWIQRqkzk/UMXm
  226. rNPW+0W5NiEu204UwAUCYNwR6wAKCRC5NiEu204UwOPnC/92PgB1c3h9FBXH1maz
  227. g29fndHIHH65VLgqMiQ7HAMojwRlT5Xnj5tdkCBmszRkv5vMvdJRa3ZY8Ed/Inqr
  228. hxBFNzpjqX4oj/RYIQLKXWWfkTKYVLJFZFPCSo00jesw2gieu3Ke/Yy4gwhtNodA
  229. v+s6QNMvffTW/K3XNrWDB0E7/LXbdidzhm+MBu8ov2tuC3tp9liLICiE1jv/2xT4
  230. CNSO6yphmk1/1zEYHS/mN9qJ2csBmte2cdmGyOcuVEHk3pyINNMDOamaURBJGRwF
  231. XB5V7gTKUFU4jCp3chywKrBHJHxGGDUmPBmZtDtfWAOgL32drK7/KUyzZL/WO7Fj
  232. akOI0hRDFOcqTYWL20H7+hAiX3oHMP7eou3L5C7wJ9+JMcACklN/WMjG9a536DFJ
  233. 4UgZ6HyKPP+wy837Hbe8b25kNMBwFgiaLR0lcgzxj7NyQWjVCMOEN+M55tRCjvL6
  234. ya6JVZCRbMXfdCy8lVPgtNQ6VlHaj8Wvnn2FLbWWO2n2r3s=
  235. =9zU5
  236. -----END PGP PRIVATE KEY BLOCK-----
  237. """
  238. NON_DEFAULT_KEY_ID = "6A93393F50C5E6ACD3D6FB45B936212EDB4E14C0"
  239. def setUp(self):
  240. super().setUp()
  241. self.gpg_dir = os.path.join(self.test_dir, "gpg")
  242. os.mkdir(self.gpg_dir, mode=0o700)
  243. # Ignore errors when deleting GNUPGHOME, because of race conditions
  244. # (e.g. the gpg-agent socket having been deleted). See
  245. # https://github.com/jelmer/dulwich/issues/1000
  246. self.addCleanup(shutil.rmtree, self.gpg_dir, ignore_errors=True)
  247. self.overrideEnv("GNUPGHOME", self.gpg_dir)
  248. def import_default_key(self):
  249. subprocess.run(
  250. ["gpg", "--import"],
  251. stdout=subprocess.DEVNULL,
  252. stderr=subprocess.DEVNULL,
  253. input=PorcelainGpgTestCase.DEFAULT_KEY,
  254. text=True,
  255. )
  256. def import_non_default_key(self):
  257. subprocess.run(
  258. ["gpg", "--import"],
  259. stdout=subprocess.DEVNULL,
  260. stderr=subprocess.DEVNULL,
  261. input=PorcelainGpgTestCase.NON_DEFAULT_KEY,
  262. text=True,
  263. )
  264. class ArchiveTests(PorcelainTestCase):
  265. """Tests for the archive command."""
  266. def test_simple(self):
  267. c1, c2, c3 = build_commit_graph(
  268. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  269. )
  270. self.repo.refs[b"refs/heads/master"] = c3.id
  271. out = BytesIO()
  272. err = BytesIO()
  273. porcelain.archive(
  274. self.repo.path, b"refs/heads/master", outstream=out, errstream=err
  275. )
  276. self.assertEqual(b"", err.getvalue())
  277. tf = tarfile.TarFile(fileobj=out)
  278. self.addCleanup(tf.close)
  279. self.assertEqual([], tf.getnames())
  280. class UpdateServerInfoTests(PorcelainTestCase):
  281. def test_simple(self):
  282. c1, c2, c3 = build_commit_graph(
  283. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  284. )
  285. self.repo.refs[b"refs/heads/foo"] = c3.id
  286. porcelain.update_server_info(self.repo.path)
  287. self.assertTrue(
  288. os.path.exists(os.path.join(self.repo.controldir(), "info", "refs"))
  289. )
  290. class CommitTests(PorcelainTestCase):
  291. def test_custom_author(self):
  292. c1, c2, c3 = build_commit_graph(
  293. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  294. )
  295. self.repo.refs[b"refs/heads/foo"] = c3.id
  296. sha = porcelain.commit(
  297. self.repo.path,
  298. message=b"Some message",
  299. author=b"Joe <joe@example.com>",
  300. committer=b"Bob <bob@example.com>",
  301. )
  302. self.assertIsInstance(sha, bytes)
  303. self.assertEqual(len(sha), 40)
  304. def test_unicode(self):
  305. c1, c2, c3 = build_commit_graph(
  306. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  307. )
  308. self.repo.refs[b"refs/heads/foo"] = c3.id
  309. sha = porcelain.commit(
  310. self.repo.path,
  311. message="Some message",
  312. author="Joe <joe@example.com>",
  313. committer="Bob <bob@example.com>",
  314. )
  315. self.assertIsInstance(sha, bytes)
  316. self.assertEqual(len(sha), 40)
  317. def test_no_verify(self):
  318. if os.name != "posix":
  319. self.skipTest("shell hook tests requires POSIX shell")
  320. self.assertTrue(os.path.exists("/bin/sh"))
  321. hooks_dir = os.path.join(self.repo.controldir(), "hooks")
  322. os.makedirs(hooks_dir, exist_ok=True)
  323. self.addCleanup(shutil.rmtree, hooks_dir)
  324. c1, c2, c3 = build_commit_graph(
  325. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  326. )
  327. hook_fail = "#!/bin/sh\nexit 1"
  328. # hooks are executed in pre-commit, commit-msg order
  329. # test commit-msg failure first, then pre-commit failure, then
  330. # no_verify to skip both hooks
  331. commit_msg = os.path.join(hooks_dir, "commit-msg")
  332. with open(commit_msg, "w") as f:
  333. f.write(hook_fail)
  334. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  335. with self.assertRaises(CommitError):
  336. porcelain.commit(
  337. self.repo.path,
  338. message="Some message",
  339. author="Joe <joe@example.com>",
  340. committer="Bob <bob@example.com>",
  341. )
  342. pre_commit = os.path.join(hooks_dir, "pre-commit")
  343. with open(pre_commit, "w") as f:
  344. f.write(hook_fail)
  345. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  346. with self.assertRaises(CommitError):
  347. porcelain.commit(
  348. self.repo.path,
  349. message="Some message",
  350. author="Joe <joe@example.com>",
  351. committer="Bob <bob@example.com>",
  352. )
  353. sha = porcelain.commit(
  354. self.repo.path,
  355. message="Some message",
  356. author="Joe <joe@example.com>",
  357. committer="Bob <bob@example.com>",
  358. no_verify=True,
  359. )
  360. self.assertIsInstance(sha, bytes)
  361. self.assertEqual(len(sha), 40)
  362. def test_timezone(self):
  363. c1, c2, c3 = build_commit_graph(
  364. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  365. )
  366. self.repo.refs[b"refs/heads/foo"] = c3.id
  367. sha = porcelain.commit(
  368. self.repo.path,
  369. message="Some message",
  370. author="Joe <joe@example.com>",
  371. author_timezone=18000,
  372. committer="Bob <bob@example.com>",
  373. commit_timezone=18000,
  374. )
  375. self.assertIsInstance(sha, bytes)
  376. self.assertEqual(len(sha), 40)
  377. commit = self.repo.get_object(sha)
  378. self.assertEqual(commit._author_timezone, 18000)
  379. self.assertEqual(commit._commit_timezone, 18000)
  380. self.overrideEnv("GIT_AUTHOR_DATE", "1995-11-20T19:12:08-0501")
  381. self.overrideEnv("GIT_COMMITTER_DATE", "1995-11-20T19:12:08-0501")
  382. sha = porcelain.commit(
  383. self.repo.path,
  384. message="Some message",
  385. author="Joe <joe@example.com>",
  386. committer="Bob <bob@example.com>",
  387. )
  388. self.assertIsInstance(sha, bytes)
  389. self.assertEqual(len(sha), 40)
  390. commit = self.repo.get_object(sha)
  391. self.assertEqual(commit._author_timezone, -18060)
  392. self.assertEqual(commit._commit_timezone, -18060)
  393. self.overrideEnv("GIT_AUTHOR_DATE", None)
  394. self.overrideEnv("GIT_COMMITTER_DATE", None)
  395. local_timezone = time.localtime().tm_gmtoff
  396. sha = porcelain.commit(
  397. self.repo.path,
  398. message="Some message",
  399. author="Joe <joe@example.com>",
  400. committer="Bob <bob@example.com>",
  401. )
  402. self.assertIsInstance(sha, bytes)
  403. self.assertEqual(len(sha), 40)
  404. commit = self.repo.get_object(sha)
  405. self.assertEqual(commit._author_timezone, local_timezone)
  406. self.assertEqual(commit._commit_timezone, local_timezone)
  407. @skipIf(
  408. platform.python_implementation() == "PyPy" or sys.platform == "win32",
  409. "gpgme not easily available or supported on Windows and PyPy",
  410. )
  411. class CommitSignTests(PorcelainGpgTestCase):
  412. def test_default_key(self):
  413. c1, c2, c3 = build_commit_graph(
  414. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  415. )
  416. self.repo.refs[b"HEAD"] = c3.id
  417. cfg = self.repo.get_config()
  418. cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
  419. self.import_default_key()
  420. sha = porcelain.commit(
  421. self.repo.path,
  422. message="Some message",
  423. author="Joe <joe@example.com>",
  424. committer="Bob <bob@example.com>",
  425. signoff=True,
  426. )
  427. self.assertIsInstance(sha, bytes)
  428. self.assertEqual(len(sha), 40)
  429. commit = self.repo.get_object(sha)
  430. # GPG Signatures aren't deterministic, so we can't do a static assertion.
  431. commit.verify()
  432. commit.verify(keyids=[PorcelainGpgTestCase.DEFAULT_KEY_ID])
  433. self.import_non_default_key()
  434. self.assertRaises(
  435. gpg.errors.MissingSignatures,
  436. commit.verify,
  437. keyids=[PorcelainGpgTestCase.NON_DEFAULT_KEY_ID],
  438. )
  439. commit.committer = b"Alice <alice@example.com>"
  440. self.assertRaises(
  441. gpg.errors.BadSignatures,
  442. commit.verify,
  443. )
  444. def test_non_default_key(self):
  445. c1, c2, c3 = build_commit_graph(
  446. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  447. )
  448. self.repo.refs[b"HEAD"] = c3.id
  449. cfg = self.repo.get_config()
  450. cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
  451. self.import_non_default_key()
  452. sha = porcelain.commit(
  453. self.repo.path,
  454. message="Some message",
  455. author="Joe <joe@example.com>",
  456. committer="Bob <bob@example.com>",
  457. signoff=PorcelainGpgTestCase.NON_DEFAULT_KEY_ID,
  458. )
  459. self.assertIsInstance(sha, bytes)
  460. self.assertEqual(len(sha), 40)
  461. commit = self.repo.get_object(sha)
  462. # GPG Signatures aren't deterministic, so we can't do a static assertion.
  463. commit.verify()
  464. class TimezoneTests(PorcelainTestCase):
  465. def put_envs(self, value):
  466. self.overrideEnv("GIT_AUTHOR_DATE", value)
  467. self.overrideEnv("GIT_COMMITTER_DATE", value)
  468. def fallback(self, value):
  469. self.put_envs(value)
  470. self.assertRaises(porcelain.TimezoneFormatError, porcelain.get_user_timezones)
  471. def test_internal_format(self):
  472. self.put_envs("0 +0500")
  473. self.assertTupleEqual((18000, 18000), porcelain.get_user_timezones())
  474. def test_rfc_2822(self):
  475. self.put_envs("Mon, 20 Nov 1995 19:12:08 -0500")
  476. self.assertTupleEqual((-18000, -18000), porcelain.get_user_timezones())
  477. self.put_envs("Mon, 20 Nov 1995 19:12:08")
  478. self.assertTupleEqual((0, 0), porcelain.get_user_timezones())
  479. def test_iso8601(self):
  480. self.put_envs("1995-11-20T19:12:08-0501")
  481. self.assertTupleEqual((-18060, -18060), porcelain.get_user_timezones())
  482. self.put_envs("1995-11-20T19:12:08+0501")
  483. self.assertTupleEqual((18060, 18060), porcelain.get_user_timezones())
  484. self.put_envs("1995-11-20T19:12:08-05:01")
  485. self.assertTupleEqual((-18060, -18060), porcelain.get_user_timezones())
  486. self.put_envs("1995-11-20 19:12:08-05")
  487. self.assertTupleEqual((-18000, -18000), porcelain.get_user_timezones())
  488. # https://github.com/git/git/blob/96b2d4fa927c5055adc5b1d08f10a5d7352e2989/t/t6300-for-each-ref.sh#L128
  489. self.put_envs("2006-07-03 17:18:44 +0200")
  490. self.assertTupleEqual((7200, 7200), porcelain.get_user_timezones())
  491. def test_missing_or_malformed(self):
  492. # TODO: add more here
  493. self.fallback("0 + 0500")
  494. self.fallback("a +0500")
  495. self.fallback("1995-11-20T19:12:08")
  496. self.fallback("1995-11-20T19:12:08-05:")
  497. self.fallback("1995.11.20")
  498. self.fallback("11/20/1995")
  499. self.fallback("20.11.1995")
  500. def test_different_envs(self):
  501. self.overrideEnv("GIT_AUTHOR_DATE", "0 +0500")
  502. self.overrideEnv("GIT_COMMITTER_DATE", "0 +0501")
  503. self.assertTupleEqual((18000, 18060), porcelain.get_user_timezones())
  504. def test_no_envs(self):
  505. local_timezone = time.localtime().tm_gmtoff
  506. self.put_envs("0 +0500")
  507. self.assertTupleEqual((18000, 18000), porcelain.get_user_timezones())
  508. self.overrideEnv("GIT_COMMITTER_DATE", None)
  509. self.assertTupleEqual((18000, local_timezone), porcelain.get_user_timezones())
  510. self.put_envs("0 +0500")
  511. self.overrideEnv("GIT_AUTHOR_DATE", None)
  512. self.assertTupleEqual((local_timezone, 18000), porcelain.get_user_timezones())
  513. self.put_envs("0 +0500")
  514. self.overrideEnv("GIT_AUTHOR_DATE", None)
  515. self.overrideEnv("GIT_COMMITTER_DATE", None)
  516. self.assertTupleEqual(
  517. (local_timezone, local_timezone), porcelain.get_user_timezones()
  518. )
  519. class CleanTests(PorcelainTestCase):
  520. def put_files(self, tracked, ignored, untracked, empty_dirs):
  521. """Put the described files in the wd."""
  522. all_files = tracked | ignored | untracked
  523. for file_path in all_files:
  524. abs_path = os.path.join(self.repo.path, file_path)
  525. # File may need to be written in a dir that doesn't exist yet, so
  526. # create the parent dir(s) as necessary
  527. parent_dir = os.path.dirname(abs_path)
  528. try:
  529. os.makedirs(parent_dir)
  530. except FileExistsError:
  531. pass
  532. with open(abs_path, "w") as f:
  533. f.write("")
  534. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  535. f.writelines(ignored)
  536. for dir_path in empty_dirs:
  537. os.mkdir(os.path.join(self.repo.path, "empty_dir"))
  538. files_to_add = [os.path.join(self.repo.path, t) for t in tracked]
  539. porcelain.add(repo=self.repo.path, paths=files_to_add)
  540. porcelain.commit(repo=self.repo.path, message="init commit")
  541. def assert_wd(self, expected_paths):
  542. """Assert paths of files and dirs in wd are same as expected_paths."""
  543. control_dir_rel = os.path.relpath(self.repo._controldir, self.repo.path)
  544. # normalize paths to simplify comparison across platforms
  545. found_paths = {
  546. os.path.normpath(p)
  547. for p in flat_walk_dir(self.repo.path)
  548. if not p.split(os.sep)[0] == control_dir_rel
  549. }
  550. norm_expected_paths = {os.path.normpath(p) for p in expected_paths}
  551. self.assertEqual(found_paths, norm_expected_paths)
  552. def test_from_root(self):
  553. self.put_files(
  554. tracked={"tracked_file", "tracked_dir/tracked_file", ".gitignore"},
  555. ignored={"ignored_file"},
  556. untracked={
  557. "untracked_file",
  558. "tracked_dir/untracked_dir/untracked_file",
  559. "untracked_dir/untracked_dir/untracked_file",
  560. },
  561. empty_dirs={"empty_dir"},
  562. )
  563. porcelain.clean(repo=self.repo.path, target_dir=self.repo.path)
  564. self.assert_wd(
  565. {
  566. "tracked_file",
  567. "tracked_dir/tracked_file",
  568. ".gitignore",
  569. "ignored_file",
  570. "tracked_dir",
  571. }
  572. )
  573. def test_from_subdir(self):
  574. self.put_files(
  575. tracked={"tracked_file", "tracked_dir/tracked_file", ".gitignore"},
  576. ignored={"ignored_file"},
  577. untracked={
  578. "untracked_file",
  579. "tracked_dir/untracked_dir/untracked_file",
  580. "untracked_dir/untracked_dir/untracked_file",
  581. },
  582. empty_dirs={"empty_dir"},
  583. )
  584. porcelain.clean(
  585. repo=self.repo,
  586. target_dir=os.path.join(self.repo.path, "untracked_dir"),
  587. )
  588. self.assert_wd(
  589. {
  590. "tracked_file",
  591. "tracked_dir/tracked_file",
  592. ".gitignore",
  593. "ignored_file",
  594. "untracked_file",
  595. "tracked_dir/untracked_dir/untracked_file",
  596. "empty_dir",
  597. "untracked_dir",
  598. "tracked_dir",
  599. "tracked_dir/untracked_dir",
  600. }
  601. )
  602. class CloneTests(PorcelainTestCase):
  603. def test_simple_local(self):
  604. f1_1 = make_object(Blob, data=b"f1")
  605. commit_spec = [[1], [2, 1], [3, 1, 2]]
  606. trees = {
  607. 1: [(b"f1", f1_1), (b"f2", f1_1)],
  608. 2: [(b"f1", f1_1), (b"f2", f1_1)],
  609. 3: [(b"f1", f1_1), (b"f2", f1_1)],
  610. }
  611. c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees)
  612. self.repo.refs[b"refs/heads/master"] = c3.id
  613. self.repo.refs[b"refs/tags/foo"] = c3.id
  614. target_path = tempfile.mkdtemp()
  615. errstream = BytesIO()
  616. self.addCleanup(shutil.rmtree, target_path)
  617. r = porcelain.clone(
  618. self.repo.path, target_path, checkout=False, errstream=errstream
  619. )
  620. self.addCleanup(r.close)
  621. self.assertEqual(r.path, target_path)
  622. target_repo = Repo(target_path)
  623. self.assertEqual(0, len(target_repo.open_index()))
  624. self.assertEqual(c3.id, target_repo.refs[b"refs/tags/foo"])
  625. self.assertNotIn(b"f1", os.listdir(target_path))
  626. self.assertNotIn(b"f2", os.listdir(target_path))
  627. c = r.get_config()
  628. encoded_path = self.repo.path
  629. if not isinstance(encoded_path, bytes):
  630. encoded_path = encoded_path.encode("utf-8")
  631. self.assertEqual(encoded_path, c.get((b"remote", b"origin"), b"url"))
  632. self.assertEqual(
  633. b"+refs/heads/*:refs/remotes/origin/*",
  634. c.get((b"remote", b"origin"), b"fetch"),
  635. )
  636. def test_simple_local_with_checkout(self):
  637. f1_1 = make_object(Blob, data=b"f1")
  638. commit_spec = [[1], [2, 1], [3, 1, 2]]
  639. trees = {
  640. 1: [(b"f1", f1_1), (b"f2", f1_1)],
  641. 2: [(b"f1", f1_1), (b"f2", f1_1)],
  642. 3: [(b"f1", f1_1), (b"f2", f1_1)],
  643. }
  644. c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees)
  645. self.repo.refs[b"refs/heads/master"] = c3.id
  646. target_path = tempfile.mkdtemp()
  647. errstream = BytesIO()
  648. self.addCleanup(shutil.rmtree, target_path)
  649. with porcelain.clone(
  650. self.repo.path, target_path, checkout=True, errstream=errstream
  651. ) as r:
  652. self.assertEqual(r.path, target_path)
  653. with Repo(target_path) as r:
  654. self.assertEqual(r.head(), c3.id)
  655. self.assertIn("f1", os.listdir(target_path))
  656. self.assertIn("f2", os.listdir(target_path))
  657. def test_bare_local_with_checkout(self):
  658. f1_1 = make_object(Blob, data=b"f1")
  659. commit_spec = [[1], [2, 1], [3, 1, 2]]
  660. trees = {
  661. 1: [(b"f1", f1_1), (b"f2", f1_1)],
  662. 2: [(b"f1", f1_1), (b"f2", f1_1)],
  663. 3: [(b"f1", f1_1), (b"f2", f1_1)],
  664. }
  665. c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees)
  666. self.repo.refs[b"refs/heads/master"] = c3.id
  667. target_path = tempfile.mkdtemp()
  668. errstream = BytesIO()
  669. self.addCleanup(shutil.rmtree, target_path)
  670. with porcelain.clone(
  671. self.repo.path, target_path, bare=True, errstream=errstream
  672. ) as r:
  673. self.assertEqual(r.path, target_path)
  674. with Repo(target_path) as r:
  675. r.head()
  676. self.assertRaises(NoIndexPresent, r.open_index)
  677. self.assertNotIn(b"f1", os.listdir(target_path))
  678. self.assertNotIn(b"f2", os.listdir(target_path))
  679. def test_no_checkout_with_bare(self):
  680. f1_1 = make_object(Blob, data=b"f1")
  681. commit_spec = [[1]]
  682. trees = {1: [(b"f1", f1_1), (b"f2", f1_1)]}
  683. (c1,) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  684. self.repo.refs[b"refs/heads/master"] = c1.id
  685. self.repo.refs[b"HEAD"] = c1.id
  686. target_path = tempfile.mkdtemp()
  687. errstream = BytesIO()
  688. self.addCleanup(shutil.rmtree, target_path)
  689. self.assertRaises(
  690. porcelain.Error,
  691. porcelain.clone,
  692. self.repo.path,
  693. target_path,
  694. checkout=True,
  695. bare=True,
  696. errstream=errstream,
  697. )
  698. def test_no_head_no_checkout(self):
  699. f1_1 = make_object(Blob, data=b"f1")
  700. commit_spec = [[1]]
  701. trees = {1: [(b"f1", f1_1), (b"f2", f1_1)]}
  702. (c1,) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  703. self.repo.refs[b"refs/heads/master"] = c1.id
  704. target_path = tempfile.mkdtemp()
  705. self.addCleanup(shutil.rmtree, target_path)
  706. errstream = BytesIO()
  707. r = porcelain.clone(
  708. self.repo.path, target_path, checkout=True, errstream=errstream
  709. )
  710. r.close()
  711. def test_no_head_no_checkout_outstream_errstream_autofallback(self):
  712. f1_1 = make_object(Blob, data=b"f1")
  713. commit_spec = [[1]]
  714. trees = {1: [(b"f1", f1_1), (b"f2", f1_1)]}
  715. (c1,) = build_commit_graph(self.repo.object_store, commit_spec, trees)
  716. self.repo.refs[b"refs/heads/master"] = c1.id
  717. target_path = tempfile.mkdtemp()
  718. self.addCleanup(shutil.rmtree, target_path)
  719. errstream = porcelain.NoneStream()
  720. r = porcelain.clone(
  721. self.repo.path, target_path, checkout=True, errstream=errstream
  722. )
  723. r.close()
  724. def test_source_broken(self):
  725. with tempfile.TemporaryDirectory() as parent:
  726. target_path = os.path.join(parent, "target")
  727. self.assertRaises(
  728. Exception, porcelain.clone, "/nonexistent/repo", target_path
  729. )
  730. self.assertFalse(os.path.exists(target_path))
  731. def test_fetch_symref(self):
  732. f1_1 = make_object(Blob, data=b"f1")
  733. trees = {1: [(b"f1", f1_1), (b"f2", f1_1)]}
  734. [c1] = build_commit_graph(self.repo.object_store, [[1]], trees)
  735. self.repo.refs.set_symbolic_ref(b"HEAD", b"refs/heads/else")
  736. self.repo.refs[b"refs/heads/else"] = c1.id
  737. target_path = tempfile.mkdtemp()
  738. errstream = BytesIO()
  739. self.addCleanup(shutil.rmtree, target_path)
  740. r = porcelain.clone(
  741. self.repo.path, target_path, checkout=False, errstream=errstream
  742. )
  743. self.addCleanup(r.close)
  744. self.assertEqual(r.path, target_path)
  745. target_repo = Repo(target_path)
  746. self.assertEqual(0, len(target_repo.open_index()))
  747. self.assertEqual(c1.id, target_repo.refs[b"refs/heads/else"])
  748. self.assertEqual(c1.id, target_repo.refs[b"HEAD"])
  749. self.assertEqual(
  750. {
  751. b"HEAD": b"refs/heads/else",
  752. b"refs/remotes/origin/HEAD": b"refs/remotes/origin/else",
  753. },
  754. target_repo.refs.get_symrefs(),
  755. )
  756. def test_detached_head(self):
  757. f1_1 = make_object(Blob, data=b"f1")
  758. commit_spec = [[1], [2, 1], [3, 1, 2]]
  759. trees = {
  760. 1: [(b"f1", f1_1), (b"f2", f1_1)],
  761. 2: [(b"f1", f1_1), (b"f2", f1_1)],
  762. 3: [(b"f1", f1_1), (b"f2", f1_1)],
  763. }
  764. c1, c2, c3 = build_commit_graph(self.repo.object_store, commit_spec, trees)
  765. self.repo.refs[b"refs/heads/master"] = c2.id
  766. self.repo.refs.remove_if_equals(b"HEAD", None)
  767. self.repo.refs[b"HEAD"] = c3.id
  768. target_path = tempfile.mkdtemp()
  769. self.addCleanup(shutil.rmtree, target_path)
  770. errstream = porcelain.NoneStream()
  771. with porcelain.clone(
  772. self.repo.path, target_path, checkout=True, errstream=errstream
  773. ) as r:
  774. self.assertEqual(c3.id, r.refs[b"HEAD"])
  775. class InitTests(TestCase):
  776. def test_non_bare(self):
  777. repo_dir = tempfile.mkdtemp()
  778. self.addCleanup(shutil.rmtree, repo_dir)
  779. porcelain.init(repo_dir)
  780. def test_bare(self):
  781. repo_dir = tempfile.mkdtemp()
  782. self.addCleanup(shutil.rmtree, repo_dir)
  783. porcelain.init(repo_dir, bare=True)
  784. class AddTests(PorcelainTestCase):
  785. def test_add_default_paths(self):
  786. # create a file for initial commit
  787. fullpath = os.path.join(self.repo.path, "blah")
  788. with open(fullpath, "w") as f:
  789. f.write("\n")
  790. porcelain.add(repo=self.repo.path, paths=[fullpath])
  791. porcelain.commit(
  792. repo=self.repo.path,
  793. message=b"test",
  794. author=b"test <email>",
  795. committer=b"test <email>",
  796. )
  797. # Add a second test file and a file in a directory
  798. with open(os.path.join(self.repo.path, "foo"), "w") as f:
  799. f.write("\n")
  800. os.mkdir(os.path.join(self.repo.path, "adir"))
  801. with open(os.path.join(self.repo.path, "adir", "afile"), "w") as f:
  802. f.write("\n")
  803. cwd = os.getcwd()
  804. try:
  805. os.chdir(self.repo.path)
  806. self.assertEqual({"foo", "blah", "adir", ".git"}, set(os.listdir(".")))
  807. self.assertEqual(
  808. (["foo", os.path.join("adir", "afile")], set()),
  809. porcelain.add(self.repo.path),
  810. )
  811. finally:
  812. os.chdir(cwd)
  813. # Check that foo was added and nothing in .git was modified
  814. index = self.repo.open_index()
  815. self.assertEqual(sorted(index), [b"adir/afile", b"blah", b"foo"])
  816. def test_add_default_paths_subdir(self):
  817. os.mkdir(os.path.join(self.repo.path, "foo"))
  818. with open(os.path.join(self.repo.path, "blah"), "w") as f:
  819. f.write("\n")
  820. with open(os.path.join(self.repo.path, "foo", "blie"), "w") as f:
  821. f.write("\n")
  822. cwd = os.getcwd()
  823. try:
  824. os.chdir(os.path.join(self.repo.path, "foo"))
  825. porcelain.add(repo=self.repo.path)
  826. porcelain.commit(
  827. repo=self.repo.path,
  828. message=b"test",
  829. author=b"test <email>",
  830. committer=b"test <email>",
  831. )
  832. finally:
  833. os.chdir(cwd)
  834. index = self.repo.open_index()
  835. self.assertEqual(sorted(index), [b"foo/blie"])
  836. def test_add_file(self):
  837. fullpath = os.path.join(self.repo.path, "foo")
  838. with open(fullpath, "w") as f:
  839. f.write("BAR")
  840. porcelain.add(self.repo.path, paths=[fullpath])
  841. self.assertIn(b"foo", self.repo.open_index())
  842. def test_add_ignored(self):
  843. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  844. f.write("foo\nsubdir/")
  845. with open(os.path.join(self.repo.path, "foo"), "w") as f:
  846. f.write("BAR")
  847. with open(os.path.join(self.repo.path, "bar"), "w") as f:
  848. f.write("BAR")
  849. os.mkdir(os.path.join(self.repo.path, "subdir"))
  850. with open(os.path.join(self.repo.path, "subdir", "baz"), "w") as f:
  851. f.write("BAZ")
  852. (added, ignored) = porcelain.add(
  853. self.repo.path,
  854. paths=[
  855. os.path.join(self.repo.path, "foo"),
  856. os.path.join(self.repo.path, "bar"),
  857. os.path.join(self.repo.path, "subdir"),
  858. ],
  859. )
  860. self.assertIn(b"bar", self.repo.open_index())
  861. self.assertEqual({"bar"}, set(added))
  862. self.assertEqual({"foo", os.path.join("subdir", "")}, ignored)
  863. def test_add_file_absolute_path(self):
  864. # Absolute paths are (not yet) supported
  865. with open(os.path.join(self.repo.path, "foo"), "w") as f:
  866. f.write("BAR")
  867. porcelain.add(self.repo, paths=[os.path.join(self.repo.path, "foo")])
  868. self.assertIn(b"foo", self.repo.open_index())
  869. def test_add_not_in_repo(self):
  870. with open(os.path.join(self.test_dir, "foo"), "w") as f:
  871. f.write("BAR")
  872. self.assertRaises(
  873. ValueError,
  874. porcelain.add,
  875. self.repo,
  876. paths=[os.path.join(self.test_dir, "foo")],
  877. )
  878. self.assertRaises(
  879. (ValueError, FileNotFoundError),
  880. porcelain.add,
  881. self.repo,
  882. paths=["../foo"],
  883. )
  884. self.assertEqual([], list(self.repo.open_index()))
  885. def test_add_file_clrf_conversion(self):
  886. # Set the right configuration to the repo
  887. c = self.repo.get_config()
  888. c.set("core", "autocrlf", "input")
  889. c.write_to_path()
  890. # Add a file with CRLF line-ending
  891. fullpath = os.path.join(self.repo.path, "foo")
  892. with open(fullpath, "wb") as f:
  893. f.write(b"line1\r\nline2")
  894. porcelain.add(self.repo.path, paths=[fullpath])
  895. # The line-endings should have been converted to LF
  896. index = self.repo.open_index()
  897. self.assertIn(b"foo", index)
  898. entry = index[b"foo"]
  899. blob = self.repo[entry.sha]
  900. self.assertEqual(blob.data, b"line1\nline2")
  901. class RemoveTests(PorcelainTestCase):
  902. def test_remove_file(self):
  903. fullpath = os.path.join(self.repo.path, "foo")
  904. with open(fullpath, "w") as f:
  905. f.write("BAR")
  906. porcelain.add(self.repo.path, paths=[fullpath])
  907. porcelain.commit(
  908. repo=self.repo,
  909. message=b"test",
  910. author=b"test <email>",
  911. committer=b"test <email>",
  912. )
  913. self.assertTrue(os.path.exists(os.path.join(self.repo.path, "foo")))
  914. cwd = os.getcwd()
  915. try:
  916. os.chdir(self.repo.path)
  917. porcelain.remove(self.repo.path, paths=["foo"])
  918. finally:
  919. os.chdir(cwd)
  920. self.assertFalse(os.path.exists(os.path.join(self.repo.path, "foo")))
  921. def test_remove_file_staged(self):
  922. fullpath = os.path.join(self.repo.path, "foo")
  923. with open(fullpath, "w") as f:
  924. f.write("BAR")
  925. cwd = os.getcwd()
  926. try:
  927. os.chdir(self.repo.path)
  928. porcelain.add(self.repo.path, paths=[fullpath])
  929. self.assertRaises(Exception, porcelain.rm, self.repo.path, paths=["foo"])
  930. finally:
  931. os.chdir(cwd)
  932. def test_remove_file_removed_on_disk(self):
  933. fullpath = os.path.join(self.repo.path, "foo")
  934. with open(fullpath, "w") as f:
  935. f.write("BAR")
  936. porcelain.add(self.repo.path, paths=[fullpath])
  937. cwd = os.getcwd()
  938. try:
  939. os.chdir(self.repo.path)
  940. os.remove(fullpath)
  941. porcelain.remove(self.repo.path, paths=["foo"])
  942. finally:
  943. os.chdir(cwd)
  944. self.assertFalse(os.path.exists(os.path.join(self.repo.path, "foo")))
  945. class LogTests(PorcelainTestCase):
  946. def test_simple(self):
  947. c1, c2, c3 = build_commit_graph(
  948. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  949. )
  950. self.repo.refs[b"HEAD"] = c3.id
  951. outstream = StringIO()
  952. porcelain.log(self.repo.path, outstream=outstream)
  953. self.assertEqual(3, outstream.getvalue().count("-" * 50))
  954. def test_max_entries(self):
  955. c1, c2, c3 = build_commit_graph(
  956. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  957. )
  958. self.repo.refs[b"HEAD"] = c3.id
  959. outstream = StringIO()
  960. porcelain.log(self.repo.path, outstream=outstream, max_entries=1)
  961. self.assertEqual(1, outstream.getvalue().count("-" * 50))
  962. class ShowTests(PorcelainTestCase):
  963. def test_nolist(self):
  964. c1, c2, c3 = build_commit_graph(
  965. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  966. )
  967. self.repo.refs[b"HEAD"] = c3.id
  968. outstream = StringIO()
  969. porcelain.show(self.repo.path, objects=c3.id, outstream=outstream)
  970. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  971. def test_simple(self):
  972. c1, c2, c3 = build_commit_graph(
  973. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  974. )
  975. self.repo.refs[b"HEAD"] = c3.id
  976. outstream = StringIO()
  977. porcelain.show(self.repo.path, objects=[c3.id], outstream=outstream)
  978. self.assertTrue(outstream.getvalue().startswith("-" * 50))
  979. def test_blob(self):
  980. b = Blob.from_string(b"The Foo\n")
  981. self.repo.object_store.add_object(b)
  982. outstream = StringIO()
  983. porcelain.show(self.repo.path, objects=[b.id], outstream=outstream)
  984. self.assertEqual(outstream.getvalue(), "The Foo\n")
  985. def test_commit_no_parent(self):
  986. a = Blob.from_string(b"The Foo\n")
  987. ta = Tree()
  988. ta.add(b"somename", 0o100644, a.id)
  989. ca = make_commit(tree=ta.id)
  990. self.repo.object_store.add_objects([(a, None), (ta, None), (ca, None)])
  991. outstream = StringIO()
  992. porcelain.show(self.repo.path, objects=[ca.id], outstream=outstream)
  993. self.assertMultiLineEqual(
  994. outstream.getvalue(),
  995. """\
  996. --------------------------------------------------
  997. commit: 344da06c1bb85901270b3e8875c988a027ec087d
  998. Author: Test Author <test@nodomain.com>
  999. Committer: Test Committer <test@nodomain.com>
  1000. Date: Fri Jan 01 2010 00:00:00 +0000
  1001. Test message.
  1002. diff --git a/somename b/somename
  1003. new file mode 100644
  1004. index 0000000..ea5c7bf
  1005. --- /dev/null
  1006. +++ b/somename
  1007. @@ -0,0 +1 @@
  1008. +The Foo
  1009. """,
  1010. )
  1011. def test_tag(self):
  1012. a = Blob.from_string(b"The Foo\n")
  1013. ta = Tree()
  1014. ta.add(b"somename", 0o100644, a.id)
  1015. ca = make_commit(tree=ta.id)
  1016. self.repo.object_store.add_objects([(a, None), (ta, None), (ca, None)])
  1017. porcelain.tag_create(
  1018. self.repo.path,
  1019. b"tryme",
  1020. b"foo <foo@bar.com>",
  1021. b"bar",
  1022. annotated=True,
  1023. objectish=ca.id,
  1024. tag_time=1552854211,
  1025. tag_timezone=0,
  1026. )
  1027. outstream = StringIO()
  1028. porcelain.show(self.repo, objects=[b"refs/tags/tryme"], outstream=outstream)
  1029. self.maxDiff = None
  1030. self.assertMultiLineEqual(
  1031. outstream.getvalue(),
  1032. """\
  1033. Tagger: foo <foo@bar.com>
  1034. Date: Sun Mar 17 2019 20:23:31 +0000
  1035. bar
  1036. --------------------------------------------------
  1037. commit: 344da06c1bb85901270b3e8875c988a027ec087d
  1038. Author: Test Author <test@nodomain.com>
  1039. Committer: Test Committer <test@nodomain.com>
  1040. Date: Fri Jan 01 2010 00:00:00 +0000
  1041. Test message.
  1042. diff --git a/somename b/somename
  1043. new file mode 100644
  1044. index 0000000..ea5c7bf
  1045. --- /dev/null
  1046. +++ b/somename
  1047. @@ -0,0 +1 @@
  1048. +The Foo
  1049. """,
  1050. )
  1051. def test_commit_with_change(self):
  1052. a = Blob.from_string(b"The Foo\n")
  1053. ta = Tree()
  1054. ta.add(b"somename", 0o100644, a.id)
  1055. ca = make_commit(tree=ta.id)
  1056. b = Blob.from_string(b"The Bar\n")
  1057. tb = Tree()
  1058. tb.add(b"somename", 0o100644, b.id)
  1059. cb = make_commit(tree=tb.id, parents=[ca.id])
  1060. self.repo.object_store.add_objects(
  1061. [
  1062. (a, None),
  1063. (b, None),
  1064. (ta, None),
  1065. (tb, None),
  1066. (ca, None),
  1067. (cb, None),
  1068. ]
  1069. )
  1070. outstream = StringIO()
  1071. porcelain.show(self.repo.path, objects=[cb.id], outstream=outstream)
  1072. self.assertMultiLineEqual(
  1073. outstream.getvalue(),
  1074. """\
  1075. --------------------------------------------------
  1076. commit: 2c6b6c9cb72c130956657e1fdae58e5b103744fa
  1077. Author: Test Author <test@nodomain.com>
  1078. Committer: Test Committer <test@nodomain.com>
  1079. Date: Fri Jan 01 2010 00:00:00 +0000
  1080. Test message.
  1081. diff --git a/somename b/somename
  1082. index ea5c7bf..fd38bcb 100644
  1083. --- a/somename
  1084. +++ b/somename
  1085. @@ -1 +1 @@
  1086. -The Foo
  1087. +The Bar
  1088. """,
  1089. )
  1090. class SymbolicRefTests(PorcelainTestCase):
  1091. def test_set_wrong_symbolic_ref(self):
  1092. c1, c2, c3 = build_commit_graph(
  1093. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1094. )
  1095. self.repo.refs[b"HEAD"] = c3.id
  1096. self.assertRaises(
  1097. porcelain.Error, porcelain.symbolic_ref, self.repo.path, b"foobar"
  1098. )
  1099. def test_set_force_wrong_symbolic_ref(self):
  1100. c1, c2, c3 = build_commit_graph(
  1101. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1102. )
  1103. self.repo.refs[b"HEAD"] = c3.id
  1104. porcelain.symbolic_ref(self.repo.path, b"force_foobar", force=True)
  1105. # test if we actually changed the file
  1106. with self.repo.get_named_file("HEAD") as f:
  1107. new_ref = f.read()
  1108. self.assertEqual(new_ref, b"ref: refs/heads/force_foobar\n")
  1109. def test_set_symbolic_ref(self):
  1110. c1, c2, c3 = build_commit_graph(
  1111. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1112. )
  1113. self.repo.refs[b"HEAD"] = c3.id
  1114. porcelain.symbolic_ref(self.repo.path, b"master")
  1115. def test_set_symbolic_ref_other_than_master(self):
  1116. c1, c2, c3 = build_commit_graph(
  1117. self.repo.object_store,
  1118. [[1], [2, 1], [3, 1, 2]],
  1119. attrs=dict(refs="develop"),
  1120. )
  1121. self.repo.refs[b"HEAD"] = c3.id
  1122. self.repo.refs[b"refs/heads/develop"] = c3.id
  1123. porcelain.symbolic_ref(self.repo.path, b"develop")
  1124. # test if we actually changed the file
  1125. with self.repo.get_named_file("HEAD") as f:
  1126. new_ref = f.read()
  1127. self.assertEqual(new_ref, b"ref: refs/heads/develop\n")
  1128. class DiffTreeTests(PorcelainTestCase):
  1129. def test_empty(self):
  1130. c1, c2, c3 = build_commit_graph(
  1131. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1132. )
  1133. self.repo.refs[b"HEAD"] = c3.id
  1134. outstream = BytesIO()
  1135. porcelain.diff_tree(self.repo.path, c2.tree, c3.tree, outstream=outstream)
  1136. self.assertEqual(outstream.getvalue(), b"")
  1137. class CommitTreeTests(PorcelainTestCase):
  1138. def test_simple(self):
  1139. c1, c2, c3 = build_commit_graph(
  1140. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1141. )
  1142. b = Blob()
  1143. b.data = b"foo the bar"
  1144. t = Tree()
  1145. t.add(b"somename", 0o100644, b.id)
  1146. self.repo.object_store.add_object(t)
  1147. self.repo.object_store.add_object(b)
  1148. sha = porcelain.commit_tree(
  1149. self.repo.path,
  1150. t.id,
  1151. message=b"Withcommit.",
  1152. author=b"Joe <joe@example.com>",
  1153. committer=b"Jane <jane@example.com>",
  1154. )
  1155. self.assertIsInstance(sha, bytes)
  1156. self.assertEqual(len(sha), 40)
  1157. class RevListTests(PorcelainTestCase):
  1158. def test_simple(self):
  1159. c1, c2, c3 = build_commit_graph(
  1160. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1161. )
  1162. outstream = BytesIO()
  1163. porcelain.rev_list(self.repo.path, [c3.id], outstream=outstream)
  1164. self.assertEqual(
  1165. c3.id + b"\n" + c2.id + b"\n" + c1.id + b"\n", outstream.getvalue()
  1166. )
  1167. @skipIf(
  1168. platform.python_implementation() == "PyPy" or sys.platform == "win32",
  1169. "gpgme not easily available or supported on Windows and PyPy",
  1170. )
  1171. class TagCreateSignTests(PorcelainGpgTestCase):
  1172. def test_default_key(self):
  1173. c1, c2, c3 = build_commit_graph(
  1174. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1175. )
  1176. self.repo.refs[b"HEAD"] = c3.id
  1177. cfg = self.repo.get_config()
  1178. cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
  1179. self.import_default_key()
  1180. porcelain.tag_create(
  1181. self.repo.path,
  1182. b"tryme",
  1183. b"foo <foo@bar.com>",
  1184. b"bar",
  1185. annotated=True,
  1186. sign=True,
  1187. )
  1188. tags = self.repo.refs.as_dict(b"refs/tags")
  1189. self.assertEqual(list(tags.keys()), [b"tryme"])
  1190. tag = self.repo[b"refs/tags/tryme"]
  1191. self.assertIsInstance(tag, Tag)
  1192. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  1193. self.assertEqual(b"bar\n", tag.message)
  1194. self.assertRecentTimestamp(tag.tag_time)
  1195. tag = self.repo[b"refs/tags/tryme"]
  1196. # GPG Signatures aren't deterministic, so we can't do a static assertion.
  1197. tag.verify()
  1198. tag.verify(keyids=[PorcelainGpgTestCase.DEFAULT_KEY_ID])
  1199. self.import_non_default_key()
  1200. self.assertRaises(
  1201. gpg.errors.MissingSignatures,
  1202. tag.verify,
  1203. keyids=[PorcelainGpgTestCase.NON_DEFAULT_KEY_ID],
  1204. )
  1205. tag._chunked_text = [b"bad data", tag._signature]
  1206. self.assertRaises(
  1207. gpg.errors.BadSignatures,
  1208. tag.verify,
  1209. )
  1210. def test_non_default_key(self):
  1211. c1, c2, c3 = build_commit_graph(
  1212. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1213. )
  1214. self.repo.refs[b"HEAD"] = c3.id
  1215. cfg = self.repo.get_config()
  1216. cfg.set(("user",), "signingKey", PorcelainGpgTestCase.DEFAULT_KEY_ID)
  1217. self.import_non_default_key()
  1218. porcelain.tag_create(
  1219. self.repo.path,
  1220. b"tryme",
  1221. b"foo <foo@bar.com>",
  1222. b"bar",
  1223. annotated=True,
  1224. sign=PorcelainGpgTestCase.NON_DEFAULT_KEY_ID,
  1225. )
  1226. tags = self.repo.refs.as_dict(b"refs/tags")
  1227. self.assertEqual(list(tags.keys()), [b"tryme"])
  1228. tag = self.repo[b"refs/tags/tryme"]
  1229. self.assertIsInstance(tag, Tag)
  1230. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  1231. self.assertEqual(b"bar\n", tag.message)
  1232. self.assertRecentTimestamp(tag.tag_time)
  1233. tag = self.repo[b"refs/tags/tryme"]
  1234. # GPG Signatures aren't deterministic, so we can't do a static assertion.
  1235. tag.verify()
  1236. class TagCreateTests(PorcelainTestCase):
  1237. def test_annotated(self):
  1238. c1, c2, c3 = build_commit_graph(
  1239. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1240. )
  1241. self.repo.refs[b"HEAD"] = c3.id
  1242. porcelain.tag_create(
  1243. self.repo.path,
  1244. b"tryme",
  1245. b"foo <foo@bar.com>",
  1246. b"bar",
  1247. annotated=True,
  1248. )
  1249. tags = self.repo.refs.as_dict(b"refs/tags")
  1250. self.assertEqual(list(tags.keys()), [b"tryme"])
  1251. tag = self.repo[b"refs/tags/tryme"]
  1252. self.assertIsInstance(tag, Tag)
  1253. self.assertEqual(b"foo <foo@bar.com>", tag.tagger)
  1254. self.assertEqual(b"bar\n", tag.message)
  1255. self.assertRecentTimestamp(tag.tag_time)
  1256. def test_unannotated(self):
  1257. c1, c2, c3 = build_commit_graph(
  1258. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1259. )
  1260. self.repo.refs[b"HEAD"] = c3.id
  1261. porcelain.tag_create(self.repo.path, b"tryme", annotated=False)
  1262. tags = self.repo.refs.as_dict(b"refs/tags")
  1263. self.assertEqual(list(tags.keys()), [b"tryme"])
  1264. self.repo[b"refs/tags/tryme"]
  1265. self.assertEqual(list(tags.values()), [self.repo.head()])
  1266. def test_unannotated_unicode(self):
  1267. c1, c2, c3 = build_commit_graph(
  1268. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  1269. )
  1270. self.repo.refs[b"HEAD"] = c3.id
  1271. porcelain.tag_create(self.repo.path, "tryme", annotated=False)
  1272. tags = self.repo.refs.as_dict(b"refs/tags")
  1273. self.assertEqual(list(tags.keys()), [b"tryme"])
  1274. self.repo[b"refs/tags/tryme"]
  1275. self.assertEqual(list(tags.values()), [self.repo.head()])
  1276. class TagListTests(PorcelainTestCase):
  1277. def test_empty(self):
  1278. tags = porcelain.tag_list(self.repo.path)
  1279. self.assertEqual([], tags)
  1280. def test_simple(self):
  1281. self.repo.refs[b"refs/tags/foo"] = b"aa" * 20
  1282. self.repo.refs[b"refs/tags/bar/bla"] = b"bb" * 20
  1283. tags = porcelain.tag_list(self.repo.path)
  1284. self.assertEqual([b"bar/bla", b"foo"], tags)
  1285. class TagDeleteTests(PorcelainTestCase):
  1286. def test_simple(self):
  1287. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  1288. self.repo[b"HEAD"] = c1.id
  1289. porcelain.tag_create(self.repo, b"foo")
  1290. self.assertIn(b"foo", porcelain.tag_list(self.repo))
  1291. porcelain.tag_delete(self.repo, b"foo")
  1292. self.assertNotIn(b"foo", porcelain.tag_list(self.repo))
  1293. class ResetTests(PorcelainTestCase):
  1294. def test_hard_head(self):
  1295. fullpath = os.path.join(self.repo.path, "foo")
  1296. with open(fullpath, "w") as f:
  1297. f.write("BAR")
  1298. porcelain.add(self.repo.path, paths=[fullpath])
  1299. porcelain.commit(
  1300. self.repo.path,
  1301. message=b"Some message",
  1302. committer=b"Jane <jane@example.com>",
  1303. author=b"John <john@example.com>",
  1304. )
  1305. with open(os.path.join(self.repo.path, "foo"), "wb") as f:
  1306. f.write(b"OOH")
  1307. porcelain.reset(self.repo, "hard", b"HEAD")
  1308. index = self.repo.open_index()
  1309. changes = list(
  1310. tree_changes(
  1311. self.repo,
  1312. index.commit(self.repo.object_store),
  1313. self.repo[b"HEAD"].tree,
  1314. )
  1315. )
  1316. self.assertEqual([], changes)
  1317. def test_hard_commit(self):
  1318. fullpath = os.path.join(self.repo.path, "foo")
  1319. with open(fullpath, "w") as f:
  1320. f.write("BAR")
  1321. porcelain.add(self.repo.path, paths=[fullpath])
  1322. sha = porcelain.commit(
  1323. self.repo.path,
  1324. message=b"Some message",
  1325. committer=b"Jane <jane@example.com>",
  1326. author=b"John <john@example.com>",
  1327. )
  1328. with open(fullpath, "wb") as f:
  1329. f.write(b"BAZ")
  1330. porcelain.add(self.repo.path, paths=[fullpath])
  1331. porcelain.commit(
  1332. self.repo.path,
  1333. message=b"Some other message",
  1334. committer=b"Jane <jane@example.com>",
  1335. author=b"John <john@example.com>",
  1336. )
  1337. porcelain.reset(self.repo, "hard", sha)
  1338. index = self.repo.open_index()
  1339. changes = list(
  1340. tree_changes(
  1341. self.repo,
  1342. index.commit(self.repo.object_store),
  1343. self.repo[sha].tree,
  1344. )
  1345. )
  1346. self.assertEqual([], changes)
  1347. class ResetFileTests(PorcelainTestCase):
  1348. def test_reset_modify_file_to_commit(self):
  1349. file = "foo"
  1350. full_path = os.path.join(self.repo.path, file)
  1351. with open(full_path, "w") as f:
  1352. f.write("hello")
  1353. porcelain.add(self.repo, paths=[full_path])
  1354. sha = porcelain.commit(
  1355. self.repo,
  1356. message=b"unitest",
  1357. committer=b"Jane <jane@example.com>",
  1358. author=b"John <john@example.com>",
  1359. )
  1360. with open(full_path, "a") as f:
  1361. f.write("something new")
  1362. porcelain.reset_file(self.repo, file, target=sha)
  1363. with open(full_path) as f:
  1364. self.assertEqual("hello", f.read())
  1365. def test_reset_remove_file_to_commit(self):
  1366. file = "foo"
  1367. full_path = os.path.join(self.repo.path, file)
  1368. with open(full_path, "w") as f:
  1369. f.write("hello")
  1370. porcelain.add(self.repo, paths=[full_path])
  1371. sha = porcelain.commit(
  1372. self.repo,
  1373. message=b"unitest",
  1374. committer=b"Jane <jane@example.com>",
  1375. author=b"John <john@example.com>",
  1376. )
  1377. os.remove(full_path)
  1378. porcelain.reset_file(self.repo, file, target=sha)
  1379. with open(full_path) as f:
  1380. self.assertEqual("hello", f.read())
  1381. def test_resetfile_with_dir(self):
  1382. os.mkdir(os.path.join(self.repo.path, "new_dir"))
  1383. full_path = os.path.join(self.repo.path, "new_dir", "foo")
  1384. with open(full_path, "w") as f:
  1385. f.write("hello")
  1386. porcelain.add(self.repo, paths=[full_path])
  1387. sha = porcelain.commit(
  1388. self.repo,
  1389. message=b"unitest",
  1390. committer=b"Jane <jane@example.com>",
  1391. author=b"John <john@example.com>",
  1392. )
  1393. with open(full_path, "a") as f:
  1394. f.write("something new")
  1395. porcelain.commit(
  1396. self.repo,
  1397. message=b"unitest 2",
  1398. committer=b"Jane <jane@example.com>",
  1399. author=b"John <john@example.com>",
  1400. )
  1401. porcelain.reset_file(self.repo, os.path.join("new_dir", "foo"), target=sha)
  1402. with open(full_path) as f:
  1403. self.assertEqual("hello", f.read())
  1404. def _commit_file_with_content(repo, filename, content):
  1405. file_path = os.path.join(repo.path, filename)
  1406. with open(file_path, "w") as f:
  1407. f.write(content)
  1408. porcelain.add(repo, paths=[file_path])
  1409. sha = porcelain.commit(
  1410. repo,
  1411. message=b"add " + filename.encode(),
  1412. committer=b"Jane <jane@example.com>",
  1413. author=b"John <john@example.com>",
  1414. )
  1415. return sha, file_path
  1416. class CheckoutTests(PorcelainTestCase):
  1417. def setUp(self):
  1418. super().setUp()
  1419. self._sha, self._foo_path = _commit_file_with_content(
  1420. self.repo, "foo", "hello\n"
  1421. )
  1422. porcelain.branch_create(self.repo, "uni")
  1423. def test_checkout_to_existing_branch(self):
  1424. self.assertEqual(b"master", porcelain.active_branch(self.repo))
  1425. porcelain.checkout_branch(self.repo, b"uni")
  1426. self.assertEqual(b"uni", porcelain.active_branch(self.repo))
  1427. def test_checkout_to_non_existing_branch(self):
  1428. self.assertEqual(b"master", porcelain.active_branch(self.repo))
  1429. with self.assertRaises(KeyError):
  1430. porcelain.checkout_branch(self.repo, b"bob")
  1431. self.assertEqual(b"master", porcelain.active_branch(self.repo))
  1432. def test_checkout_to_branch_with_modified_files(self):
  1433. with open(self._foo_path, "a") as f:
  1434. f.write("new message\n")
  1435. porcelain.add(self.repo, paths=[self._foo_path])
  1436. status = list(porcelain.status(self.repo))
  1437. self.assertEqual(
  1438. [{"add": [], "delete": [], "modify": [b"foo"]}, [], []], status
  1439. )
  1440. # Both branches have file 'foo' checkout should be fine.
  1441. porcelain.checkout_branch(self.repo, b"uni")
  1442. self.assertEqual(b"uni", porcelain.active_branch(self.repo))
  1443. status = list(porcelain.status(self.repo))
  1444. self.assertEqual(
  1445. [{"add": [], "delete": [], "modify": [b"foo"]}, [], []], status
  1446. )
  1447. def test_checkout_with_deleted_files(self):
  1448. porcelain.remove(self.repo.path, [os.path.join(self.repo.path, "foo")])
  1449. status = list(porcelain.status(self.repo))
  1450. self.assertEqual(
  1451. [{"add": [], "delete": [b"foo"], "modify": []}, [], []], status
  1452. )
  1453. # Both branches have file 'foo' checkout should be fine.
  1454. porcelain.checkout_branch(self.repo, b"uni")
  1455. self.assertEqual(b"uni", porcelain.active_branch(self.repo))
  1456. status = list(porcelain.status(self.repo))
  1457. self.assertEqual(
  1458. [{"add": [], "delete": [b"foo"], "modify": []}, [], []], status
  1459. )
  1460. def test_checkout_to_branch_with_added_files(self):
  1461. file_path = os.path.join(self.repo.path, "bar")
  1462. with open(file_path, "w") as f:
  1463. f.write("bar content\n")
  1464. porcelain.add(self.repo, paths=[file_path])
  1465. status = list(porcelain.status(self.repo))
  1466. self.assertEqual(
  1467. [{"add": [b"bar"], "delete": [], "modify": []}, [], []], status
  1468. )
  1469. # Both branches have file 'foo' checkout should be fine.
  1470. porcelain.checkout_branch(self.repo, b"uni")
  1471. self.assertEqual(b"uni", porcelain.active_branch(self.repo))
  1472. status = list(porcelain.status(self.repo))
  1473. self.assertEqual(
  1474. [{"add": [b"bar"], "delete": [], "modify": []}, [], []], status
  1475. )
  1476. def test_checkout_to_branch_with_modified_file_not_present(self):
  1477. # Commit a new file that the other branch doesn't have.
  1478. _, nee_path = _commit_file_with_content(self.repo, "nee", "Good content\n")
  1479. # Modify the file the other branch doesn't have.
  1480. with open(nee_path, "a") as f:
  1481. f.write("bar content\n")
  1482. porcelain.add(self.repo, paths=[nee_path])
  1483. status = list(porcelain.status(self.repo))
  1484. self.assertEqual(
  1485. [{"add": [], "delete": [], "modify": [b"nee"]}, [], []], status
  1486. )
  1487. # 'uni' branch doesn't have 'nee' and it has been modified, should result in the checkout being aborted.
  1488. with self.assertRaises(CheckoutError):
  1489. porcelain.checkout_branch(self.repo, b"uni")
  1490. self.assertEqual(b"master", porcelain.active_branch(self.repo))
  1491. status = list(porcelain.status(self.repo))
  1492. self.assertEqual(
  1493. [{"add": [], "delete": [], "modify": [b"nee"]}, [], []], status
  1494. )
  1495. def test_checkout_to_branch_with_modified_file_not_present_forced(self):
  1496. # Commit a new file that the other branch doesn't have.
  1497. _, nee_path = _commit_file_with_content(self.repo, "nee", "Good content\n")
  1498. # Modify the file the other branch doesn't have.
  1499. with open(nee_path, "a") as f:
  1500. f.write("bar content\n")
  1501. porcelain.add(self.repo, paths=[nee_path])
  1502. status = list(porcelain.status(self.repo))
  1503. self.assertEqual(
  1504. [{"add": [], "delete": [], "modify": [b"nee"]}, [], []], status
  1505. )
  1506. # 'uni' branch doesn't have 'nee' and it has been modified, but we force to reset the entire index.
  1507. porcelain.checkout_branch(self.repo, b"uni", force=True)
  1508. self.assertEqual(b"uni", porcelain.active_branch(self.repo))
  1509. status = list(porcelain.status(self.repo))
  1510. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1511. def test_checkout_to_branch_with_unstaged_files(self):
  1512. # Edit `foo`.
  1513. with open(self._foo_path, "a") as f:
  1514. f.write("new message")
  1515. status = list(porcelain.status(self.repo))
  1516. self.assertEqual(
  1517. [{"add": [], "delete": [], "modify": []}, [b"foo"], []], status
  1518. )
  1519. porcelain.checkout_branch(self.repo, b"uni")
  1520. status = list(porcelain.status(self.repo))
  1521. self.assertEqual(
  1522. [{"add": [], "delete": [], "modify": []}, [b"foo"], []], status
  1523. )
  1524. def test_checkout_to_branch_with_untracked_files(self):
  1525. with open(os.path.join(self.repo.path, "neu"), "a") as f:
  1526. f.write("new message\n")
  1527. status = list(porcelain.status(self.repo))
  1528. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], ["neu"]], status)
  1529. porcelain.checkout_branch(self.repo, b"uni")
  1530. status = list(porcelain.status(self.repo))
  1531. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], ["neu"]], status)
  1532. def test_checkout_to_branch_with_new_files(self):
  1533. porcelain.checkout_branch(self.repo, b"uni")
  1534. sub_directory = os.path.join(self.repo.path, "sub1")
  1535. os.mkdir(sub_directory)
  1536. for index in range(5):
  1537. _commit_file_with_content(
  1538. self.repo, "new_file_" + str(index + 1), "Some content\n"
  1539. )
  1540. _commit_file_with_content(
  1541. self.repo,
  1542. os.path.join("sub1", "new_file_" + str(index + 10)),
  1543. "Good content\n",
  1544. )
  1545. status = list(porcelain.status(self.repo))
  1546. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1547. porcelain.checkout_branch(self.repo, b"master")
  1548. self.assertEqual(b"master", porcelain.active_branch(self.repo))
  1549. status = list(porcelain.status(self.repo))
  1550. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1551. porcelain.checkout_branch(self.repo, b"uni")
  1552. self.assertEqual(b"uni", porcelain.active_branch(self.repo))
  1553. status = list(porcelain.status(self.repo))
  1554. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1555. def test_checkout_to_branch_with_file_in_sub_directory(self):
  1556. sub_directory = os.path.join(self.repo.path, "sub1", "sub2")
  1557. os.makedirs(sub_directory)
  1558. sub_directory_file = os.path.join(sub_directory, "neu")
  1559. with open(sub_directory_file, "w") as f:
  1560. f.write("new message\n")
  1561. porcelain.add(self.repo, paths=[sub_directory_file])
  1562. porcelain.commit(
  1563. self.repo,
  1564. message=b"add " + sub_directory_file.encode(),
  1565. committer=b"Jane <jane@example.com>",
  1566. author=b"John <john@example.com>",
  1567. )
  1568. status = list(porcelain.status(self.repo))
  1569. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1570. self.assertTrue(os.path.isdir(sub_directory))
  1571. self.assertTrue(os.path.isdir(os.path.dirname(sub_directory)))
  1572. porcelain.checkout_branch(self.repo, b"uni")
  1573. status = list(porcelain.status(self.repo))
  1574. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1575. self.assertFalse(os.path.isdir(sub_directory))
  1576. self.assertFalse(os.path.isdir(os.path.dirname(sub_directory)))
  1577. porcelain.checkout_branch(self.repo, b"master")
  1578. self.assertTrue(os.path.isdir(sub_directory))
  1579. self.assertTrue(os.path.isdir(os.path.dirname(sub_directory)))
  1580. def test_checkout_to_branch_with_multiple_files_in_sub_directory(self):
  1581. sub_directory = os.path.join(self.repo.path, "sub1", "sub2")
  1582. os.makedirs(sub_directory)
  1583. sub_directory_file_1 = os.path.join(sub_directory, "neu")
  1584. with open(sub_directory_file_1, "w") as f:
  1585. f.write("new message\n")
  1586. sub_directory_file_2 = os.path.join(sub_directory, "gus")
  1587. with open(sub_directory_file_2, "w") as f:
  1588. f.write("alternative message\n")
  1589. porcelain.add(self.repo, paths=[sub_directory_file_1, sub_directory_file_2])
  1590. porcelain.commit(
  1591. self.repo,
  1592. message=b"add files neu and gus.",
  1593. committer=b"Jane <jane@example.com>",
  1594. author=b"John <john@example.com>",
  1595. )
  1596. status = list(porcelain.status(self.repo))
  1597. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1598. self.assertTrue(os.path.isdir(sub_directory))
  1599. self.assertTrue(os.path.isdir(os.path.dirname(sub_directory)))
  1600. porcelain.checkout_branch(self.repo, b"uni")
  1601. status = list(porcelain.status(self.repo))
  1602. self.assertEqual([{"add": [], "delete": [], "modify": []}, [], []], status)
  1603. self.assertFalse(os.path.isdir(sub_directory))
  1604. self.assertFalse(os.path.isdir(os.path.dirname(sub_directory)))
  1605. def _commit_something_wrong(self):
  1606. with open(self._foo_path, "a") as f:
  1607. f.write("something wrong")
  1608. porcelain.add(self.repo, paths=[self._foo_path])
  1609. return porcelain.commit(
  1610. self.repo,
  1611. message=b"I may added something wrong",
  1612. committer=b"Jane <jane@example.com>",
  1613. author=b"John <john@example.com>",
  1614. )
  1615. def test_checkout_to_commit_sha(self):
  1616. self._commit_something_wrong()
  1617. porcelain.checkout_branch(self.repo, self._sha)
  1618. self.assertEqual(self._sha, self.repo.head())
  1619. def test_checkout_to_head(self):
  1620. new_sha = self._commit_something_wrong()
  1621. porcelain.checkout_branch(self.repo, b"HEAD")
  1622. self.assertEqual(new_sha, self.repo.head())
  1623. def _checkout_remote_branch(self):
  1624. errstream = BytesIO()
  1625. outstream = BytesIO()
  1626. porcelain.commit(
  1627. repo=self.repo.path,
  1628. message=b"init",
  1629. author=b"author <email>",
  1630. committer=b"committer <email>",
  1631. )
  1632. # Setup target repo cloned from temp test repo
  1633. clone_path = tempfile.mkdtemp()
  1634. self.addCleanup(shutil.rmtree, clone_path)
  1635. target_repo = porcelain.clone(
  1636. self.repo.path, target=clone_path, errstream=errstream
  1637. )
  1638. try:
  1639. self.assertEqual(target_repo[b"HEAD"], self.repo[b"HEAD"])
  1640. finally:
  1641. target_repo.close()
  1642. # create a second file to be pushed back to origin
  1643. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  1644. os.close(handle)
  1645. porcelain.add(repo=clone_path, paths=[fullpath])
  1646. porcelain.commit(
  1647. repo=clone_path,
  1648. message=b"push",
  1649. author=b"author <email>",
  1650. committer=b"committer <email>",
  1651. )
  1652. # Setup a non-checked out branch in the remote
  1653. refs_path = b"refs/heads/foo"
  1654. new_id = self.repo[b"HEAD"].id
  1655. self.assertNotEqual(new_id, ZERO_SHA)
  1656. self.repo.refs[refs_path] = new_id
  1657. # Push to the remote
  1658. porcelain.push(
  1659. clone_path,
  1660. "origin",
  1661. b"HEAD:" + refs_path,
  1662. outstream=outstream,
  1663. errstream=errstream,
  1664. )
  1665. self.assertEqual(
  1666. target_repo.refs[b"refs/remotes/origin/foo"],
  1667. target_repo.refs[b"HEAD"],
  1668. )
  1669. porcelain.checkout_branch(target_repo, b"origin/foo")
  1670. original_id = target_repo[b"HEAD"].id
  1671. uni_id = target_repo[b"refs/remotes/origin/uni"].id
  1672. expected_refs = {
  1673. b"HEAD": original_id,
  1674. b"refs/heads/master": original_id,
  1675. b"refs/heads/foo": original_id,
  1676. b"refs/remotes/origin/foo": original_id,
  1677. b"refs/remotes/origin/uni": uni_id,
  1678. b"refs/remotes/origin/HEAD": new_id,
  1679. b"refs/remotes/origin/master": new_id,
  1680. }
  1681. self.assertEqual(expected_refs, target_repo.get_refs())
  1682. return target_repo
  1683. def test_checkout_remote_branch(self):
  1684. repo = self._checkout_remote_branch()
  1685. repo.close()
  1686. def test_checkout_remote_branch_then_master_then_remote_branch_again(self):
  1687. target_repo = self._checkout_remote_branch()
  1688. self.assertEqual(b"foo", porcelain.active_branch(target_repo))
  1689. _commit_file_with_content(target_repo, "bar", "something\n")
  1690. self.assertTrue(os.path.isfile(os.path.join(target_repo.path, "bar")))
  1691. porcelain.checkout_branch(target_repo, b"master")
  1692. self.assertEqual(b"master", porcelain.active_branch(target_repo))
  1693. self.assertFalse(os.path.isfile(os.path.join(target_repo.path, "bar")))
  1694. porcelain.checkout_branch(target_repo, b"origin/foo")
  1695. self.assertEqual(b"foo", porcelain.active_branch(target_repo))
  1696. self.assertTrue(os.path.isfile(os.path.join(target_repo.path, "bar")))
  1697. target_repo.close()
  1698. class SubmoduleTests(PorcelainTestCase):
  1699. def test_empty(self):
  1700. porcelain.commit(
  1701. repo=self.repo.path,
  1702. message=b"init",
  1703. author=b"author <email>",
  1704. committer=b"committer <email>",
  1705. )
  1706. self.assertEqual([], list(porcelain.submodule_list(self.repo)))
  1707. def test_add(self):
  1708. porcelain.submodule_add(self.repo, "../bar.git", "bar")
  1709. with open(f"{self.repo.path}/.gitmodules") as f:
  1710. self.assertEqual(
  1711. """\
  1712. [submodule "bar"]
  1713. \turl = ../bar.git
  1714. \tpath = bar
  1715. """,
  1716. f.read(),
  1717. )
  1718. def test_init(self):
  1719. porcelain.submodule_add(self.repo, "../bar.git", "bar")
  1720. porcelain.submodule_init(self.repo)
  1721. class PushTests(PorcelainTestCase):
  1722. def test_simple(self):
  1723. """Basic test of porcelain push where self.repo is the remote. First
  1724. clone the remote, commit a file to the clone, then push the changes
  1725. back to the remote.
  1726. """
  1727. outstream = BytesIO()
  1728. errstream = BytesIO()
  1729. porcelain.commit(
  1730. repo=self.repo.path,
  1731. message=b"init",
  1732. author=b"author <email>",
  1733. committer=b"committer <email>",
  1734. )
  1735. # Setup target repo cloned from temp test repo
  1736. clone_path = tempfile.mkdtemp()
  1737. self.addCleanup(shutil.rmtree, clone_path)
  1738. target_repo = porcelain.clone(
  1739. self.repo.path, target=clone_path, errstream=errstream
  1740. )
  1741. try:
  1742. self.assertEqual(target_repo[b"HEAD"], self.repo[b"HEAD"])
  1743. finally:
  1744. target_repo.close()
  1745. # create a second file to be pushed back to origin
  1746. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  1747. os.close(handle)
  1748. porcelain.add(repo=clone_path, paths=[fullpath])
  1749. porcelain.commit(
  1750. repo=clone_path,
  1751. message=b"push",
  1752. author=b"author <email>",
  1753. committer=b"committer <email>",
  1754. )
  1755. # Setup a non-checked out branch in the remote
  1756. refs_path = b"refs/heads/foo"
  1757. new_id = self.repo[b"HEAD"].id
  1758. self.assertNotEqual(new_id, ZERO_SHA)
  1759. self.repo.refs[refs_path] = new_id
  1760. # Push to the remote
  1761. porcelain.push(
  1762. clone_path,
  1763. "origin",
  1764. b"HEAD:" + refs_path,
  1765. outstream=outstream,
  1766. errstream=errstream,
  1767. )
  1768. self.assertEqual(
  1769. target_repo.refs[b"refs/remotes/origin/foo"],
  1770. target_repo.refs[b"HEAD"],
  1771. )
  1772. # Check that the target and source
  1773. with Repo(clone_path) as r_clone:
  1774. self.assertEqual(
  1775. {
  1776. b"HEAD": new_id,
  1777. b"refs/heads/foo": r_clone[b"HEAD"].id,
  1778. b"refs/heads/master": new_id,
  1779. },
  1780. self.repo.get_refs(),
  1781. )
  1782. self.assertEqual(r_clone[b"HEAD"].id, self.repo[refs_path].id)
  1783. # Get the change in the target repo corresponding to the add
  1784. # this will be in the foo branch.
  1785. change = next(
  1786. iter(
  1787. tree_changes(
  1788. self.repo,
  1789. self.repo[b"HEAD"].tree,
  1790. self.repo[b"refs/heads/foo"].tree,
  1791. )
  1792. )
  1793. )
  1794. self.assertEqual(
  1795. os.path.basename(fullpath), change.new.path.decode("ascii")
  1796. )
  1797. def test_local_missing(self):
  1798. """Pushing a new branch."""
  1799. outstream = BytesIO()
  1800. errstream = BytesIO()
  1801. # Setup target repo cloned from temp test repo
  1802. clone_path = tempfile.mkdtemp()
  1803. self.addCleanup(shutil.rmtree, clone_path)
  1804. target_repo = porcelain.init(clone_path)
  1805. target_repo.close()
  1806. self.assertRaises(
  1807. porcelain.Error,
  1808. porcelain.push,
  1809. self.repo,
  1810. clone_path,
  1811. b"HEAD:refs/heads/master",
  1812. outstream=outstream,
  1813. errstream=errstream,
  1814. )
  1815. def test_new(self):
  1816. """Pushing a new branch."""
  1817. outstream = BytesIO()
  1818. errstream = BytesIO()
  1819. # Setup target repo cloned from temp test repo
  1820. clone_path = tempfile.mkdtemp()
  1821. self.addCleanup(shutil.rmtree, clone_path)
  1822. target_repo = porcelain.init(clone_path)
  1823. target_repo.close()
  1824. # create a second file to be pushed back to origin
  1825. handle, fullpath = tempfile.mkstemp(dir=clone_path)
  1826. os.close(handle)
  1827. porcelain.add(repo=clone_path, paths=[fullpath])
  1828. new_id = porcelain.commit(
  1829. repo=self.repo,
  1830. message=b"push",
  1831. author=b"author <email>",
  1832. committer=b"committer <email>",
  1833. )
  1834. # Push to the remote
  1835. porcelain.push(
  1836. self.repo,
  1837. clone_path,
  1838. b"HEAD:refs/heads/master",
  1839. outstream=outstream,
  1840. errstream=errstream,
  1841. )
  1842. with Repo(clone_path) as r_clone:
  1843. self.assertEqual(
  1844. {
  1845. b"HEAD": new_id,
  1846. b"refs/heads/master": new_id,
  1847. },
  1848. r_clone.get_refs(),
  1849. )
  1850. def test_delete(self):
  1851. """Basic test of porcelain push, removing a branch."""
  1852. outstream = BytesIO()
  1853. errstream = BytesIO()
  1854. porcelain.commit(
  1855. repo=self.repo.path,
  1856. message=b"init",
  1857. author=b"author <email>",
  1858. committer=b"committer <email>",
  1859. )
  1860. # Setup target repo cloned from temp test repo
  1861. clone_path = tempfile.mkdtemp()
  1862. self.addCleanup(shutil.rmtree, clone_path)
  1863. target_repo = porcelain.clone(
  1864. self.repo.path, target=clone_path, errstream=errstream
  1865. )
  1866. target_repo.close()
  1867. # Setup a non-checked out branch in the remote
  1868. refs_path = b"refs/heads/foo"
  1869. new_id = self.repo[b"HEAD"].id
  1870. self.assertNotEqual(new_id, ZERO_SHA)
  1871. self.repo.refs[refs_path] = new_id
  1872. # Push to the remote
  1873. porcelain.push(
  1874. clone_path,
  1875. self.repo.path,
  1876. b":" + refs_path,
  1877. outstream=outstream,
  1878. errstream=errstream,
  1879. )
  1880. self.assertEqual(
  1881. {
  1882. b"HEAD": new_id,
  1883. b"refs/heads/master": new_id,
  1884. },
  1885. self.repo.get_refs(),
  1886. )
  1887. def test_diverged(self):
  1888. outstream = BytesIO()
  1889. errstream = BytesIO()
  1890. porcelain.commit(
  1891. repo=self.repo.path,
  1892. message=b"init",
  1893. author=b"author <email>",
  1894. committer=b"committer <email>",
  1895. )
  1896. # Setup target repo cloned from temp test repo
  1897. clone_path = tempfile.mkdtemp()
  1898. self.addCleanup(shutil.rmtree, clone_path)
  1899. target_repo = porcelain.clone(
  1900. self.repo.path, target=clone_path, errstream=errstream
  1901. )
  1902. target_repo.close()
  1903. remote_id = porcelain.commit(
  1904. repo=self.repo.path,
  1905. message=b"remote change",
  1906. author=b"author <email>",
  1907. committer=b"committer <email>",
  1908. )
  1909. local_id = porcelain.commit(
  1910. repo=clone_path,
  1911. message=b"local change",
  1912. author=b"author <email>",
  1913. committer=b"committer <email>",
  1914. )
  1915. outstream = BytesIO()
  1916. errstream = BytesIO()
  1917. # Push to the remote
  1918. self.assertRaises(
  1919. porcelain.DivergedBranches,
  1920. porcelain.push,
  1921. clone_path,
  1922. self.repo.path,
  1923. b"refs/heads/master",
  1924. outstream=outstream,
  1925. errstream=errstream,
  1926. )
  1927. self.assertEqual(
  1928. {
  1929. b"HEAD": remote_id,
  1930. b"refs/heads/master": remote_id,
  1931. },
  1932. self.repo.get_refs(),
  1933. )
  1934. self.assertEqual(b"", outstream.getvalue())
  1935. self.assertEqual(b"", errstream.getvalue())
  1936. outstream = BytesIO()
  1937. errstream = BytesIO()
  1938. # Push to the remote with --force
  1939. porcelain.push(
  1940. clone_path,
  1941. self.repo.path,
  1942. b"refs/heads/master",
  1943. outstream=outstream,
  1944. errstream=errstream,
  1945. force=True,
  1946. )
  1947. self.assertEqual(
  1948. {
  1949. b"HEAD": local_id,
  1950. b"refs/heads/master": local_id,
  1951. },
  1952. self.repo.get_refs(),
  1953. )
  1954. self.assertEqual(b"", outstream.getvalue())
  1955. self.assertTrue(re.match(b"Push to .* successful.\n", errstream.getvalue()))
  1956. class PullTests(PorcelainTestCase):
  1957. def setUp(self):
  1958. super().setUp()
  1959. # create a file for initial commit
  1960. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  1961. os.close(handle)
  1962. porcelain.add(repo=self.repo.path, paths=fullpath)
  1963. porcelain.commit(
  1964. repo=self.repo.path,
  1965. message=b"test",
  1966. author=b"test <email>",
  1967. committer=b"test <email>",
  1968. )
  1969. # Setup target repo
  1970. self.target_path = tempfile.mkdtemp()
  1971. self.addCleanup(shutil.rmtree, self.target_path)
  1972. target_repo = porcelain.clone(
  1973. self.repo.path, target=self.target_path, errstream=BytesIO()
  1974. )
  1975. target_repo.close()
  1976. # create a second file to be pushed
  1977. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  1978. os.close(handle)
  1979. porcelain.add(repo=self.repo.path, paths=fullpath)
  1980. porcelain.commit(
  1981. repo=self.repo.path,
  1982. message=b"test2",
  1983. author=b"test2 <email>",
  1984. committer=b"test2 <email>",
  1985. )
  1986. self.assertIn(b"refs/heads/master", self.repo.refs)
  1987. self.assertIn(b"refs/heads/master", target_repo.refs)
  1988. def test_simple(self):
  1989. outstream = BytesIO()
  1990. errstream = BytesIO()
  1991. # Pull changes into the cloned repo
  1992. porcelain.pull(
  1993. self.target_path,
  1994. self.repo.path,
  1995. b"refs/heads/master",
  1996. outstream=outstream,
  1997. errstream=errstream,
  1998. )
  1999. # Check the target repo for pushed changes
  2000. with Repo(self.target_path) as r:
  2001. self.assertEqual(r[b"HEAD"].id, self.repo[b"HEAD"].id)
  2002. def test_diverged(self):
  2003. outstream = BytesIO()
  2004. errstream = BytesIO()
  2005. c3a = porcelain.commit(
  2006. repo=self.target_path,
  2007. message=b"test3a",
  2008. author=b"test2 <email>",
  2009. committer=b"test2 <email>",
  2010. )
  2011. porcelain.commit(
  2012. repo=self.repo.path,
  2013. message=b"test3b",
  2014. author=b"test2 <email>",
  2015. committer=b"test2 <email>",
  2016. )
  2017. # Pull changes into the cloned repo
  2018. self.assertRaises(
  2019. porcelain.DivergedBranches,
  2020. porcelain.pull,
  2021. self.target_path,
  2022. self.repo.path,
  2023. b"refs/heads/master",
  2024. outstream=outstream,
  2025. errstream=errstream,
  2026. )
  2027. # Check the target repo for pushed changes
  2028. with Repo(self.target_path) as r:
  2029. self.assertEqual(r[b"refs/heads/master"].id, c3a)
  2030. self.assertRaises(
  2031. NotImplementedError,
  2032. porcelain.pull,
  2033. self.target_path,
  2034. self.repo.path,
  2035. b"refs/heads/master",
  2036. outstream=outstream,
  2037. errstream=errstream,
  2038. fast_forward=False,
  2039. )
  2040. # Check the target repo for pushed changes
  2041. with Repo(self.target_path) as r:
  2042. self.assertEqual(r[b"refs/heads/master"].id, c3a)
  2043. def test_no_refspec(self):
  2044. outstream = BytesIO()
  2045. errstream = BytesIO()
  2046. # Pull changes into the cloned repo
  2047. porcelain.pull(
  2048. self.target_path,
  2049. self.repo.path,
  2050. outstream=outstream,
  2051. errstream=errstream,
  2052. )
  2053. # Check the target repo for pushed changes
  2054. with Repo(self.target_path) as r:
  2055. self.assertEqual(r[b"HEAD"].id, self.repo[b"HEAD"].id)
  2056. def test_no_remote_location(self):
  2057. outstream = BytesIO()
  2058. errstream = BytesIO()
  2059. # Pull changes into the cloned repo
  2060. porcelain.pull(
  2061. self.target_path,
  2062. refspecs=b"refs/heads/master",
  2063. outstream=outstream,
  2064. errstream=errstream,
  2065. )
  2066. # Check the target repo for pushed changes
  2067. with Repo(self.target_path) as r:
  2068. self.assertEqual(r[b"HEAD"].id, self.repo[b"HEAD"].id)
  2069. class StatusTests(PorcelainTestCase):
  2070. def test_empty(self):
  2071. results = porcelain.status(self.repo)
  2072. self.assertEqual({"add": [], "delete": [], "modify": []}, results.staged)
  2073. self.assertEqual([], results.unstaged)
  2074. def test_status_base(self):
  2075. """Integration test for `status` functionality."""
  2076. # Commit a dummy file then modify it
  2077. fullpath = os.path.join(self.repo.path, "foo")
  2078. with open(fullpath, "w") as f:
  2079. f.write("origstuff")
  2080. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2081. porcelain.commit(
  2082. repo=self.repo.path,
  2083. message=b"test status",
  2084. author=b"author <email>",
  2085. committer=b"committer <email>",
  2086. )
  2087. # modify access and modify time of path
  2088. os.utime(fullpath, (0, 0))
  2089. with open(fullpath, "wb") as f:
  2090. f.write(b"stuff")
  2091. # Make a dummy file and stage it
  2092. filename_add = "bar"
  2093. fullpath = os.path.join(self.repo.path, filename_add)
  2094. with open(fullpath, "w") as f:
  2095. f.write("stuff")
  2096. porcelain.add(repo=self.repo.path, paths=fullpath)
  2097. results = porcelain.status(self.repo)
  2098. self.assertEqual(results.staged["add"][0], filename_add.encode("ascii"))
  2099. self.assertEqual(results.unstaged, [b"foo"])
  2100. def test_status_all(self):
  2101. del_path = os.path.join(self.repo.path, "foo")
  2102. mod_path = os.path.join(self.repo.path, "bar")
  2103. add_path = os.path.join(self.repo.path, "baz")
  2104. us_path = os.path.join(self.repo.path, "blye")
  2105. ut_path = os.path.join(self.repo.path, "blyat")
  2106. with open(del_path, "w") as f:
  2107. f.write("origstuff")
  2108. with open(mod_path, "w") as f:
  2109. f.write("origstuff")
  2110. with open(us_path, "w") as f:
  2111. f.write("origstuff")
  2112. porcelain.add(repo=self.repo.path, paths=[del_path, mod_path, us_path])
  2113. porcelain.commit(
  2114. repo=self.repo.path,
  2115. message=b"test status",
  2116. author=b"author <email>",
  2117. committer=b"committer <email>",
  2118. )
  2119. porcelain.remove(self.repo.path, [del_path])
  2120. with open(add_path, "w") as f:
  2121. f.write("origstuff")
  2122. with open(mod_path, "w") as f:
  2123. f.write("more_origstuff")
  2124. with open(us_path, "w") as f:
  2125. f.write("more_origstuff")
  2126. porcelain.add(repo=self.repo.path, paths=[add_path, mod_path])
  2127. with open(us_path, "w") as f:
  2128. f.write("\norigstuff")
  2129. with open(ut_path, "w") as f:
  2130. f.write("origstuff")
  2131. results = porcelain.status(self.repo.path)
  2132. self.assertDictEqual(
  2133. {"add": [b"baz"], "delete": [b"foo"], "modify": [b"bar"]},
  2134. results.staged,
  2135. )
  2136. self.assertListEqual(results.unstaged, [b"blye"])
  2137. results_no_untracked = porcelain.status(self.repo.path, untracked_files="no")
  2138. self.assertListEqual(results_no_untracked.untracked, [])
  2139. def test_status_wrong_untracked_files_value(self):
  2140. with self.assertRaises(ValueError):
  2141. porcelain.status(self.repo.path, untracked_files="antani")
  2142. def test_status_untracked_path(self):
  2143. untracked_dir = os.path.join(self.repo_path, "untracked_dir")
  2144. os.mkdir(untracked_dir)
  2145. untracked_file = os.path.join(untracked_dir, "untracked_file")
  2146. with open(untracked_file, "w") as fh:
  2147. fh.write("untracked")
  2148. _, _, untracked = porcelain.status(self.repo.path, untracked_files="all")
  2149. self.assertEqual(untracked, ["untracked_dir/untracked_file"])
  2150. def test_status_crlf_mismatch(self):
  2151. # First make a commit as if the file has been added on a Linux system
  2152. # or with core.autocrlf=True
  2153. file_path = os.path.join(self.repo.path, "crlf")
  2154. with open(file_path, "wb") as f:
  2155. f.write(b"line1\nline2")
  2156. porcelain.add(repo=self.repo.path, paths=[file_path])
  2157. porcelain.commit(
  2158. repo=self.repo.path,
  2159. message=b"test status",
  2160. author=b"author <email>",
  2161. committer=b"committer <email>",
  2162. )
  2163. # Then update the file as if it was created by CGit on a Windows
  2164. # system with core.autocrlf=true
  2165. with open(file_path, "wb") as f:
  2166. f.write(b"line1\r\nline2")
  2167. results = porcelain.status(self.repo)
  2168. self.assertDictEqual({"add": [], "delete": [], "modify": []}, results.staged)
  2169. self.assertListEqual(results.unstaged, [b"crlf"])
  2170. self.assertListEqual(results.untracked, [])
  2171. def test_status_autocrlf_true(self):
  2172. # First make a commit as if the file has been added on a Linux system
  2173. # or with core.autocrlf=True
  2174. file_path = os.path.join(self.repo.path, "crlf")
  2175. with open(file_path, "wb") as f:
  2176. f.write(b"line1\nline2")
  2177. porcelain.add(repo=self.repo.path, paths=[file_path])
  2178. porcelain.commit(
  2179. repo=self.repo.path,
  2180. message=b"test status",
  2181. author=b"author <email>",
  2182. committer=b"committer <email>",
  2183. )
  2184. # Then update the file as if it was created by CGit on a Windows
  2185. # system with core.autocrlf=true
  2186. with open(file_path, "wb") as f:
  2187. f.write(b"line1\r\nline2")
  2188. # TODO: It should be set automatically by looking at the configuration
  2189. c = self.repo.get_config()
  2190. c.set("core", "autocrlf", True)
  2191. c.write_to_path()
  2192. results = porcelain.status(self.repo)
  2193. self.assertDictEqual({"add": [], "delete": [], "modify": []}, results.staged)
  2194. self.assertListEqual(results.unstaged, [])
  2195. self.assertListEqual(results.untracked, [])
  2196. def test_status_autocrlf_input(self):
  2197. # Commit existing file with CRLF
  2198. file_path = os.path.join(self.repo.path, "crlf-exists")
  2199. with open(file_path, "wb") as f:
  2200. f.write(b"line1\r\nline2")
  2201. porcelain.add(repo=self.repo.path, paths=[file_path])
  2202. porcelain.commit(
  2203. repo=self.repo.path,
  2204. message=b"test status",
  2205. author=b"author <email>",
  2206. committer=b"committer <email>",
  2207. )
  2208. c = self.repo.get_config()
  2209. c.set("core", "autocrlf", "input")
  2210. c.write_to_path()
  2211. # Add new (untracked) file
  2212. file_path = os.path.join(self.repo.path, "crlf-new")
  2213. with open(file_path, "wb") as f:
  2214. f.write(b"line1\r\nline2")
  2215. porcelain.add(repo=self.repo.path, paths=[file_path])
  2216. results = porcelain.status(self.repo)
  2217. self.assertDictEqual(
  2218. {"add": [b"crlf-new"], "delete": [], "modify": []}, results.staged
  2219. )
  2220. self.assertListEqual(results.unstaged, [])
  2221. self.assertListEqual(results.untracked, [])
  2222. def test_get_tree_changes_add(self):
  2223. """Unit test for get_tree_changes add."""
  2224. # Make a dummy file, stage
  2225. filename = "bar"
  2226. fullpath = os.path.join(self.repo.path, filename)
  2227. with open(fullpath, "w") as f:
  2228. f.write("stuff")
  2229. porcelain.add(repo=self.repo.path, paths=fullpath)
  2230. porcelain.commit(
  2231. repo=self.repo.path,
  2232. message=b"test status",
  2233. author=b"author <email>",
  2234. committer=b"committer <email>",
  2235. )
  2236. filename = "foo"
  2237. fullpath = os.path.join(self.repo.path, filename)
  2238. with open(fullpath, "w") as f:
  2239. f.write("stuff")
  2240. porcelain.add(repo=self.repo.path, paths=fullpath)
  2241. changes = porcelain.get_tree_changes(self.repo.path)
  2242. self.assertEqual(changes["add"][0], filename.encode("ascii"))
  2243. self.assertEqual(len(changes["add"]), 1)
  2244. self.assertEqual(len(changes["modify"]), 0)
  2245. self.assertEqual(len(changes["delete"]), 0)
  2246. def test_get_tree_changes_modify(self):
  2247. """Unit test for get_tree_changes modify."""
  2248. # Make a dummy file, stage, commit, modify
  2249. filename = "foo"
  2250. fullpath = os.path.join(self.repo.path, filename)
  2251. with open(fullpath, "w") as f:
  2252. f.write("stuff")
  2253. porcelain.add(repo=self.repo.path, paths=fullpath)
  2254. porcelain.commit(
  2255. repo=self.repo.path,
  2256. message=b"test status",
  2257. author=b"author <email>",
  2258. committer=b"committer <email>",
  2259. )
  2260. with open(fullpath, "w") as f:
  2261. f.write("otherstuff")
  2262. porcelain.add(repo=self.repo.path, paths=fullpath)
  2263. changes = porcelain.get_tree_changes(self.repo.path)
  2264. self.assertEqual(changes["modify"][0], filename.encode("ascii"))
  2265. self.assertEqual(len(changes["add"]), 0)
  2266. self.assertEqual(len(changes["modify"]), 1)
  2267. self.assertEqual(len(changes["delete"]), 0)
  2268. def test_get_tree_changes_delete(self):
  2269. """Unit test for get_tree_changes delete."""
  2270. # Make a dummy file, stage, commit, remove
  2271. filename = "foo"
  2272. fullpath = os.path.join(self.repo.path, filename)
  2273. with open(fullpath, "w") as f:
  2274. f.write("stuff")
  2275. porcelain.add(repo=self.repo.path, paths=fullpath)
  2276. porcelain.commit(
  2277. repo=self.repo.path,
  2278. message=b"test status",
  2279. author=b"author <email>",
  2280. committer=b"committer <email>",
  2281. )
  2282. cwd = os.getcwd()
  2283. try:
  2284. os.chdir(self.repo.path)
  2285. porcelain.remove(repo=self.repo.path, paths=[filename])
  2286. finally:
  2287. os.chdir(cwd)
  2288. changes = porcelain.get_tree_changes(self.repo.path)
  2289. self.assertEqual(changes["delete"][0], filename.encode("ascii"))
  2290. self.assertEqual(len(changes["add"]), 0)
  2291. self.assertEqual(len(changes["modify"]), 0)
  2292. self.assertEqual(len(changes["delete"]), 1)
  2293. def test_get_untracked_paths(self):
  2294. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  2295. f.write("ignored\n")
  2296. with open(os.path.join(self.repo.path, "ignored"), "w") as f:
  2297. f.write("blah\n")
  2298. with open(os.path.join(self.repo.path, "notignored"), "w") as f:
  2299. f.write("blah\n")
  2300. os.symlink(
  2301. os.path.join(self.repo.path, os.pardir, "external_target"),
  2302. os.path.join(self.repo.path, "link"),
  2303. )
  2304. self.assertEqual(
  2305. {"ignored", "notignored", ".gitignore", "link"},
  2306. set(
  2307. porcelain.get_untracked_paths(
  2308. self.repo.path, self.repo.path, self.repo.open_index()
  2309. )
  2310. ),
  2311. )
  2312. self.assertEqual(
  2313. {".gitignore", "notignored", "link"},
  2314. set(porcelain.status(self.repo).untracked),
  2315. )
  2316. self.assertEqual(
  2317. {".gitignore", "notignored", "ignored", "link"},
  2318. set(porcelain.status(self.repo, ignored=True).untracked),
  2319. )
  2320. def test_get_untracked_paths_subrepo(self):
  2321. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  2322. f.write("nested/\n")
  2323. with open(os.path.join(self.repo.path, "notignored"), "w") as f:
  2324. f.write("blah\n")
  2325. subrepo = Repo.init(os.path.join(self.repo.path, "nested"), mkdir=True)
  2326. with open(os.path.join(subrepo.path, "ignored"), "w") as f:
  2327. f.write("bleep\n")
  2328. with open(os.path.join(subrepo.path, "with"), "w") as f:
  2329. f.write("bloop\n")
  2330. with open(os.path.join(subrepo.path, "manager"), "w") as f:
  2331. f.write("blop\n")
  2332. self.assertEqual(
  2333. {".gitignore", "notignored", os.path.join("nested", "")},
  2334. set(
  2335. porcelain.get_untracked_paths(
  2336. self.repo.path, self.repo.path, self.repo.open_index()
  2337. )
  2338. ),
  2339. )
  2340. self.assertEqual(
  2341. {".gitignore", "notignored"},
  2342. set(
  2343. porcelain.get_untracked_paths(
  2344. self.repo.path,
  2345. self.repo.path,
  2346. self.repo.open_index(),
  2347. exclude_ignored=True,
  2348. )
  2349. ),
  2350. )
  2351. self.assertEqual(
  2352. {"ignored", "with", "manager"},
  2353. set(
  2354. porcelain.get_untracked_paths(
  2355. subrepo.path, subrepo.path, subrepo.open_index()
  2356. )
  2357. ),
  2358. )
  2359. self.assertEqual(
  2360. set(),
  2361. set(
  2362. porcelain.get_untracked_paths(
  2363. subrepo.path,
  2364. self.repo.path,
  2365. self.repo.open_index(),
  2366. )
  2367. ),
  2368. )
  2369. self.assertEqual(
  2370. {
  2371. os.path.join("nested", "ignored"),
  2372. os.path.join("nested", "with"),
  2373. os.path.join("nested", "manager"),
  2374. },
  2375. set(
  2376. porcelain.get_untracked_paths(
  2377. self.repo.path,
  2378. subrepo.path,
  2379. self.repo.open_index(),
  2380. )
  2381. ),
  2382. )
  2383. def test_get_untracked_paths_subdir(self):
  2384. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  2385. f.write("subdir/\nignored")
  2386. with open(os.path.join(self.repo.path, "notignored"), "w") as f:
  2387. f.write("blah\n")
  2388. os.mkdir(os.path.join(self.repo.path, "subdir"))
  2389. with open(os.path.join(self.repo.path, "ignored"), "w") as f:
  2390. f.write("foo")
  2391. with open(os.path.join(self.repo.path, "subdir", "ignored"), "w") as f:
  2392. f.write("foo")
  2393. self.assertEqual(
  2394. {
  2395. ".gitignore",
  2396. "notignored",
  2397. "ignored",
  2398. os.path.join("subdir", ""),
  2399. },
  2400. set(
  2401. porcelain.get_untracked_paths(
  2402. self.repo.path,
  2403. self.repo.path,
  2404. self.repo.open_index(),
  2405. )
  2406. ),
  2407. )
  2408. self.assertEqual(
  2409. {".gitignore", "notignored"},
  2410. set(
  2411. porcelain.get_untracked_paths(
  2412. self.repo.path,
  2413. self.repo.path,
  2414. self.repo.open_index(),
  2415. exclude_ignored=True,
  2416. )
  2417. ),
  2418. )
  2419. def test_get_untracked_paths_invalid_untracked_files(self):
  2420. with self.assertRaises(ValueError):
  2421. list(
  2422. porcelain.get_untracked_paths(
  2423. self.repo.path,
  2424. self.repo.path,
  2425. self.repo.open_index(),
  2426. untracked_files="invalid_value",
  2427. )
  2428. )
  2429. def test_get_untracked_paths_normal(self):
  2430. with self.assertRaises(NotImplementedError):
  2431. _, _, _ = porcelain.status(repo=self.repo.path, untracked_files="normal")
  2432. # TODO(jelmer): Add test for dulwich.porcelain.daemon
  2433. class UploadPackTests(PorcelainTestCase):
  2434. """Tests for upload_pack."""
  2435. def test_upload_pack(self):
  2436. outf = BytesIO()
  2437. exitcode = porcelain.upload_pack(self.repo.path, BytesIO(b"0000"), outf)
  2438. outlines = outf.getvalue().splitlines()
  2439. self.assertEqual([b"0000"], outlines)
  2440. self.assertEqual(0, exitcode)
  2441. class ReceivePackTests(PorcelainTestCase):
  2442. """Tests for receive_pack."""
  2443. def test_receive_pack(self):
  2444. filename = "foo"
  2445. fullpath = os.path.join(self.repo.path, filename)
  2446. with open(fullpath, "w") as f:
  2447. f.write("stuff")
  2448. porcelain.add(repo=self.repo.path, paths=fullpath)
  2449. self.repo.do_commit(
  2450. message=b"test status",
  2451. author=b"author <email>",
  2452. committer=b"committer <email>",
  2453. author_timestamp=1402354300,
  2454. commit_timestamp=1402354300,
  2455. author_timezone=0,
  2456. commit_timezone=0,
  2457. )
  2458. outf = BytesIO()
  2459. exitcode = porcelain.receive_pack(self.repo.path, BytesIO(b"0000"), outf)
  2460. outlines = outf.getvalue().splitlines()
  2461. self.assertEqual(
  2462. [
  2463. b"0091319b56ce3aee2d489f759736a79cc552c9bb86d9 HEAD\x00 report-status "
  2464. b"delete-refs quiet ofs-delta side-band-64k "
  2465. b"no-done symref=HEAD:refs/heads/master",
  2466. b"003f319b56ce3aee2d489f759736a79cc552c9bb86d9 refs/heads/master",
  2467. b"0000",
  2468. ],
  2469. outlines,
  2470. )
  2471. self.assertEqual(0, exitcode)
  2472. class BranchListTests(PorcelainTestCase):
  2473. def test_standard(self):
  2474. self.assertEqual(set(), set(porcelain.branch_list(self.repo)))
  2475. def test_new_branch(self):
  2476. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2477. self.repo[b"HEAD"] = c1.id
  2478. porcelain.branch_create(self.repo, b"foo")
  2479. self.assertEqual({b"master", b"foo"}, set(porcelain.branch_list(self.repo)))
  2480. class BranchCreateTests(PorcelainTestCase):
  2481. def test_branch_exists(self):
  2482. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2483. self.repo[b"HEAD"] = c1.id
  2484. porcelain.branch_create(self.repo, b"foo")
  2485. self.assertRaises(porcelain.Error, porcelain.branch_create, self.repo, b"foo")
  2486. porcelain.branch_create(self.repo, b"foo", force=True)
  2487. def test_new_branch(self):
  2488. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2489. self.repo[b"HEAD"] = c1.id
  2490. porcelain.branch_create(self.repo, b"foo")
  2491. self.assertEqual({b"master", b"foo"}, set(porcelain.branch_list(self.repo)))
  2492. class BranchDeleteTests(PorcelainTestCase):
  2493. def test_simple(self):
  2494. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2495. self.repo[b"HEAD"] = c1.id
  2496. porcelain.branch_create(self.repo, b"foo")
  2497. self.assertIn(b"foo", porcelain.branch_list(self.repo))
  2498. porcelain.branch_delete(self.repo, b"foo")
  2499. self.assertNotIn(b"foo", porcelain.branch_list(self.repo))
  2500. def test_simple_unicode(self):
  2501. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2502. self.repo[b"HEAD"] = c1.id
  2503. porcelain.branch_create(self.repo, "foo")
  2504. self.assertIn(b"foo", porcelain.branch_list(self.repo))
  2505. porcelain.branch_delete(self.repo, "foo")
  2506. self.assertNotIn(b"foo", porcelain.branch_list(self.repo))
  2507. class FetchTests(PorcelainTestCase):
  2508. def test_simple(self):
  2509. outstream = BytesIO()
  2510. errstream = BytesIO()
  2511. # create a file for initial commit
  2512. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  2513. os.close(handle)
  2514. porcelain.add(repo=self.repo.path, paths=fullpath)
  2515. porcelain.commit(
  2516. repo=self.repo.path,
  2517. message=b"test",
  2518. author=b"test <email>",
  2519. committer=b"test <email>",
  2520. )
  2521. # Setup target repo
  2522. target_path = tempfile.mkdtemp()
  2523. self.addCleanup(shutil.rmtree, target_path)
  2524. target_repo = porcelain.clone(
  2525. self.repo.path, target=target_path, errstream=errstream
  2526. )
  2527. # create a second file to be pushed
  2528. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  2529. os.close(handle)
  2530. porcelain.add(repo=self.repo.path, paths=fullpath)
  2531. porcelain.commit(
  2532. repo=self.repo.path,
  2533. message=b"test2",
  2534. author=b"test2 <email>",
  2535. committer=b"test2 <email>",
  2536. )
  2537. self.assertNotIn(self.repo[b"HEAD"].id, target_repo)
  2538. target_repo.close()
  2539. # Fetch changes into the cloned repo
  2540. porcelain.fetch(target_path, "origin", outstream=outstream, errstream=errstream)
  2541. # Assert that fetch updated the local image of the remote
  2542. self.assert_correct_remote_refs(target_repo.get_refs(), self.repo.get_refs())
  2543. # Check the target repo for pushed changes
  2544. with Repo(target_path) as r:
  2545. self.assertIn(self.repo[b"HEAD"].id, r)
  2546. def test_with_remote_name(self):
  2547. remote_name = "origin"
  2548. outstream = BytesIO()
  2549. errstream = BytesIO()
  2550. # create a file for initial commit
  2551. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  2552. os.close(handle)
  2553. porcelain.add(repo=self.repo.path, paths=fullpath)
  2554. porcelain.commit(
  2555. repo=self.repo.path,
  2556. message=b"test",
  2557. author=b"test <email>",
  2558. committer=b"test <email>",
  2559. )
  2560. # Setup target repo
  2561. target_path = tempfile.mkdtemp()
  2562. self.addCleanup(shutil.rmtree, target_path)
  2563. target_repo = porcelain.clone(
  2564. self.repo.path, target=target_path, errstream=errstream
  2565. )
  2566. # Capture current refs
  2567. target_refs = target_repo.get_refs()
  2568. # create a second file to be pushed
  2569. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  2570. os.close(handle)
  2571. porcelain.add(repo=self.repo.path, paths=fullpath)
  2572. porcelain.commit(
  2573. repo=self.repo.path,
  2574. message=b"test2",
  2575. author=b"test2 <email>",
  2576. committer=b"test2 <email>",
  2577. )
  2578. self.assertNotIn(self.repo[b"HEAD"].id, target_repo)
  2579. target_config = target_repo.get_config()
  2580. target_config.set(
  2581. (b"remote", remote_name.encode()), b"url", self.repo.path.encode()
  2582. )
  2583. target_repo.close()
  2584. # Fetch changes into the cloned repo
  2585. porcelain.fetch(
  2586. target_path, remote_name, outstream=outstream, errstream=errstream
  2587. )
  2588. # Assert that fetch updated the local image of the remote
  2589. self.assert_correct_remote_refs(target_repo.get_refs(), self.repo.get_refs())
  2590. # Check the target repo for pushed changes, as well as updates
  2591. # for the refs
  2592. with Repo(target_path) as r:
  2593. self.assertIn(self.repo[b"HEAD"].id, r)
  2594. self.assertNotEqual(self.repo.get_refs(), target_refs)
  2595. def assert_correct_remote_refs(
  2596. self, local_refs, remote_refs, remote_name=b"origin"
  2597. ):
  2598. """Assert that known remote refs corresponds to actual remote refs."""
  2599. local_ref_prefix = b"refs/heads"
  2600. remote_ref_prefix = b"refs/remotes/" + remote_name
  2601. locally_known_remote_refs = {
  2602. k[len(remote_ref_prefix) + 1 :]: v
  2603. for k, v in local_refs.items()
  2604. if k.startswith(remote_ref_prefix)
  2605. }
  2606. normalized_remote_refs = {
  2607. k[len(local_ref_prefix) + 1 :]: v
  2608. for k, v in remote_refs.items()
  2609. if k.startswith(local_ref_prefix)
  2610. }
  2611. if b"HEAD" in locally_known_remote_refs and b"HEAD" in remote_refs:
  2612. normalized_remote_refs[b"HEAD"] = remote_refs[b"HEAD"]
  2613. self.assertEqual(locally_known_remote_refs, normalized_remote_refs)
  2614. class RepackTests(PorcelainTestCase):
  2615. def test_empty(self):
  2616. porcelain.repack(self.repo)
  2617. def test_simple(self):
  2618. handle, fullpath = tempfile.mkstemp(dir=self.repo.path)
  2619. os.close(handle)
  2620. porcelain.add(repo=self.repo.path, paths=fullpath)
  2621. porcelain.repack(self.repo)
  2622. class LsTreeTests(PorcelainTestCase):
  2623. def test_empty(self):
  2624. porcelain.commit(
  2625. repo=self.repo.path,
  2626. message=b"test status",
  2627. author=b"author <email>",
  2628. committer=b"committer <email>",
  2629. )
  2630. f = StringIO()
  2631. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  2632. self.assertEqual(f.getvalue(), "")
  2633. def test_simple(self):
  2634. # Commit a dummy file then modify it
  2635. fullpath = os.path.join(self.repo.path, "foo")
  2636. with open(fullpath, "w") as f:
  2637. f.write("origstuff")
  2638. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2639. porcelain.commit(
  2640. repo=self.repo.path,
  2641. message=b"test status",
  2642. author=b"author <email>",
  2643. committer=b"committer <email>",
  2644. )
  2645. f = StringIO()
  2646. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  2647. self.assertEqual(
  2648. f.getvalue(),
  2649. "100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tfoo\n",
  2650. )
  2651. def test_recursive(self):
  2652. # Create a directory then write a dummy file in it
  2653. dirpath = os.path.join(self.repo.path, "adir")
  2654. filepath = os.path.join(dirpath, "afile")
  2655. os.mkdir(dirpath)
  2656. with open(filepath, "w") as f:
  2657. f.write("origstuff")
  2658. porcelain.add(repo=self.repo.path, paths=[filepath])
  2659. porcelain.commit(
  2660. repo=self.repo.path,
  2661. message=b"test status",
  2662. author=b"author <email>",
  2663. committer=b"committer <email>",
  2664. )
  2665. f = StringIO()
  2666. porcelain.ls_tree(self.repo, b"HEAD", outstream=f)
  2667. self.assertEqual(
  2668. f.getvalue(),
  2669. "40000 tree b145cc69a5e17693e24d8a7be0016ed8075de66d\tadir\n",
  2670. )
  2671. f = StringIO()
  2672. porcelain.ls_tree(self.repo, b"HEAD", outstream=f, recursive=True)
  2673. self.assertEqual(
  2674. f.getvalue(),
  2675. "40000 tree b145cc69a5e17693e24d8a7be0016ed8075de66d\tadir\n"
  2676. "100644 blob 8b82634d7eae019850bb883f06abf428c58bc9aa\tadir"
  2677. "/afile\n",
  2678. )
  2679. class LsRemoteTests(PorcelainTestCase):
  2680. def test_empty(self):
  2681. self.assertEqual({}, porcelain.ls_remote(self.repo.path))
  2682. def test_some(self):
  2683. cid = porcelain.commit(
  2684. repo=self.repo.path,
  2685. message=b"test status",
  2686. author=b"author <email>",
  2687. committer=b"committer <email>",
  2688. )
  2689. self.assertEqual(
  2690. {b"refs/heads/master": cid, b"HEAD": cid},
  2691. porcelain.ls_remote(self.repo.path),
  2692. )
  2693. class LsFilesTests(PorcelainTestCase):
  2694. def test_empty(self):
  2695. self.assertEqual([], list(porcelain.ls_files(self.repo)))
  2696. def test_simple(self):
  2697. # Commit a dummy file then modify it
  2698. fullpath = os.path.join(self.repo.path, "foo")
  2699. with open(fullpath, "w") as f:
  2700. f.write("origstuff")
  2701. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2702. self.assertEqual([b"foo"], list(porcelain.ls_files(self.repo)))
  2703. class RemoteAddTests(PorcelainTestCase):
  2704. def test_new(self):
  2705. porcelain.remote_add(self.repo, "jelmer", "git://jelmer.uk/code/dulwich")
  2706. c = self.repo.get_config()
  2707. self.assertEqual(
  2708. c.get((b"remote", b"jelmer"), b"url"),
  2709. b"git://jelmer.uk/code/dulwich",
  2710. )
  2711. def test_exists(self):
  2712. porcelain.remote_add(self.repo, "jelmer", "git://jelmer.uk/code/dulwich")
  2713. self.assertRaises(
  2714. porcelain.RemoteExists,
  2715. porcelain.remote_add,
  2716. self.repo,
  2717. "jelmer",
  2718. "git://jelmer.uk/code/dulwich",
  2719. )
  2720. class RemoteRemoveTests(PorcelainTestCase):
  2721. def test_remove(self):
  2722. porcelain.remote_add(self.repo, "jelmer", "git://jelmer.uk/code/dulwich")
  2723. c = self.repo.get_config()
  2724. self.assertEqual(
  2725. c.get((b"remote", b"jelmer"), b"url"),
  2726. b"git://jelmer.uk/code/dulwich",
  2727. )
  2728. porcelain.remote_remove(self.repo, "jelmer")
  2729. self.assertRaises(KeyError, porcelain.remote_remove, self.repo, "jelmer")
  2730. c = self.repo.get_config()
  2731. self.assertRaises(KeyError, c.get, (b"remote", b"jelmer"), b"url")
  2732. class CheckIgnoreTests(PorcelainTestCase):
  2733. def test_check_ignored(self):
  2734. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  2735. f.write("foo")
  2736. foo_path = os.path.join(self.repo.path, "foo")
  2737. with open(foo_path, "w") as f:
  2738. f.write("BAR")
  2739. bar_path = os.path.join(self.repo.path, "bar")
  2740. with open(bar_path, "w") as f:
  2741. f.write("BAR")
  2742. self.assertEqual(["foo"], list(porcelain.check_ignore(self.repo, [foo_path])))
  2743. self.assertEqual([], list(porcelain.check_ignore(self.repo, [bar_path])))
  2744. def test_check_added_abs(self):
  2745. path = os.path.join(self.repo.path, "foo")
  2746. with open(path, "w") as f:
  2747. f.write("BAR")
  2748. self.repo.stage(["foo"])
  2749. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  2750. f.write("foo\n")
  2751. self.assertEqual([], list(porcelain.check_ignore(self.repo, [path])))
  2752. self.assertEqual(
  2753. ["foo"],
  2754. list(porcelain.check_ignore(self.repo, [path], no_index=True)),
  2755. )
  2756. def test_check_added_rel(self):
  2757. with open(os.path.join(self.repo.path, "foo"), "w") as f:
  2758. f.write("BAR")
  2759. self.repo.stage(["foo"])
  2760. with open(os.path.join(self.repo.path, ".gitignore"), "w") as f:
  2761. f.write("foo\n")
  2762. cwd = os.getcwd()
  2763. os.mkdir(os.path.join(self.repo.path, "bar"))
  2764. os.chdir(os.path.join(self.repo.path, "bar"))
  2765. try:
  2766. self.assertEqual(list(porcelain.check_ignore(self.repo, ["../foo"])), [])
  2767. self.assertEqual(
  2768. ["../foo"],
  2769. list(porcelain.check_ignore(self.repo, ["../foo"], no_index=True)),
  2770. )
  2771. finally:
  2772. os.chdir(cwd)
  2773. class UpdateHeadTests(PorcelainTestCase):
  2774. def test_set_to_branch(self):
  2775. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2776. self.repo.refs[b"refs/heads/blah"] = c1.id
  2777. porcelain.update_head(self.repo, "blah")
  2778. self.assertEqual(c1.id, self.repo.head())
  2779. self.assertEqual(b"ref: refs/heads/blah", self.repo.refs.read_ref(b"HEAD"))
  2780. def test_set_to_branch_detached(self):
  2781. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2782. self.repo.refs[b"refs/heads/blah"] = c1.id
  2783. porcelain.update_head(self.repo, "blah", detached=True)
  2784. self.assertEqual(c1.id, self.repo.head())
  2785. self.assertEqual(c1.id, self.repo.refs.read_ref(b"HEAD"))
  2786. def test_set_to_commit_detached(self):
  2787. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2788. self.repo.refs[b"refs/heads/blah"] = c1.id
  2789. porcelain.update_head(self.repo, c1.id, detached=True)
  2790. self.assertEqual(c1.id, self.repo.head())
  2791. self.assertEqual(c1.id, self.repo.refs.read_ref(b"HEAD"))
  2792. def test_set_new_branch(self):
  2793. [c1] = build_commit_graph(self.repo.object_store, [[1]])
  2794. self.repo.refs[b"refs/heads/blah"] = c1.id
  2795. porcelain.update_head(self.repo, "blah", new_branch="bar")
  2796. self.assertEqual(c1.id, self.repo.head())
  2797. self.assertEqual(b"ref: refs/heads/bar", self.repo.refs.read_ref(b"HEAD"))
  2798. class MailmapTests(PorcelainTestCase):
  2799. def test_no_mailmap(self):
  2800. self.assertEqual(
  2801. b"Jelmer Vernooij <jelmer@samba.org>",
  2802. porcelain.check_mailmap(self.repo, b"Jelmer Vernooij <jelmer@samba.org>"),
  2803. )
  2804. def test_mailmap_lookup(self):
  2805. with open(os.path.join(self.repo.path, ".mailmap"), "wb") as f:
  2806. f.write(
  2807. b"""\
  2808. Jelmer Vernooij <jelmer@debian.org>
  2809. """
  2810. )
  2811. self.assertEqual(
  2812. b"Jelmer Vernooij <jelmer@debian.org>",
  2813. porcelain.check_mailmap(self.repo, b"Jelmer Vernooij <jelmer@samba.org>"),
  2814. )
  2815. class FsckTests(PorcelainTestCase):
  2816. def test_none(self):
  2817. self.assertEqual([], list(porcelain.fsck(self.repo)))
  2818. def test_git_dir(self):
  2819. obj = Tree()
  2820. a = Blob()
  2821. a.data = b"foo"
  2822. obj.add(b".git", 0o100644, a.id)
  2823. self.repo.object_store.add_objects([(a, None), (obj, None)])
  2824. self.assertEqual(
  2825. [(obj.id, "invalid name .git")],
  2826. [(sha, str(e)) for (sha, e) in porcelain.fsck(self.repo)],
  2827. )
  2828. class DescribeTests(PorcelainTestCase):
  2829. def test_no_commits(self):
  2830. self.assertRaises(KeyError, porcelain.describe, self.repo.path)
  2831. def test_single_commit(self):
  2832. fullpath = os.path.join(self.repo.path, "foo")
  2833. with open(fullpath, "w") as f:
  2834. f.write("BAR")
  2835. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2836. sha = porcelain.commit(
  2837. self.repo.path,
  2838. message=b"Some message",
  2839. author=b"Joe <joe@example.com>",
  2840. committer=b"Bob <bob@example.com>",
  2841. )
  2842. self.assertEqual(
  2843. "g{}".format(sha[:7].decode("ascii")),
  2844. porcelain.describe(self.repo.path),
  2845. )
  2846. def test_tag(self):
  2847. fullpath = os.path.join(self.repo.path, "foo")
  2848. with open(fullpath, "w") as f:
  2849. f.write("BAR")
  2850. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2851. porcelain.commit(
  2852. self.repo.path,
  2853. message=b"Some message",
  2854. author=b"Joe <joe@example.com>",
  2855. committer=b"Bob <bob@example.com>",
  2856. )
  2857. porcelain.tag_create(
  2858. self.repo.path,
  2859. b"tryme",
  2860. b"foo <foo@bar.com>",
  2861. b"bar",
  2862. annotated=True,
  2863. )
  2864. self.assertEqual("tryme", porcelain.describe(self.repo.path))
  2865. def test_tag_and_commit(self):
  2866. fullpath = os.path.join(self.repo.path, "foo")
  2867. with open(fullpath, "w") as f:
  2868. f.write("BAR")
  2869. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2870. porcelain.commit(
  2871. self.repo.path,
  2872. message=b"Some message",
  2873. author=b"Joe <joe@example.com>",
  2874. committer=b"Bob <bob@example.com>",
  2875. )
  2876. porcelain.tag_create(
  2877. self.repo.path,
  2878. b"tryme",
  2879. b"foo <foo@bar.com>",
  2880. b"bar",
  2881. annotated=True,
  2882. )
  2883. with open(fullpath, "w") as f:
  2884. f.write("BAR2")
  2885. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2886. sha = porcelain.commit(
  2887. self.repo.path,
  2888. message=b"Some message",
  2889. author=b"Joe <joe@example.com>",
  2890. committer=b"Bob <bob@example.com>",
  2891. )
  2892. self.assertEqual(
  2893. "tryme-1-g{}".format(sha[:7].decode("ascii")),
  2894. porcelain.describe(self.repo.path),
  2895. )
  2896. def test_tag_and_commit_full(self):
  2897. fullpath = os.path.join(self.repo.path, "foo")
  2898. with open(fullpath, "w") as f:
  2899. f.write("BAR")
  2900. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2901. porcelain.commit(
  2902. self.repo.path,
  2903. message=b"Some message",
  2904. author=b"Joe <joe@example.com>",
  2905. committer=b"Bob <bob@example.com>",
  2906. )
  2907. porcelain.tag_create(
  2908. self.repo.path,
  2909. b"tryme",
  2910. b"foo <foo@bar.com>",
  2911. b"bar",
  2912. annotated=True,
  2913. )
  2914. with open(fullpath, "w") as f:
  2915. f.write("BAR2")
  2916. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2917. sha = porcelain.commit(
  2918. self.repo.path,
  2919. message=b"Some message",
  2920. author=b"Joe <joe@example.com>",
  2921. committer=b"Bob <bob@example.com>",
  2922. )
  2923. self.assertEqual(
  2924. "tryme-1-g{}".format(sha.decode("ascii")),
  2925. porcelain.describe(self.repo.path, abbrev=40),
  2926. )
  2927. class PathToTreeTests(PorcelainTestCase):
  2928. def setUp(self):
  2929. super().setUp()
  2930. self.fp = os.path.join(self.test_dir, "bar")
  2931. with open(self.fp, "w") as f:
  2932. f.write("something")
  2933. oldcwd = os.getcwd()
  2934. self.addCleanup(os.chdir, oldcwd)
  2935. os.chdir(self.test_dir)
  2936. def test_path_to_tree_path_base(self):
  2937. self.assertEqual(b"bar", porcelain.path_to_tree_path(self.test_dir, self.fp))
  2938. self.assertEqual(b"bar", porcelain.path_to_tree_path(".", "./bar"))
  2939. self.assertEqual(b"bar", porcelain.path_to_tree_path(".", "bar"))
  2940. cwd = os.getcwd()
  2941. self.assertEqual(
  2942. b"bar", porcelain.path_to_tree_path(".", os.path.join(cwd, "bar"))
  2943. )
  2944. self.assertEqual(b"bar", porcelain.path_to_tree_path(cwd, "bar"))
  2945. def test_path_to_tree_path_syntax(self):
  2946. self.assertEqual(b"bar", porcelain.path_to_tree_path(".", "./bar"))
  2947. def test_path_to_tree_path_error(self):
  2948. with self.assertRaises(ValueError):
  2949. with tempfile.TemporaryDirectory() as od:
  2950. porcelain.path_to_tree_path(od, self.fp)
  2951. def test_path_to_tree_path_rel(self):
  2952. cwd = os.getcwd()
  2953. os.mkdir(os.path.join(self.repo.path, "foo"))
  2954. os.mkdir(os.path.join(self.repo.path, "foo/bar"))
  2955. try:
  2956. os.chdir(os.path.join(self.repo.path, "foo/bar"))
  2957. with open("baz", "w") as f:
  2958. f.write("contents")
  2959. self.assertEqual(b"bar/baz", porcelain.path_to_tree_path("..", "baz"))
  2960. self.assertEqual(
  2961. b"bar/baz",
  2962. porcelain.path_to_tree_path(
  2963. os.path.join(os.getcwd(), ".."),
  2964. os.path.join(os.getcwd(), "baz"),
  2965. ),
  2966. )
  2967. self.assertEqual(
  2968. b"bar/baz",
  2969. porcelain.path_to_tree_path("..", os.path.join(os.getcwd(), "baz")),
  2970. )
  2971. self.assertEqual(
  2972. b"bar/baz",
  2973. porcelain.path_to_tree_path(os.path.join(os.getcwd(), ".."), "baz"),
  2974. )
  2975. finally:
  2976. os.chdir(cwd)
  2977. class GetObjectByPathTests(PorcelainTestCase):
  2978. def test_simple(self):
  2979. fullpath = os.path.join(self.repo.path, "foo")
  2980. with open(fullpath, "w") as f:
  2981. f.write("BAR")
  2982. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2983. porcelain.commit(
  2984. self.repo.path,
  2985. message=b"Some message",
  2986. author=b"Joe <joe@example.com>",
  2987. committer=b"Bob <bob@example.com>",
  2988. )
  2989. self.assertEqual(b"BAR", porcelain.get_object_by_path(self.repo, "foo").data)
  2990. self.assertEqual(b"BAR", porcelain.get_object_by_path(self.repo, b"foo").data)
  2991. def test_encoding(self):
  2992. fullpath = os.path.join(self.repo.path, "foo")
  2993. with open(fullpath, "w") as f:
  2994. f.write("BAR")
  2995. porcelain.add(repo=self.repo.path, paths=[fullpath])
  2996. porcelain.commit(
  2997. self.repo.path,
  2998. message=b"Some message",
  2999. author=b"Joe <joe@example.com>",
  3000. committer=b"Bob <bob@example.com>",
  3001. encoding=b"utf-8",
  3002. )
  3003. self.assertEqual(b"BAR", porcelain.get_object_by_path(self.repo, "foo").data)
  3004. self.assertEqual(b"BAR", porcelain.get_object_by_path(self.repo, b"foo").data)
  3005. def test_missing(self):
  3006. self.assertRaises(KeyError, porcelain.get_object_by_path, self.repo, "foo")
  3007. class WriteTreeTests(PorcelainTestCase):
  3008. def test_simple(self):
  3009. fullpath = os.path.join(self.repo.path, "foo")
  3010. with open(fullpath, "w") as f:
  3011. f.write("BAR")
  3012. porcelain.add(repo=self.repo.path, paths=[fullpath])
  3013. self.assertEqual(
  3014. b"d2092c8a9f311f0311083bf8d177f2ca0ab5b241",
  3015. porcelain.write_tree(self.repo),
  3016. )
  3017. class ActiveBranchTests(PorcelainTestCase):
  3018. def test_simple(self):
  3019. self.assertEqual(b"master", porcelain.active_branch(self.repo))
  3020. class FindUniqueAbbrevTests(PorcelainTestCase):
  3021. def test_simple(self):
  3022. c1, c2, c3 = build_commit_graph(
  3023. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  3024. )
  3025. self.repo.refs[b"HEAD"] = c3.id
  3026. self.assertEqual(
  3027. c1.id.decode("ascii")[:7],
  3028. porcelain.find_unique_abbrev(self.repo.object_store, c1.id),
  3029. )
  3030. class PackRefsTests(PorcelainTestCase):
  3031. def test_all(self):
  3032. c1, c2, c3 = build_commit_graph(
  3033. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  3034. )
  3035. self.repo.refs[b"HEAD"] = c3.id
  3036. self.repo.refs[b"refs/heads/master"] = c2.id
  3037. self.repo.refs[b"refs/tags/foo"] = c1.id
  3038. porcelain.pack_refs(self.repo, all=True)
  3039. self.assertEqual(
  3040. self.repo.refs.get_packed_refs(),
  3041. {
  3042. b"refs/heads/master": c2.id,
  3043. b"refs/tags/foo": c1.id,
  3044. },
  3045. )
  3046. def test_not_all(self):
  3047. c1, c2, c3 = build_commit_graph(
  3048. self.repo.object_store, [[1], [2, 1], [3, 1, 2]]
  3049. )
  3050. self.repo.refs[b"HEAD"] = c3.id
  3051. self.repo.refs[b"refs/heads/master"] = c2.id
  3052. self.repo.refs[b"refs/tags/foo"] = c1.id
  3053. porcelain.pack_refs(self.repo)
  3054. self.assertEqual(
  3055. self.repo.refs.get_packed_refs(),
  3056. {
  3057. b"refs/tags/foo": c1.id,
  3058. },
  3059. )
  3060. class ServerTests(PorcelainTestCase):
  3061. @contextlib.contextmanager
  3062. def _serving(self):
  3063. with make_server("localhost", 0, self.app) as server:
  3064. thread = threading.Thread(target=server.serve_forever, daemon=True)
  3065. thread.start()
  3066. try:
  3067. yield f"http://localhost:{server.server_port}"
  3068. finally:
  3069. server.shutdown()
  3070. thread.join(10)
  3071. def setUp(self):
  3072. super().setUp()
  3073. self.served_repo_path = os.path.join(self.test_dir, "served_repo.git")
  3074. self.served_repo = Repo.init_bare(self.served_repo_path, mkdir=True)
  3075. self.addCleanup(self.served_repo.close)
  3076. backend = DictBackend({"/": self.served_repo})
  3077. self.app = make_wsgi_chain(backend)
  3078. def test_pull(self):
  3079. (c1,) = build_commit_graph(self.served_repo.object_store, [[1]])
  3080. self.served_repo.refs[b"refs/heads/master"] = c1.id
  3081. with self._serving() as url:
  3082. porcelain.pull(self.repo, url, "master")
  3083. def test_push(self):
  3084. (c1,) = build_commit_graph(self.repo.object_store, [[1]])
  3085. self.repo.refs[b"refs/heads/master"] = c1.id
  3086. with self._serving() as url:
  3087. porcelain.push(self.repo, url, "master")
  3088. class ForEachTests(PorcelainTestCase):
  3089. def setUp(self):
  3090. super().setUp()
  3091. c1, c2, c3, c4 = build_commit_graph(
  3092. self.repo.object_store, [[1], [2, 1], [3, 1, 2], [4]]
  3093. )
  3094. porcelain.tag_create(
  3095. self.repo.path,
  3096. b"v0.1",
  3097. objectish=c1.id,
  3098. annotated=True,
  3099. message=b"0.1",
  3100. )
  3101. porcelain.tag_create(
  3102. self.repo.path,
  3103. b"v1.0",
  3104. objectish=c2.id,
  3105. annotated=True,
  3106. message=b"1.0",
  3107. )
  3108. porcelain.tag_create(self.repo.path, b"simple-tag", objectish=c3.id)
  3109. porcelain.tag_create(
  3110. self.repo.path,
  3111. b"v1.1",
  3112. objectish=c4.id,
  3113. annotated=True,
  3114. message=b"1.1",
  3115. )
  3116. porcelain.branch_create(
  3117. self.repo.path, b"feat", objectish=c2.id.decode("ascii")
  3118. )
  3119. self.repo.refs[b"HEAD"] = c4.id
  3120. def test_for_each_ref(self):
  3121. refs = porcelain.for_each_ref(self.repo)
  3122. self.assertEqual(
  3123. [(object_type, tag) for _, object_type, tag in refs],
  3124. [
  3125. (b"commit", b"refs/heads/feat"),
  3126. (b"commit", b"refs/heads/master"),
  3127. (b"commit", b"refs/tags/simple-tag"),
  3128. (b"tag", b"refs/tags/v0.1"),
  3129. (b"tag", b"refs/tags/v1.0"),
  3130. (b"tag", b"refs/tags/v1.1"),
  3131. ],
  3132. )
  3133. def test_for_each_ref_pattern(self):
  3134. versions = porcelain.for_each_ref(self.repo, pattern="refs/tags/v*")
  3135. self.assertEqual(
  3136. [(object_type, tag) for _, object_type, tag in versions],
  3137. [
  3138. (b"tag", b"refs/tags/v0.1"),
  3139. (b"tag", b"refs/tags/v1.0"),
  3140. (b"tag", b"refs/tags/v1.1"),
  3141. ],
  3142. )
  3143. versions = porcelain.for_each_ref(self.repo, pattern="refs/tags/v1.?")
  3144. self.assertEqual(
  3145. [(object_type, tag) for _, object_type, tag in versions],
  3146. [
  3147. (b"tag", b"refs/tags/v1.0"),
  3148. (b"tag", b"refs/tags/v1.1"),
  3149. ],
  3150. )