porcelain.py 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543
  1. # porcelain.py -- Porcelain-like layer on top of Dulwich
  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. """Simple wrapper that provides porcelain-like functions on top of Dulwich.
  22. Currently implemented:
  23. * archive
  24. * add
  25. * bisect{_start,_bad,_good,_skip,_reset,_log,_replay}
  26. * branch{_create,_delete,_list}
  27. * check_ignore
  28. * checkout
  29. * checkout_branch
  30. * clone
  31. * cone mode{_init, _set, _add}
  32. * commit
  33. * commit_tree
  34. * daemon
  35. * describe
  36. * diff_tree
  37. * fetch
  38. * filter_branch
  39. * for_each_ref
  40. * init
  41. * ls_files
  42. * ls_remote
  43. * ls_tree
  44. * merge
  45. * merge_tree
  46. * mv/move
  47. * prune
  48. * pull
  49. * push
  50. * rm
  51. * remote{_add}
  52. * receive_pack
  53. * reset
  54. * revert
  55. * sparse_checkout
  56. * submodule_add
  57. * submodule_init
  58. * submodule_list
  59. * rev_list
  60. * tag{_create,_delete,_list}
  61. * upload_pack
  62. * update_server_info
  63. * write_commit_graph
  64. * status
  65. * symbolic_ref
  66. These functions are meant to behave similarly to the git subcommands.
  67. Differences in behaviour are considered bugs.
  68. Note: one of the consequences of this is that paths tend to be
  69. interpreted relative to the current working directory rather than relative
  70. to the repository root.
  71. Functions should generally accept both unicode strings and bytestrings
  72. """
  73. import datetime
  74. import fnmatch
  75. import os
  76. import posixpath
  77. import stat
  78. import sys
  79. import time
  80. from collections import namedtuple
  81. from contextlib import closing, contextmanager
  82. from dataclasses import dataclass
  83. from io import BytesIO, RawIOBase
  84. from pathlib import Path
  85. from typing import Optional, Union
  86. from . import replace_me
  87. from .archive import tar_stream
  88. from .bisect import BisectState
  89. from .client import get_transport_and_path
  90. from .config import Config, ConfigFile, StackedConfig, read_submodules
  91. from .diff_tree import (
  92. CHANGE_ADD,
  93. CHANGE_COPY,
  94. CHANGE_DELETE,
  95. CHANGE_MODIFY,
  96. CHANGE_RENAME,
  97. RENAME_CHANGE_TYPES,
  98. )
  99. from .errors import SendPackError
  100. from .graph import can_fast_forward
  101. from .ignore import IgnoreFilterManager
  102. from .index import (
  103. IndexEntry,
  104. _fs_to_tree_path,
  105. blob_from_path_and_stat,
  106. build_file_from_blob,
  107. build_index_from_tree,
  108. get_unstaged_changes,
  109. index_entry_from_stat,
  110. symlink,
  111. update_working_tree,
  112. validate_path_element_default,
  113. validate_path_element_hfs,
  114. validate_path_element_ntfs,
  115. )
  116. from .object_store import tree_lookup_path
  117. from .objects import (
  118. Blob,
  119. Commit,
  120. Tag,
  121. Tree,
  122. format_timezone,
  123. parse_timezone,
  124. pretty_format_tree_entry,
  125. )
  126. from .objectspec import (
  127. parse_commit,
  128. parse_object,
  129. parse_ref,
  130. parse_reftuples,
  131. parse_tree,
  132. )
  133. from .pack import write_pack_from_container, write_pack_index
  134. from .patch import (
  135. get_summary,
  136. write_commit_patch,
  137. write_object_diff,
  138. write_tree_diff,
  139. )
  140. from .protocol import ZERO_SHA, Protocol
  141. from .refs import (
  142. LOCAL_BRANCH_PREFIX,
  143. LOCAL_NOTES_PREFIX,
  144. LOCAL_TAG_PREFIX,
  145. Ref,
  146. SymrefLoop,
  147. _import_remote_refs,
  148. )
  149. from .repo import BaseRepo, Repo, get_user_identity
  150. from .server import (
  151. FileSystemBackend,
  152. ReceivePackHandler,
  153. TCPGitServer,
  154. UploadPackHandler,
  155. )
  156. from .server import update_server_info as server_update_server_info
  157. from .sparse_patterns import (
  158. SparseCheckoutConflictError,
  159. apply_included_paths,
  160. determine_included_paths,
  161. )
  162. # Module level tuple definition for status output
  163. GitStatus = namedtuple("GitStatus", "staged unstaged untracked")
  164. @dataclass
  165. class CountObjectsResult:
  166. """Result of counting objects in a repository.
  167. Attributes:
  168. count: Number of loose objects
  169. size: Total size of loose objects in bytes
  170. in_pack: Number of objects in pack files
  171. packs: Number of pack files
  172. size_pack: Total size of pack files in bytes
  173. """
  174. count: int
  175. size: int
  176. in_pack: Optional[int] = None
  177. packs: Optional[int] = None
  178. size_pack: Optional[int] = None
  179. class NoneStream(RawIOBase):
  180. """Fallback if stdout or stderr are unavailable, does nothing."""
  181. def read(self, size=-1) -> None:
  182. return None
  183. def readall(self) -> bytes:
  184. return b""
  185. def readinto(self, b) -> None:
  186. return None
  187. def write(self, b) -> None:
  188. return None
  189. default_bytes_out_stream = getattr(sys.stdout, "buffer", None) or NoneStream()
  190. default_bytes_err_stream = getattr(sys.stderr, "buffer", None) or NoneStream()
  191. DEFAULT_ENCODING = "utf-8"
  192. class Error(Exception):
  193. """Porcelain-based error."""
  194. def __init__(self, msg) -> None:
  195. super().__init__(msg)
  196. class RemoteExists(Error):
  197. """Raised when the remote already exists."""
  198. class TimezoneFormatError(Error):
  199. """Raised when the timezone cannot be determined from a given string."""
  200. class CheckoutError(Error):
  201. """Indicates that a checkout cannot be performed."""
  202. def parse_timezone_format(tz_str):
  203. """Parse given string and attempt to return a timezone offset.
  204. Different formats are considered in the following order:
  205. - Git internal format: <unix timestamp> <timezone offset>
  206. - RFC 2822: e.g. Mon, 20 Nov 1995 19:12:08 -0500
  207. - ISO 8601: e.g. 1995-11-20T19:12:08-0500
  208. Args:
  209. tz_str: datetime string
  210. Returns: Timezone offset as integer
  211. Raises:
  212. TimezoneFormatError: if timezone information cannot be extracted
  213. """
  214. import re
  215. # Git internal format
  216. internal_format_pattern = re.compile("^[0-9]+ [+-][0-9]{,4}$")
  217. if re.match(internal_format_pattern, tz_str):
  218. try:
  219. tz_internal = parse_timezone(tz_str.split(" ")[1].encode(DEFAULT_ENCODING))
  220. return tz_internal[0]
  221. except ValueError:
  222. pass
  223. # RFC 2822
  224. import email.utils
  225. rfc_2822 = email.utils.parsedate_tz(tz_str)
  226. if rfc_2822:
  227. return rfc_2822[9]
  228. # ISO 8601
  229. # Supported offsets:
  230. # sHHMM, sHH:MM, sHH
  231. iso_8601_pattern = re.compile(
  232. "[0-9] ?([+-])([0-9]{2})(?::(?=[0-9]{2}))?([0-9]{2})?$"
  233. )
  234. match = re.search(iso_8601_pattern, tz_str)
  235. total_secs = 0
  236. if match:
  237. sign, hours, minutes = match.groups()
  238. total_secs += int(hours) * 3600
  239. if minutes:
  240. total_secs += int(minutes) * 60
  241. total_secs = -total_secs if sign == "-" else total_secs
  242. return total_secs
  243. # YYYY.MM.DD, MM/DD/YYYY, DD.MM.YYYY contain no timezone information
  244. raise TimezoneFormatError(tz_str)
  245. def get_user_timezones():
  246. """Retrieve local timezone as described in
  247. https://raw.githubusercontent.com/git/git/v2.3.0/Documentation/date-formats.txt
  248. Returns: A tuple containing author timezone, committer timezone.
  249. """
  250. local_timezone = time.localtime().tm_gmtoff
  251. if os.environ.get("GIT_AUTHOR_DATE"):
  252. author_timezone = parse_timezone_format(os.environ["GIT_AUTHOR_DATE"])
  253. else:
  254. author_timezone = local_timezone
  255. if os.environ.get("GIT_COMMITTER_DATE"):
  256. commit_timezone = parse_timezone_format(os.environ["GIT_COMMITTER_DATE"])
  257. else:
  258. commit_timezone = local_timezone
  259. return author_timezone, commit_timezone
  260. def open_repo(path_or_repo: Union[str, os.PathLike, BaseRepo]):
  261. """Open an argument that can be a repository or a path for a repository."""
  262. if isinstance(path_or_repo, BaseRepo):
  263. return path_or_repo
  264. return Repo(path_or_repo)
  265. @contextmanager
  266. def _noop_context_manager(obj):
  267. """Context manager that has the same api as closing but does nothing."""
  268. yield obj
  269. def open_repo_closing(path_or_repo: Union[str, os.PathLike, BaseRepo]):
  270. """Open an argument that can be a repository or a path for a repository.
  271. returns a context manager that will close the repo on exit if the argument
  272. is a path, else does nothing if the argument is a repo.
  273. """
  274. if isinstance(path_or_repo, BaseRepo):
  275. return _noop_context_manager(path_or_repo)
  276. return closing(Repo(path_or_repo))
  277. def path_to_tree_path(repopath, path, tree_encoding=DEFAULT_ENCODING):
  278. """Convert a path to a path usable in an index, e.g. bytes and relative to
  279. the repository root.
  280. Args:
  281. repopath: Repository path, absolute or relative to the cwd
  282. path: A path, absolute or relative to the cwd
  283. Returns: A path formatted for use in e.g. an index
  284. """
  285. # Resolve might returns a relative path on Windows
  286. # https://bugs.python.org/issue38671
  287. if sys.platform == "win32":
  288. path = os.path.abspath(path)
  289. path = Path(path)
  290. resolved_path = path.resolve()
  291. # Resolve and abspath seems to behave differently regarding symlinks,
  292. # as we are doing abspath on the file path, we need to do the same on
  293. # the repo path or they might not match
  294. if sys.platform == "win32":
  295. repopath = os.path.abspath(repopath)
  296. repopath = Path(repopath).resolve()
  297. try:
  298. relpath = resolved_path.relative_to(repopath)
  299. except ValueError:
  300. # If path is a symlink that points to a file outside the repo, we
  301. # want the relpath for the link itself, not the resolved target
  302. if path.is_symlink():
  303. parent = path.parent.resolve()
  304. relpath = (parent / path.name).relative_to(repopath)
  305. else:
  306. raise
  307. if sys.platform == "win32":
  308. return str(relpath).replace(os.path.sep, "/").encode(tree_encoding)
  309. else:
  310. return bytes(relpath)
  311. class DivergedBranches(Error):
  312. """Branches have diverged and fast-forward is not possible."""
  313. def __init__(self, current_sha, new_sha) -> None:
  314. self.current_sha = current_sha
  315. self.new_sha = new_sha
  316. def check_diverged(repo, current_sha, new_sha) -> None:
  317. """Check if updating to a sha can be done with fast forwarding.
  318. Args:
  319. repo: Repository object
  320. current_sha: Current head sha
  321. new_sha: New head sha
  322. """
  323. try:
  324. can = can_fast_forward(repo, current_sha, new_sha)
  325. except KeyError:
  326. can = False
  327. if not can:
  328. raise DivergedBranches(current_sha, new_sha)
  329. def archive(
  330. repo,
  331. committish: Optional[Union[str, bytes, Commit, Tag]] = None,
  332. outstream=default_bytes_out_stream,
  333. errstream=default_bytes_err_stream,
  334. ) -> None:
  335. """Create an archive.
  336. Args:
  337. repo: Path of repository for which to generate an archive.
  338. committish: Commit SHA1 or ref to use
  339. outstream: Output stream (defaults to stdout)
  340. errstream: Error stream (defaults to stderr)
  341. """
  342. if committish is None:
  343. committish = "HEAD"
  344. with open_repo_closing(repo) as repo_obj:
  345. c = parse_commit(repo_obj, committish)
  346. for chunk in tar_stream(
  347. repo_obj.object_store, repo_obj.object_store[c.tree], c.commit_time
  348. ):
  349. outstream.write(chunk)
  350. def update_server_info(repo=".") -> None:
  351. """Update server info files for a repository.
  352. Args:
  353. repo: path to the repository
  354. """
  355. with open_repo_closing(repo) as r:
  356. server_update_server_info(r)
  357. def write_commit_graph(repo=".", reachable=True) -> None:
  358. """Write a commit graph file for a repository.
  359. Args:
  360. repo: path to the repository or a Repo object
  361. reachable: if True, include all commits reachable from refs.
  362. if False, only include direct ref targets.
  363. """
  364. with open_repo_closing(repo) as r:
  365. # Get all refs
  366. refs = list(r.refs.as_dict().values())
  367. if refs:
  368. r.object_store.write_commit_graph(refs, reachable=reachable)
  369. def symbolic_ref(repo, ref_name, force=False) -> None:
  370. """Set git symbolic ref into HEAD.
  371. Args:
  372. repo: path to the repository
  373. ref_name: short name of the new ref
  374. force: force settings without checking if it exists in refs/heads
  375. """
  376. with open_repo_closing(repo) as repo_obj:
  377. ref_path = _make_branch_ref(ref_name)
  378. if not force and ref_path not in repo_obj.refs.keys():
  379. raise Error(f"fatal: ref `{ref_name}` is not a ref")
  380. repo_obj.refs.set_symbolic_ref(b"HEAD", ref_path)
  381. def pack_refs(repo, all=False) -> None:
  382. with open_repo_closing(repo) as repo_obj:
  383. repo_obj.refs.pack_refs(all=all)
  384. def commit(
  385. repo=".",
  386. message=None,
  387. author=None,
  388. author_timezone=None,
  389. committer=None,
  390. commit_timezone=None,
  391. encoding=None,
  392. no_verify=False,
  393. signoff=False,
  394. ):
  395. """Create a new commit.
  396. Args:
  397. repo: Path to repository
  398. message: Optional commit message
  399. author: Optional author name and email
  400. author_timezone: Author timestamp timezone
  401. committer: Optional committer name and email
  402. commit_timezone: Commit timestamp timezone
  403. no_verify: Skip pre-commit and commit-msg hooks
  404. signoff: GPG Sign the commit (bool, defaults to False,
  405. pass True to use default GPG key,
  406. pass a str containing Key ID to use a specific GPG key)
  407. Returns: SHA1 of the new commit
  408. """
  409. # FIXME: Support --all argument
  410. if getattr(message, "encode", None):
  411. message = message.encode(encoding or DEFAULT_ENCODING)
  412. if getattr(author, "encode", None):
  413. author = author.encode(encoding or DEFAULT_ENCODING)
  414. if getattr(committer, "encode", None):
  415. committer = committer.encode(encoding or DEFAULT_ENCODING)
  416. local_timezone = get_user_timezones()
  417. if author_timezone is None:
  418. author_timezone = local_timezone[0]
  419. if commit_timezone is None:
  420. commit_timezone = local_timezone[1]
  421. with open_repo_closing(repo) as r:
  422. return r.do_commit(
  423. message=message,
  424. author=author,
  425. author_timezone=author_timezone,
  426. committer=committer,
  427. commit_timezone=commit_timezone,
  428. encoding=encoding,
  429. no_verify=no_verify,
  430. sign=signoff if isinstance(signoff, (str, bool)) else None,
  431. )
  432. def commit_tree(repo, tree, message=None, author=None, committer=None):
  433. """Create a new commit object.
  434. Args:
  435. repo: Path to repository
  436. tree: An existing tree object
  437. author: Optional author name and email
  438. committer: Optional committer name and email
  439. """
  440. with open_repo_closing(repo) as r:
  441. return r.do_commit(
  442. message=message, tree=tree, committer=committer, author=author
  443. )
  444. def init(
  445. path: Union[str, os.PathLike] = ".", *, bare=False, symlinks: Optional[bool] = None
  446. ):
  447. """Create a new git repository.
  448. Args:
  449. path: Path to repository.
  450. bare: Whether to create a bare repository.
  451. symlinks: Whether to create actual symlinks (defaults to autodetect)
  452. Returns: A Repo instance
  453. """
  454. if not os.path.exists(path):
  455. os.mkdir(path)
  456. if bare:
  457. return Repo.init_bare(path)
  458. else:
  459. return Repo.init(path, symlinks=symlinks)
  460. def clone(
  461. source,
  462. target: Optional[Union[str, os.PathLike]] = None,
  463. bare=False,
  464. checkout=None,
  465. errstream=default_bytes_err_stream,
  466. outstream=None,
  467. origin: Optional[str] = "origin",
  468. depth: Optional[int] = None,
  469. branch: Optional[Union[str, bytes]] = None,
  470. config: Optional[Config] = None,
  471. filter_spec=None,
  472. protocol_version: Optional[int] = None,
  473. recurse_submodules: bool = False,
  474. **kwargs,
  475. ):
  476. """Clone a local or remote git repository.
  477. Args:
  478. source: Path or URL for source repository
  479. target: Path to target repository (optional)
  480. bare: Whether or not to create a bare repository
  481. checkout: Whether or not to check-out HEAD after cloning
  482. errstream: Optional stream to write progress to
  483. outstream: Optional stream to write progress to (deprecated)
  484. origin: Name of remote from the repository used to clone
  485. depth: Depth to fetch at
  486. branch: Optional branch or tag to be used as HEAD in the new repository
  487. instead of the cloned repository's HEAD.
  488. config: Configuration to use
  489. filter_spec: A git-rev-list-style object filter spec, as an ASCII string.
  490. Only used if the server supports the Git protocol-v2 'filter'
  491. feature, and ignored otherwise.
  492. protocol_version: desired Git protocol version. By default the highest
  493. mutually supported protocol version will be used.
  494. recurse_submodules: Whether to initialize and clone submodules
  495. Keyword Args:
  496. refspecs: refspecs to fetch. Can be a bytestring, a string, or a list of
  497. bytestring/string.
  498. Returns: The new repository
  499. """
  500. if outstream is not None:
  501. import warnings
  502. warnings.warn(
  503. "outstream= has been deprecated in favour of errstream=.",
  504. DeprecationWarning,
  505. stacklevel=3,
  506. )
  507. # TODO(jelmer): Capture logging output and stream to errstream
  508. if config is None:
  509. config = StackedConfig.default()
  510. if checkout is None:
  511. checkout = not bare
  512. if checkout and bare:
  513. raise Error("checkout and bare are incompatible")
  514. if target is None:
  515. target = source.split("/")[-1]
  516. if isinstance(branch, str):
  517. branch = branch.encode(DEFAULT_ENCODING)
  518. mkdir = not os.path.exists(target)
  519. (client, path) = get_transport_and_path(source, config=config, **kwargs)
  520. if filter_spec:
  521. filter_spec = filter_spec.encode("ascii")
  522. repo = client.clone(
  523. path,
  524. target,
  525. mkdir=mkdir,
  526. bare=bare,
  527. origin=origin,
  528. checkout=checkout,
  529. branch=branch,
  530. progress=errstream.write,
  531. depth=depth,
  532. filter_spec=filter_spec,
  533. protocol_version=protocol_version,
  534. )
  535. # Initialize and update submodules if requested
  536. if recurse_submodules and not bare:
  537. try:
  538. submodule_init(repo)
  539. submodule_update(repo, init=True)
  540. except FileNotFoundError as e:
  541. # .gitmodules file doesn't exist - no submodules to process
  542. import logging
  543. logging.debug("No .gitmodules file found: %s", e)
  544. except KeyError as e:
  545. # Submodule configuration missing
  546. import logging
  547. logging.warning("Submodule configuration error: %s", e)
  548. if errstream:
  549. errstream.write(
  550. f"Warning: Submodule configuration error: {e}\n".encode()
  551. )
  552. return repo
  553. def add(repo: Union[str, os.PathLike, BaseRepo] = ".", paths=None):
  554. """Add files to the staging area.
  555. Args:
  556. repo: Repository for the files
  557. paths: Paths to add. If None, stages all untracked and modified files from the
  558. current working directory (mimicking 'git add .' behavior).
  559. Returns: Tuple with set of added files and ignored files
  560. If the repository contains ignored directories, the returned set will
  561. contain the path to an ignored directory (with trailing slash). Individual
  562. files within ignored directories will not be returned.
  563. Note: When paths=None, this function adds all untracked and modified files
  564. from the entire repository, mimicking 'git add -A' behavior.
  565. """
  566. ignored = set()
  567. with open_repo_closing(repo) as r:
  568. repo_path = Path(r.path).resolve()
  569. ignore_manager = IgnoreFilterManager.from_repo(r)
  570. # Get unstaged changes once for the entire operation
  571. index = r.open_index()
  572. normalizer = r.get_blob_normalizer()
  573. filter_callback = normalizer.checkin_normalize
  574. all_unstaged_paths = list(get_unstaged_changes(index, r.path, filter_callback))
  575. if not paths:
  576. # When no paths specified, add all untracked and modified files from repo root
  577. paths = [str(repo_path)]
  578. relpaths = []
  579. if not isinstance(paths, list):
  580. paths = [paths]
  581. for p in paths:
  582. path = Path(p)
  583. if not path.is_absolute():
  584. # Make relative paths relative to the repo directory
  585. path = repo_path / path
  586. # Don't resolve symlinks completely - only resolve the parent directory
  587. # to avoid issues when symlinks point outside the repository
  588. if path.is_symlink():
  589. # For symlinks, resolve only the parent directory
  590. parent_resolved = path.parent.resolve()
  591. resolved_path = parent_resolved / path.name
  592. else:
  593. # For regular files/dirs, resolve normally
  594. resolved_path = path.resolve()
  595. try:
  596. relpath = str(resolved_path.relative_to(repo_path)).replace(os.sep, "/")
  597. except ValueError as e:
  598. # Path is not within the repository
  599. raise ValueError(
  600. f"Path {p} is not within repository {repo_path}"
  601. ) from e
  602. # Handle directories by scanning their contents
  603. if resolved_path.is_dir():
  604. # Check if the directory itself is ignored
  605. dir_relpath = posixpath.join(relpath, "") if relpath != "." else ""
  606. if dir_relpath and ignore_manager.is_ignored(dir_relpath):
  607. ignored.add(dir_relpath)
  608. continue
  609. # When adding a directory, add all untracked files within it
  610. current_untracked = list(
  611. get_untracked_paths(
  612. str(resolved_path),
  613. str(repo_path),
  614. index,
  615. )
  616. )
  617. for untracked_path in current_untracked:
  618. # If we're scanning a subdirectory, adjust the path
  619. if relpath != ".":
  620. untracked_path = posixpath.join(relpath, untracked_path)
  621. if not ignore_manager.is_ignored(untracked_path):
  622. relpaths.append(untracked_path)
  623. else:
  624. ignored.add(untracked_path)
  625. # Also add unstaged (modified) files within this directory
  626. for unstaged_path in all_unstaged_paths:
  627. if isinstance(unstaged_path, bytes):
  628. unstaged_path_str = unstaged_path.decode("utf-8")
  629. else:
  630. unstaged_path_str = unstaged_path
  631. # Check if this unstaged file is within the directory we're processing
  632. unstaged_full_path = repo_path / unstaged_path_str
  633. try:
  634. unstaged_full_path.relative_to(resolved_path)
  635. # File is within this directory, add it
  636. if not ignore_manager.is_ignored(unstaged_path_str):
  637. relpaths.append(unstaged_path_str)
  638. else:
  639. ignored.add(unstaged_path_str)
  640. except ValueError:
  641. # File is not within this directory, skip it
  642. continue
  643. continue
  644. # FIXME: Support patterns
  645. if ignore_manager.is_ignored(relpath):
  646. ignored.add(relpath)
  647. continue
  648. relpaths.append(relpath)
  649. r.stage(relpaths)
  650. return (relpaths, ignored)
  651. def _is_subdir(subdir, parentdir):
  652. """Check whether subdir is parentdir or a subdir of parentdir.
  653. If parentdir or subdir is a relative path, it will be disamgibuated
  654. relative to the pwd.
  655. """
  656. parentdir_abs = os.path.realpath(parentdir) + os.path.sep
  657. subdir_abs = os.path.realpath(subdir) + os.path.sep
  658. return subdir_abs.startswith(parentdir_abs)
  659. # TODO: option to remove ignored files also, in line with `git clean -fdx`
  660. def clean(repo=".", target_dir=None) -> None:
  661. """Remove any untracked files from the target directory recursively.
  662. Equivalent to running ``git clean -fd`` in target_dir.
  663. Args:
  664. repo: Repository where the files may be tracked
  665. target_dir: Directory to clean - current directory if None
  666. """
  667. if target_dir is None:
  668. target_dir = os.getcwd()
  669. with open_repo_closing(repo) as r:
  670. if not _is_subdir(target_dir, r.path):
  671. raise Error("target_dir must be in the repo's working dir")
  672. config = r.get_config_stack()
  673. config.get_boolean((b"clean",), b"requireForce", True)
  674. # TODO(jelmer): if require_force is set, then make sure that -f, -i or
  675. # -n is specified.
  676. index = r.open_index()
  677. ignore_manager = IgnoreFilterManager.from_repo(r)
  678. paths_in_wd = _walk_working_dir_paths(target_dir, r.path)
  679. # Reverse file visit order, so that files and subdirectories are
  680. # removed before containing directory
  681. for ap, is_dir in reversed(list(paths_in_wd)):
  682. if is_dir:
  683. # All subdirectories and files have been removed if untracked,
  684. # so dir contains no tracked files iff it is empty.
  685. is_empty = len(os.listdir(ap)) == 0
  686. if is_empty:
  687. os.rmdir(ap)
  688. else:
  689. ip = path_to_tree_path(r.path, ap)
  690. is_tracked = ip in index
  691. rp = os.path.relpath(ap, r.path)
  692. is_ignored = ignore_manager.is_ignored(rp)
  693. if not is_tracked and not is_ignored:
  694. os.remove(ap)
  695. def remove(repo=".", paths=None, cached=False) -> None:
  696. """Remove files from the staging area.
  697. Args:
  698. repo: Repository for the files
  699. paths: Paths to remove. Can be absolute or relative to the repository root.
  700. """
  701. with open_repo_closing(repo) as r:
  702. index = r.open_index()
  703. blob_normalizer = r.get_blob_normalizer()
  704. for p in paths:
  705. # If path is absolute, use it as-is. Otherwise, treat it as relative to repo
  706. if os.path.isabs(p):
  707. full_path = p
  708. else:
  709. # Treat relative paths as relative to the repository root
  710. full_path = os.path.join(r.path, p)
  711. tree_path = path_to_tree_path(r.path, full_path)
  712. # Convert to bytes for file operations
  713. full_path_bytes = os.fsencode(full_path)
  714. try:
  715. index_sha = index[tree_path].sha
  716. except KeyError as exc:
  717. raise Error(f"{p} did not match any files") from exc
  718. if not cached:
  719. try:
  720. st = os.lstat(full_path_bytes)
  721. except OSError:
  722. pass
  723. else:
  724. try:
  725. blob = blob_from_path_and_stat(full_path_bytes, st)
  726. # Apply checkin normalization to compare apples to apples
  727. if blob_normalizer is not None:
  728. blob = blob_normalizer.checkin_normalize(blob, tree_path)
  729. except OSError:
  730. pass
  731. else:
  732. try:
  733. committed_sha = tree_lookup_path(
  734. r.__getitem__, r[r.head()].tree, tree_path
  735. )[1]
  736. except KeyError:
  737. committed_sha = None
  738. if blob.id != index_sha and index_sha != committed_sha:
  739. raise Error(
  740. "file has staged content differing "
  741. f"from both the file and head: {p}"
  742. )
  743. if index_sha != committed_sha:
  744. raise Error(f"file has staged changes: {p}")
  745. os.remove(full_path_bytes)
  746. del index[tree_path]
  747. index.write()
  748. rm = remove
  749. def mv(
  750. repo: Union[str, os.PathLike, BaseRepo],
  751. source: Union[str, bytes, os.PathLike],
  752. destination: Union[str, bytes, os.PathLike],
  753. force: bool = False,
  754. ) -> None:
  755. """Move or rename a file, directory, or symlink.
  756. Args:
  757. repo: Path to the repository
  758. source: Path to move from
  759. destination: Path to move to
  760. force: Force move even if destination exists
  761. Raises:
  762. Error: If source doesn't exist, is not tracked, or destination already exists (without force)
  763. """
  764. with open_repo_closing(repo) as r:
  765. index = r.open_index()
  766. # Handle paths - convert to string if necessary
  767. if isinstance(source, bytes):
  768. source = source.decode(sys.getfilesystemencoding())
  769. elif hasattr(source, "__fspath__"):
  770. source = os.fspath(source)
  771. else:
  772. source = str(source)
  773. if isinstance(destination, bytes):
  774. destination = destination.decode(sys.getfilesystemencoding())
  775. elif hasattr(destination, "__fspath__"):
  776. destination = os.fspath(destination)
  777. else:
  778. destination = str(destination)
  779. # Get full paths
  780. if os.path.isabs(source):
  781. source_full_path = source
  782. else:
  783. # Treat relative paths as relative to the repository root
  784. source_full_path = os.path.join(r.path, source)
  785. if os.path.isabs(destination):
  786. destination_full_path = destination
  787. else:
  788. # Treat relative paths as relative to the repository root
  789. destination_full_path = os.path.join(r.path, destination)
  790. # Check if destination is a directory
  791. if os.path.isdir(destination_full_path):
  792. # Move source into destination directory
  793. basename = os.path.basename(source_full_path)
  794. destination_full_path = os.path.join(destination_full_path, basename)
  795. # Convert to tree paths for index
  796. source_tree_path = path_to_tree_path(r.path, source_full_path)
  797. destination_tree_path = path_to_tree_path(r.path, destination_full_path)
  798. # Check if source exists in index
  799. if source_tree_path not in index:
  800. raise Error(f"source '{source}' is not under version control")
  801. # Check if source exists in filesystem
  802. if not os.path.exists(source_full_path):
  803. raise Error(f"source '{source}' does not exist")
  804. # Check if destination already exists
  805. if os.path.exists(destination_full_path) and not force:
  806. raise Error(f"destination '{destination}' already exists (use -f to force)")
  807. # Check if destination is already in index
  808. if destination_tree_path in index and not force:
  809. raise Error(
  810. f"destination '{destination}' already exists in index (use -f to force)"
  811. )
  812. # Get the index entry for the source
  813. source_entry = index[source_tree_path]
  814. # Convert to bytes for file operations
  815. source_full_path_bytes = os.fsencode(source_full_path)
  816. destination_full_path_bytes = os.fsencode(destination_full_path)
  817. # Create parent directory for destination if needed
  818. dest_dir = os.path.dirname(destination_full_path_bytes)
  819. if dest_dir and not os.path.exists(dest_dir):
  820. os.makedirs(dest_dir)
  821. # Move the file in the filesystem
  822. if os.path.exists(destination_full_path_bytes) and force:
  823. os.remove(destination_full_path_bytes)
  824. os.rename(source_full_path_bytes, destination_full_path_bytes)
  825. # Update the index
  826. del index[source_tree_path]
  827. index[destination_tree_path] = source_entry
  828. index.write()
  829. move = mv
  830. def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
  831. if commit.encoding:
  832. encoding = commit.encoding.decode("ascii")
  833. else:
  834. encoding = default_encoding
  835. return contents.decode(encoding, "replace")
  836. def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
  837. if commit.encoding:
  838. encoding = commit.encoding.decode("ascii")
  839. else:
  840. encoding = default_encoding
  841. return contents.encode(encoding)
  842. def print_commit(commit, decode, outstream=sys.stdout) -> None:
  843. """Write a human-readable commit log entry.
  844. Args:
  845. commit: A `Commit` object
  846. outstream: A stream file to write to
  847. """
  848. outstream.write("-" * 50 + "\n")
  849. outstream.write("commit: " + commit.id.decode("ascii") + "\n")
  850. if len(commit.parents) > 1:
  851. outstream.write(
  852. "merge: "
  853. + "...".join([c.decode("ascii") for c in commit.parents[1:]])
  854. + "\n"
  855. )
  856. outstream.write("Author: " + decode(commit.author) + "\n")
  857. if commit.author != commit.committer:
  858. outstream.write("Committer: " + decode(commit.committer) + "\n")
  859. time_tuple = time.gmtime(commit.author_time + commit.author_timezone)
  860. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  861. timezone_str = format_timezone(commit.author_timezone).decode("ascii")
  862. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  863. if commit.message:
  864. outstream.write("\n")
  865. outstream.write(decode(commit.message) + "\n")
  866. outstream.write("\n")
  867. def print_tag(tag, decode, outstream=sys.stdout) -> None:
  868. """Write a human-readable tag.
  869. Args:
  870. tag: A `Tag` object
  871. decode: Function for decoding bytes to unicode string
  872. outstream: A stream to write to
  873. """
  874. outstream.write("Tagger: " + decode(tag.tagger) + "\n")
  875. time_tuple = time.gmtime(tag.tag_time + tag.tag_timezone)
  876. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  877. timezone_str = format_timezone(tag.tag_timezone).decode("ascii")
  878. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  879. outstream.write("\n")
  880. outstream.write(decode(tag.message))
  881. outstream.write("\n")
  882. def show_blob(repo, blob, decode, outstream=sys.stdout) -> None:
  883. """Write a blob to a stream.
  884. Args:
  885. repo: A `Repo` object
  886. blob: A `Blob` object
  887. decode: Function for decoding bytes to unicode string
  888. outstream: A stream file to write to
  889. """
  890. outstream.write(decode(blob.data))
  891. def show_commit(repo, commit, decode, outstream=sys.stdout) -> None:
  892. """Show a commit to a stream.
  893. Args:
  894. repo: A `Repo` object
  895. commit: A `Commit` object
  896. decode: Function for decoding bytes to unicode string
  897. outstream: Stream to write to
  898. """
  899. print_commit(commit, decode=decode, outstream=outstream)
  900. if commit.parents:
  901. parent_commit = repo[commit.parents[0]]
  902. base_tree = parent_commit.tree
  903. else:
  904. base_tree = None
  905. diffstream = BytesIO()
  906. write_tree_diff(diffstream, repo.object_store, base_tree, commit.tree)
  907. diffstream.seek(0)
  908. outstream.write(commit_decode(commit, diffstream.getvalue()))
  909. def show_tree(repo, tree, decode, outstream=sys.stdout) -> None:
  910. """Print a tree to a stream.
  911. Args:
  912. repo: A `Repo` object
  913. tree: A `Tree` object
  914. decode: Function for decoding bytes to unicode string
  915. outstream: Stream to write to
  916. """
  917. for n in tree:
  918. outstream.write(decode(n) + "\n")
  919. def show_tag(repo, tag, decode, outstream=sys.stdout) -> None:
  920. """Print a tag to a stream.
  921. Args:
  922. repo: A `Repo` object
  923. tag: A `Tag` object
  924. decode: Function for decoding bytes to unicode string
  925. outstream: Stream to write to
  926. """
  927. print_tag(tag, decode, outstream)
  928. show_object(repo, repo[tag.object[1]], decode, outstream)
  929. def show_object(repo, obj, decode, outstream):
  930. return {
  931. b"tree": show_tree,
  932. b"blob": show_blob,
  933. b"commit": show_commit,
  934. b"tag": show_tag,
  935. }[obj.type_name](repo, obj, decode, outstream)
  936. def print_name_status(changes):
  937. """Print a simple status summary, listing changed files."""
  938. for change in changes:
  939. if not change:
  940. continue
  941. if isinstance(change, list):
  942. change = change[0]
  943. if change.type == CHANGE_ADD:
  944. path1 = change.new.path
  945. path2 = ""
  946. kind = "A"
  947. elif change.type == CHANGE_DELETE:
  948. path1 = change.old.path
  949. path2 = ""
  950. kind = "D"
  951. elif change.type == CHANGE_MODIFY:
  952. path1 = change.new.path
  953. path2 = ""
  954. kind = "M"
  955. elif change.type in RENAME_CHANGE_TYPES:
  956. path1 = change.old.path
  957. path2 = change.new.path
  958. if change.type == CHANGE_RENAME:
  959. kind = "R"
  960. elif change.type == CHANGE_COPY:
  961. kind = "C"
  962. yield "%-8s%-20s%-20s" % (kind, path1, path2) # noqa: UP031
  963. def log(
  964. repo=".",
  965. paths=None,
  966. outstream=sys.stdout,
  967. max_entries=None,
  968. reverse=False,
  969. name_status=False,
  970. ) -> None:
  971. """Write commit logs.
  972. Args:
  973. repo: Path to repository
  974. paths: Optional set of specific paths to print entries for
  975. outstream: Stream to write log output to
  976. reverse: Reverse order in which entries are printed
  977. name_status: Print name status
  978. max_entries: Optional maximum number of entries to display
  979. """
  980. with open_repo_closing(repo) as r:
  981. try:
  982. include = [r.head()]
  983. except KeyError:
  984. include = []
  985. walker = r.get_walker(
  986. include=include, max_entries=max_entries, paths=paths, reverse=reverse
  987. )
  988. for entry in walker:
  989. def decode(x):
  990. return commit_decode(entry.commit, x)
  991. print_commit(entry.commit, decode, outstream)
  992. if name_status:
  993. outstream.writelines(
  994. [line + "\n" for line in print_name_status(entry.changes())]
  995. )
  996. # TODO(jelmer): better default for encoding?
  997. def show(
  998. repo=".",
  999. objects=None,
  1000. outstream=sys.stdout,
  1001. default_encoding=DEFAULT_ENCODING,
  1002. ) -> None:
  1003. """Print the changes in a commit.
  1004. Args:
  1005. repo: Path to repository
  1006. objects: Objects to show (defaults to [HEAD])
  1007. outstream: Stream to write to
  1008. default_encoding: Default encoding to use if none is set in the
  1009. commit
  1010. """
  1011. if objects is None:
  1012. objects = ["HEAD"]
  1013. if not isinstance(objects, list):
  1014. objects = [objects]
  1015. with open_repo_closing(repo) as r:
  1016. for objectish in objects:
  1017. o = parse_object(r, objectish)
  1018. if isinstance(o, Commit):
  1019. def decode(x):
  1020. return commit_decode(o, x, default_encoding)
  1021. else:
  1022. def decode(x):
  1023. return x.decode(default_encoding)
  1024. show_object(r, o, decode, outstream)
  1025. def diff_tree(repo, old_tree, new_tree, outstream=default_bytes_out_stream) -> None:
  1026. """Compares the content and mode of blobs found via two tree objects.
  1027. Args:
  1028. repo: Path to repository
  1029. old_tree: Id of old tree
  1030. new_tree: Id of new tree
  1031. outstream: Stream to write to
  1032. """
  1033. with open_repo_closing(repo) as r:
  1034. write_tree_diff(outstream, r.object_store, old_tree, new_tree)
  1035. def diff(
  1036. repo=".",
  1037. commit=None,
  1038. commit2=None,
  1039. staged=False,
  1040. paths=None,
  1041. outstream=default_bytes_out_stream,
  1042. ) -> None:
  1043. """Show diff.
  1044. Args:
  1045. repo: Path to repository
  1046. commit: First commit to compare. If staged is True, compare
  1047. index to this commit. If staged is False, compare working tree
  1048. to this commit. If None, defaults to HEAD for staged and index
  1049. for unstaged.
  1050. commit2: Second commit to compare against first commit. If provided,
  1051. show diff between commit and commit2 (ignoring staged flag).
  1052. staged: If True, show staged changes (index vs commit).
  1053. If False, show unstaged changes (working tree vs commit/index).
  1054. Ignored if commit2 is provided.
  1055. paths: Optional list of paths to limit diff
  1056. outstream: Stream to write to
  1057. """
  1058. from . import diff as diff_module
  1059. with open_repo_closing(repo) as r:
  1060. # Normalize paths to bytes
  1061. if paths is not None and paths: # Check if paths is not empty
  1062. byte_paths = []
  1063. for p in paths:
  1064. if isinstance(p, str):
  1065. byte_paths.append(p.encode("utf-8"))
  1066. else:
  1067. byte_paths.append(p)
  1068. paths = byte_paths
  1069. elif paths == []: # Convert empty list to None
  1070. paths = None
  1071. # Resolve commit refs to SHAs if provided
  1072. if commit is not None:
  1073. if isinstance(commit, Commit):
  1074. # Already a Commit object
  1075. commit_sha = commit.id
  1076. commit_obj = commit
  1077. else:
  1078. # parse_commit handles both refs and SHAs, and always returns a Commit object
  1079. commit_obj = parse_commit(r, commit)
  1080. commit_sha = commit_obj.id
  1081. else:
  1082. commit_sha = None
  1083. commit_obj = None
  1084. if commit2 is not None:
  1085. # Compare two commits
  1086. if isinstance(commit2, Commit):
  1087. commit2_obj = commit2
  1088. else:
  1089. commit2_obj = parse_commit(r, commit2)
  1090. # Get trees from commits
  1091. old_tree = commit_obj.tree if commit_obj else None
  1092. new_tree = commit2_obj.tree
  1093. # Use tree_changes to get the changes and apply path filtering
  1094. changes = r.object_store.tree_changes(old_tree, new_tree)
  1095. for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
  1096. # Skip if paths are specified and this change doesn't match
  1097. if paths:
  1098. path_to_check = newpath or oldpath
  1099. if not any(
  1100. path_to_check == p or path_to_check.startswith(p + b"/")
  1101. for p in paths
  1102. ):
  1103. continue
  1104. write_object_diff(
  1105. outstream,
  1106. r.object_store,
  1107. (oldpath, oldmode, oldsha),
  1108. (newpath, newmode, newsha),
  1109. )
  1110. elif staged:
  1111. # Show staged changes (index vs commit)
  1112. diff_module.diff_index_to_tree(r, outstream, commit_sha, paths)
  1113. elif commit is not None:
  1114. # Compare working tree to a specific commit
  1115. assert (
  1116. commit_sha is not None
  1117. ) # mypy: commit_sha is set when commit is not None
  1118. diff_module.diff_working_tree_to_tree(r, outstream, commit_sha, paths)
  1119. else:
  1120. # Compare working tree to index
  1121. diff_module.diff_working_tree_to_index(r, outstream, paths)
  1122. def rev_list(repo, commits, outstream=sys.stdout) -> None:
  1123. """Lists commit objects in reverse chronological order.
  1124. Args:
  1125. repo: Path to repository
  1126. commits: Commits over which to iterate
  1127. outstream: Stream to write to
  1128. """
  1129. with open_repo_closing(repo) as r:
  1130. for entry in r.get_walker(include=[r[c].id for c in commits]):
  1131. outstream.write(entry.commit.id + b"\n")
  1132. def _canonical_part(url: str) -> str:
  1133. name = url.rsplit("/", 1)[-1]
  1134. if name.endswith(".git"):
  1135. name = name[:-4]
  1136. return name
  1137. def submodule_add(repo, url, path=None, name=None) -> None:
  1138. """Add a new submodule.
  1139. Args:
  1140. repo: Path to repository
  1141. url: URL of repository to add as submodule
  1142. path: Path where submodule should live
  1143. name: Name for the submodule
  1144. """
  1145. with open_repo_closing(repo) as r:
  1146. if path is None:
  1147. path = os.path.relpath(_canonical_part(url), r.path)
  1148. if name is None:
  1149. name = path
  1150. # TODO(jelmer): Move this logic to dulwich.submodule
  1151. gitmodules_path = os.path.join(r.path, ".gitmodules")
  1152. try:
  1153. config = ConfigFile.from_path(gitmodules_path)
  1154. except FileNotFoundError:
  1155. config = ConfigFile()
  1156. config.path = gitmodules_path
  1157. config.set(("submodule", name), "url", url)
  1158. config.set(("submodule", name), "path", path)
  1159. config.write_to_path()
  1160. def submodule_init(repo) -> None:
  1161. """Initialize submodules.
  1162. Args:
  1163. repo: Path to repository
  1164. """
  1165. with open_repo_closing(repo) as r:
  1166. config = r.get_config()
  1167. gitmodules_path = os.path.join(r.path, ".gitmodules")
  1168. for path, url, name in read_submodules(gitmodules_path):
  1169. config.set((b"submodule", name), b"active", True)
  1170. config.set((b"submodule", name), b"url", url)
  1171. config.write_to_path()
  1172. def submodule_list(repo):
  1173. """List submodules.
  1174. Args:
  1175. repo: Path to repository
  1176. """
  1177. from .submodule import iter_cached_submodules
  1178. with open_repo_closing(repo) as r:
  1179. for path, sha in iter_cached_submodules(r.object_store, r[r.head()].tree):
  1180. yield path, sha.decode(DEFAULT_ENCODING)
  1181. def submodule_update(repo, paths=None, init=False, force=False, errstream=None) -> None:
  1182. """Update submodules.
  1183. Args:
  1184. repo: Path to repository
  1185. paths: Optional list of specific submodule paths to update. If None, updates all.
  1186. init: If True, initialize submodules first
  1187. force: Force update even if local changes exist
  1188. """
  1189. from .submodule import iter_cached_submodules
  1190. with open_repo_closing(repo) as r:
  1191. if init:
  1192. submodule_init(r)
  1193. config = r.get_config()
  1194. gitmodules_path = os.path.join(r.path, ".gitmodules")
  1195. # Get list of submodules to update
  1196. submodules_to_update = []
  1197. for path, sha in iter_cached_submodules(r.object_store, r[r.head()].tree):
  1198. path_str = (
  1199. path.decode(DEFAULT_ENCODING) if isinstance(path, bytes) else path
  1200. )
  1201. if paths is None or path_str in paths:
  1202. submodules_to_update.append((path, sha))
  1203. # Read submodule configuration
  1204. for path, target_sha in submodules_to_update:
  1205. path_str = (
  1206. path.decode(DEFAULT_ENCODING) if isinstance(path, bytes) else path
  1207. )
  1208. # Find the submodule name from .gitmodules
  1209. submodule_name = None
  1210. for sm_path, sm_url, sm_name in read_submodules(gitmodules_path):
  1211. if sm_path == path:
  1212. submodule_name = sm_name
  1213. break
  1214. if not submodule_name:
  1215. continue
  1216. # Get the URL from config
  1217. section = (
  1218. b"submodule",
  1219. submodule_name
  1220. if isinstance(submodule_name, bytes)
  1221. else submodule_name.encode(),
  1222. )
  1223. try:
  1224. url = config.get(section, b"url")
  1225. if isinstance(url, bytes):
  1226. url = url.decode(DEFAULT_ENCODING)
  1227. except KeyError:
  1228. # URL not in config, skip this submodule
  1229. continue
  1230. # Get or create the submodule repository paths
  1231. submodule_path = os.path.join(r.path, path_str)
  1232. submodule_git_dir = os.path.join(r.path, ".git", "modules", path_str)
  1233. # Clone or fetch the submodule
  1234. if not os.path.exists(submodule_git_dir):
  1235. # Clone the submodule as bare repository
  1236. os.makedirs(os.path.dirname(submodule_git_dir), exist_ok=True)
  1237. # Clone to the git directory
  1238. sub_repo = clone(url, submodule_git_dir, bare=True, checkout=False)
  1239. sub_repo.close()
  1240. # Create the submodule directory if it doesn't exist
  1241. if not os.path.exists(submodule_path):
  1242. os.makedirs(submodule_path)
  1243. # Create .git file in the submodule directory
  1244. depth = path_str.count("/") + 1
  1245. relative_git_dir = "../" * depth + ".git/modules/" + path_str
  1246. git_file_path = os.path.join(submodule_path, ".git")
  1247. with open(git_file_path, "w") as f:
  1248. f.write(f"gitdir: {relative_git_dir}\n")
  1249. # Set up working directory configuration
  1250. with open_repo_closing(submodule_git_dir) as sub_repo:
  1251. sub_config = sub_repo.get_config()
  1252. sub_config.set(
  1253. (b"core",),
  1254. b"worktree",
  1255. os.path.abspath(submodule_path).encode(),
  1256. )
  1257. sub_config.write_to_path()
  1258. # Checkout the target commit
  1259. sub_repo.refs[b"HEAD"] = target_sha
  1260. # Build the index and checkout files
  1261. tree = sub_repo[target_sha]
  1262. if hasattr(tree, "tree"): # If it's a commit, get the tree
  1263. tree_id = tree.tree
  1264. else:
  1265. tree_id = target_sha
  1266. build_index_from_tree(
  1267. submodule_path,
  1268. sub_repo.index_path(),
  1269. sub_repo.object_store,
  1270. tree_id,
  1271. )
  1272. else:
  1273. # Fetch and checkout in existing submodule
  1274. with open_repo_closing(submodule_git_dir) as sub_repo:
  1275. # Fetch from remote
  1276. client, path_segments = get_transport_and_path(url)
  1277. client.fetch(path_segments, sub_repo)
  1278. # Update to the target commit
  1279. sub_repo.refs[b"HEAD"] = target_sha
  1280. # Reset the working directory
  1281. reset(sub_repo, "hard", target_sha)
  1282. def tag_create(
  1283. repo,
  1284. tag: Union[str, bytes],
  1285. author: Optional[Union[str, bytes]] = None,
  1286. message: Optional[Union[str, bytes]] = None,
  1287. annotated=False,
  1288. objectish: Union[str, bytes] = "HEAD",
  1289. tag_time=None,
  1290. tag_timezone=None,
  1291. sign: bool = False,
  1292. encoding: str = DEFAULT_ENCODING,
  1293. ) -> None:
  1294. """Creates a tag in git via dulwich calls.
  1295. Args:
  1296. repo: Path to repository
  1297. tag: tag string
  1298. author: tag author (optional, if annotated is set)
  1299. message: tag message (optional)
  1300. annotated: whether to create an annotated tag
  1301. objectish: object the tag should point at, defaults to HEAD
  1302. tag_time: Optional time for annotated tag
  1303. tag_timezone: Optional timezone for annotated tag
  1304. sign: GPG Sign the tag (bool, defaults to False,
  1305. pass True to use default GPG key,
  1306. pass a str containing Key ID to use a specific GPG key)
  1307. """
  1308. with open_repo_closing(repo) as r:
  1309. object = parse_object(r, objectish)
  1310. if isinstance(tag, str):
  1311. tag = tag.encode(encoding)
  1312. if annotated:
  1313. # Create the tag object
  1314. tag_obj = Tag()
  1315. if author is None:
  1316. author = get_user_identity(r.get_config_stack())
  1317. elif isinstance(author, str):
  1318. author = author.encode(encoding)
  1319. else:
  1320. assert isinstance(author, bytes)
  1321. tag_obj.tagger = author
  1322. if isinstance(message, str):
  1323. message = message.encode(encoding)
  1324. elif isinstance(message, bytes):
  1325. pass
  1326. else:
  1327. message = b""
  1328. tag_obj.message = message + "\n".encode(encoding)
  1329. tag_obj.name = tag
  1330. tag_obj.object = (type(object), object.id)
  1331. if tag_time is None:
  1332. tag_time = int(time.time())
  1333. tag_obj.tag_time = tag_time
  1334. if tag_timezone is None:
  1335. tag_timezone = get_user_timezones()[1]
  1336. elif isinstance(tag_timezone, str):
  1337. tag_timezone = parse_timezone(tag_timezone.encode())
  1338. tag_obj.tag_timezone = tag_timezone
  1339. # Check if we should sign the tag
  1340. should_sign = sign
  1341. if sign is None:
  1342. # Check tag.gpgSign configuration when sign is not explicitly set
  1343. config = r.get_config_stack()
  1344. try:
  1345. should_sign = config.get_boolean((b"tag",), b"gpgSign")
  1346. except KeyError:
  1347. should_sign = False # Default to not signing if no config
  1348. if should_sign:
  1349. keyid = sign if isinstance(sign, str) else None
  1350. # If sign is True but no keyid specified, check user.signingKey config
  1351. if should_sign is True and keyid is None:
  1352. config = r.get_config_stack()
  1353. try:
  1354. keyid = config.get((b"user",), b"signingKey").decode("ascii")
  1355. except KeyError:
  1356. # No user.signingKey configured, will use default GPG key
  1357. pass
  1358. tag_obj.sign(keyid)
  1359. r.object_store.add_object(tag_obj)
  1360. tag_id = tag_obj.id
  1361. else:
  1362. tag_id = object.id
  1363. r.refs[_make_tag_ref(tag)] = tag_id
  1364. def tag_list(repo, outstream=sys.stdout):
  1365. """List all tags.
  1366. Args:
  1367. repo: Path to repository
  1368. outstream: Stream to write tags to
  1369. """
  1370. with open_repo_closing(repo) as r:
  1371. tags = sorted(r.refs.as_dict(b"refs/tags"))
  1372. return tags
  1373. def tag_delete(repo, name) -> None:
  1374. """Remove a tag.
  1375. Args:
  1376. repo: Path to repository
  1377. name: Name of tag to remove
  1378. """
  1379. with open_repo_closing(repo) as r:
  1380. if isinstance(name, bytes):
  1381. names = [name]
  1382. elif isinstance(name, list):
  1383. names = name
  1384. else:
  1385. raise Error(f"Unexpected tag name type {name!r}")
  1386. for name in names:
  1387. del r.refs[_make_tag_ref(name)]
  1388. def _make_notes_ref(name: bytes) -> bytes:
  1389. """Make a notes ref name."""
  1390. if name.startswith(b"refs/notes/"):
  1391. return name
  1392. return LOCAL_NOTES_PREFIX + name
  1393. def notes_add(
  1394. repo, object_sha, note, ref=b"commits", author=None, committer=None, message=None
  1395. ):
  1396. """Add or update a note for an object.
  1397. Args:
  1398. repo: Path to repository
  1399. object_sha: SHA of the object to annotate
  1400. note: Note content
  1401. ref: Notes ref to use (defaults to "commits" for refs/notes/commits)
  1402. author: Author identity (defaults to committer)
  1403. committer: Committer identity (defaults to config)
  1404. message: Commit message for the notes update
  1405. Returns:
  1406. SHA of the new notes commit
  1407. """
  1408. with open_repo_closing(repo) as r:
  1409. # Parse the object to get its SHA
  1410. obj = parse_object(r, object_sha)
  1411. object_sha = obj.id
  1412. if isinstance(note, str):
  1413. note = note.encode(DEFAULT_ENCODING)
  1414. if isinstance(ref, str):
  1415. ref = ref.encode(DEFAULT_ENCODING)
  1416. notes_ref = _make_notes_ref(ref)
  1417. config = r.get_config_stack()
  1418. return r.notes.set_note(
  1419. object_sha,
  1420. note,
  1421. notes_ref,
  1422. author=author,
  1423. committer=committer,
  1424. message=message,
  1425. config=config,
  1426. )
  1427. def notes_remove(
  1428. repo, object_sha, ref=b"commits", author=None, committer=None, message=None
  1429. ):
  1430. """Remove a note for an object.
  1431. Args:
  1432. repo: Path to repository
  1433. object_sha: SHA of the object to remove notes from
  1434. ref: Notes ref to use (defaults to "commits" for refs/notes/commits)
  1435. author: Author identity (defaults to committer)
  1436. committer: Committer identity (defaults to config)
  1437. message: Commit message for the notes removal
  1438. Returns:
  1439. SHA of the new notes commit, or None if no note existed
  1440. """
  1441. with open_repo_closing(repo) as r:
  1442. # Parse the object to get its SHA
  1443. obj = parse_object(r, object_sha)
  1444. object_sha = obj.id
  1445. if isinstance(ref, str):
  1446. ref = ref.encode(DEFAULT_ENCODING)
  1447. notes_ref = _make_notes_ref(ref)
  1448. config = r.get_config_stack()
  1449. return r.notes.remove_note(
  1450. object_sha,
  1451. notes_ref,
  1452. author=author,
  1453. committer=committer,
  1454. message=message,
  1455. config=config,
  1456. )
  1457. def notes_show(repo, object_sha, ref=b"commits"):
  1458. """Show the note for an object.
  1459. Args:
  1460. repo: Path to repository
  1461. object_sha: SHA of the object
  1462. ref: Notes ref to use (defaults to "commits" for refs/notes/commits)
  1463. Returns:
  1464. Note content as bytes, or None if no note exists
  1465. """
  1466. with open_repo_closing(repo) as r:
  1467. # Parse the object to get its SHA
  1468. obj = parse_object(r, object_sha)
  1469. object_sha = obj.id
  1470. if isinstance(ref, str):
  1471. ref = ref.encode(DEFAULT_ENCODING)
  1472. notes_ref = _make_notes_ref(ref)
  1473. config = r.get_config_stack()
  1474. return r.notes.get_note(object_sha, notes_ref, config=config)
  1475. def notes_list(repo, ref=b"commits"):
  1476. """List all notes in a notes ref.
  1477. Args:
  1478. repo: Path to repository
  1479. ref: Notes ref to use (defaults to "commits" for refs/notes/commits)
  1480. Returns:
  1481. List of tuples of (object_sha, note_content)
  1482. """
  1483. with open_repo_closing(repo) as r:
  1484. if isinstance(ref, str):
  1485. ref = ref.encode(DEFAULT_ENCODING)
  1486. notes_ref = _make_notes_ref(ref)
  1487. config = r.get_config_stack()
  1488. return r.notes.list_notes(notes_ref, config=config)
  1489. def reset(repo, mode, treeish: Union[str, bytes, Commit, Tree, Tag] = "HEAD") -> None:
  1490. """Reset current HEAD to the specified state.
  1491. Args:
  1492. repo: Path to repository
  1493. mode: Mode ("hard", "soft", "mixed")
  1494. treeish: Treeish to reset to
  1495. """
  1496. with open_repo_closing(repo) as r:
  1497. # Parse the target tree
  1498. tree = parse_tree(r, treeish)
  1499. # Only parse as commit if treeish is not a Tree object
  1500. if isinstance(treeish, Tree):
  1501. # For Tree objects, we can't determine the commit, skip updating HEAD
  1502. target_commit = None
  1503. else:
  1504. target_commit = parse_commit(r, treeish)
  1505. # Update HEAD to point to the target commit
  1506. if target_commit is not None:
  1507. r.refs[b"HEAD"] = target_commit.id
  1508. if mode == "soft":
  1509. # Soft reset: only update HEAD, leave index and working tree unchanged
  1510. return
  1511. elif mode == "mixed":
  1512. # Mixed reset: update HEAD and index, but leave working tree unchanged
  1513. from .object_store import iter_tree_contents
  1514. # Open the index
  1515. index = r.open_index()
  1516. # Clear the current index
  1517. index.clear()
  1518. # Populate index from the target tree
  1519. for entry in iter_tree_contents(r.object_store, tree.id):
  1520. # Create an IndexEntry from the tree entry
  1521. # Use zeros for filesystem-specific fields since we're not touching the working tree
  1522. index_entry = IndexEntry(
  1523. ctime=(0, 0),
  1524. mtime=(0, 0),
  1525. dev=0,
  1526. ino=0,
  1527. mode=entry.mode,
  1528. uid=0,
  1529. gid=0,
  1530. size=0, # Size will be 0 since we're not reading from disk
  1531. sha=entry.sha,
  1532. flags=0,
  1533. )
  1534. index[entry.path] = index_entry
  1535. # Write the updated index
  1536. index.write()
  1537. elif mode == "hard":
  1538. # Hard reset: update HEAD, index, and working tree
  1539. # Get current HEAD tree for comparison
  1540. try:
  1541. current_head = r.refs[b"HEAD"]
  1542. current_tree = r[current_head].tree
  1543. except KeyError:
  1544. current_tree = None
  1545. # Get configuration for working directory update
  1546. config = r.get_config()
  1547. honor_filemode = config.get_boolean(b"core", b"filemode", os.name != "nt")
  1548. if config.get_boolean(b"core", b"core.protectNTFS", os.name == "nt"):
  1549. validate_path_element = validate_path_element_ntfs
  1550. elif config.get_boolean(
  1551. b"core", b"core.protectHFS", sys.platform == "darwin"
  1552. ):
  1553. validate_path_element = validate_path_element_hfs
  1554. else:
  1555. validate_path_element = validate_path_element_default
  1556. if config.get_boolean(b"core", b"symlinks", True):
  1557. symlink_fn = symlink
  1558. else:
  1559. def symlink_fn( # type: ignore
  1560. source, target, target_is_directory=False, *, dir_fd=None
  1561. ) -> None:
  1562. mode = "w" + ("b" if isinstance(source, bytes) else "")
  1563. with open(target, mode) as f:
  1564. f.write(source)
  1565. # Update working tree and index
  1566. blob_normalizer = r.get_blob_normalizer()
  1567. update_working_tree(
  1568. r,
  1569. current_tree,
  1570. tree.id,
  1571. honor_filemode=honor_filemode,
  1572. validate_path_element=validate_path_element,
  1573. symlink_fn=symlink_fn,
  1574. force_remove_untracked=True,
  1575. blob_normalizer=blob_normalizer,
  1576. )
  1577. else:
  1578. raise Error(f"Invalid reset mode: {mode}")
  1579. def get_remote_repo(
  1580. repo: Repo, remote_location: Optional[Union[str, bytes]] = None
  1581. ) -> tuple[Optional[str], str]:
  1582. config = repo.get_config()
  1583. if remote_location is None:
  1584. remote_location = get_branch_remote(repo)
  1585. if isinstance(remote_location, str):
  1586. encoded_location = remote_location.encode()
  1587. else:
  1588. encoded_location = remote_location
  1589. section = (b"remote", encoded_location)
  1590. remote_name: Optional[str] = None
  1591. if config.has_section(section):
  1592. remote_name = encoded_location.decode()
  1593. encoded_location = config.get(section, "url")
  1594. else:
  1595. remote_name = None
  1596. return (remote_name, encoded_location.decode())
  1597. def push(
  1598. repo,
  1599. remote_location=None,
  1600. refspecs=None,
  1601. outstream=default_bytes_out_stream,
  1602. errstream=default_bytes_err_stream,
  1603. force=False,
  1604. **kwargs,
  1605. ):
  1606. """Remote push with dulwich via dulwich.client.
  1607. Args:
  1608. repo: Path to repository
  1609. remote_location: Location of the remote
  1610. refspecs: Refs to push to remote
  1611. outstream: A stream file to write output
  1612. errstream: A stream file to write errors
  1613. force: Force overwriting refs
  1614. """
  1615. # Open the repo
  1616. with open_repo_closing(repo) as r:
  1617. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1618. # Check if mirror mode is enabled
  1619. mirror_mode = False
  1620. if remote_name:
  1621. try:
  1622. mirror_mode = r.get_config_stack().get_boolean(
  1623. (b"remote", remote_name.encode()), b"mirror"
  1624. )
  1625. except KeyError:
  1626. pass
  1627. if mirror_mode:
  1628. # Mirror mode: push all refs and delete non-existent ones
  1629. refspecs = []
  1630. for ref in r.refs.keys():
  1631. # Push all refs to the same name on remote
  1632. refspecs.append(ref + b":" + ref)
  1633. elif refspecs is None:
  1634. refspecs = [active_branch(r)]
  1635. # Get the client and path
  1636. client, path = get_transport_and_path(
  1637. remote_location, config=r.get_config_stack(), **kwargs
  1638. )
  1639. selected_refs = []
  1640. remote_changed_refs = {}
  1641. def update_refs(refs):
  1642. selected_refs.extend(parse_reftuples(r.refs, refs, refspecs, force=force))
  1643. new_refs = {}
  1644. # In mirror mode, delete remote refs that don't exist locally
  1645. if mirror_mode:
  1646. local_refs = set(r.refs.keys())
  1647. for remote_ref in refs.keys():
  1648. if remote_ref not in local_refs:
  1649. new_refs[remote_ref] = ZERO_SHA
  1650. remote_changed_refs[remote_ref] = None
  1651. # TODO: Handle selected_refs == {None: None}
  1652. for lh, rh, force_ref in selected_refs:
  1653. if lh is None:
  1654. new_refs[rh] = ZERO_SHA
  1655. remote_changed_refs[rh] = None
  1656. else:
  1657. try:
  1658. localsha = r.refs[lh]
  1659. except KeyError as exc:
  1660. raise Error(f"No valid ref {lh} in local repository") from exc
  1661. if not force_ref and rh in refs:
  1662. check_diverged(r, refs[rh], localsha)
  1663. new_refs[rh] = localsha
  1664. remote_changed_refs[rh] = localsha
  1665. return new_refs
  1666. err_encoding = getattr(errstream, "encoding", None) or DEFAULT_ENCODING
  1667. remote_location = client.get_url(path)
  1668. try:
  1669. result = client.send_pack(
  1670. path,
  1671. update_refs,
  1672. generate_pack_data=r.generate_pack_data,
  1673. progress=errstream.write,
  1674. )
  1675. except SendPackError as exc:
  1676. raise Error(
  1677. "Push to " + remote_location + " failed -> " + exc.args[0].decode(),
  1678. ) from exc
  1679. else:
  1680. errstream.write(
  1681. b"Push to " + remote_location.encode(err_encoding) + b" successful.\n"
  1682. )
  1683. for ref, error in (result.ref_status or {}).items():
  1684. if error is not None:
  1685. errstream.write(
  1686. b"Push of ref %s failed: %s\n" % (ref, error.encode(err_encoding))
  1687. )
  1688. else:
  1689. errstream.write(b"Ref %s updated\n" % ref)
  1690. if remote_name is not None:
  1691. _import_remote_refs(r.refs, remote_name, remote_changed_refs)
  1692. return result
  1693. # Trigger auto GC if needed
  1694. from .gc import maybe_auto_gc
  1695. with open_repo_closing(repo) as r:
  1696. maybe_auto_gc(r)
  1697. def pull(
  1698. repo,
  1699. remote_location=None,
  1700. refspecs=None,
  1701. outstream=default_bytes_out_stream,
  1702. errstream=default_bytes_err_stream,
  1703. fast_forward=True,
  1704. ff_only=False,
  1705. force=False,
  1706. filter_spec=None,
  1707. protocol_version=None,
  1708. **kwargs,
  1709. ) -> None:
  1710. """Pull from remote via dulwich.client.
  1711. Args:
  1712. repo: Path to repository
  1713. remote_location: Location of the remote
  1714. refspecs: refspecs to fetch. Can be a bytestring, a string, or a list of
  1715. bytestring/string.
  1716. outstream: A stream file to write to output
  1717. errstream: A stream file to write to errors
  1718. fast_forward: If True, raise an exception when fast-forward is not possible
  1719. ff_only: If True, only allow fast-forward merges. Raises DivergedBranches
  1720. when branches have diverged rather than performing a merge.
  1721. filter_spec: A git-rev-list-style object filter spec, as an ASCII string.
  1722. Only used if the server supports the Git protocol-v2 'filter'
  1723. feature, and ignored otherwise.
  1724. protocol_version: desired Git protocol version. By default the highest
  1725. mutually supported protocol version will be used
  1726. """
  1727. # Open the repo
  1728. with open_repo_closing(repo) as r:
  1729. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1730. selected_refs = []
  1731. if refspecs is None:
  1732. refspecs = [b"HEAD"]
  1733. def determine_wants(remote_refs, *args, **kwargs):
  1734. selected_refs.extend(
  1735. parse_reftuples(remote_refs, r.refs, refspecs, force=force)
  1736. )
  1737. return [
  1738. remote_refs[lh]
  1739. for (lh, rh, force_ref) in selected_refs
  1740. if remote_refs[lh] not in r.object_store
  1741. ]
  1742. client, path = get_transport_and_path(
  1743. remote_location, config=r.get_config_stack(), **kwargs
  1744. )
  1745. if filter_spec:
  1746. filter_spec = filter_spec.encode("ascii")
  1747. fetch_result = client.fetch(
  1748. path,
  1749. r,
  1750. progress=errstream.write,
  1751. determine_wants=determine_wants,
  1752. filter_spec=filter_spec,
  1753. protocol_version=protocol_version,
  1754. )
  1755. # Store the old HEAD tree before making changes
  1756. try:
  1757. old_head = r.refs[b"HEAD"]
  1758. old_tree_id = r[old_head].tree
  1759. except KeyError:
  1760. old_tree_id = None
  1761. merged = False
  1762. for lh, rh, force_ref in selected_refs:
  1763. if not force_ref and rh in r.refs:
  1764. try:
  1765. check_diverged(r, r.refs.follow(rh)[1], fetch_result.refs[lh])
  1766. except DivergedBranches as exc:
  1767. if ff_only or fast_forward:
  1768. raise
  1769. else:
  1770. # Perform merge
  1771. merge_result, conflicts = _do_merge(r, fetch_result.refs[lh])
  1772. if conflicts:
  1773. raise Error(
  1774. f"Merge conflicts occurred: {conflicts}"
  1775. ) from exc
  1776. merged = True
  1777. # Skip updating ref since merge already updated HEAD
  1778. continue
  1779. r.refs[rh] = fetch_result.refs[lh]
  1780. # Only update HEAD if we didn't perform a merge
  1781. if selected_refs and not merged:
  1782. r[b"HEAD"] = fetch_result.refs[selected_refs[0][1]]
  1783. # Update working tree to match the new HEAD
  1784. # Skip if merge was performed as merge already updates the working tree
  1785. if not merged and old_tree_id is not None:
  1786. new_tree_id = r[b"HEAD"].tree
  1787. blob_normalizer = r.get_blob_normalizer()
  1788. update_working_tree(
  1789. r, old_tree_id, new_tree_id, blob_normalizer=blob_normalizer
  1790. )
  1791. if remote_name is not None:
  1792. _import_remote_refs(r.refs, remote_name, fetch_result.refs)
  1793. # Trigger auto GC if needed
  1794. from .gc import maybe_auto_gc
  1795. with open_repo_closing(repo) as r:
  1796. maybe_auto_gc(r)
  1797. def status(repo=".", ignored=False, untracked_files="normal"):
  1798. """Returns staged, unstaged, and untracked changes relative to the HEAD.
  1799. Args:
  1800. repo: Path to repository or repository object
  1801. ignored: Whether to include ignored files in untracked
  1802. untracked_files: How to handle untracked files, defaults to "all":
  1803. "no": do not return untracked files
  1804. "normal": return untracked directories, not their contents
  1805. "all": include all files in untracked directories
  1806. Using untracked_files="no" can be faster than "all" when the worktree
  1807. contains many untracked files/directories.
  1808. Using untracked_files="normal" provides a good balance, only showing
  1809. directories that are entirely untracked without listing all their contents.
  1810. Returns: GitStatus tuple,
  1811. staged - dict with lists of staged paths (diff index/HEAD)
  1812. unstaged - list of unstaged paths (diff index/working-tree)
  1813. untracked - list of untracked, un-ignored & non-.git paths
  1814. """
  1815. with open_repo_closing(repo) as r:
  1816. # 1. Get status of staged
  1817. tracked_changes = get_tree_changes(r)
  1818. # 2. Get status of unstaged
  1819. index = r.open_index()
  1820. normalizer = r.get_blob_normalizer()
  1821. filter_callback = normalizer.checkin_normalize
  1822. unstaged_changes = list(get_unstaged_changes(index, r.path, filter_callback))
  1823. untracked_paths = get_untracked_paths(
  1824. r.path,
  1825. r.path,
  1826. index,
  1827. exclude_ignored=not ignored,
  1828. untracked_files=untracked_files,
  1829. )
  1830. if sys.platform == "win32":
  1831. untracked_changes = [
  1832. path.replace(os.path.sep, "/") for path in untracked_paths
  1833. ]
  1834. else:
  1835. untracked_changes = list(untracked_paths)
  1836. return GitStatus(tracked_changes, unstaged_changes, untracked_changes)
  1837. def _walk_working_dir_paths(frompath, basepath, prune_dirnames=None):
  1838. """Get path, is_dir for files in working dir from frompath.
  1839. Args:
  1840. frompath: Path to begin walk
  1841. basepath: Path to compare to
  1842. prune_dirnames: Optional callback to prune dirnames during os.walk
  1843. dirnames will be set to result of prune_dirnames(dirpath, dirnames)
  1844. """
  1845. for dirpath, dirnames, filenames in os.walk(frompath):
  1846. # Skip .git and below.
  1847. if ".git" in dirnames:
  1848. dirnames.remove(".git")
  1849. if dirpath != basepath:
  1850. continue
  1851. if ".git" in filenames:
  1852. filenames.remove(".git")
  1853. if dirpath != basepath:
  1854. continue
  1855. if dirpath != frompath:
  1856. yield dirpath, True
  1857. for filename in filenames:
  1858. filepath = os.path.join(dirpath, filename)
  1859. yield filepath, False
  1860. if prune_dirnames:
  1861. dirnames[:] = prune_dirnames(dirpath, dirnames)
  1862. def get_untracked_paths(
  1863. frompath, basepath, index, exclude_ignored=False, untracked_files="all"
  1864. ):
  1865. """Get untracked paths.
  1866. Args:
  1867. frompath: Path to walk
  1868. basepath: Path to compare to
  1869. index: Index to check against
  1870. exclude_ignored: Whether to exclude ignored paths
  1871. untracked_files: How to handle untracked files:
  1872. - "no": return an empty list
  1873. - "all": return all files in untracked directories
  1874. - "normal": return untracked directories without listing their contents
  1875. Note: ignored directories will never be walked for performance reasons.
  1876. If exclude_ignored is False, only the path to an ignored directory will
  1877. be yielded, no files inside the directory will be returned
  1878. """
  1879. if untracked_files not in ("no", "all", "normal"):
  1880. raise ValueError("untracked_files must be one of (no, all, normal)")
  1881. if untracked_files == "no":
  1882. return
  1883. with open_repo_closing(basepath) as r:
  1884. ignore_manager = IgnoreFilterManager.from_repo(r)
  1885. ignored_dirs = []
  1886. # List to store untracked directories found during traversal
  1887. untracked_dir_list = []
  1888. def prune_dirnames(dirpath, dirnames):
  1889. for i in range(len(dirnames) - 1, -1, -1):
  1890. path = os.path.join(dirpath, dirnames[i])
  1891. ip = os.path.join(os.path.relpath(path, basepath), "")
  1892. # Check if directory is ignored
  1893. if ignore_manager.is_ignored(ip):
  1894. if not exclude_ignored:
  1895. ignored_dirs.append(
  1896. os.path.join(os.path.relpath(path, frompath), "")
  1897. )
  1898. del dirnames[i]
  1899. continue
  1900. # For "normal" mode, check if the directory is entirely untracked
  1901. if untracked_files == "normal":
  1902. # Convert directory path to tree path for index lookup
  1903. dir_tree_path = path_to_tree_path(basepath, path)
  1904. # Check if any file in this directory is tracked
  1905. dir_prefix = dir_tree_path + b"/" if dir_tree_path else b""
  1906. has_tracked_files = any(name.startswith(dir_prefix) for name in index)
  1907. if not has_tracked_files:
  1908. # This directory is entirely untracked
  1909. # Check if it should be excluded due to ignore rules
  1910. is_ignored = ignore_manager.is_ignored(
  1911. os.path.relpath(path, basepath)
  1912. )
  1913. if not exclude_ignored or not is_ignored:
  1914. rel_path = os.path.join(os.path.relpath(path, frompath), "")
  1915. untracked_dir_list.append(rel_path)
  1916. del dirnames[i]
  1917. return dirnames
  1918. # For "all" mode, use the original behavior
  1919. if untracked_files == "all":
  1920. for ap, is_dir in _walk_working_dir_paths(
  1921. frompath, basepath, prune_dirnames=prune_dirnames
  1922. ):
  1923. if not is_dir:
  1924. ip = path_to_tree_path(basepath, ap)
  1925. if ip not in index:
  1926. if not exclude_ignored or not ignore_manager.is_ignored(
  1927. os.path.relpath(ap, basepath)
  1928. ):
  1929. yield os.path.relpath(ap, frompath)
  1930. else: # "normal" mode
  1931. # Walk directories, handling both files and directories
  1932. for ap, is_dir in _walk_working_dir_paths(
  1933. frompath, basepath, prune_dirnames=prune_dirnames
  1934. ):
  1935. # This part won't be reached for pruned directories
  1936. if is_dir:
  1937. # Check if this directory is entirely untracked
  1938. dir_tree_path = path_to_tree_path(basepath, ap)
  1939. dir_prefix = dir_tree_path + b"/" if dir_tree_path else b""
  1940. has_tracked_files = any(name.startswith(dir_prefix) for name in index)
  1941. if not has_tracked_files:
  1942. if not exclude_ignored or not ignore_manager.is_ignored(
  1943. os.path.relpath(ap, basepath)
  1944. ):
  1945. yield os.path.join(os.path.relpath(ap, frompath), "")
  1946. else:
  1947. # Check individual files in directories that contain tracked files
  1948. ip = path_to_tree_path(basepath, ap)
  1949. if ip not in index:
  1950. if not exclude_ignored or not ignore_manager.is_ignored(
  1951. os.path.relpath(ap, basepath)
  1952. ):
  1953. yield os.path.relpath(ap, frompath)
  1954. # Yield any untracked directories found during pruning
  1955. yield from untracked_dir_list
  1956. yield from ignored_dirs
  1957. def get_tree_changes(repo):
  1958. """Return add/delete/modify changes to tree by comparing index to HEAD.
  1959. Args:
  1960. repo: repo path or object
  1961. Returns: dict with lists for each type of change
  1962. """
  1963. with open_repo_closing(repo) as r:
  1964. index = r.open_index()
  1965. # Compares the Index to the HEAD & determines changes
  1966. # Iterate through the changes and report add/delete/modify
  1967. # TODO: call out to dulwich.diff_tree somehow.
  1968. tracked_changes = {
  1969. "add": [],
  1970. "delete": [],
  1971. "modify": [],
  1972. }
  1973. try:
  1974. tree_id = r[b"HEAD"].tree
  1975. except KeyError:
  1976. tree_id = None
  1977. for change in index.changes_from_tree(r.object_store, tree_id):
  1978. if not change[0][0]:
  1979. tracked_changes["add"].append(change[0][1])
  1980. elif not change[0][1]:
  1981. tracked_changes["delete"].append(change[0][0])
  1982. elif change[0][0] == change[0][1]:
  1983. tracked_changes["modify"].append(change[0][0])
  1984. else:
  1985. raise NotImplementedError("git mv ops not yet supported")
  1986. return tracked_changes
  1987. def daemon(path=".", address=None, port=None) -> None:
  1988. """Run a daemon serving Git requests over TCP/IP.
  1989. Args:
  1990. path: Path to the directory to serve.
  1991. address: Optional address to listen on (defaults to ::)
  1992. port: Optional port to listen on (defaults to TCP_GIT_PORT)
  1993. """
  1994. # TODO(jelmer): Support git-daemon-export-ok and --export-all.
  1995. backend = FileSystemBackend(path)
  1996. server = TCPGitServer(backend, address, port)
  1997. server.serve_forever()
  1998. def web_daemon(path=".", address=None, port=None) -> None:
  1999. """Run a daemon serving Git requests over HTTP.
  2000. Args:
  2001. path: Path to the directory to serve
  2002. address: Optional address to listen on (defaults to ::)
  2003. port: Optional port to listen on (defaults to 80)
  2004. """
  2005. from .web import (
  2006. WSGIRequestHandlerLogger,
  2007. WSGIServerLogger,
  2008. make_server,
  2009. make_wsgi_chain,
  2010. )
  2011. backend = FileSystemBackend(path)
  2012. app = make_wsgi_chain(backend)
  2013. server = make_server(
  2014. address,
  2015. port,
  2016. app,
  2017. handler_class=WSGIRequestHandlerLogger,
  2018. server_class=WSGIServerLogger,
  2019. )
  2020. server.serve_forever()
  2021. def upload_pack(path=".", inf=None, outf=None) -> int:
  2022. """Upload a pack file after negotiating its contents using smart protocol.
  2023. Args:
  2024. path: Path to the repository
  2025. inf: Input stream to communicate with client
  2026. outf: Output stream to communicate with client
  2027. """
  2028. if outf is None:
  2029. outf = getattr(sys.stdout, "buffer", sys.stdout)
  2030. if inf is None:
  2031. inf = getattr(sys.stdin, "buffer", sys.stdin)
  2032. path = os.path.expanduser(path)
  2033. backend = FileSystemBackend(path)
  2034. def send_fn(data) -> None:
  2035. outf.write(data)
  2036. outf.flush()
  2037. proto = Protocol(inf.read, send_fn)
  2038. handler = UploadPackHandler(backend, [path], proto)
  2039. # FIXME: Catch exceptions and write a single-line summary to outf.
  2040. handler.handle()
  2041. return 0
  2042. def receive_pack(path=".", inf=None, outf=None) -> int:
  2043. """Receive a pack file after negotiating its contents using smart protocol.
  2044. Args:
  2045. path: Path to the repository
  2046. inf: Input stream to communicate with client
  2047. outf: Output stream to communicate with client
  2048. """
  2049. if outf is None:
  2050. outf = getattr(sys.stdout, "buffer", sys.stdout)
  2051. if inf is None:
  2052. inf = getattr(sys.stdin, "buffer", sys.stdin)
  2053. path = os.path.expanduser(path)
  2054. backend = FileSystemBackend(path)
  2055. def send_fn(data) -> None:
  2056. outf.write(data)
  2057. outf.flush()
  2058. proto = Protocol(inf.read, send_fn)
  2059. handler = ReceivePackHandler(backend, [path], proto)
  2060. # FIXME: Catch exceptions and write a single-line summary to outf.
  2061. handler.handle()
  2062. return 0
  2063. def _make_branch_ref(name: Union[str, bytes]) -> Ref:
  2064. if isinstance(name, str):
  2065. name = name.encode(DEFAULT_ENCODING)
  2066. return LOCAL_BRANCH_PREFIX + name
  2067. def _make_tag_ref(name: Union[str, bytes]) -> Ref:
  2068. if isinstance(name, str):
  2069. name = name.encode(DEFAULT_ENCODING)
  2070. return LOCAL_TAG_PREFIX + name
  2071. def branch_delete(repo, name) -> None:
  2072. """Delete a branch.
  2073. Args:
  2074. repo: Path to the repository
  2075. name: Name of the branch
  2076. """
  2077. with open_repo_closing(repo) as r:
  2078. if isinstance(name, list):
  2079. names = name
  2080. else:
  2081. names = [name]
  2082. for name in names:
  2083. del r.refs[_make_branch_ref(name)]
  2084. def branch_create(repo, name, objectish=None, force=False) -> None:
  2085. """Create a branch.
  2086. Args:
  2087. repo: Path to the repository
  2088. name: Name of the new branch
  2089. objectish: Target object to point new branch at (defaults to HEAD)
  2090. force: Force creation of branch, even if it already exists
  2091. """
  2092. with open_repo_closing(repo) as r:
  2093. if objectish is None:
  2094. objectish = "HEAD"
  2095. # Try to expand branch shorthand before parsing
  2096. original_objectish = objectish
  2097. objectish_bytes = (
  2098. objectish.encode(DEFAULT_ENCODING)
  2099. if isinstance(objectish, str)
  2100. else objectish
  2101. )
  2102. if b"refs/remotes/" + objectish_bytes in r.refs:
  2103. objectish = b"refs/remotes/" + objectish_bytes
  2104. elif b"refs/heads/" + objectish_bytes in r.refs:
  2105. objectish = b"refs/heads/" + objectish_bytes
  2106. object = parse_object(r, objectish)
  2107. refname = _make_branch_ref(name)
  2108. ref_message = (
  2109. b"branch: Created from " + original_objectish.encode(DEFAULT_ENCODING)
  2110. if isinstance(original_objectish, str)
  2111. else b"branch: Created from " + original_objectish
  2112. )
  2113. if force:
  2114. r.refs.set_if_equals(refname, None, object.id, message=ref_message)
  2115. else:
  2116. if not r.refs.add_if_new(refname, object.id, message=ref_message):
  2117. raise Error(f"Branch with name {name} already exists.")
  2118. # Check if we should set up tracking
  2119. config = r.get_config_stack()
  2120. try:
  2121. auto_setup_merge = config.get((b"branch",), b"autoSetupMerge").decode()
  2122. except KeyError:
  2123. auto_setup_merge = "true" # Default value
  2124. # Determine if the objectish refers to a remote-tracking branch
  2125. objectish_ref = None
  2126. if original_objectish != "HEAD":
  2127. # Try to resolve objectish as a ref
  2128. objectish_bytes = (
  2129. original_objectish.encode(DEFAULT_ENCODING)
  2130. if isinstance(original_objectish, str)
  2131. else original_objectish
  2132. )
  2133. if objectish_bytes in r.refs:
  2134. objectish_ref = objectish_bytes
  2135. elif b"refs/remotes/" + objectish_bytes in r.refs:
  2136. objectish_ref = b"refs/remotes/" + objectish_bytes
  2137. elif b"refs/heads/" + objectish_bytes in r.refs:
  2138. objectish_ref = b"refs/heads/" + objectish_bytes
  2139. else:
  2140. # HEAD might point to a remote-tracking branch
  2141. head_ref = r.refs.follow(b"HEAD")[0][1]
  2142. if head_ref.startswith(b"refs/remotes/"):
  2143. objectish_ref = head_ref
  2144. # Set up tracking if appropriate
  2145. if objectish_ref and (
  2146. (auto_setup_merge == "always")
  2147. or (
  2148. auto_setup_merge == "true"
  2149. and objectish_ref.startswith(b"refs/remotes/")
  2150. )
  2151. ):
  2152. # Extract remote name and branch from the ref
  2153. if objectish_ref.startswith(b"refs/remotes/"):
  2154. parts = objectish_ref[len(b"refs/remotes/") :].split(b"/", 1)
  2155. if len(parts) == 2:
  2156. remote_name = parts[0]
  2157. remote_branch = b"refs/heads/" + parts[1]
  2158. # Set up tracking
  2159. config = r.get_config()
  2160. branch_name_bytes = (
  2161. name.encode(DEFAULT_ENCODING) if isinstance(name, str) else name
  2162. )
  2163. config.set((b"branch", branch_name_bytes), b"remote", remote_name)
  2164. config.set((b"branch", branch_name_bytes), b"merge", remote_branch)
  2165. config.write_to_path()
  2166. def branch_list(repo):
  2167. """List all branches.
  2168. Args:
  2169. repo: Path to the repository
  2170. Returns:
  2171. List of branch names (without refs/heads/ prefix)
  2172. """
  2173. with open_repo_closing(repo) as r:
  2174. branches = list(r.refs.keys(base=LOCAL_BRANCH_PREFIX))
  2175. # Check for branch.sort configuration
  2176. config = r.get_config_stack()
  2177. try:
  2178. sort_key = config.get((b"branch",), b"sort").decode()
  2179. except KeyError:
  2180. # Default is refname (alphabetical)
  2181. sort_key = "refname"
  2182. # Parse sort key
  2183. reverse = False
  2184. if sort_key.startswith("-"):
  2185. reverse = True
  2186. sort_key = sort_key[1:]
  2187. # Apply sorting
  2188. if sort_key == "refname":
  2189. # Simple alphabetical sort (default)
  2190. branches.sort(reverse=reverse)
  2191. elif sort_key in ("committerdate", "authordate"):
  2192. # Sort by date
  2193. def get_commit_date(branch_name):
  2194. ref = LOCAL_BRANCH_PREFIX + branch_name
  2195. sha = r.refs[ref]
  2196. commit = r.object_store[sha]
  2197. if sort_key == "committerdate":
  2198. return commit.commit_time
  2199. else: # authordate
  2200. return commit.author_time
  2201. # Sort branches by date
  2202. # Note: Python's sort naturally orders smaller values first (ascending)
  2203. # For dates, this means oldest first by default
  2204. # Use a stable sort with branch name as secondary key for consistent ordering
  2205. if reverse:
  2206. # For reverse sort, we want newest dates first but alphabetical names second
  2207. branches.sort(key=lambda b: (-get_commit_date(b), b))
  2208. else:
  2209. branches.sort(key=lambda b: (get_commit_date(b), b))
  2210. else:
  2211. # Unknown sort key, fall back to default
  2212. branches.sort()
  2213. return branches
  2214. def active_branch(repo):
  2215. """Return the active branch in the repository, if any.
  2216. Args:
  2217. repo: Repository to open
  2218. Returns:
  2219. branch name
  2220. Raises:
  2221. KeyError: if the repository does not have a working tree
  2222. IndexError: if HEAD is floating
  2223. """
  2224. with open_repo_closing(repo) as r:
  2225. active_ref = r.refs.follow(b"HEAD")[0][1]
  2226. if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
  2227. raise ValueError(active_ref)
  2228. return active_ref[len(LOCAL_BRANCH_PREFIX) :]
  2229. def get_branch_remote(repo):
  2230. """Return the active branch's remote name, if any.
  2231. Args:
  2232. repo: Repository to open
  2233. Returns:
  2234. remote name
  2235. Raises:
  2236. KeyError: if the repository does not have a working tree
  2237. """
  2238. with open_repo_closing(repo) as r:
  2239. branch_name = active_branch(r.path)
  2240. config = r.get_config()
  2241. try:
  2242. remote_name = config.get((b"branch", branch_name), b"remote")
  2243. except KeyError:
  2244. remote_name = b"origin"
  2245. return remote_name
  2246. def get_branch_merge(repo, branch_name=None):
  2247. """Return the branch's merge reference (upstream branch), if any.
  2248. Args:
  2249. repo: Repository to open
  2250. branch_name: Name of the branch (defaults to active branch)
  2251. Returns:
  2252. merge reference name (e.g. b"refs/heads/main")
  2253. Raises:
  2254. KeyError: if the branch does not have a merge configuration
  2255. """
  2256. with open_repo_closing(repo) as r:
  2257. if branch_name is None:
  2258. branch_name = active_branch(r.path)
  2259. config = r.get_config()
  2260. return config.get((b"branch", branch_name), b"merge")
  2261. def set_branch_tracking(repo, branch_name, remote_name, remote_ref):
  2262. """Set up branch tracking configuration.
  2263. Args:
  2264. repo: Repository to open
  2265. branch_name: Name of the local branch
  2266. remote_name: Name of the remote (e.g. b"origin")
  2267. remote_ref: Remote reference to track (e.g. b"refs/heads/main")
  2268. """
  2269. with open_repo_closing(repo) as r:
  2270. config = r.get_config()
  2271. config.set((b"branch", branch_name), b"remote", remote_name)
  2272. config.set((b"branch", branch_name), b"merge", remote_ref)
  2273. config.write_to_path()
  2274. def fetch(
  2275. repo,
  2276. remote_location=None,
  2277. outstream=sys.stdout,
  2278. errstream=default_bytes_err_stream,
  2279. message=None,
  2280. depth=None,
  2281. prune=False,
  2282. prune_tags=False,
  2283. force=False,
  2284. **kwargs,
  2285. ):
  2286. """Fetch objects from a remote server.
  2287. Args:
  2288. repo: Path to the repository
  2289. remote_location: String identifying a remote server
  2290. outstream: Output stream (defaults to stdout)
  2291. errstream: Error stream (defaults to stderr)
  2292. message: Reflog message (defaults to b"fetch: from <remote_name>")
  2293. depth: Depth to fetch at
  2294. prune: Prune remote removed refs
  2295. prune_tags: Prune reomte removed tags
  2296. Returns:
  2297. Dictionary with refs on the remote
  2298. """
  2299. with open_repo_closing(repo) as r:
  2300. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  2301. if message is None:
  2302. message = b"fetch: from " + remote_location.encode(DEFAULT_ENCODING)
  2303. client, path = get_transport_and_path(
  2304. remote_location, config=r.get_config_stack(), **kwargs
  2305. )
  2306. fetch_result = client.fetch(path, r, progress=errstream.write, depth=depth)
  2307. if remote_name is not None:
  2308. _import_remote_refs(
  2309. r.refs,
  2310. remote_name,
  2311. fetch_result.refs,
  2312. message,
  2313. prune=prune,
  2314. prune_tags=prune_tags,
  2315. )
  2316. # Trigger auto GC if needed
  2317. from .gc import maybe_auto_gc
  2318. with open_repo_closing(repo) as r:
  2319. maybe_auto_gc(r)
  2320. return fetch_result
  2321. def for_each_ref(
  2322. repo: Union[Repo, str] = ".",
  2323. pattern: Optional[Union[str, bytes]] = None,
  2324. ) -> list[tuple[bytes, bytes, bytes]]:
  2325. """Iterate over all refs that match the (optional) pattern.
  2326. Args:
  2327. repo: Path to the repository
  2328. pattern: Optional glob (7) patterns to filter the refs with
  2329. Returns: List of bytes tuples with: (sha, object_type, ref_name)
  2330. """
  2331. if isinstance(pattern, str):
  2332. pattern = os.fsencode(pattern)
  2333. with open_repo_closing(repo) as r:
  2334. refs = r.get_refs()
  2335. if pattern:
  2336. matching_refs: dict[bytes, bytes] = {}
  2337. pattern_parts = pattern.split(b"/")
  2338. for ref, sha in refs.items():
  2339. matches = False
  2340. # git for-each-ref uses glob (7) style patterns, but fnmatch
  2341. # is greedy and also matches slashes, unlike glob.glob.
  2342. # We have to check parts of the pattern individually.
  2343. # See https://github.com/python/cpython/issues/72904
  2344. ref_parts = ref.split(b"/")
  2345. if len(ref_parts) > len(pattern_parts):
  2346. continue
  2347. for pat, ref_part in zip(pattern_parts, ref_parts):
  2348. matches = fnmatch.fnmatchcase(ref_part, pat)
  2349. if not matches:
  2350. break
  2351. if matches:
  2352. matching_refs[ref] = sha
  2353. refs = matching_refs
  2354. ret: list[tuple[bytes, bytes, bytes]] = [
  2355. (sha, r.get_object(sha).type_name, ref)
  2356. for ref, sha in sorted(
  2357. refs.items(),
  2358. key=lambda ref_sha: ref_sha[0],
  2359. )
  2360. if ref != b"HEAD"
  2361. ]
  2362. return ret
  2363. def ls_remote(remote, config: Optional[Config] = None, **kwargs):
  2364. """List the refs in a remote.
  2365. Args:
  2366. remote: Remote repository location
  2367. config: Configuration to use
  2368. Returns:
  2369. LsRemoteResult object with refs and symrefs
  2370. """
  2371. if config is None:
  2372. config = StackedConfig.default()
  2373. client, host_path = get_transport_and_path(remote, config=config, **kwargs)
  2374. return client.get_refs(host_path)
  2375. def repack(repo) -> None:
  2376. """Repack loose files in a repository.
  2377. Currently this only packs loose objects.
  2378. Args:
  2379. repo: Path to the repository
  2380. """
  2381. with open_repo_closing(repo) as r:
  2382. r.object_store.pack_loose_objects()
  2383. def pack_objects(
  2384. repo,
  2385. object_ids,
  2386. packf,
  2387. idxf,
  2388. delta_window_size=None,
  2389. deltify=None,
  2390. reuse_deltas=True,
  2391. pack_index_version=None,
  2392. ) -> None:
  2393. """Pack objects into a file.
  2394. Args:
  2395. repo: Path to the repository
  2396. object_ids: List of object ids to write
  2397. packf: File-like object to write to
  2398. idxf: File-like object to write to (can be None)
  2399. delta_window_size: Sliding window size for searching for deltas;
  2400. Set to None for default window size.
  2401. deltify: Whether to deltify objects
  2402. reuse_deltas: Allow reuse of existing deltas while deltifying
  2403. pack_index_version: Pack index version to use (1, 2, or 3). If None, uses default version.
  2404. """
  2405. with open_repo_closing(repo) as r:
  2406. entries, data_sum = write_pack_from_container(
  2407. packf.write,
  2408. r.object_store,
  2409. [(oid, None) for oid in object_ids],
  2410. deltify=deltify,
  2411. delta_window_size=delta_window_size,
  2412. reuse_deltas=reuse_deltas,
  2413. )
  2414. if idxf is not None:
  2415. entries = sorted([(k, v[0], v[1]) for (k, v) in entries.items()])
  2416. write_pack_index(idxf, entries, data_sum, version=pack_index_version)
  2417. def ls_tree(
  2418. repo,
  2419. treeish: Union[str, bytes, Commit, Tree, Tag] = b"HEAD",
  2420. outstream=sys.stdout,
  2421. recursive=False,
  2422. name_only=False,
  2423. ) -> None:
  2424. """List contents of a tree.
  2425. Args:
  2426. repo: Path to the repository
  2427. treeish: Tree id to list
  2428. outstream: Output stream (defaults to stdout)
  2429. recursive: Whether to recursively list files
  2430. name_only: Only print item name
  2431. """
  2432. def list_tree(store, treeid, base) -> None:
  2433. for name, mode, sha in store[treeid].iteritems():
  2434. if base:
  2435. name = posixpath.join(base, name)
  2436. if name_only:
  2437. outstream.write(name + b"\n")
  2438. else:
  2439. outstream.write(pretty_format_tree_entry(name, mode, sha))
  2440. if stat.S_ISDIR(mode) and recursive:
  2441. list_tree(store, sha, name)
  2442. with open_repo_closing(repo) as r:
  2443. tree = parse_tree(r, treeish)
  2444. list_tree(r.object_store, tree.id, "")
  2445. def remote_add(repo, name: Union[bytes, str], url: Union[bytes, str]) -> None:
  2446. """Add a remote.
  2447. Args:
  2448. repo: Path to the repository
  2449. name: Remote name
  2450. url: Remote URL
  2451. """
  2452. if not isinstance(name, bytes):
  2453. name = name.encode(DEFAULT_ENCODING)
  2454. if not isinstance(url, bytes):
  2455. url = url.encode(DEFAULT_ENCODING)
  2456. with open_repo_closing(repo) as r:
  2457. c = r.get_config()
  2458. section = (b"remote", name)
  2459. if c.has_section(section):
  2460. raise RemoteExists(section)
  2461. c.set(section, b"url", url)
  2462. c.write_to_path()
  2463. def remote_remove(repo: Repo, name: Union[bytes, str]) -> None:
  2464. """Remove a remote.
  2465. Args:
  2466. repo: Path to the repository
  2467. name: Remote name
  2468. """
  2469. if not isinstance(name, bytes):
  2470. name = name.encode(DEFAULT_ENCODING)
  2471. with open_repo_closing(repo) as r:
  2472. c = r.get_config()
  2473. section = (b"remote", name)
  2474. del c[section]
  2475. c.write_to_path()
  2476. def _quote_path(path: str) -> str:
  2477. """Quote a path using C-style quoting similar to git's core.quotePath.
  2478. Args:
  2479. path: Path to quote
  2480. Returns:
  2481. Quoted path string
  2482. """
  2483. # Check if path needs quoting (non-ASCII or special characters)
  2484. needs_quoting = False
  2485. for char in path:
  2486. if ord(char) > 127 or char in '"\\':
  2487. needs_quoting = True
  2488. break
  2489. if not needs_quoting:
  2490. return path
  2491. # Apply C-style quoting
  2492. quoted = '"'
  2493. for char in path:
  2494. if ord(char) > 127:
  2495. # Non-ASCII character, encode as octal escape
  2496. utf8_bytes = char.encode("utf-8")
  2497. for byte in utf8_bytes:
  2498. quoted += f"\\{byte:03o}"
  2499. elif char == '"':
  2500. quoted += '\\"'
  2501. elif char == "\\":
  2502. quoted += "\\\\"
  2503. else:
  2504. quoted += char
  2505. quoted += '"'
  2506. return quoted
  2507. def check_ignore(repo, paths, no_index=False, quote_path=True):
  2508. r"""Debug gitignore files.
  2509. Args:
  2510. repo: Path to the repository
  2511. paths: List of paths to check for
  2512. no_index: Don't check index
  2513. quote_path: If True, quote non-ASCII characters in returned paths using
  2514. C-style octal escapes (e.g. "тест.txt" becomes "\\321\\202\\320\\265\\321\\201\\321\\202.txt").
  2515. If False, return raw unicode paths.
  2516. Returns: List of ignored files
  2517. """
  2518. with open_repo_closing(repo) as r:
  2519. index = r.open_index()
  2520. ignore_manager = IgnoreFilterManager.from_repo(r)
  2521. for original_path in paths:
  2522. if not no_index and path_to_tree_path(r.path, original_path) in index:
  2523. continue
  2524. # Preserve whether the original path had a trailing slash
  2525. had_trailing_slash = original_path.endswith(("/", os.path.sep))
  2526. if os.path.isabs(original_path):
  2527. path = os.path.relpath(original_path, r.path)
  2528. # Normalize Windows paths to use forward slashes
  2529. if os.path.sep != "/":
  2530. path = path.replace(os.path.sep, "/")
  2531. else:
  2532. path = original_path
  2533. # Restore trailing slash if it was in the original
  2534. if had_trailing_slash and not path.endswith("/"):
  2535. path = path + "/"
  2536. # For directories, check with trailing slash to get correct ignore behavior
  2537. test_path = path
  2538. path_without_slash = path.rstrip("/")
  2539. is_directory = os.path.isdir(os.path.join(r.path, path_without_slash))
  2540. # If this is a directory path, ensure we test it correctly
  2541. if is_directory and not path.endswith("/"):
  2542. test_path = path + "/"
  2543. if ignore_manager.is_ignored(test_path):
  2544. # Return relative path (like git does) when absolute path was provided
  2545. if os.path.isabs(original_path):
  2546. output_path = path
  2547. else:
  2548. output_path = original_path
  2549. yield _quote_path(output_path) if quote_path else output_path
  2550. def update_head(repo, target, detached=False, new_branch=None) -> None:
  2551. """Update HEAD to point at a new branch/commit.
  2552. Note that this does not actually update the working tree.
  2553. Args:
  2554. repo: Path to the repository
  2555. detached: Create a detached head
  2556. target: Branch or committish to switch to
  2557. new_branch: New branch to create
  2558. """
  2559. with open_repo_closing(repo) as r:
  2560. if new_branch is not None:
  2561. to_set = _make_branch_ref(new_branch)
  2562. else:
  2563. to_set = b"HEAD"
  2564. if detached:
  2565. # TODO(jelmer): Provide some way so that the actual ref gets
  2566. # updated rather than what it points to, so the delete isn't
  2567. # necessary.
  2568. del r.refs[to_set]
  2569. r.refs[to_set] = parse_commit(r, target).id
  2570. else:
  2571. r.refs.set_symbolic_ref(to_set, parse_ref(r, target))
  2572. if new_branch is not None:
  2573. r.refs.set_symbolic_ref(b"HEAD", to_set)
  2574. def checkout(
  2575. repo,
  2576. target: Optional[Union[str, bytes, Commit, Tag]] = None,
  2577. force: bool = False,
  2578. new_branch: Optional[Union[bytes, str]] = None,
  2579. paths: Optional[list[Union[bytes, str]]] = None,
  2580. ) -> None:
  2581. """Switch to a branch or commit, updating both HEAD and the working tree.
  2582. This is similar to 'git checkout', allowing you to switch to a branch,
  2583. tag, or specific commit. Unlike update_head, this function also updates
  2584. the working tree to match the target.
  2585. Args:
  2586. repo: Path to repository or repository object
  2587. target: Branch name, tag, or commit SHA to checkout. If None and paths is specified,
  2588. restores files from HEAD
  2589. force: Force checkout even if there are local changes
  2590. new_branch: Create a new branch at target (like git checkout -b)
  2591. paths: List of specific paths to checkout. If specified, only these paths are updated
  2592. and HEAD is not changed
  2593. Raises:
  2594. CheckoutError: If checkout cannot be performed due to conflicts
  2595. KeyError: If the target reference cannot be found
  2596. """
  2597. with open_repo_closing(repo) as r:
  2598. # Store the original target for later reference checks
  2599. original_target = target
  2600. # Handle path-specific checkout (like git checkout -- <paths>)
  2601. if paths is not None:
  2602. # Convert paths to bytes
  2603. byte_paths = []
  2604. for path in paths:
  2605. if isinstance(path, str):
  2606. byte_paths.append(path.encode(DEFAULT_ENCODING))
  2607. else:
  2608. byte_paths.append(path)
  2609. # If no target specified, use HEAD
  2610. if target is None:
  2611. try:
  2612. target = r.refs[b"HEAD"]
  2613. except KeyError:
  2614. raise CheckoutError("No HEAD reference found")
  2615. else:
  2616. if isinstance(target, str):
  2617. target = target.encode(DEFAULT_ENCODING)
  2618. # Get the target commit and tree
  2619. target_commit = parse_commit(r, target)
  2620. target_tree = r[target_commit.tree]
  2621. # Get blob normalizer for line ending conversion
  2622. blob_normalizer = r.get_blob_normalizer()
  2623. # Restore specified paths from target tree
  2624. for path in byte_paths:
  2625. try:
  2626. # Look up the path in the target tree
  2627. mode, sha = target_tree.lookup_path(
  2628. r.object_store.__getitem__, path
  2629. )
  2630. obj = r[sha]
  2631. # Create directories if needed
  2632. # Handle path as string
  2633. if isinstance(path, bytes):
  2634. path_str = path.decode(DEFAULT_ENCODING)
  2635. else:
  2636. path_str = path
  2637. file_path = os.path.join(r.path, path_str)
  2638. os.makedirs(os.path.dirname(file_path), exist_ok=True)
  2639. # Write the file content
  2640. if stat.S_ISREG(mode):
  2641. # Apply checkout filters (smudge)
  2642. if blob_normalizer:
  2643. obj = blob_normalizer.checkout_normalize(obj, path)
  2644. flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
  2645. if sys.platform == "win32":
  2646. flags |= os.O_BINARY
  2647. with os.fdopen(os.open(file_path, flags, mode), "wb") as f:
  2648. f.write(obj.data)
  2649. # Update the index
  2650. r.stage(path)
  2651. except KeyError:
  2652. # Path doesn't exist in target tree
  2653. pass
  2654. return
  2655. # Normal checkout (switching branches/commits)
  2656. if target is None:
  2657. raise ValueError("Target must be specified for branch/commit checkout")
  2658. if isinstance(target, str):
  2659. target_bytes = target.encode(DEFAULT_ENCODING)
  2660. elif isinstance(target, bytes):
  2661. target_bytes = target
  2662. else:
  2663. # For Commit/Tag objects, we'll use their SHA
  2664. target_bytes = target.id
  2665. if isinstance(new_branch, str):
  2666. new_branch = new_branch.encode(DEFAULT_ENCODING)
  2667. # Parse the target to get the commit
  2668. assert (
  2669. original_target is not None
  2670. ) # Guaranteed by earlier check for normal checkout
  2671. target_commit = parse_commit(r, original_target)
  2672. target_tree_id = target_commit.tree
  2673. # Get current HEAD tree for comparison
  2674. try:
  2675. current_head = r.refs[b"HEAD"]
  2676. current_tree_id = r[current_head].tree
  2677. except KeyError:
  2678. # No HEAD yet (empty repo)
  2679. current_tree_id = None
  2680. # Check for uncommitted changes if not forcing
  2681. if not force and current_tree_id is not None:
  2682. status_report = status(r)
  2683. changes = []
  2684. # staged is a dict with 'add', 'delete', 'modify' keys
  2685. if isinstance(status_report.staged, dict):
  2686. changes.extend(status_report.staged.get("add", []))
  2687. changes.extend(status_report.staged.get("delete", []))
  2688. changes.extend(status_report.staged.get("modify", []))
  2689. # unstaged is a list
  2690. changes.extend(status_report.unstaged)
  2691. if changes:
  2692. # Check if any changes would conflict with checkout
  2693. target_tree = r[target_tree_id]
  2694. for change in changes:
  2695. if isinstance(change, str):
  2696. change = change.encode(DEFAULT_ENCODING)
  2697. try:
  2698. target_tree.lookup_path(r.object_store.__getitem__, change)
  2699. # File exists in target tree - would overwrite local changes
  2700. raise CheckoutError(
  2701. f"Your local changes to '{change.decode()}' would be "
  2702. "overwritten by checkout. Please commit or stash before switching."
  2703. )
  2704. except KeyError:
  2705. # File doesn't exist in target tree - change can be preserved
  2706. pass
  2707. # Get configuration for working directory update
  2708. config = r.get_config()
  2709. honor_filemode = config.get_boolean(b"core", b"filemode", os.name != "nt")
  2710. if config.get_boolean(b"core", b"core.protectNTFS", os.name == "nt"):
  2711. validate_path_element = validate_path_element_ntfs
  2712. else:
  2713. validate_path_element = validate_path_element_default
  2714. if config.get_boolean(b"core", b"symlinks", True):
  2715. symlink_fn = symlink
  2716. else:
  2717. def symlink_fn(source, target) -> None: # type: ignore
  2718. mode = "w" + ("b" if isinstance(source, bytes) else "")
  2719. with open(target, mode) as f:
  2720. f.write(source)
  2721. # Get blob normalizer for line ending conversion
  2722. blob_normalizer = r.get_blob_normalizer()
  2723. # Update working tree
  2724. update_working_tree(
  2725. r,
  2726. current_tree_id,
  2727. target_tree_id,
  2728. honor_filemode=honor_filemode,
  2729. validate_path_element=validate_path_element,
  2730. symlink_fn=symlink_fn,
  2731. force_remove_untracked=force,
  2732. blob_normalizer=blob_normalizer,
  2733. )
  2734. # Update HEAD
  2735. if new_branch:
  2736. # Create new branch and switch to it
  2737. branch_create(r, new_branch, objectish=target_commit.id.decode("ascii"))
  2738. update_head(r, new_branch)
  2739. # Set up tracking if creating from a remote branch
  2740. from .refs import LOCAL_REMOTE_PREFIX, parse_remote_ref
  2741. if isinstance(original_target, bytes) and target_bytes.startswith(
  2742. LOCAL_REMOTE_PREFIX
  2743. ):
  2744. try:
  2745. remote_name, branch_name = parse_remote_ref(target_bytes)
  2746. # Set tracking to refs/heads/<branch> on the remote
  2747. set_branch_tracking(
  2748. r, new_branch, remote_name, b"refs/heads/" + branch_name
  2749. )
  2750. except ValueError:
  2751. # Invalid remote ref format, skip tracking setup
  2752. pass
  2753. else:
  2754. # Check if target is a branch name (with or without refs/heads/ prefix)
  2755. branch_ref = None
  2756. if (
  2757. isinstance(original_target, (str, bytes))
  2758. and target_bytes in r.refs.keys()
  2759. ):
  2760. if target_bytes.startswith(LOCAL_BRANCH_PREFIX):
  2761. branch_ref = target_bytes
  2762. else:
  2763. # Try adding refs/heads/ prefix
  2764. potential_branch = (
  2765. _make_branch_ref(target_bytes)
  2766. if isinstance(original_target, (str, bytes))
  2767. else None
  2768. )
  2769. if potential_branch in r.refs.keys():
  2770. branch_ref = potential_branch
  2771. if branch_ref:
  2772. # It's a branch - update HEAD symbolically
  2773. update_head(r, branch_ref)
  2774. else:
  2775. # It's a tag, other ref, or commit SHA - detached HEAD
  2776. update_head(r, target_commit.id.decode("ascii"), detached=True)
  2777. def reset_file(
  2778. repo,
  2779. file_path: str,
  2780. target: Union[str, bytes, Commit, Tree, Tag] = b"HEAD",
  2781. symlink_fn=None,
  2782. ) -> None:
  2783. """Reset the file to specific commit or branch.
  2784. Args:
  2785. repo: dulwich Repo object
  2786. file_path: file to reset, relative to the repository path
  2787. target: branch or commit or b'HEAD' to reset
  2788. """
  2789. tree = parse_tree(repo, treeish=target)
  2790. tree_path = _fs_to_tree_path(file_path)
  2791. file_entry = tree.lookup_path(repo.object_store.__getitem__, tree_path)
  2792. full_path = os.path.join(os.fsencode(repo.path), tree_path)
  2793. blob = repo.object_store[file_entry[1]]
  2794. mode = file_entry[0]
  2795. build_file_from_blob(blob, mode, full_path, symlink_fn=symlink_fn)
  2796. @replace_me(since="0.22.9", remove_in="0.24.0")
  2797. def checkout_branch(repo, target: Union[bytes, str], force: bool = False) -> None:
  2798. """Switch branches or restore working tree files.
  2799. This is now a wrapper around the general checkout() function.
  2800. Preserved for backward compatibility.
  2801. Args:
  2802. repo: dulwich Repo object
  2803. target: branch name or commit sha to checkout
  2804. force: true or not to force checkout
  2805. """
  2806. # Simply delegate to the new checkout function
  2807. return checkout(repo, target, force=force)
  2808. def sparse_checkout(
  2809. repo, patterns=None, force: bool = False, cone: Union[bool, None] = None
  2810. ):
  2811. """Perform a sparse checkout in the repository (either 'full' or 'cone mode').
  2812. Perform sparse checkout in either 'cone' (directory-based) mode or
  2813. 'full pattern' (.gitignore) mode, depending on the ``cone`` parameter.
  2814. If ``cone`` is ``None``, the mode is inferred from the repository's
  2815. ``core.sparseCheckoutCone`` config setting.
  2816. Steps:
  2817. 1) If ``patterns`` is provided, write them to ``.git/info/sparse-checkout``.
  2818. 2) Determine which paths in the index are included vs. excluded.
  2819. - If ``cone=True``, use "cone-compatible" directory-based logic.
  2820. - If ``cone=False``, use standard .gitignore-style matching.
  2821. 3) Update the index's skip-worktree bits and add/remove files in
  2822. the working tree accordingly.
  2823. 4) If ``force=False``, refuse to remove files that have local modifications.
  2824. Args:
  2825. repo: Path to the repository or a Repo object.
  2826. patterns: Optional list of sparse-checkout patterns to write.
  2827. force: Whether to force removal of locally modified files (default False).
  2828. cone: Boolean indicating cone mode (True/False). If None, read from config.
  2829. Returns:
  2830. None
  2831. """
  2832. with open_repo_closing(repo) as repo_obj:
  2833. # --- 0) Possibly infer 'cone' from config ---
  2834. if cone is None:
  2835. cone = repo_obj.infer_cone_mode()
  2836. # --- 1) Read or write patterns ---
  2837. if patterns is None:
  2838. lines = repo_obj.get_sparse_checkout_patterns()
  2839. if lines is None:
  2840. raise Error("No sparse checkout patterns found.")
  2841. else:
  2842. lines = patterns
  2843. repo_obj.set_sparse_checkout_patterns(patterns)
  2844. # --- 2) Determine the set of included paths ---
  2845. included_paths = determine_included_paths(repo_obj, lines, cone)
  2846. # --- 3) Apply those results to the index & working tree ---
  2847. try:
  2848. apply_included_paths(repo_obj, included_paths, force=force)
  2849. except SparseCheckoutConflictError as exc:
  2850. raise CheckoutError(*exc.args) from exc
  2851. def cone_mode_init(repo):
  2852. """Initialize a repository to use sparse checkout in 'cone' mode.
  2853. Sets ``core.sparseCheckout`` and ``core.sparseCheckoutCone`` in the config.
  2854. Writes an initial ``.git/info/sparse-checkout`` file that includes only
  2855. top-level files (and excludes all subdirectories), e.g. ``["/*", "!/*/"]``.
  2856. Then performs a sparse checkout to update the working tree accordingly.
  2857. If no directories are specified, then only top-level files are included:
  2858. https://git-scm.com/docs/git-sparse-checkout#_internalscone_mode_handling
  2859. Args:
  2860. repo: Path to the repository or a Repo object.
  2861. Returns:
  2862. None
  2863. """
  2864. with open_repo_closing(repo) as repo_obj:
  2865. repo_obj.configure_for_cone_mode()
  2866. patterns = ["/*", "!/*/"] # root-level files only
  2867. sparse_checkout(repo_obj, patterns, force=True, cone=True)
  2868. def cone_mode_set(repo, dirs, force=False):
  2869. """Overwrite the existing 'cone-mode' sparse patterns with a new set of directories.
  2870. Ensures ``core.sparseCheckout`` and ``core.sparseCheckoutCone`` are enabled.
  2871. Writes new patterns so that only the specified directories (and top-level files)
  2872. remain in the working tree, and applies the sparse checkout update.
  2873. Args:
  2874. repo: Path to the repository or a Repo object.
  2875. dirs: List of directory names to include.
  2876. force: Whether to forcibly discard local modifications (default False).
  2877. Returns:
  2878. None
  2879. """
  2880. with open_repo_closing(repo) as repo_obj:
  2881. repo_obj.configure_for_cone_mode()
  2882. repo_obj.set_cone_mode_patterns(dirs=dirs)
  2883. new_patterns = repo_obj.get_sparse_checkout_patterns()
  2884. # Finally, apply the patterns and update the working tree
  2885. sparse_checkout(repo_obj, new_patterns, force=force, cone=True)
  2886. def cone_mode_add(repo, dirs, force=False):
  2887. """Add new directories to the existing 'cone-mode' sparse-checkout patterns.
  2888. Reads the current patterns from ``.git/info/sparse-checkout``, adds pattern
  2889. lines to include the specified directories, and then performs a sparse
  2890. checkout to update the working tree accordingly.
  2891. Args:
  2892. repo: Path to the repository or a Repo object.
  2893. dirs: List of directory names to add to the sparse-checkout.
  2894. force: Whether to forcibly discard local modifications (default False).
  2895. Returns:
  2896. None
  2897. """
  2898. with open_repo_closing(repo) as repo_obj:
  2899. repo_obj.configure_for_cone_mode()
  2900. # Do not pass base patterns as dirs
  2901. base_patterns = ["/*", "!/*/"]
  2902. existing_dirs = [
  2903. pat.strip("/")
  2904. for pat in repo_obj.get_sparse_checkout_patterns()
  2905. if pat not in base_patterns
  2906. ]
  2907. added_dirs = existing_dirs + (dirs or [])
  2908. repo_obj.set_cone_mode_patterns(dirs=added_dirs)
  2909. new_patterns = repo_obj.get_sparse_checkout_patterns()
  2910. sparse_checkout(repo_obj, patterns=new_patterns, force=force, cone=True)
  2911. def check_mailmap(repo, contact):
  2912. """Check canonical name and email of contact.
  2913. Args:
  2914. repo: Path to the repository
  2915. contact: Contact name and/or email
  2916. Returns: Canonical contact data
  2917. """
  2918. with open_repo_closing(repo) as r:
  2919. from .mailmap import Mailmap
  2920. try:
  2921. mailmap = Mailmap.from_path(os.path.join(r.path, ".mailmap"))
  2922. except FileNotFoundError:
  2923. mailmap = Mailmap()
  2924. return mailmap.lookup(contact)
  2925. def fsck(repo):
  2926. """Check a repository.
  2927. Args:
  2928. repo: A path to the repository
  2929. Returns: Iterator over errors/warnings
  2930. """
  2931. with open_repo_closing(repo) as r:
  2932. # TODO(jelmer): check pack files
  2933. # TODO(jelmer): check graph
  2934. # TODO(jelmer): check refs
  2935. for sha in r.object_store:
  2936. o = r.object_store[sha]
  2937. try:
  2938. o.check()
  2939. except Exception as e:
  2940. yield (sha, e)
  2941. def stash_list(repo):
  2942. """List all stashes in a repository."""
  2943. with open_repo_closing(repo) as r:
  2944. from .stash import Stash
  2945. stash = Stash.from_repo(r)
  2946. return enumerate(list(stash.stashes()))
  2947. def stash_push(repo) -> None:
  2948. """Push a new stash onto the stack."""
  2949. with open_repo_closing(repo) as r:
  2950. from .stash import Stash
  2951. stash = Stash.from_repo(r)
  2952. stash.push()
  2953. def stash_pop(repo) -> None:
  2954. """Pop a stash from the stack."""
  2955. with open_repo_closing(repo) as r:
  2956. from .stash import Stash
  2957. stash = Stash.from_repo(r)
  2958. stash.pop(0)
  2959. def stash_drop(repo, index) -> None:
  2960. """Drop a stash from the stack."""
  2961. with open_repo_closing(repo) as r:
  2962. from .stash import Stash
  2963. stash = Stash.from_repo(r)
  2964. stash.drop(index)
  2965. def ls_files(repo):
  2966. """List all files in an index."""
  2967. with open_repo_closing(repo) as r:
  2968. return sorted(r.open_index())
  2969. def find_unique_abbrev(object_store, object_id, min_length=7):
  2970. """Find the shortest unique abbreviation for an object ID.
  2971. Args:
  2972. object_store: Object store to search in
  2973. object_id: The full object ID to abbreviate
  2974. min_length: Minimum length of abbreviation (default 7)
  2975. Returns:
  2976. The shortest unique prefix of the object ID (at least min_length chars)
  2977. """
  2978. if isinstance(object_id, bytes):
  2979. hex_id = object_id.decode("ascii")
  2980. else:
  2981. hex_id = object_id
  2982. # Start with minimum length
  2983. for length in range(min_length, len(hex_id) + 1):
  2984. prefix = hex_id[:length]
  2985. matches = 0
  2986. # Check if this prefix is unique
  2987. for obj_id in object_store:
  2988. if obj_id.decode("ascii").startswith(prefix):
  2989. matches += 1
  2990. if matches > 1:
  2991. # Not unique, need more characters
  2992. break
  2993. if matches == 1:
  2994. # Found unique prefix
  2995. return prefix
  2996. # If we get here, return the full ID
  2997. return hex_id
  2998. def describe(repo, abbrev=None):
  2999. """Describe the repository version.
  3000. Args:
  3001. repo: git repository
  3002. abbrev: number of characters of commit to take, default is 7
  3003. Returns: a string description of the current git revision
  3004. Examples: "gabcdefh", "v0.1" or "v0.1-5-gabcdefh".
  3005. """
  3006. abbrev_slice = slice(0, abbrev if abbrev is not None else 7)
  3007. # Get the repository
  3008. with open_repo_closing(repo) as r:
  3009. # Get a list of all tags
  3010. refs = r.get_refs()
  3011. tags = {}
  3012. for key, value in refs.items():
  3013. key = key.decode()
  3014. obj = r.get_object(value)
  3015. if "tags" not in key:
  3016. continue
  3017. _, tag = key.rsplit("/", 1)
  3018. try:
  3019. # Annotated tag case
  3020. commit = obj.object
  3021. commit = r.get_object(commit[1])
  3022. except AttributeError:
  3023. # Lightweight tag case - obj is already the commit
  3024. commit = obj
  3025. tags[tag] = [
  3026. datetime.datetime(*time.gmtime(commit.commit_time)[:6]),
  3027. commit.id.decode("ascii"),
  3028. ]
  3029. sorted_tags = sorted(tags.items(), key=lambda tag: tag[1][0], reverse=True)
  3030. # Get the latest commit
  3031. latest_commit = r[r.head()]
  3032. # If there are no tags, return the latest commit
  3033. if len(sorted_tags) == 0:
  3034. if abbrev is not None:
  3035. return "g{}".format(latest_commit.id.decode("ascii")[abbrev_slice])
  3036. return f"g{find_unique_abbrev(r.object_store, latest_commit.id)}"
  3037. # We're now 0 commits from the top
  3038. commit_count = 0
  3039. # Walk through all commits
  3040. walker = r.get_walker()
  3041. for entry in walker:
  3042. # Check if tag
  3043. commit_id = entry.commit.id.decode("ascii")
  3044. for tag in sorted_tags:
  3045. tag_name = tag[0]
  3046. tag_commit = tag[1][1]
  3047. if commit_id == tag_commit:
  3048. if commit_count == 0:
  3049. return tag_name
  3050. else:
  3051. if abbrev is not None:
  3052. abbrev_hash = latest_commit.id.decode("ascii")[abbrev_slice]
  3053. else:
  3054. abbrev_hash = find_unique_abbrev(
  3055. r.object_store, latest_commit.id
  3056. )
  3057. return f"{tag_name}-{commit_count}-g{abbrev_hash}"
  3058. commit_count += 1
  3059. # Return plain commit if no parent tag can be found
  3060. if abbrev is not None:
  3061. return "g{}".format(latest_commit.id.decode("ascii")[abbrev_slice])
  3062. return f"g{find_unique_abbrev(r.object_store, latest_commit.id)}"
  3063. def get_object_by_path(
  3064. repo, path, committish: Optional[Union[str, bytes, Commit, Tag]] = None
  3065. ):
  3066. """Get an object by path.
  3067. Args:
  3068. repo: A path to the repository
  3069. path: Path to look up
  3070. committish: Commit to look up path in
  3071. Returns: A `ShaFile` object
  3072. """
  3073. if committish is None:
  3074. committish = "HEAD"
  3075. # Get the repository
  3076. with open_repo_closing(repo) as r:
  3077. commit = parse_commit(r, committish)
  3078. base_tree = commit.tree
  3079. if not isinstance(path, bytes):
  3080. path = commit_encode(commit, path)
  3081. (mode, sha) = tree_lookup_path(r.object_store.__getitem__, base_tree, path)
  3082. return r[sha]
  3083. def write_tree(repo):
  3084. """Write a tree object from the index.
  3085. Args:
  3086. repo: Repository for which to write tree
  3087. Returns: tree id for the tree that was written
  3088. """
  3089. with open_repo_closing(repo) as r:
  3090. return r.open_index().commit(r.object_store)
  3091. def _do_merge(
  3092. r,
  3093. merge_commit_id,
  3094. no_commit=False,
  3095. no_ff=False,
  3096. message=None,
  3097. author=None,
  3098. committer=None,
  3099. ):
  3100. """Internal merge implementation that operates on an open repository.
  3101. Args:
  3102. r: Open repository object
  3103. merge_commit_id: SHA of commit to merge
  3104. no_commit: If True, do not create a merge commit
  3105. no_ff: If True, force creation of a merge commit
  3106. message: Optional merge commit message
  3107. author: Optional author for merge commit
  3108. committer: Optional committer for merge commit
  3109. Returns:
  3110. Tuple of (merge_commit_sha, conflicts) where merge_commit_sha is None
  3111. if no_commit=True or there were conflicts
  3112. """
  3113. from .graph import find_merge_base
  3114. from .merge import three_way_merge
  3115. # Get HEAD commit
  3116. try:
  3117. head_commit_id = r.refs[b"HEAD"]
  3118. except KeyError:
  3119. raise Error("No HEAD reference found")
  3120. head_commit = r[head_commit_id]
  3121. merge_commit = r[merge_commit_id]
  3122. # Check if fast-forward is possible
  3123. merge_bases = find_merge_base(r, [head_commit_id, merge_commit_id])
  3124. if not merge_bases:
  3125. raise Error("No common ancestor found")
  3126. # Use the first merge base
  3127. base_commit_id = merge_bases[0]
  3128. # Check if we're trying to merge the same commit
  3129. if head_commit_id == merge_commit_id:
  3130. # Already up to date
  3131. return (None, [])
  3132. # Check for fast-forward
  3133. if base_commit_id == head_commit_id and not no_ff:
  3134. # Fast-forward merge
  3135. r.refs[b"HEAD"] = merge_commit_id
  3136. # Update the working directory
  3137. update_working_tree(r, head_commit.tree, merge_commit.tree)
  3138. return (merge_commit_id, [])
  3139. if base_commit_id == merge_commit_id:
  3140. # Already up to date
  3141. return (None, [])
  3142. # Perform three-way merge
  3143. base_commit = r[base_commit_id]
  3144. merged_tree, conflicts = three_way_merge(
  3145. r.object_store, base_commit, head_commit, merge_commit
  3146. )
  3147. # Add merged tree to object store
  3148. r.object_store.add_object(merged_tree)
  3149. # Update index and working directory
  3150. update_working_tree(r, head_commit.tree, merged_tree.id)
  3151. if conflicts or no_commit:
  3152. # Don't create a commit if there are conflicts or no_commit is True
  3153. return (None, conflicts)
  3154. # Create merge commit
  3155. merge_commit_obj = Commit()
  3156. merge_commit_obj.tree = merged_tree.id
  3157. merge_commit_obj.parents = [head_commit_id, merge_commit_id]
  3158. # Set author/committer
  3159. if author is None:
  3160. author = get_user_identity(r.get_config_stack())
  3161. if committer is None:
  3162. committer = author
  3163. merge_commit_obj.author = author
  3164. merge_commit_obj.committer = committer
  3165. # Set timestamps
  3166. timestamp = int(time.time())
  3167. timezone = 0 # UTC
  3168. merge_commit_obj.author_time = timestamp
  3169. merge_commit_obj.author_timezone = timezone
  3170. merge_commit_obj.commit_time = timestamp
  3171. merge_commit_obj.commit_timezone = timezone
  3172. # Set commit message
  3173. if message is None:
  3174. message = f"Merge commit '{merge_commit_id.decode()[:7]}'\n"
  3175. merge_commit_obj.message = message.encode() if isinstance(message, str) else message
  3176. # Add commit to object store
  3177. r.object_store.add_object(merge_commit_obj)
  3178. # Update HEAD
  3179. r.refs[b"HEAD"] = merge_commit_obj.id
  3180. return (merge_commit_obj.id, [])
  3181. def merge(
  3182. repo,
  3183. committish: Union[str, bytes, Commit, Tag],
  3184. no_commit=False,
  3185. no_ff=False,
  3186. message=None,
  3187. author=None,
  3188. committer=None,
  3189. ):
  3190. """Merge a commit into the current branch.
  3191. Args:
  3192. repo: Repository to merge into
  3193. committish: Commit to merge
  3194. no_commit: If True, do not create a merge commit
  3195. no_ff: If True, force creation of a merge commit
  3196. message: Optional merge commit message
  3197. author: Optional author for merge commit
  3198. committer: Optional committer for merge commit
  3199. Returns:
  3200. Tuple of (merge_commit_sha, conflicts) where merge_commit_sha is None
  3201. if no_commit=True or there were conflicts
  3202. Raises:
  3203. Error: If there is no HEAD reference or commit cannot be found
  3204. """
  3205. with open_repo_closing(repo) as r:
  3206. # Parse the commit to merge
  3207. try:
  3208. merge_commit_id = parse_commit(r, committish).id
  3209. except KeyError:
  3210. raise Error(
  3211. f"Cannot find commit '{committish.decode() if isinstance(committish, bytes) else committish}'"
  3212. )
  3213. result = _do_merge(
  3214. r, merge_commit_id, no_commit, no_ff, message, author, committer
  3215. )
  3216. # Trigger auto GC if needed
  3217. from .gc import maybe_auto_gc
  3218. maybe_auto_gc(r)
  3219. return result
  3220. def unpack_objects(pack_path, target="."):
  3221. """Unpack objects from a pack file into the repository.
  3222. Args:
  3223. pack_path: Path to the pack file to unpack
  3224. target: Path to the repository to unpack into
  3225. Returns:
  3226. Number of objects unpacked
  3227. """
  3228. from .pack import Pack
  3229. with open_repo_closing(target) as r:
  3230. pack_basename = os.path.splitext(pack_path)[0]
  3231. with Pack(pack_basename) as pack:
  3232. count = 0
  3233. for unpacked in pack.iter_unpacked():
  3234. obj = unpacked.sha_file()
  3235. r.object_store.add_object(obj)
  3236. count += 1
  3237. return count
  3238. def merge_tree(
  3239. repo,
  3240. base_tree: Optional[Union[str, bytes, Tree, Commit, Tag]],
  3241. our_tree: Union[str, bytes, Tree, Commit, Tag],
  3242. their_tree: Union[str, bytes, Tree, Commit, Tag],
  3243. ):
  3244. """Perform a three-way tree merge without touching the working directory.
  3245. This is similar to git merge-tree, performing a merge at the tree level
  3246. without creating commits or updating any references.
  3247. Args:
  3248. repo: Repository containing the trees
  3249. base_tree: Tree-ish of the common ancestor (or None for no common ancestor)
  3250. our_tree: Tree-ish of our side of the merge
  3251. their_tree: Tree-ish of their side of the merge
  3252. Returns:
  3253. tuple: A tuple of (merged_tree_id, conflicts) where:
  3254. - merged_tree_id is the SHA-1 of the merged tree
  3255. - conflicts is a list of paths (as bytes) that had conflicts
  3256. Raises:
  3257. KeyError: If any of the tree-ish arguments cannot be resolved
  3258. """
  3259. from .merge import Merger
  3260. with open_repo_closing(repo) as r:
  3261. # Resolve tree-ish arguments to actual trees
  3262. base = parse_tree(r, base_tree) if base_tree else None
  3263. ours = parse_tree(r, our_tree)
  3264. theirs = parse_tree(r, their_tree)
  3265. # Perform the merge
  3266. merger = Merger(r.object_store)
  3267. merged_tree, conflicts = merger.merge_trees(base, ours, theirs)
  3268. # Add the merged tree to the object store
  3269. r.object_store.add_object(merged_tree)
  3270. return merged_tree.id, conflicts
  3271. def cherry_pick(
  3272. repo,
  3273. committish: Union[str, bytes, Commit, Tag, None],
  3274. no_commit=False,
  3275. continue_=False,
  3276. abort=False,
  3277. ):
  3278. r"""Cherry-pick a commit onto the current branch.
  3279. Args:
  3280. repo: Repository to cherry-pick into
  3281. committish: Commit to cherry-pick (can be None only when ``continue_`` or abort is True)
  3282. no_commit: If True, do not create a commit after applying changes
  3283. ``continue_``: Continue an in-progress cherry-pick after resolving conflicts
  3284. abort: Abort an in-progress cherry-pick
  3285. Returns:
  3286. The SHA of the newly created commit, or None if no_commit=True or there were conflicts
  3287. Raises:
  3288. Error: If there is no HEAD reference, commit cannot be found, or operation fails
  3289. """
  3290. from .merge import three_way_merge
  3291. # Validate that committish is provided when needed
  3292. if not (continue_ or abort) and committish is None:
  3293. raise ValueError("committish is required when not using --continue or --abort")
  3294. with open_repo_closing(repo) as r:
  3295. # Handle abort
  3296. if abort:
  3297. # Clean up any cherry-pick state
  3298. try:
  3299. os.remove(os.path.join(r.controldir(), "CHERRY_PICK_HEAD"))
  3300. except FileNotFoundError:
  3301. pass
  3302. try:
  3303. os.remove(os.path.join(r.controldir(), "MERGE_MSG"))
  3304. except FileNotFoundError:
  3305. pass
  3306. # Reset index to HEAD
  3307. r.reset_index(r[b"HEAD"].tree)
  3308. return None
  3309. # Handle continue
  3310. if continue_:
  3311. # Check if there's a cherry-pick in progress
  3312. cherry_pick_head_path = os.path.join(r.controldir(), "CHERRY_PICK_HEAD")
  3313. try:
  3314. with open(cherry_pick_head_path, "rb") as f:
  3315. cherry_pick_commit_id = f.read().strip()
  3316. cherry_pick_commit = r[cherry_pick_commit_id]
  3317. except FileNotFoundError:
  3318. raise Error("No cherry-pick in progress")
  3319. # Check for unresolved conflicts
  3320. conflicts = list(r.open_index().conflicts())
  3321. if conflicts:
  3322. raise Error("Unresolved conflicts remain")
  3323. # Create the commit
  3324. tree_id = r.open_index().commit(r.object_store)
  3325. # Read saved message if any
  3326. merge_msg_path = os.path.join(r.controldir(), "MERGE_MSG")
  3327. try:
  3328. with open(merge_msg_path, "rb") as f:
  3329. message = f.read()
  3330. except FileNotFoundError:
  3331. message = cherry_pick_commit.message
  3332. new_commit = r.do_commit(
  3333. message=message,
  3334. tree=tree_id,
  3335. author=cherry_pick_commit.author,
  3336. author_timestamp=cherry_pick_commit.author_time,
  3337. author_timezone=cherry_pick_commit.author_timezone,
  3338. )
  3339. # Clean up state files
  3340. try:
  3341. os.remove(cherry_pick_head_path)
  3342. except FileNotFoundError:
  3343. pass
  3344. try:
  3345. os.remove(merge_msg_path)
  3346. except FileNotFoundError:
  3347. pass
  3348. return new_commit
  3349. # Normal cherry-pick operation
  3350. # Get current HEAD
  3351. try:
  3352. head_commit = r[b"HEAD"]
  3353. except KeyError:
  3354. raise Error("No HEAD reference found")
  3355. # Parse the commit to cherry-pick
  3356. # committish cannot be None here due to validation above
  3357. assert committish is not None
  3358. try:
  3359. cherry_pick_commit = parse_commit(r, committish)
  3360. except KeyError:
  3361. raise Error(
  3362. f"Cannot find commit '{committish.decode() if isinstance(committish, bytes) else committish}'"
  3363. )
  3364. # Check if commit has parents
  3365. if not cherry_pick_commit.parents:
  3366. raise Error("Cannot cherry-pick root commit")
  3367. # Get parent of cherry-pick commit
  3368. parent_commit = r[cherry_pick_commit.parents[0]]
  3369. # Perform three-way merge
  3370. try:
  3371. merged_tree, conflicts = three_way_merge(
  3372. r.object_store, parent_commit, head_commit, cherry_pick_commit
  3373. )
  3374. except Exception as e:
  3375. raise Error(f"Cherry-pick failed: {e}")
  3376. # Add merged tree to object store
  3377. r.object_store.add_object(merged_tree)
  3378. # Update working tree and index
  3379. # Reset index to match merged tree
  3380. r.reset_index(merged_tree.id)
  3381. # Update working tree from the new index
  3382. update_working_tree(r, head_commit.tree, merged_tree.id)
  3383. if conflicts:
  3384. # Save state for later continuation
  3385. with open(os.path.join(r.controldir(), "CHERRY_PICK_HEAD"), "wb") as f:
  3386. f.write(cherry_pick_commit.id + b"\n")
  3387. # Save commit message
  3388. with open(os.path.join(r.controldir(), "MERGE_MSG"), "wb") as f:
  3389. f.write(cherry_pick_commit.message)
  3390. raise Error(
  3391. f"Conflicts in: {', '.join(c.decode('utf-8', 'replace') for c in conflicts)}\n"
  3392. f"Fix conflicts and run 'dulwich cherry-pick --continue'"
  3393. )
  3394. if no_commit:
  3395. return None
  3396. # Create the commit
  3397. new_commit = r.do_commit(
  3398. message=cherry_pick_commit.message,
  3399. tree=merged_tree.id,
  3400. author=cherry_pick_commit.author,
  3401. author_timestamp=cherry_pick_commit.author_time,
  3402. author_timezone=cherry_pick_commit.author_timezone,
  3403. )
  3404. return new_commit
  3405. def revert(
  3406. repo,
  3407. commits: Union[str, bytes, Commit, Tag, list[Union[str, bytes, Commit, Tag]]],
  3408. no_commit=False,
  3409. message=None,
  3410. author=None,
  3411. committer=None,
  3412. ):
  3413. """Revert one or more commits.
  3414. This creates a new commit that undoes the changes introduced by the
  3415. specified commits. Unlike reset, revert creates a new commit that
  3416. preserves history.
  3417. Args:
  3418. repo: Path to repository or repository object
  3419. commits: List of commit-ish (SHA, ref, etc.) to revert, or a single commit-ish
  3420. no_commit: If True, apply changes to index/working tree but don't commit
  3421. message: Optional commit message (default: "Revert <original subject>")
  3422. author: Optional author for revert commit
  3423. committer: Optional committer for revert commit
  3424. Returns:
  3425. SHA1 of the new revert commit, or None if no_commit=True
  3426. Raises:
  3427. Error: If revert fails due to conflicts or other issues
  3428. """
  3429. from .merge import three_way_merge
  3430. # Normalize commits to a list
  3431. if isinstance(commits, (str, bytes, Commit, Tag)):
  3432. commits = [commits]
  3433. with open_repo_closing(repo) as r:
  3434. # Convert string refs to bytes
  3435. commits_to_revert = []
  3436. for commit_ref in commits:
  3437. if isinstance(commit_ref, str):
  3438. commit_ref = commit_ref.encode("utf-8")
  3439. commit = parse_commit(r, commit_ref)
  3440. commits_to_revert.append(commit)
  3441. # Get current HEAD
  3442. try:
  3443. head_commit_id = r.refs[b"HEAD"]
  3444. except KeyError:
  3445. raise Error("No HEAD reference found")
  3446. head_commit = r[head_commit_id]
  3447. current_tree = head_commit.tree
  3448. # Process commits in order
  3449. for commit_to_revert in commits_to_revert:
  3450. # For revert, we want to apply the inverse of the commit
  3451. # This means using the commit's tree as "base" and its parent as "theirs"
  3452. if not commit_to_revert.parents:
  3453. raise Error(
  3454. f"Cannot revert commit {commit_to_revert.id.decode() if isinstance(commit_to_revert.id, bytes) else commit_to_revert.id} - it has no parents"
  3455. )
  3456. # For simplicity, we only handle commits with one parent (no merge commits)
  3457. if len(commit_to_revert.parents) > 1:
  3458. raise Error(
  3459. f"Cannot revert merge commit {commit_to_revert.id.decode() if isinstance(commit_to_revert.id, bytes) else commit_to_revert.id} - not yet implemented"
  3460. )
  3461. parent_commit = r[commit_to_revert.parents[0]]
  3462. # Perform three-way merge:
  3463. # - base: the commit we're reverting (what we want to remove)
  3464. # - ours: current HEAD (what we have now)
  3465. # - theirs: parent of commit being reverted (what we want to go back to)
  3466. merged_tree, conflicts = three_way_merge(
  3467. r.object_store,
  3468. commit_to_revert, # base
  3469. r[head_commit_id], # ours
  3470. parent_commit, # theirs
  3471. )
  3472. if conflicts:
  3473. # Update working tree with conflicts
  3474. update_working_tree(r, current_tree, merged_tree.id)
  3475. conflicted_paths = [c.decode("utf-8", "replace") for c in conflicts]
  3476. raise Error(f"Conflicts while reverting: {', '.join(conflicted_paths)}")
  3477. # Add merged tree to object store
  3478. r.object_store.add_object(merged_tree)
  3479. # Update working tree
  3480. update_working_tree(r, current_tree, merged_tree.id)
  3481. current_tree = merged_tree.id
  3482. if not no_commit:
  3483. # Create revert commit
  3484. revert_commit = Commit()
  3485. revert_commit.tree = merged_tree.id
  3486. revert_commit.parents = [head_commit_id]
  3487. # Set author/committer
  3488. if author is None:
  3489. author = get_user_identity(r.get_config_stack())
  3490. if committer is None:
  3491. committer = author
  3492. revert_commit.author = author
  3493. revert_commit.committer = committer
  3494. # Set timestamps
  3495. timestamp = int(time.time())
  3496. timezone = 0 # UTC
  3497. revert_commit.author_time = timestamp
  3498. revert_commit.author_timezone = timezone
  3499. revert_commit.commit_time = timestamp
  3500. revert_commit.commit_timezone = timezone
  3501. # Set message
  3502. if message is None:
  3503. # Extract original commit subject
  3504. original_message = commit_to_revert.message
  3505. if isinstance(original_message, bytes):
  3506. original_message = original_message.decode("utf-8", "replace")
  3507. subject = original_message.split("\n")[0]
  3508. message = f'Revert "{subject}"\n\nThis reverts commit {commit_to_revert.id.decode("ascii")}.'.encode()
  3509. elif isinstance(message, str):
  3510. message = message.encode("utf-8")
  3511. revert_commit.message = message
  3512. # Add commit to object store
  3513. r.object_store.add_object(revert_commit)
  3514. # Update HEAD
  3515. r.refs[b"HEAD"] = revert_commit.id
  3516. head_commit_id = revert_commit.id
  3517. return head_commit_id if not no_commit else None
  3518. def gc(
  3519. repo,
  3520. auto: bool = False,
  3521. aggressive: bool = False,
  3522. prune: bool = True,
  3523. grace_period: Optional[int] = 1209600, # 2 weeks default
  3524. dry_run: bool = False,
  3525. progress=None,
  3526. ):
  3527. """Run garbage collection on a repository.
  3528. Args:
  3529. repo: Path to the repository or a Repo object
  3530. auto: If True, only run gc if needed
  3531. aggressive: If True, use more aggressive settings
  3532. prune: If True, prune unreachable objects
  3533. grace_period: Grace period in seconds for pruning (default 2 weeks)
  3534. dry_run: If True, only report what would be done
  3535. progress: Optional progress callback
  3536. Returns:
  3537. GCStats object with garbage collection statistics
  3538. """
  3539. from .gc import garbage_collect
  3540. with open_repo_closing(repo) as r:
  3541. return garbage_collect(
  3542. r,
  3543. auto=auto,
  3544. aggressive=aggressive,
  3545. prune=prune,
  3546. grace_period=grace_period,
  3547. dry_run=dry_run,
  3548. progress=progress,
  3549. )
  3550. def prune(
  3551. repo,
  3552. grace_period: Optional[int] = None,
  3553. dry_run: bool = False,
  3554. progress=None,
  3555. ):
  3556. """Prune/clean up a repository's object store.
  3557. This removes temporary files that were left behind by interrupted
  3558. pack operations.
  3559. Args:
  3560. repo: Path to the repository or a Repo object
  3561. grace_period: Grace period in seconds for removing temporary files
  3562. (default 2 weeks)
  3563. dry_run: If True, only report what would be done
  3564. progress: Optional progress callback
  3565. """
  3566. with open_repo_closing(repo) as r:
  3567. if progress:
  3568. progress("Pruning temporary files")
  3569. if not dry_run:
  3570. r.object_store.prune(grace_period=grace_period)
  3571. def count_objects(repo=".", verbose=False) -> CountObjectsResult:
  3572. """Count unpacked objects and their disk usage.
  3573. Args:
  3574. repo: Path to repository or repository object
  3575. verbose: Whether to return verbose information
  3576. Returns:
  3577. CountObjectsResult object with detailed statistics
  3578. """
  3579. with open_repo_closing(repo) as r:
  3580. object_store = r.object_store
  3581. # Count loose objects
  3582. loose_count = 0
  3583. loose_size = 0
  3584. for sha in object_store._iter_loose_objects():
  3585. loose_count += 1
  3586. path = object_store._get_shafile_path(sha)
  3587. try:
  3588. stat_info = os.stat(path)
  3589. # Git uses disk usage, not file size. st_blocks is always in
  3590. # 512-byte blocks per POSIX standard
  3591. if hasattr(stat_info, "st_blocks"):
  3592. # Available on Linux and macOS
  3593. loose_size += stat_info.st_blocks * 512 # type: ignore
  3594. else:
  3595. # Fallback for Windows
  3596. loose_size += stat_info.st_size
  3597. except FileNotFoundError:
  3598. # Object may have been removed between iteration and stat
  3599. pass
  3600. if not verbose:
  3601. return CountObjectsResult(count=loose_count, size=loose_size)
  3602. # Count pack information
  3603. pack_count = len(object_store.packs)
  3604. in_pack_count = 0
  3605. pack_size = 0
  3606. for pack in object_store.packs:
  3607. in_pack_count += len(pack)
  3608. # Get pack file size
  3609. pack_path = pack._data_path
  3610. try:
  3611. pack_size += os.path.getsize(pack_path)
  3612. except FileNotFoundError:
  3613. pass
  3614. # Get index file size
  3615. idx_path = pack._idx_path
  3616. try:
  3617. pack_size += os.path.getsize(idx_path)
  3618. except FileNotFoundError:
  3619. pass
  3620. return CountObjectsResult(
  3621. count=loose_count,
  3622. size=loose_size,
  3623. in_pack=in_pack_count,
  3624. packs=pack_count,
  3625. size_pack=pack_size,
  3626. )
  3627. def rebase(
  3628. repo: Union[Repo, str],
  3629. upstream: Union[bytes, str],
  3630. onto: Optional[Union[bytes, str]] = None,
  3631. branch: Optional[Union[bytes, str]] = None,
  3632. abort: bool = False,
  3633. continue_rebase: bool = False,
  3634. skip: bool = False,
  3635. ) -> list[bytes]:
  3636. """Rebase commits onto another branch.
  3637. Args:
  3638. repo: Repository to rebase in
  3639. upstream: Upstream branch/commit to rebase onto
  3640. onto: Specific commit to rebase onto (defaults to upstream)
  3641. branch: Branch to rebase (defaults to current branch)
  3642. abort: Abort an in-progress rebase
  3643. continue_rebase: Continue an in-progress rebase
  3644. skip: Skip current commit and continue rebase
  3645. Returns:
  3646. List of new commit SHAs created by rebase
  3647. Raises:
  3648. Error: If rebase fails or conflicts occur
  3649. """
  3650. from .rebase import RebaseConflict, RebaseError, Rebaser
  3651. with open_repo_closing(repo) as r:
  3652. rebaser = Rebaser(r)
  3653. if abort:
  3654. try:
  3655. rebaser.abort()
  3656. return []
  3657. except RebaseError as e:
  3658. raise Error(str(e))
  3659. if continue_rebase:
  3660. try:
  3661. result = rebaser.continue_()
  3662. if result is None:
  3663. # Rebase complete
  3664. return []
  3665. elif isinstance(result, tuple) and result[1]:
  3666. # Still have conflicts
  3667. raise Error(
  3668. f"Conflicts in: {', '.join(f.decode('utf-8', 'replace') for f in result[1])}"
  3669. )
  3670. except RebaseError as e:
  3671. raise Error(str(e))
  3672. # Convert string refs to bytes
  3673. if isinstance(upstream, str):
  3674. upstream = upstream.encode("utf-8")
  3675. if isinstance(onto, str):
  3676. onto = onto.encode("utf-8") if onto else None
  3677. if isinstance(branch, str):
  3678. branch = branch.encode("utf-8") if branch else None
  3679. try:
  3680. # Start rebase
  3681. rebaser.start(upstream, onto, branch)
  3682. # Continue rebase automatically
  3683. result = rebaser.continue_()
  3684. if result is not None:
  3685. # Conflicts
  3686. raise RebaseConflict(result[1])
  3687. # Return the SHAs of the rebased commits
  3688. return [c.id for c in rebaser._done]
  3689. except RebaseConflict as e:
  3690. raise Error(str(e))
  3691. except RebaseError as e:
  3692. raise Error(str(e))
  3693. def annotate(repo, path, committish: Optional[Union[str, bytes, Commit, Tag]] = None):
  3694. """Annotate the history of a file.
  3695. :param repo: Path to the repository
  3696. :param path: Path to annotate
  3697. :param committish: Commit id to find path in
  3698. :return: List of ((Commit, TreeChange), line) tuples
  3699. """
  3700. if committish is None:
  3701. committish = "HEAD"
  3702. from dulwich.annotate import annotate_lines
  3703. with open_repo_closing(repo) as r:
  3704. commit_id = parse_commit(r, committish).id
  3705. # Ensure path is bytes
  3706. if isinstance(path, str):
  3707. path = path.encode()
  3708. return annotate_lines(r.object_store, commit_id, path)
  3709. blame = annotate
  3710. def filter_branch(
  3711. repo=".",
  3712. branch="HEAD",
  3713. *,
  3714. filter_fn=None,
  3715. filter_author=None,
  3716. filter_committer=None,
  3717. filter_message=None,
  3718. tree_filter=None,
  3719. index_filter=None,
  3720. parent_filter=None,
  3721. commit_filter=None,
  3722. subdirectory_filter=None,
  3723. prune_empty=False,
  3724. tag_name_filter=None,
  3725. force=False,
  3726. keep_original=True,
  3727. refs=None,
  3728. ):
  3729. """Rewrite branch history by creating new commits with filtered properties.
  3730. This is similar to git filter-branch, allowing you to rewrite commit
  3731. history by modifying trees, parents, author, committer, or commit messages.
  3732. Args:
  3733. repo: Path to repository
  3734. branch: Branch to rewrite (defaults to HEAD)
  3735. filter_fn: Optional callable that takes a Commit object and returns
  3736. a dict of updated fields (author, committer, message, etc.)
  3737. filter_author: Optional callable that takes author bytes and returns
  3738. updated author bytes or None to keep unchanged
  3739. filter_committer: Optional callable that takes committer bytes and returns
  3740. updated committer bytes or None to keep unchanged
  3741. filter_message: Optional callable that takes commit message bytes
  3742. and returns updated message bytes
  3743. tree_filter: Optional callable that takes (tree_sha, temp_dir) and returns
  3744. new tree SHA after modifying working directory
  3745. index_filter: Optional callable that takes (tree_sha, temp_index_path) and
  3746. returns new tree SHA after modifying index
  3747. parent_filter: Optional callable that takes parent list and returns
  3748. modified parent list
  3749. commit_filter: Optional callable that takes (Commit, tree_sha) and returns
  3750. new commit SHA or None to skip commit
  3751. subdirectory_filter: Optional subdirectory path to extract as new root
  3752. prune_empty: Whether to prune commits that become empty
  3753. tag_name_filter: Optional callable to rename tags
  3754. force: Force operation even if branch has been filtered before
  3755. keep_original: Keep original refs under refs/original/
  3756. refs: List of refs to rewrite (defaults to [branch])
  3757. Returns:
  3758. Dict mapping old commit SHAs to new commit SHAs
  3759. Raises:
  3760. Error: If branch is already filtered and force is False
  3761. """
  3762. from .filter_branch import CommitFilter, filter_refs
  3763. with open_repo_closing(repo) as r:
  3764. # Parse branch/committish
  3765. if isinstance(branch, str):
  3766. branch = branch.encode()
  3767. # Determine which refs to process
  3768. if refs is None:
  3769. if branch == b"HEAD":
  3770. # Resolve HEAD to actual branch
  3771. try:
  3772. resolved = r.refs.follow(b"HEAD")
  3773. if resolved and resolved[0]:
  3774. # resolved is a list of (refname, sha) tuples
  3775. resolved_ref = resolved[0][-1]
  3776. if resolved_ref and resolved_ref != b"HEAD":
  3777. refs = [resolved_ref]
  3778. else:
  3779. # HEAD points directly to a commit
  3780. refs = [b"HEAD"]
  3781. else:
  3782. refs = [b"HEAD"]
  3783. except SymrefLoop:
  3784. refs = [b"HEAD"]
  3785. else:
  3786. # Convert branch name to full ref if needed
  3787. if not branch.startswith(b"refs/"):
  3788. branch = b"refs/heads/" + branch
  3789. refs = [branch]
  3790. # Convert subdirectory filter to bytes if needed
  3791. if subdirectory_filter and isinstance(subdirectory_filter, str):
  3792. subdirectory_filter = subdirectory_filter.encode()
  3793. # Create commit filter
  3794. commit_filter = CommitFilter(
  3795. r.object_store,
  3796. filter_fn=filter_fn,
  3797. filter_author=filter_author,
  3798. filter_committer=filter_committer,
  3799. filter_message=filter_message,
  3800. tree_filter=tree_filter,
  3801. index_filter=index_filter,
  3802. parent_filter=parent_filter,
  3803. commit_filter=commit_filter,
  3804. subdirectory_filter=subdirectory_filter,
  3805. prune_empty=prune_empty,
  3806. tag_name_filter=tag_name_filter,
  3807. )
  3808. # Tag callback for renaming tags
  3809. def rename_tag(old_ref, new_ref):
  3810. # Copy tag to new name
  3811. r.refs[new_ref] = r.refs[old_ref]
  3812. # Delete old tag
  3813. del r.refs[old_ref]
  3814. # Filter refs
  3815. try:
  3816. return filter_refs(
  3817. r.refs,
  3818. r.object_store,
  3819. refs,
  3820. commit_filter,
  3821. keep_original=keep_original,
  3822. force=force,
  3823. tag_callback=rename_tag if tag_name_filter else None,
  3824. )
  3825. except ValueError as e:
  3826. raise Error(str(e)) from e
  3827. def format_patch(
  3828. repo=".",
  3829. committish=None,
  3830. outstream=sys.stdout,
  3831. outdir=None,
  3832. n=1,
  3833. stdout=False,
  3834. version=None,
  3835. ) -> list[str]:
  3836. """Generate patches suitable for git am.
  3837. Args:
  3838. repo: Path to repository
  3839. committish: Commit-ish or commit range to generate patches for.
  3840. Can be a single commit id, or a tuple of (start, end) commit ids
  3841. for a range. If None, formats the last n commits from HEAD.
  3842. outstream: Stream to write to if stdout=True
  3843. outdir: Directory to write patch files to (default: current directory)
  3844. n: Number of patches to generate if committish is None
  3845. stdout: Write patches to stdout instead of files
  3846. version: Version string to include in patches (default: Dulwich version)
  3847. Returns:
  3848. List of patch filenames that were created (empty if stdout=True)
  3849. """
  3850. if outdir is None:
  3851. outdir = "."
  3852. filenames = []
  3853. with open_repo_closing(repo) as r:
  3854. # Determine which commits to format
  3855. commits_to_format = []
  3856. if committish is None:
  3857. # Get the last n commits from HEAD
  3858. try:
  3859. walker = r.get_walker()
  3860. for entry in walker:
  3861. commits_to_format.append(entry.commit)
  3862. if len(commits_to_format) >= n:
  3863. break
  3864. commits_to_format.reverse()
  3865. except KeyError:
  3866. # No HEAD or empty repository
  3867. pass
  3868. elif isinstance(committish, tuple):
  3869. # Handle commit range (start, end)
  3870. start_id, end_id = committish
  3871. # Walk from end back to start
  3872. walker = r.get_walker(include=[end_id], exclude=[start_id])
  3873. for entry in walker:
  3874. commits_to_format.append(entry.commit)
  3875. commits_to_format.reverse()
  3876. else:
  3877. # Single commit
  3878. commit = r.object_store[committish]
  3879. commits_to_format.append(commit)
  3880. # Generate patches
  3881. total = len(commits_to_format)
  3882. for i, commit in enumerate(commits_to_format, 1):
  3883. # Get the parent
  3884. if commit.parents:
  3885. parent_id = commit.parents[0]
  3886. parent = r.object_store[parent_id]
  3887. else:
  3888. parent = None
  3889. # Generate the diff
  3890. from io import BytesIO
  3891. diff_content = BytesIO()
  3892. if parent:
  3893. write_tree_diff(
  3894. diff_content,
  3895. r.object_store,
  3896. parent.tree,
  3897. commit.tree,
  3898. )
  3899. else:
  3900. # Initial commit - diff against empty tree
  3901. write_tree_diff(
  3902. diff_content,
  3903. r.object_store,
  3904. None,
  3905. commit.tree,
  3906. )
  3907. # Generate patch with commit metadata
  3908. if stdout:
  3909. write_commit_patch(
  3910. outstream.buffer if hasattr(outstream, "buffer") else outstream,
  3911. commit,
  3912. diff_content.getvalue(),
  3913. (i, total),
  3914. version=version,
  3915. )
  3916. else:
  3917. # Generate filename
  3918. summary = get_summary(commit)
  3919. filename = os.path.join(outdir, f"{i:04d}-{summary}.patch")
  3920. with open(filename, "wb") as f:
  3921. write_commit_patch(
  3922. f,
  3923. commit,
  3924. diff_content.getvalue(),
  3925. (i, total),
  3926. version=version,
  3927. )
  3928. filenames.append(filename)
  3929. return filenames
  3930. def bisect_start(
  3931. repo=".",
  3932. bad: Optional[Union[str, bytes, Commit, Tag]] = None,
  3933. good: Optional[
  3934. Union[str, bytes, Commit, Tag, list[Union[str, bytes, Commit, Tag]]]
  3935. ] = None,
  3936. paths=None,
  3937. no_checkout=False,
  3938. term_bad="bad",
  3939. term_good="good",
  3940. ):
  3941. """Start a new bisect session.
  3942. Args:
  3943. repo: Path to repository or a Repo object
  3944. bad: The bad commit (defaults to HEAD)
  3945. good: List of good commits or a single good commit
  3946. paths: Optional paths to limit bisect to
  3947. no_checkout: If True, don't checkout commits during bisect
  3948. term_bad: Term to use for bad commits (default: "bad")
  3949. term_good: Term to use for good commits (default: "good")
  3950. """
  3951. with open_repo_closing(repo) as r:
  3952. state = BisectState(r)
  3953. # Convert single good commit to list
  3954. if good is not None and not isinstance(good, list):
  3955. good = [good]
  3956. # Parse commits
  3957. bad_sha = parse_commit(r, bad).id if bad else None
  3958. good_shas = [parse_commit(r, g).id for g in good] if good else None
  3959. state.start(bad_sha, good_shas, paths, no_checkout, term_bad, term_good)
  3960. # Return the next commit to test if we have both good and bad
  3961. if bad_sha and good_shas:
  3962. next_sha = state._find_next_commit()
  3963. if next_sha and not no_checkout:
  3964. # Checkout the next commit
  3965. old_tree = r[r.head()].tree if r.head() else None
  3966. r.refs[b"HEAD"] = next_sha
  3967. commit = r[next_sha]
  3968. update_working_tree(r, old_tree, commit.tree)
  3969. return next_sha
  3970. def bisect_bad(repo=".", rev: Optional[Union[str, bytes, Commit, Tag]] = None):
  3971. """Mark a commit as bad.
  3972. Args:
  3973. repo: Path to repository or a Repo object
  3974. rev: Commit to mark as bad (defaults to HEAD)
  3975. Returns:
  3976. The SHA of the next commit to test, or None if bisect is complete
  3977. """
  3978. with open_repo_closing(repo) as r:
  3979. state = BisectState(r)
  3980. rev_sha = parse_commit(r, rev).id if rev else None
  3981. next_sha = state.mark_bad(rev_sha)
  3982. if next_sha:
  3983. # Checkout the next commit
  3984. old_tree = r[r.head()].tree if r.head() else None
  3985. r.refs[b"HEAD"] = next_sha
  3986. commit = r[next_sha]
  3987. update_working_tree(r, old_tree, commit.tree)
  3988. return next_sha
  3989. def bisect_good(repo=".", rev: Optional[Union[str, bytes, Commit, Tag]] = None):
  3990. """Mark a commit as good.
  3991. Args:
  3992. repo: Path to repository or a Repo object
  3993. rev: Commit to mark as good (defaults to HEAD)
  3994. Returns:
  3995. The SHA of the next commit to test, or None if bisect is complete
  3996. """
  3997. with open_repo_closing(repo) as r:
  3998. state = BisectState(r)
  3999. rev_sha = parse_commit(r, rev).id if rev else None
  4000. next_sha = state.mark_good(rev_sha)
  4001. if next_sha:
  4002. # Checkout the next commit
  4003. old_tree = r[r.head()].tree if r.head() else None
  4004. r.refs[b"HEAD"] = next_sha
  4005. commit = r[next_sha]
  4006. update_working_tree(r, old_tree, commit.tree)
  4007. return next_sha
  4008. def bisect_skip(
  4009. repo=".",
  4010. revs: Optional[
  4011. Union[str, bytes, Commit, Tag, list[Union[str, bytes, Commit, Tag]]]
  4012. ] = None,
  4013. ):
  4014. """Skip one or more commits.
  4015. Args:
  4016. repo: Path to repository or a Repo object
  4017. revs: List of commits to skip (defaults to [HEAD])
  4018. Returns:
  4019. The SHA of the next commit to test, or None if bisect is complete
  4020. """
  4021. with open_repo_closing(repo) as r:
  4022. state = BisectState(r)
  4023. if revs is None:
  4024. rev_shas = None
  4025. else:
  4026. # Convert single rev to list
  4027. if not isinstance(revs, list):
  4028. revs = [revs]
  4029. rev_shas = [parse_commit(r, rev).id for rev in revs]
  4030. next_sha = state.skip(rev_shas)
  4031. if next_sha:
  4032. # Checkout the next commit
  4033. old_tree = r[r.head()].tree if r.head() else None
  4034. r.refs[b"HEAD"] = next_sha
  4035. commit = r[next_sha]
  4036. update_working_tree(r, old_tree, commit.tree)
  4037. return next_sha
  4038. def bisect_reset(repo=".", commit: Optional[Union[str, bytes, Commit, Tag]] = None):
  4039. """Reset bisect state and return to original branch/commit.
  4040. Args:
  4041. repo: Path to repository or a Repo object
  4042. commit: Optional commit to reset to (defaults to original branch/commit)
  4043. """
  4044. with open_repo_closing(repo) as r:
  4045. state = BisectState(r)
  4046. # Get old tree before reset
  4047. try:
  4048. old_tree = r[r.head()].tree
  4049. except KeyError:
  4050. old_tree = None
  4051. commit_sha = parse_commit(r, commit).id if commit else None
  4052. state.reset(commit_sha)
  4053. # Update working tree to new HEAD
  4054. try:
  4055. new_head = r.head()
  4056. if new_head:
  4057. new_commit = r[new_head]
  4058. update_working_tree(r, old_tree, new_commit.tree)
  4059. except KeyError:
  4060. # No HEAD after reset
  4061. pass
  4062. def bisect_log(repo="."):
  4063. """Get the bisect log.
  4064. Args:
  4065. repo: Path to repository or a Repo object
  4066. Returns:
  4067. The bisect log as a string
  4068. """
  4069. with open_repo_closing(repo) as r:
  4070. state = BisectState(r)
  4071. return state.get_log()
  4072. def bisect_replay(repo, log_file):
  4073. """Replay a bisect log.
  4074. Args:
  4075. repo: Path to repository or a Repo object
  4076. log_file: Path to the log file or file-like object
  4077. """
  4078. with open_repo_closing(repo) as r:
  4079. state = BisectState(r)
  4080. if isinstance(log_file, str):
  4081. with open(log_file) as f:
  4082. log_content = f.read()
  4083. else:
  4084. log_content = log_file.read()
  4085. state.replay(log_content)
  4086. def reflog(repo=".", ref=b"HEAD", all=False):
  4087. """Show reflog entries for a reference or all references.
  4088. Args:
  4089. repo: Path to repository or a Repo object
  4090. ref: Reference name (defaults to HEAD)
  4091. all: If True, show reflogs for all refs (ignores ref parameter)
  4092. Yields:
  4093. If all=False: ReflogEntry objects
  4094. If all=True: Tuples of (ref_name, ReflogEntry) for all refs with reflogs
  4095. """
  4096. import os
  4097. from .reflog import iter_reflogs
  4098. if isinstance(ref, str):
  4099. ref = ref.encode("utf-8")
  4100. with open_repo_closing(repo) as r:
  4101. if not all:
  4102. yield from r.read_reflog(ref)
  4103. else:
  4104. logs_dir = os.path.join(r.controldir(), "logs")
  4105. # Use iter_reflogs to discover all reflogs
  4106. for ref_bytes in iter_reflogs(logs_dir):
  4107. # Read the reflog entries for this ref
  4108. for entry in r.read_reflog(ref_bytes):
  4109. yield (ref_bytes, entry)
  4110. def lfs_track(repo=".", patterns=None):
  4111. """Track file patterns with Git LFS.
  4112. Args:
  4113. repo: Path to repository
  4114. patterns: List of file patterns to track (e.g., ["*.bin", "*.pdf"])
  4115. If None, returns current tracked patterns
  4116. Returns:
  4117. List of tracked patterns
  4118. """
  4119. from .attrs import GitAttributes
  4120. with open_repo_closing(repo) as r:
  4121. gitattributes_path = os.path.join(r.path, ".gitattributes")
  4122. # Load existing GitAttributes
  4123. if os.path.exists(gitattributes_path):
  4124. gitattributes = GitAttributes.from_file(gitattributes_path)
  4125. else:
  4126. gitattributes = GitAttributes()
  4127. if patterns is None:
  4128. # Return current LFS tracked patterns
  4129. tracked = []
  4130. for pattern_obj, attrs in gitattributes:
  4131. if attrs.get(b"filter") == b"lfs":
  4132. tracked.append(pattern_obj.pattern.decode())
  4133. return tracked
  4134. # Add new patterns
  4135. for pattern in patterns:
  4136. # Ensure pattern is bytes
  4137. if isinstance(pattern, str):
  4138. pattern = pattern.encode()
  4139. # Set LFS attributes for the pattern
  4140. gitattributes.set_attribute(pattern, b"filter", b"lfs")
  4141. gitattributes.set_attribute(pattern, b"diff", b"lfs")
  4142. gitattributes.set_attribute(pattern, b"merge", b"lfs")
  4143. gitattributes.set_attribute(pattern, b"text", False)
  4144. # Write updated attributes
  4145. gitattributes.write_to_file(gitattributes_path)
  4146. # Stage the .gitattributes file
  4147. add(r, [".gitattributes"])
  4148. return lfs_track(r) # Return updated list
  4149. def lfs_untrack(repo=".", patterns=None):
  4150. """Untrack file patterns from Git LFS.
  4151. Args:
  4152. repo: Path to repository
  4153. patterns: List of file patterns to untrack
  4154. Returns:
  4155. List of remaining tracked patterns
  4156. """
  4157. from .attrs import GitAttributes
  4158. if not patterns:
  4159. return lfs_track(repo)
  4160. with open_repo_closing(repo) as r:
  4161. gitattributes_path = os.path.join(r.path, ".gitattributes")
  4162. if not os.path.exists(gitattributes_path):
  4163. return []
  4164. # Load existing GitAttributes
  4165. gitattributes = GitAttributes.from_file(gitattributes_path)
  4166. # Remove specified patterns
  4167. for pattern in patterns:
  4168. if isinstance(pattern, str):
  4169. pattern = pattern.encode()
  4170. # Check if pattern is tracked by LFS
  4171. for pattern_obj, attrs in list(gitattributes):
  4172. if pattern_obj.pattern == pattern and attrs.get(b"filter") == b"lfs":
  4173. gitattributes.remove_pattern(pattern)
  4174. break
  4175. # Write updated attributes
  4176. gitattributes.write_to_file(gitattributes_path)
  4177. # Stage the .gitattributes file
  4178. add(r, [".gitattributes"])
  4179. return lfs_track(r) # Return updated list
  4180. def lfs_init(repo="."):
  4181. """Initialize Git LFS in a repository.
  4182. Args:
  4183. repo: Path to repository
  4184. Returns:
  4185. None
  4186. """
  4187. from .lfs import LFSStore
  4188. with open_repo_closing(repo) as r:
  4189. # Create LFS store
  4190. LFSStore.from_repo(r, create=True)
  4191. # Set up Git config for LFS
  4192. config = r.get_config()
  4193. config.set((b"filter", b"lfs"), b"process", b"git-lfs filter-process")
  4194. config.set((b"filter", b"lfs"), b"required", b"true")
  4195. config.set((b"filter", b"lfs"), b"clean", b"git-lfs clean -- %f")
  4196. config.set((b"filter", b"lfs"), b"smudge", b"git-lfs smudge -- %f")
  4197. config.write_to_path()
  4198. def lfs_clean(repo=".", path=None):
  4199. """Clean a file by converting it to an LFS pointer.
  4200. Args:
  4201. repo: Path to repository
  4202. path: Path to file to clean (relative to repo root)
  4203. Returns:
  4204. LFS pointer content as bytes
  4205. """
  4206. from .lfs import LFSFilterDriver, LFSStore
  4207. with open_repo_closing(repo) as r:
  4208. if path is None:
  4209. raise ValueError("Path must be specified")
  4210. # Get LFS store
  4211. lfs_store = LFSStore.from_repo(r)
  4212. filter_driver = LFSFilterDriver(lfs_store)
  4213. # Read file content
  4214. full_path = os.path.join(r.path, path)
  4215. with open(full_path, "rb") as f:
  4216. content = f.read()
  4217. # Clean the content (convert to LFS pointer)
  4218. return filter_driver.clean(content)
  4219. def lfs_smudge(repo=".", pointer_content=None):
  4220. """Smudge an LFS pointer by retrieving the actual content.
  4221. Args:
  4222. repo: Path to repository
  4223. pointer_content: LFS pointer content as bytes
  4224. Returns:
  4225. Actual file content as bytes
  4226. """
  4227. from .lfs import LFSFilterDriver, LFSStore
  4228. with open_repo_closing(repo) as r:
  4229. if pointer_content is None:
  4230. raise ValueError("Pointer content must be specified")
  4231. # Get LFS store
  4232. lfs_store = LFSStore.from_repo(r)
  4233. filter_driver = LFSFilterDriver(lfs_store)
  4234. # Smudge the pointer (retrieve actual content)
  4235. return filter_driver.smudge(pointer_content)
  4236. def lfs_ls_files(repo=".", ref=None):
  4237. """List files tracked by Git LFS.
  4238. Args:
  4239. repo: Path to repository
  4240. ref: Git ref to check (defaults to HEAD)
  4241. Returns:
  4242. List of (path, oid, size) tuples for LFS files
  4243. """
  4244. from .lfs import LFSPointer
  4245. from .object_store import iter_tree_contents
  4246. with open_repo_closing(repo) as r:
  4247. if ref is None:
  4248. ref = b"HEAD"
  4249. elif isinstance(ref, str):
  4250. ref = ref.encode()
  4251. # Get the commit and tree
  4252. try:
  4253. commit = r[ref]
  4254. tree = r[commit.tree]
  4255. except KeyError:
  4256. return []
  4257. lfs_files = []
  4258. # Walk the tree
  4259. for path, mode, sha in iter_tree_contents(r.object_store, tree.id):
  4260. if not stat.S_ISREG(mode):
  4261. continue
  4262. # Check if it's an LFS pointer
  4263. obj = r.object_store[sha]
  4264. pointer = LFSPointer.from_bytes(obj.data)
  4265. if pointer is not None:
  4266. lfs_files.append((path.decode(), pointer.oid, pointer.size))
  4267. return lfs_files
  4268. def lfs_migrate(repo=".", include=None, exclude=None, everything=False):
  4269. """Migrate files to Git LFS.
  4270. Args:
  4271. repo: Path to repository
  4272. include: Patterns of files to include
  4273. exclude: Patterns of files to exclude
  4274. everything: Migrate all files above a certain size
  4275. Returns:
  4276. Number of migrated files
  4277. """
  4278. from .lfs import LFSFilterDriver, LFSStore
  4279. with open_repo_closing(repo) as r:
  4280. # Initialize LFS if needed
  4281. lfs_store = LFSStore.from_repo(r, create=True)
  4282. filter_driver = LFSFilterDriver(lfs_store)
  4283. # Get current index
  4284. index = r.open_index()
  4285. migrated = 0
  4286. # Determine files to migrate
  4287. files_to_migrate = []
  4288. if everything:
  4289. # Migrate all files above 100MB
  4290. for path, entry in index.items():
  4291. full_path = os.path.join(r.path, path.decode())
  4292. if os.path.exists(full_path):
  4293. size = os.path.getsize(full_path)
  4294. if size > 100 * 1024 * 1024: # 100MB
  4295. files_to_migrate.append(path.decode())
  4296. else:
  4297. # Use include/exclude patterns
  4298. for path, entry in index.items():
  4299. path_str = path.decode()
  4300. # Check include patterns
  4301. if include:
  4302. matched = any(
  4303. fnmatch.fnmatch(path_str, pattern) for pattern in include
  4304. )
  4305. if not matched:
  4306. continue
  4307. # Check exclude patterns
  4308. if exclude:
  4309. excluded = any(
  4310. fnmatch.fnmatch(path_str, pattern) for pattern in exclude
  4311. )
  4312. if excluded:
  4313. continue
  4314. files_to_migrate.append(path_str)
  4315. # Migrate files
  4316. for path in files_to_migrate:
  4317. full_path = os.path.join(r.path, path)
  4318. if not os.path.exists(full_path):
  4319. continue
  4320. # Read file content
  4321. with open(full_path, "rb") as f:
  4322. content = f.read()
  4323. # Convert to LFS pointer
  4324. pointer_content = filter_driver.clean(content)
  4325. # Write pointer back to file
  4326. with open(full_path, "wb") as f:
  4327. f.write(pointer_content)
  4328. # Create blob for pointer content and update index
  4329. blob = Blob()
  4330. blob.data = pointer_content
  4331. r.object_store.add_object(blob)
  4332. st = os.stat(full_path)
  4333. index_entry = index_entry_from_stat(st, blob.id, 0)
  4334. index[path.encode()] = index_entry
  4335. migrated += 1
  4336. # Write updated index
  4337. index.write()
  4338. # Track patterns if include was specified
  4339. if include:
  4340. lfs_track(r, include)
  4341. return migrated
  4342. def lfs_pointer_check(repo=".", paths=None):
  4343. """Check if files are valid LFS pointers.
  4344. Args:
  4345. repo: Path to repository
  4346. paths: List of file paths to check (if None, check all files)
  4347. Returns:
  4348. Dict mapping paths to LFSPointer objects (or None if not a pointer)
  4349. """
  4350. from .lfs import LFSPointer
  4351. with open_repo_closing(repo) as r:
  4352. results = {}
  4353. if paths is None:
  4354. # Check all files in index
  4355. index = r.open_index()
  4356. paths = [path.decode() for path in index]
  4357. for path in paths:
  4358. full_path = os.path.join(r.path, path)
  4359. if os.path.exists(full_path):
  4360. try:
  4361. with open(full_path, "rb") as f:
  4362. content = f.read()
  4363. pointer = LFSPointer.from_bytes(content)
  4364. results[path] = pointer
  4365. except OSError:
  4366. results[path] = None
  4367. else:
  4368. results[path] = None
  4369. return results
  4370. def lfs_fetch(repo=".", remote="origin", refs=None):
  4371. """Fetch LFS objects from remote.
  4372. Args:
  4373. repo: Path to repository
  4374. remote: Remote name (default: origin)
  4375. refs: Specific refs to fetch LFS objects for (default: all refs)
  4376. Returns:
  4377. Number of objects fetched
  4378. """
  4379. from .lfs import LFSClient, LFSPointer, LFSStore
  4380. with open_repo_closing(repo) as r:
  4381. # Get LFS server URL from config
  4382. config = r.get_config()
  4383. lfs_url = config.get((b"lfs",), b"url")
  4384. if not lfs_url:
  4385. # Try remote URL
  4386. remote_url = config.get((b"remote", remote.encode()), b"url")
  4387. if remote_url:
  4388. # Append /info/lfs to remote URL
  4389. remote_url = remote_url.decode()
  4390. if remote_url.endswith(".git"):
  4391. remote_url = remote_url[:-4]
  4392. lfs_url = f"{remote_url}/info/lfs"
  4393. else:
  4394. raise ValueError(f"No LFS URL configured for remote {remote}")
  4395. else:
  4396. lfs_url = lfs_url.decode()
  4397. # Get authentication
  4398. auth = None
  4399. # TODO: Support credential helpers and other auth methods
  4400. # Create LFS client and store
  4401. client = LFSClient(lfs_url, auth)
  4402. store = LFSStore.from_repo(r)
  4403. # Find all LFS pointers in the refs
  4404. pointers_to_fetch = []
  4405. if refs is None:
  4406. # Get all refs
  4407. refs = list(r.refs.keys())
  4408. for ref in refs:
  4409. if isinstance(ref, str):
  4410. ref = ref.encode()
  4411. try:
  4412. commit = r[r.refs[ref]]
  4413. except KeyError:
  4414. continue
  4415. # Walk the commit tree
  4416. for entry in r.object_store.iter_tree_contents(commit.tree):
  4417. try:
  4418. obj = r.object_store[entry.sha]
  4419. if obj.type_name == b"blob":
  4420. pointer = LFSPointer.from_bytes(obj.data)
  4421. if pointer and pointer.is_valid_oid():
  4422. # Check if we already have it
  4423. try:
  4424. store.open_object(pointer.oid)
  4425. except KeyError:
  4426. pointers_to_fetch.append((pointer.oid, pointer.size))
  4427. except KeyError:
  4428. pass
  4429. # Fetch missing objects
  4430. fetched = 0
  4431. for oid, size in pointers_to_fetch:
  4432. try:
  4433. content = client.download(oid, size)
  4434. store.write_object([content])
  4435. fetched += 1
  4436. except Exception as e:
  4437. # Log error but continue
  4438. print(f"Failed to fetch {oid}: {e}")
  4439. return fetched
  4440. def lfs_pull(repo=".", remote="origin"):
  4441. """Pull LFS objects for current checkout.
  4442. Args:
  4443. repo: Path to repository
  4444. remote: Remote name (default: origin)
  4445. Returns:
  4446. Number of objects fetched
  4447. """
  4448. from .lfs import LFSPointer, LFSStore
  4449. with open_repo_closing(repo) as r:
  4450. # First do a fetch for HEAD
  4451. fetched = lfs_fetch(repo, remote, [b"HEAD"])
  4452. # Then checkout LFS files in working directory
  4453. store = LFSStore.from_repo(r)
  4454. index = r.open_index()
  4455. for path, entry in index.items():
  4456. full_path = os.path.join(r.path, path.decode())
  4457. if os.path.exists(full_path):
  4458. with open(full_path, "rb") as f:
  4459. content = f.read()
  4460. pointer = LFSPointer.from_bytes(content)
  4461. if pointer and pointer.is_valid_oid():
  4462. try:
  4463. # Replace pointer with actual content
  4464. with store.open_object(pointer.oid) as lfs_file:
  4465. lfs_content = lfs_file.read()
  4466. with open(full_path, "wb") as f:
  4467. f.write(lfs_content)
  4468. except KeyError:
  4469. # Object not available
  4470. pass
  4471. return fetched
  4472. def lfs_push(repo=".", remote="origin", refs=None):
  4473. """Push LFS objects to remote.
  4474. Args:
  4475. repo: Path to repository
  4476. remote: Remote name (default: origin)
  4477. refs: Specific refs to push LFS objects for (default: current branch)
  4478. Returns:
  4479. Number of objects pushed
  4480. """
  4481. from .lfs import LFSClient, LFSPointer, LFSStore
  4482. with open_repo_closing(repo) as r:
  4483. # Get LFS server URL from config
  4484. config = r.get_config()
  4485. lfs_url = config.get((b"lfs",), b"url")
  4486. if not lfs_url:
  4487. # Try remote URL
  4488. remote_url = config.get((b"remote", remote.encode()), b"url")
  4489. if remote_url:
  4490. # Append /info/lfs to remote URL
  4491. remote_url = remote_url.decode()
  4492. if remote_url.endswith(".git"):
  4493. remote_url = remote_url[:-4]
  4494. lfs_url = f"{remote_url}/info/lfs"
  4495. else:
  4496. raise ValueError(f"No LFS URL configured for remote {remote}")
  4497. else:
  4498. lfs_url = lfs_url.decode()
  4499. # Get authentication
  4500. auth = None
  4501. # TODO: Support credential helpers and other auth methods
  4502. # Create LFS client and store
  4503. client = LFSClient(lfs_url, auth)
  4504. store = LFSStore.from_repo(r)
  4505. # Find all LFS objects to push
  4506. if refs is None:
  4507. # Push current branch
  4508. refs = [r.refs.read_ref(b"HEAD")]
  4509. objects_to_push = set()
  4510. for ref in refs:
  4511. if isinstance(ref, str):
  4512. ref = ref.encode()
  4513. try:
  4514. if ref.startswith(b"refs/"):
  4515. commit = r[r.refs[ref]]
  4516. else:
  4517. commit = r[ref]
  4518. except KeyError:
  4519. continue
  4520. # Walk the commit tree
  4521. for entry in r.object_store.iter_tree_contents(commit.tree):
  4522. try:
  4523. obj = r.object_store[entry.sha]
  4524. if obj.type_name == b"blob":
  4525. pointer = LFSPointer.from_bytes(obj.data)
  4526. if pointer and pointer.is_valid_oid():
  4527. objects_to_push.add((pointer.oid, pointer.size))
  4528. except KeyError:
  4529. pass
  4530. # Push objects
  4531. pushed = 0
  4532. for oid, size in objects_to_push:
  4533. try:
  4534. with store.open_object(oid) as f:
  4535. content = f.read()
  4536. client.upload(oid, size, content)
  4537. pushed += 1
  4538. except KeyError:
  4539. # Object not in local store
  4540. print(f"Warning: LFS object {oid} not found locally")
  4541. except Exception as e:
  4542. # Log error but continue
  4543. print(f"Failed to push {oid}: {e}")
  4544. return pushed
  4545. def lfs_status(repo="."):
  4546. """Show status of LFS files.
  4547. Args:
  4548. repo: Path to repository
  4549. Returns:
  4550. Dict with status information
  4551. """
  4552. from .lfs import LFSPointer, LFSStore
  4553. with open_repo_closing(repo) as r:
  4554. store = LFSStore.from_repo(r)
  4555. index = r.open_index()
  4556. status = {
  4557. "tracked": [],
  4558. "not_staged": [],
  4559. "not_committed": [],
  4560. "not_pushed": [],
  4561. "missing": [],
  4562. }
  4563. # Check working directory files
  4564. for path, entry in index.items():
  4565. path_str = path.decode()
  4566. full_path = os.path.join(r.path, path_str)
  4567. if os.path.exists(full_path):
  4568. with open(full_path, "rb") as f:
  4569. content = f.read()
  4570. pointer = LFSPointer.from_bytes(content)
  4571. if pointer and pointer.is_valid_oid():
  4572. status["tracked"].append(path_str)
  4573. # Check if object exists locally
  4574. try:
  4575. store.open_object(pointer.oid)
  4576. except KeyError:
  4577. status["missing"].append(path_str)
  4578. # Check if file has been modified
  4579. try:
  4580. staged_obj = r.object_store[entry.binsha]
  4581. staged_pointer = LFSPointer.from_bytes(staged_obj.data)
  4582. if staged_pointer and staged_pointer.oid != pointer.oid:
  4583. status["not_staged"].append(path_str)
  4584. except KeyError:
  4585. pass
  4586. # TODO: Check for not committed and not pushed files
  4587. return status