porcelain.py 225 KB

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