test_porcelain.py 130 KB

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