cli.py 222 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783
  1. #
  2. # dulwich - Simple command-line interface to Dulwich
  3. # Copyright (C) 2008-2011 Jelmer Vernooij <jelmer@jelmer.uk>
  4. # vim: expandtab
  5. #
  6. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  8. # General Public License as published by the Free Software Foundation; version 2.0
  9. # or (at your option) any later version. You can redistribute it and/or
  10. # modify it under the terms of either of these two licenses.
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # You should have received a copy of the licenses; if not, see
  19. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  20. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  21. # License, Version 2.0.
  22. #
  23. """Simple command-line interface to Dulwich>.
  24. This is a very simple command-line wrapper for Dulwich. It is by
  25. no means intended to be a full-blown Git command-line interface but just
  26. a way to test Dulwich.
  27. """
  28. # TODO: Add support for GIT_NAMESPACE environment variable by wrapping
  29. # repository refs with NamespacedRefsContainer when the environment
  30. # variable is set. See issue #1809 and dulwich.refs.NamespacedRefsContainer.
  31. import argparse
  32. import io
  33. import logging
  34. import os
  35. import shutil
  36. import signal
  37. import subprocess
  38. import sys
  39. import tempfile
  40. import types
  41. from collections.abc import Callable, Iterable, Iterator, Mapping, Sequence
  42. from pathlib import Path
  43. from types import TracebackType
  44. from typing import (
  45. BinaryIO,
  46. ClassVar,
  47. TextIO,
  48. )
  49. from dulwich import porcelain
  50. from dulwich._typing import Buffer
  51. from dulwich.refs import HEADREF, Ref
  52. from .bundle import Bundle, create_bundle_from_repo, read_bundle, write_bundle
  53. from .client import get_transport_and_path
  54. from .config import Config
  55. from .errors import (
  56. ApplyDeltaError,
  57. FileFormatException,
  58. GitProtocolError,
  59. NotGitRepository,
  60. )
  61. from .index import Index
  62. from .log_utils import _configure_logging_from_trace
  63. from .objects import Commit, ObjectID, RawObjectID, sha_to_hex, valid_hexsha
  64. from .objectspec import parse_commit_range
  65. from .pack import Pack
  66. from .patch import DiffAlgorithmNotAvailable
  67. from .repo import Repo
  68. logger = logging.getLogger(__name__)
  69. def to_display_str(value: bytes | str) -> str:
  70. """Convert a bytes or string value to a display string.
  71. Args:
  72. value: The value to convert (bytes or str)
  73. Returns:
  74. A string suitable for display
  75. """
  76. if isinstance(value, bytes):
  77. return value.decode("utf-8", "replace")
  78. return value
  79. def _should_auto_flush(
  80. stream: TextIO | BinaryIO, env: Mapping[str, str] | None = None
  81. ) -> bool:
  82. """Determine if output should be auto-flushed based on GIT_FLUSH environment variable.
  83. Args:
  84. stream: The output stream to check
  85. env: Environment variables dict (defaults to os.environ)
  86. Returns:
  87. True if output should be flushed after each write, False otherwise
  88. """
  89. if env is None:
  90. env = os.environ
  91. git_flush = env.get("GIT_FLUSH", "").strip()
  92. if git_flush == "1":
  93. return True
  94. elif git_flush == "0":
  95. return False
  96. else:
  97. # Auto-detect: don't flush if redirected to a file
  98. return hasattr(stream, "isatty") and not stream.isatty()
  99. class AutoFlushTextIOWrapper:
  100. """Wrapper that automatically flushes a TextIO stream based on configuration.
  101. This wrapper can be configured to flush after each write operation,
  102. which is useful for real-time output monitoring in CI/CD systems.
  103. """
  104. def __init__(self, stream: TextIO) -> None:
  105. """Initialize the wrapper.
  106. Args:
  107. stream: The stream to wrap
  108. """
  109. self._stream = stream
  110. @classmethod
  111. def env(
  112. cls, stream: TextIO, env: Mapping[str, str] | None = None
  113. ) -> "AutoFlushTextIOWrapper | TextIO":
  114. """Create wrapper respecting the GIT_FLUSH environment variable.
  115. Respects the GIT_FLUSH environment variable:
  116. - GIT_FLUSH=1: Always flush after each write
  117. - GIT_FLUSH=0: Never auto-flush (use buffered I/O)
  118. - Not set: Auto-detect based on whether output is redirected
  119. Args:
  120. stream: The stream to wrap
  121. env: Environment variables dict (defaults to os.environ)
  122. Returns:
  123. AutoFlushTextIOWrapper instance configured based on GIT_FLUSH
  124. """
  125. if _should_auto_flush(stream, env):
  126. return cls(stream)
  127. else:
  128. return stream
  129. def write(self, data: str) -> int:
  130. """Write data to the stream and optionally flush.
  131. Args:
  132. data: Data to write
  133. Returns:
  134. Number of characters written
  135. """
  136. result = self._stream.write(data)
  137. self._stream.flush()
  138. return result
  139. def writelines(self, lines: Iterable[str]) -> None:
  140. """Write multiple lines to the stream and optionally flush.
  141. Args:
  142. lines: Lines to write
  143. """
  144. self._stream.writelines(lines)
  145. self._stream.flush()
  146. def flush(self) -> None:
  147. """Flush the underlying stream."""
  148. self._stream.flush()
  149. def __getattr__(self, name: str) -> object:
  150. """Delegate all other attributes to the underlying stream."""
  151. return getattr(self._stream, name)
  152. def __enter__(self) -> "AutoFlushTextIOWrapper":
  153. """Support context manager protocol."""
  154. return self
  155. def __exit__(
  156. self,
  157. exc_type: type[BaseException] | None,
  158. exc_val: BaseException | None,
  159. exc_tb: TracebackType | None,
  160. ) -> None:
  161. """Support context manager protocol."""
  162. if hasattr(self._stream, "__exit__"):
  163. self._stream.__exit__(exc_type, exc_val, exc_tb)
  164. class AutoFlushBinaryIOWrapper:
  165. """Wrapper that automatically flushes a BinaryIO stream based on configuration.
  166. This wrapper can be configured to flush after each write operation,
  167. which is useful for real-time output monitoring in CI/CD systems.
  168. """
  169. def __init__(self, stream: BinaryIO) -> None:
  170. """Initialize the wrapper.
  171. Args:
  172. stream: The stream to wrap
  173. """
  174. self._stream = stream
  175. @classmethod
  176. def env(
  177. cls, stream: BinaryIO, env: Mapping[str, str] | None = None
  178. ) -> "AutoFlushBinaryIOWrapper | BinaryIO":
  179. """Create wrapper respecting the GIT_FLUSH environment variable.
  180. Respects the GIT_FLUSH environment variable:
  181. - GIT_FLUSH=1: Always flush after each write
  182. - GIT_FLUSH=0: Never auto-flush (use buffered I/O)
  183. - Not set: Auto-detect based on whether output is redirected
  184. Args:
  185. stream: The stream to wrap
  186. env: Environment variables dict (defaults to os.environ)
  187. Returns:
  188. AutoFlushBinaryIOWrapper instance configured based on GIT_FLUSH
  189. """
  190. if _should_auto_flush(stream, env):
  191. return cls(stream)
  192. else:
  193. return stream
  194. def write(self, data: Buffer) -> int:
  195. """Write data to the stream and optionally flush.
  196. Args:
  197. data: Data to write
  198. Returns:
  199. Number of bytes written
  200. """
  201. result = self._stream.write(data)
  202. self._stream.flush()
  203. return result
  204. def writelines(self, lines: Iterable[Buffer]) -> None:
  205. """Write multiple lines to the stream and optionally flush.
  206. Args:
  207. lines: Lines to write
  208. """
  209. self._stream.writelines(lines)
  210. self._stream.flush()
  211. def flush(self) -> None:
  212. """Flush the underlying stream."""
  213. self._stream.flush()
  214. def __getattr__(self, name: str) -> object:
  215. """Delegate all other attributes to the underlying stream."""
  216. return getattr(self._stream, name)
  217. def __enter__(self) -> "AutoFlushBinaryIOWrapper":
  218. """Support context manager protocol."""
  219. return self
  220. def __exit__(
  221. self,
  222. exc_type: type[BaseException] | None,
  223. exc_val: BaseException | None,
  224. exc_tb: TracebackType | None,
  225. ) -> None:
  226. """Support context manager protocol."""
  227. if hasattr(self._stream, "__exit__"):
  228. self._stream.__exit__(exc_type, exc_val, exc_tb)
  229. class CommitMessageError(Exception):
  230. """Raised when there's an issue with the commit message."""
  231. def signal_int(signal: int, frame: types.FrameType | None) -> None:
  232. """Handle interrupt signal by exiting.
  233. Args:
  234. signal: Signal number
  235. frame: Current stack frame
  236. """
  237. sys.exit(1)
  238. def signal_quit(signal: int, frame: types.FrameType | None) -> None:
  239. """Handle quit signal by entering debugger.
  240. Args:
  241. signal: Signal number
  242. frame: Current stack frame
  243. """
  244. import pdb
  245. pdb.set_trace()
  246. def parse_time_to_timestamp(time_spec: str) -> int:
  247. """Parse a time specification and return a Unix timestamp.
  248. Args:
  249. time_spec: Time specification. Can be:
  250. - A Unix timestamp (integer as string)
  251. - A relative time like "2 weeks ago"
  252. - "now" for current time
  253. - "all" to expire all entries (returns future time)
  254. - "never" to never expire (returns 0 - epoch start)
  255. Returns:
  256. Unix timestamp
  257. Raises:
  258. ValueError: If the time specification cannot be parsed
  259. """
  260. import time
  261. from .approxidate import parse_approxidate
  262. # Handle special cases specific to CLI
  263. if time_spec == "all":
  264. # Expire all entries - set to future time so everything is "older"
  265. return int(time.time()) + (100 * 365 * 24 * 60 * 60) # 100 years in future
  266. if time_spec == "never":
  267. # Never expire - set to epoch start so nothing is older
  268. return 0
  269. # Use approxidate parser for everything else
  270. return parse_approxidate(time_spec)
  271. def format_bytes(bytes: float) -> str:
  272. """Format bytes as human-readable string.
  273. Args:
  274. bytes: Number of bytes
  275. Returns:
  276. Human-readable string like "1.5 MB"
  277. """
  278. for unit in ["B", "KB", "MB", "GB"]:
  279. if bytes < 1024.0:
  280. return f"{bytes:.1f} {unit}"
  281. bytes /= 1024.0
  282. return f"{bytes:.1f} TB"
  283. def launch_editor(template_content: bytes = b"") -> bytes:
  284. """Launch an editor for the user to enter text.
  285. Args:
  286. template_content: Initial content for the editor
  287. Returns:
  288. The edited content as bytes
  289. """
  290. # Determine which editor to use
  291. editor = os.environ.get("GIT_EDITOR") or os.environ.get("EDITOR") or "vi"
  292. # Create a temporary file
  293. with tempfile.NamedTemporaryFile(mode="wb", delete=False, suffix=".txt") as f:
  294. temp_file = f.name
  295. f.write(template_content)
  296. try:
  297. # Launch the editor
  298. subprocess.run([editor, temp_file], check=True)
  299. # Read the edited content
  300. with open(temp_file, "rb") as f:
  301. content = f.read()
  302. return content
  303. finally:
  304. # Clean up the temporary file
  305. os.unlink(temp_file)
  306. def detect_terminal_width() -> int:
  307. """Detect the width of the terminal.
  308. Returns:
  309. Width of the terminal in characters, or 80 if it cannot be determined
  310. """
  311. try:
  312. return os.get_terminal_size().columns
  313. except OSError:
  314. return 80
  315. def write_columns(
  316. items: Iterator[bytes] | Sequence[bytes],
  317. out: TextIO,
  318. width: int | None = None,
  319. ) -> None:
  320. """Display items in formatted columns based on terminal width.
  321. Args:
  322. items: List or iterator of bytes objects to display in columns
  323. out: Output stream to write to
  324. width: Optional width of the terminal (if None, auto-detect)
  325. The function calculates the optimal number of columns to fit the terminal
  326. width and displays the items in a formatted column layout with proper
  327. padding and alignment.
  328. """
  329. if width is None:
  330. ter_width = detect_terminal_width()
  331. else:
  332. ter_width = width
  333. item_names = [item.decode() for item in items]
  334. def columns(
  335. names: Sequence[str], width: int, num_cols: int
  336. ) -> tuple[bool, list[int]]:
  337. if num_cols <= 0:
  338. return False, []
  339. num_rows = (len(names) + num_cols - 1) // num_cols
  340. col_widths = []
  341. for col in range(num_cols):
  342. max_width = 0
  343. for row in range(num_rows):
  344. idx = row + col * num_rows
  345. if idx < len(names):
  346. max_width = max(max_width, len(names[idx]))
  347. col_widths.append(max_width + 2) # add padding
  348. total_width = sum(col_widths)
  349. if total_width <= width:
  350. return True, col_widths
  351. return False, []
  352. best_cols = 1
  353. best_widths = []
  354. for num_cols in range(min(8, len(item_names)), 0, -1):
  355. fits, widths = columns(item_names, ter_width, num_cols)
  356. if fits:
  357. best_cols = num_cols
  358. best_widths = widths
  359. break
  360. if not best_widths:
  361. best_cols = 1
  362. best_widths = [max(len(name) for name in item_names) + 2]
  363. num_rows = (len(item_names) + best_cols - 1) // best_cols
  364. for row in range(num_rows):
  365. lines = []
  366. for col in range(best_cols):
  367. idx = row + col * num_rows
  368. if idx < len(item_names):
  369. branch_name = item_names[idx]
  370. if col < len(best_widths):
  371. lines.append(branch_name.ljust(best_widths[col]))
  372. else:
  373. lines.append(branch_name)
  374. if lines:
  375. out.write("".join(lines).rstrip() + "\n")
  376. def format_columns(
  377. items: list[str],
  378. width: int | None = None,
  379. mode: str = "column",
  380. padding: int = 1,
  381. indent: str = "",
  382. nl: str = "\n",
  383. ) -> str:
  384. r"""Format items into columns with various layout modes.
  385. Args:
  386. items: List of strings to format
  387. width: Terminal width (auto-detected if None)
  388. mode: Layout mode - "column" (fill columns first), "row" (fill rows first),
  389. "plain" (one column), or add ",dense" for unequal column widths
  390. padding: Number of spaces between columns
  391. indent: String to prepend to each line
  392. nl: String to append to each line (including newline)
  393. Returns:
  394. Formatted string with items in columns
  395. Examples:
  396. >>> format_columns(["a", "b", "c"], width=20, mode="column")
  397. "a b\\nc\\n"
  398. >>> format_columns(["a", "b", "c"], width=20, mode="row")
  399. "a b c\\n"
  400. """
  401. if not items:
  402. return ""
  403. if width is None:
  404. width = detect_terminal_width()
  405. # Parse mode
  406. mode_parts = mode.split(",")
  407. layout_mode = "column"
  408. dense = False
  409. for part in mode_parts:
  410. part = part.strip()
  411. if part in ("column", "row", "plain"):
  412. layout_mode = part
  413. elif part == "dense":
  414. dense = True
  415. elif part == "nodense":
  416. dense = False
  417. # Plain mode - one item per line
  418. if layout_mode == "plain":
  419. return "".join(indent + item + nl for item in items)
  420. # Calculate available width for content (excluding indent)
  421. available_width = width - len(indent)
  422. if available_width <= 0:
  423. available_width = width
  424. # Find optimal number of columns
  425. max_item_len = max(len(item) for item in items)
  426. # Start with maximum possible columns and work down
  427. best_num_cols = 1
  428. best_col_widths: list[int] = []
  429. for num_cols in range(min(len(items), 20), 0, -1):
  430. if layout_mode == "column":
  431. # Column mode: fill columns first (items go down, then across)
  432. num_rows = (len(items) + num_cols - 1) // num_cols
  433. else: # row mode
  434. # Row mode: fill rows first (items go across, then down)
  435. num_rows = (len(items) + num_cols - 1) // num_cols
  436. col_widths: list[int] = []
  437. if dense:
  438. # Calculate width for each column based on its contents
  439. for col in range(num_cols):
  440. max_width = 0
  441. for row in range(num_rows):
  442. if layout_mode == "column":
  443. idx = row + col * num_rows
  444. else: # row mode
  445. idx = row * num_cols + col
  446. if idx < len(items):
  447. max_width = max(max_width, len(items[idx]))
  448. if max_width > 0:
  449. col_widths.append(max_width)
  450. else:
  451. # All columns same width (nodense)
  452. max_width = 0
  453. for col in range(num_cols):
  454. for row in range(num_rows):
  455. if layout_mode == "column":
  456. idx = row + col * num_rows
  457. else: # row mode
  458. idx = row * num_cols + col
  459. if idx < len(items):
  460. max_width = max(max_width, len(items[idx]))
  461. col_widths = [max_width] * num_cols
  462. # Calculate total width including padding (but not after last column)
  463. total_width = sum(col_widths) + padding * (len(col_widths) - 1)
  464. if total_width <= available_width:
  465. best_num_cols = num_cols
  466. best_col_widths = col_widths
  467. break
  468. # If no fit found, use single column
  469. if not best_col_widths:
  470. best_num_cols = 1
  471. best_col_widths = [max_item_len]
  472. # Format output
  473. num_rows = (len(items) + best_num_cols - 1) // best_num_cols
  474. lines = []
  475. for row in range(num_rows):
  476. line_parts = []
  477. for col in range(best_num_cols):
  478. if layout_mode == "column":
  479. idx = row + col * num_rows
  480. else: # row mode
  481. idx = row * best_num_cols + col
  482. if idx < len(items):
  483. item = items[idx]
  484. # Pad item to column width, except for last column in row
  485. if col < best_num_cols - 1 and col < len(best_col_widths) - 1:
  486. item = item.ljust(best_col_widths[col] + padding)
  487. line_parts.append(item)
  488. if line_parts:
  489. lines.append(indent + "".join(line_parts).rstrip() + nl)
  490. return "".join(lines)
  491. class PagerBuffer(BinaryIO):
  492. """Binary buffer wrapper for Pager to mimic sys.stdout.buffer."""
  493. def __init__(self, pager: "Pager") -> None:
  494. """Initialize PagerBuffer.
  495. Args:
  496. pager: Pager instance to wrap
  497. """
  498. self.pager = pager
  499. def write(self, data: bytes | bytearray | memoryview) -> int: # type: ignore[override]
  500. """Write bytes to pager."""
  501. # Convert to bytes and decode to string for the pager
  502. text = bytes(data).decode("utf-8", errors="replace")
  503. return self.pager.write(text)
  504. def flush(self) -> None:
  505. """Flush the pager."""
  506. return self.pager.flush()
  507. def writelines(self, lines: Iterable[bytes | bytearray | memoryview]) -> None: # type: ignore[override]
  508. """Write multiple lines to pager."""
  509. for line in lines:
  510. self.write(line)
  511. def readable(self) -> bool:
  512. """Return whether the buffer is readable (it's not)."""
  513. return False
  514. def writable(self) -> bool:
  515. """Return whether the buffer is writable."""
  516. return not self.pager._closed
  517. def seekable(self) -> bool:
  518. """Return whether the buffer is seekable (it's not)."""
  519. return False
  520. def close(self) -> None:
  521. """Close the pager."""
  522. return self.pager.close()
  523. @property
  524. def closed(self) -> bool:
  525. """Return whether the buffer is closed."""
  526. return self.pager.closed
  527. @property
  528. def mode(self) -> str:
  529. """Return the mode."""
  530. return "wb"
  531. @property
  532. def name(self) -> str:
  533. """Return the name."""
  534. return "<pager.buffer>"
  535. def fileno(self) -> int:
  536. """Return the file descriptor (not supported)."""
  537. raise io.UnsupportedOperation("PagerBuffer does not support fileno()")
  538. def isatty(self) -> bool:
  539. """Return whether the buffer is a TTY."""
  540. return False
  541. def read(self, size: int = -1) -> bytes:
  542. """Read from the buffer (not supported)."""
  543. raise io.UnsupportedOperation("PagerBuffer does not support reading")
  544. def read1(self, size: int = -1) -> bytes:
  545. """Read from the buffer (not supported)."""
  546. raise io.UnsupportedOperation("PagerBuffer does not support reading")
  547. def readinto(self, b: bytearray) -> int:
  548. """Read into buffer (not supported)."""
  549. raise io.UnsupportedOperation("PagerBuffer does not support reading")
  550. def readinto1(self, b: bytearray) -> int:
  551. """Read into buffer (not supported)."""
  552. raise io.UnsupportedOperation("PagerBuffer does not support reading")
  553. def readline(self, size: int = -1) -> bytes:
  554. """Read a line from the buffer (not supported)."""
  555. raise io.UnsupportedOperation("PagerBuffer does not support reading")
  556. def readlines(self, hint: int = -1) -> list[bytes]:
  557. """Read lines from the buffer (not supported)."""
  558. raise io.UnsupportedOperation("PagerBuffer does not support reading")
  559. def seek(self, offset: int, whence: int = 0) -> int:
  560. """Seek in the buffer (not supported)."""
  561. raise io.UnsupportedOperation("PagerBuffer does not support seeking")
  562. def tell(self) -> int:
  563. """Return the current position (not supported)."""
  564. raise io.UnsupportedOperation("PagerBuffer does not support tell()")
  565. def truncate(self, size: int | None = None) -> int:
  566. """Truncate the buffer (not supported)."""
  567. raise io.UnsupportedOperation("PagerBuffer does not support truncation")
  568. def __iter__(self) -> "PagerBuffer":
  569. """Return iterator (not supported)."""
  570. raise io.UnsupportedOperation("PagerBuffer does not support iteration")
  571. def __next__(self) -> bytes:
  572. """Return next line (not supported)."""
  573. raise io.UnsupportedOperation("PagerBuffer does not support iteration")
  574. def __enter__(self) -> "PagerBuffer":
  575. """Enter context manager."""
  576. return self
  577. def __exit__(
  578. self,
  579. exc_type: type[BaseException] | None,
  580. exc_val: BaseException | None,
  581. exc_tb: TracebackType | None,
  582. ) -> None:
  583. """Exit context manager."""
  584. self.close()
  585. class Pager(TextIO):
  586. """File-like object that pages output through external pager programs."""
  587. def __init__(self, pager_cmd: str = "cat") -> None:
  588. """Initialize Pager.
  589. Args:
  590. pager_cmd: Command to use for paging (default: "cat")
  591. """
  592. self.pager_process: subprocess.Popen[str] | None = None
  593. self._buffer = PagerBuffer(self)
  594. self._closed = False
  595. self.pager_cmd = pager_cmd
  596. self._pager_died = False
  597. def _get_pager_command(self) -> str:
  598. """Get the pager command to use."""
  599. return self.pager_cmd
  600. def _ensure_pager_started(self) -> None:
  601. """Start the pager process if not already started."""
  602. if self.pager_process is None and not self._closed:
  603. try:
  604. pager_cmd = self._get_pager_command()
  605. self.pager_process = subprocess.Popen(
  606. pager_cmd,
  607. shell=True,
  608. stdin=subprocess.PIPE,
  609. stdout=sys.stdout,
  610. stderr=sys.stderr,
  611. text=True,
  612. )
  613. except (OSError, subprocess.SubprocessError):
  614. # Pager failed to start, fall back to direct output
  615. self.pager_process = None
  616. def write(self, text: str) -> int:
  617. """Write text to the pager."""
  618. if self._closed:
  619. raise ValueError("I/O operation on closed file")
  620. # If pager died (user quit), stop writing output
  621. if self._pager_died:
  622. return len(text)
  623. self._ensure_pager_started()
  624. if self.pager_process and self.pager_process.stdin:
  625. try:
  626. result = self.pager_process.stdin.write(text)
  627. assert isinstance(result, int)
  628. return result
  629. except (OSError, subprocess.SubprocessError, BrokenPipeError):
  630. # Pager died (user quit), stop writing output
  631. self._pager_died = True
  632. return len(text)
  633. else:
  634. # No pager available, write directly to stdout
  635. return sys.stdout.write(text)
  636. def flush(self) -> None:
  637. """Flush the pager."""
  638. if self._closed or self._pager_died:
  639. return
  640. if self.pager_process and self.pager_process.stdin:
  641. try:
  642. self.pager_process.stdin.flush()
  643. except (OSError, subprocess.SubprocessError, BrokenPipeError):
  644. self._pager_died = True
  645. else:
  646. sys.stdout.flush()
  647. def close(self) -> None:
  648. """Close the pager."""
  649. if self._closed:
  650. return
  651. self._closed = True
  652. if self.pager_process:
  653. try:
  654. if self.pager_process.stdin:
  655. self.pager_process.stdin.close()
  656. self.pager_process.wait()
  657. except (OSError, subprocess.SubprocessError):
  658. pass
  659. self.pager_process = None
  660. def __enter__(self) -> "Pager":
  661. """Context manager entry."""
  662. return self
  663. def __exit__(
  664. self,
  665. exc_type: type | None,
  666. exc_val: BaseException | None,
  667. exc_tb: types.TracebackType | None,
  668. ) -> None:
  669. """Context manager exit."""
  670. self.close()
  671. # Additional file-like methods for compatibility
  672. def writelines(self, lines: Iterable[str]) -> None:
  673. """Write a list of lines to the pager."""
  674. if self._pager_died:
  675. return
  676. for line in lines:
  677. self.write(line)
  678. @property
  679. def closed(self) -> bool:
  680. """Return whether the pager is closed."""
  681. return self._closed
  682. def readable(self) -> bool:
  683. """Return whether the pager is readable (it's not)."""
  684. return False
  685. def writable(self) -> bool:
  686. """Return whether the pager is writable."""
  687. return not self._closed
  688. def seekable(self) -> bool:
  689. """Return whether the pager is seekable (it's not)."""
  690. return False
  691. @property
  692. def buffer(self) -> BinaryIO:
  693. """Return the underlying binary buffer."""
  694. return self._buffer
  695. @property
  696. def encoding(self) -> str:
  697. """Return the encoding used."""
  698. return "utf-8"
  699. @property
  700. def errors(self) -> str | None:
  701. """Return the error handling scheme."""
  702. return "replace"
  703. def fileno(self) -> int:
  704. """Return the file descriptor (not supported)."""
  705. raise io.UnsupportedOperation("Pager does not support fileno()")
  706. def isatty(self) -> bool:
  707. """Return whether the pager is a TTY."""
  708. return False
  709. @property
  710. def line_buffering(self) -> bool:
  711. """Return whether line buffering is enabled."""
  712. return True
  713. @property
  714. def mode(self) -> str:
  715. """Return the mode."""
  716. return "w"
  717. @property
  718. def name(self) -> str:
  719. """Return the name."""
  720. return "<pager>"
  721. @property
  722. def newlines(self) -> str | tuple[str, ...] | None:
  723. """Return the newlines mode."""
  724. return None
  725. def read(self, size: int = -1) -> str:
  726. """Read from the pager (not supported)."""
  727. raise io.UnsupportedOperation("Pager does not support reading")
  728. def readline(self, size: int = -1) -> str:
  729. """Read a line from the pager (not supported)."""
  730. raise io.UnsupportedOperation("Pager does not support reading")
  731. def readlines(self, hint: int = -1) -> list[str]:
  732. """Read lines from the pager (not supported)."""
  733. raise io.UnsupportedOperation("Pager does not support reading")
  734. def seek(self, offset: int, whence: int = 0) -> int:
  735. """Seek in the pager (not supported)."""
  736. raise io.UnsupportedOperation("Pager does not support seeking")
  737. def tell(self) -> int:
  738. """Return the current position (not supported)."""
  739. raise io.UnsupportedOperation("Pager does not support tell()")
  740. def truncate(self, size: int | None = None) -> int:
  741. """Truncate the pager (not supported)."""
  742. raise io.UnsupportedOperation("Pager does not support truncation")
  743. def __iter__(self) -> "Pager":
  744. """Return iterator (not supported)."""
  745. raise io.UnsupportedOperation("Pager does not support iteration")
  746. def __next__(self) -> str:
  747. """Return next line (not supported)."""
  748. raise io.UnsupportedOperation("Pager does not support iteration")
  749. class _StreamContextAdapter:
  750. """Adapter to make streams work with context manager protocol."""
  751. def __init__(self, stream: TextIO | BinaryIO) -> None:
  752. self.stream = stream
  753. # Expose buffer if it exists
  754. if hasattr(stream, "buffer"):
  755. self.buffer = stream.buffer
  756. else:
  757. self.buffer = stream
  758. def __enter__(self) -> TextIO:
  759. # We only use this with sys.stdout which is TextIO
  760. return self.stream # type: ignore[return-value]
  761. def __exit__(
  762. self,
  763. exc_type: type[BaseException] | None,
  764. exc_val: BaseException | None,
  765. exc_tb: TracebackType | None,
  766. ) -> None:
  767. # For stdout/stderr, we don't close them
  768. pass
  769. def __getattr__(self, name: str) -> object:
  770. return getattr(self.stream, name)
  771. def get_pager(
  772. config: Config | None = None, cmd_name: str | None = None
  773. ) -> "_StreamContextAdapter | Pager":
  774. """Get a pager instance if paging should be used, otherwise return sys.stdout.
  775. Args:
  776. config: Optional config instance (e.g., StackedConfig) to read settings from
  777. cmd_name: Optional command name for per-command pager settings
  778. Returns:
  779. Either a wrapped sys.stdout or a Pager instance (both context managers)
  780. """
  781. # Check global pager disable flag
  782. if getattr(get_pager, "_disabled", False):
  783. return _StreamContextAdapter(sys.stdout)
  784. # Don't page if stdout is not a terminal
  785. if not sys.stdout.isatty():
  786. return _StreamContextAdapter(sys.stdout)
  787. # Priority order for pager command (following git's behavior):
  788. # 1. Check pager.<cmd> config (if cmd_name provided)
  789. # 2. Check environment variables: DULWICH_PAGER, GIT_PAGER, PAGER
  790. # 3. Check core.pager config
  791. # 4. Fallback to common pagers
  792. pager_cmd = None
  793. # 1. Check per-command pager config (pager.<cmd>)
  794. if config and cmd_name:
  795. try:
  796. pager_value = config.get(
  797. ("pager",), cmd_name.encode() if isinstance(cmd_name, str) else cmd_name
  798. )
  799. except KeyError:
  800. pass
  801. else:
  802. if pager_value == b"false":
  803. return _StreamContextAdapter(sys.stdout)
  804. elif pager_value != b"true":
  805. # It's a custom pager command
  806. pager_cmd = (
  807. pager_value.decode()
  808. if isinstance(pager_value, bytes)
  809. else pager_value
  810. )
  811. # 2. Check environment variables
  812. if not pager_cmd:
  813. for env_var in ["DULWICH_PAGER", "GIT_PAGER", "PAGER"]:
  814. pager = os.environ.get(env_var)
  815. if pager:
  816. if pager == "false":
  817. return _StreamContextAdapter(sys.stdout)
  818. pager_cmd = pager
  819. break
  820. # 3. Check core.pager config
  821. if not pager_cmd and config:
  822. try:
  823. core_pager = config.get(("core",), b"pager")
  824. except KeyError:
  825. pass
  826. else:
  827. if core_pager == b"false" or core_pager == b"":
  828. return _StreamContextAdapter(sys.stdout)
  829. pager_cmd = (
  830. core_pager.decode() if isinstance(core_pager, bytes) else core_pager
  831. )
  832. # 4. Fallback to common pagers
  833. if not pager_cmd:
  834. for pager in ["less", "more", "cat"]:
  835. if shutil.which(pager):
  836. if pager == "less":
  837. pager_cmd = "less -FRX" # -F: quit if one screen, -R: raw control chars, -X: no init/deinit
  838. else:
  839. pager_cmd = pager
  840. break
  841. else:
  842. pager_cmd = "cat" # Ultimate fallback
  843. return Pager(pager_cmd)
  844. def disable_pager() -> None:
  845. """Disable pager for this session."""
  846. get_pager._disabled = True # type: ignore[attr-defined]
  847. def enable_pager() -> None:
  848. """Enable pager for this session."""
  849. get_pager._disabled = False # type: ignore[attr-defined]
  850. class Command:
  851. """A Dulwich subcommand."""
  852. def run(self, args: Sequence[str]) -> int | None:
  853. """Run the command."""
  854. raise NotImplementedError(self.run)
  855. class cmd_archive(Command):
  856. """Create an archive of files from a named tree."""
  857. def run(self, args: Sequence[str]) -> None:
  858. """Execute the archive command.
  859. Args:
  860. args: Command line arguments
  861. """
  862. parser = argparse.ArgumentParser()
  863. parser.add_argument(
  864. "--remote",
  865. type=str,
  866. help="Retrieve archive from specified remote repo",
  867. )
  868. parser.add_argument("committish", type=str, nargs="?")
  869. parsed_args = parser.parse_args(args)
  870. if parsed_args.remote:
  871. client, path = get_transport_and_path(parsed_args.remote)
  872. def stdout_write(data: bytes) -> None:
  873. sys.stdout.buffer.write(data)
  874. def stderr_write(data: bytes) -> None:
  875. sys.stderr.buffer.write(data)
  876. client.archive(
  877. path.encode("utf-8") if isinstance(path, str) else path,
  878. parsed_args.committish.encode("utf-8")
  879. if isinstance(parsed_args.committish, str)
  880. else parsed_args.committish,
  881. stdout_write,
  882. write_error=stderr_write,
  883. )
  884. else:
  885. # Use binary buffer for archive output
  886. outstream: BinaryIO = sys.stdout.buffer
  887. errstream: BinaryIO = sys.stderr.buffer
  888. porcelain.archive(
  889. ".",
  890. parsed_args.committish,
  891. outstream=outstream,
  892. errstream=errstream,
  893. )
  894. class cmd_add(Command):
  895. """Add file contents to the index."""
  896. def run(self, argv: Sequence[str]) -> None:
  897. """Execute the add command.
  898. Args:
  899. argv: Command line arguments
  900. """
  901. parser = argparse.ArgumentParser()
  902. parser.add_argument("path", nargs="+")
  903. args = parser.parse_args(argv)
  904. # Convert '.' to None to add all files
  905. paths = args.path
  906. if len(paths) == 1 and paths[0] == ".":
  907. paths = None
  908. porcelain.add(".", paths=paths)
  909. class cmd_annotate(Command):
  910. """Annotate each line in a file with commit information."""
  911. def run(self, argv: Sequence[str]) -> None:
  912. """Execute the annotate command.
  913. Args:
  914. argv: Command line arguments
  915. """
  916. parser = argparse.ArgumentParser()
  917. parser.add_argument("path", help="Path to file to annotate")
  918. parser.add_argument("committish", nargs="?", help="Commit to start from")
  919. args = parser.parse_args(argv)
  920. with Repo(".") as repo:
  921. config = repo.get_config_stack()
  922. with get_pager(config=config, cmd_name="annotate") as outstream:
  923. results = porcelain.annotate(repo, args.path, args.committish)
  924. for (commit, entry), line in results:
  925. # Show shortened commit hash and line content
  926. commit_hash = commit.id[:8]
  927. outstream.write(f"{commit_hash.decode()} {line.decode()}\n")
  928. class cmd_blame(Command):
  929. """Show what revision and author last modified each line of a file."""
  930. def run(self, argv: Sequence[str]) -> None:
  931. """Execute the blame command.
  932. Args:
  933. argv: Command line arguments
  934. """
  935. # blame is an alias for annotate
  936. cmd_annotate().run(argv)
  937. class cmd_rm(Command):
  938. """Remove files from the working tree and from the index."""
  939. def run(self, argv: Sequence[str]) -> None:
  940. """Execute the rm command.
  941. Args:
  942. argv: Command line arguments
  943. """
  944. parser = argparse.ArgumentParser()
  945. parser.add_argument(
  946. "--cached", action="store_true", help="Remove from index only"
  947. )
  948. parser.add_argument("path", type=Path, nargs="+")
  949. args = parser.parse_args(argv)
  950. porcelain.remove(".", paths=args.path, cached=args.cached)
  951. class cmd_mv(Command):
  952. """Move or rename a file, a directory, or a symlink."""
  953. def run(self, argv: Sequence[str]) -> None:
  954. """Execute the mv command.
  955. Args:
  956. argv: Command line arguments
  957. """
  958. parser = argparse.ArgumentParser()
  959. parser.add_argument(
  960. "-f",
  961. "--force",
  962. action="store_true",
  963. help="Force move even if destination exists",
  964. )
  965. parser.add_argument("source", type=Path)
  966. parser.add_argument("destination", type=Path)
  967. args = parser.parse_args(argv)
  968. porcelain.mv(".", args.source, args.destination, force=args.force)
  969. class cmd_fetch_pack(Command):
  970. """Receive missing objects from another repository."""
  971. def run(self, argv: Sequence[str]) -> None:
  972. """Execute the fetch-pack command.
  973. Args:
  974. argv: Command line arguments
  975. """
  976. parser = argparse.ArgumentParser()
  977. parser.add_argument("--all", action="store_true")
  978. parser.add_argument("location", nargs="?", type=str)
  979. parser.add_argument("refs", nargs="*", type=str)
  980. args = parser.parse_args(argv)
  981. client, path = get_transport_and_path(args.location)
  982. r = Repo(".")
  983. if args.all:
  984. determine_wants = r.object_store.determine_wants_all
  985. else:
  986. def determine_wants(
  987. refs: Mapping[Ref, ObjectID], depth: int | None = None
  988. ) -> list[ObjectID]:
  989. return [
  990. ObjectID(y.encode("utf-8"))
  991. for y in args.refs
  992. if y not in r.object_store
  993. ]
  994. client.fetch(path.encode("utf-8"), r, determine_wants)
  995. class cmd_fetch(Command):
  996. """Download objects and refs from another repository."""
  997. def run(self, args: Sequence[str]) -> None:
  998. """Execute the fetch command.
  999. Args:
  1000. args: Command line arguments
  1001. """
  1002. parser = argparse.ArgumentParser()
  1003. # Mutually exclusive group for location vs --all
  1004. location_group = parser.add_mutually_exclusive_group(required=True)
  1005. location_group.add_argument(
  1006. "location", nargs="?", default=None, help="Remote location to fetch from"
  1007. )
  1008. location_group.add_argument(
  1009. "--all", action="store_true", help="Fetch all remotes"
  1010. )
  1011. # Mutually exclusive group for tag handling
  1012. tag_group = parser.add_mutually_exclusive_group()
  1013. tag_group.add_argument(
  1014. "--tags", action="store_true", help="Fetch all tags from remote"
  1015. )
  1016. tag_group.add_argument(
  1017. "--no-tags", action="store_true", help="Don't fetch any tags from remote"
  1018. )
  1019. parser.add_argument(
  1020. "--depth",
  1021. type=int,
  1022. help="Create a shallow clone with a history truncated to the specified number of commits",
  1023. )
  1024. parser.add_argument(
  1025. "--shallow-since",
  1026. type=str,
  1027. help="Deepen or shorten the history of a shallow repository to include all reachable commits after <date>",
  1028. )
  1029. parser.add_argument(
  1030. "--shallow-exclude",
  1031. type=str,
  1032. action="append",
  1033. help="Deepen or shorten the history of a shallow repository to exclude commits reachable from a specified remote branch or tag",
  1034. )
  1035. parsed_args = parser.parse_args(args)
  1036. r = Repo(".")
  1037. def progress(msg: bytes) -> None:
  1038. sys.stdout.buffer.write(msg)
  1039. # Determine include_tags setting
  1040. include_tags = False
  1041. if parsed_args.tags:
  1042. include_tags = True
  1043. elif not parsed_args.no_tags:
  1044. # Default behavior - don't force tag inclusion
  1045. include_tags = False
  1046. if parsed_args.all:
  1047. # Fetch from all remotes
  1048. config = r.get_config()
  1049. remotes = set()
  1050. for section in config.sections():
  1051. if len(section) == 2 and section[0] == b"remote":
  1052. remotes.add(section[1].decode())
  1053. if not remotes:
  1054. logger.warning("No remotes configured")
  1055. return
  1056. for remote_name in sorted(remotes):
  1057. logger.info("Fetching %s", remote_name)
  1058. porcelain.fetch(
  1059. r,
  1060. remote_location=remote_name,
  1061. depth=parsed_args.depth,
  1062. include_tags=include_tags,
  1063. shallow_since=parsed_args.shallow_since,
  1064. shallow_exclude=parsed_args.shallow_exclude,
  1065. )
  1066. else:
  1067. # Fetch from specific location
  1068. porcelain.fetch(
  1069. r,
  1070. remote_location=parsed_args.location,
  1071. depth=parsed_args.depth,
  1072. include_tags=include_tags,
  1073. shallow_since=parsed_args.shallow_since,
  1074. shallow_exclude=parsed_args.shallow_exclude,
  1075. )
  1076. class cmd_for_each_ref(Command):
  1077. """Output information on each ref."""
  1078. def run(self, args: Sequence[str]) -> None:
  1079. """Execute the for-each-ref command.
  1080. Args:
  1081. args: Command line arguments
  1082. """
  1083. parser = argparse.ArgumentParser()
  1084. parser.add_argument("pattern", type=str, nargs="?")
  1085. parsed_args = parser.parse_args(args)
  1086. for sha, object_type, ref in porcelain.for_each_ref(".", parsed_args.pattern):
  1087. logger.info("%s %s\t%s", sha.decode(), object_type.decode(), ref.decode())
  1088. class cmd_fsck(Command):
  1089. """Verify the connectivity and validity of objects in the database."""
  1090. def run(self, args: Sequence[str]) -> None:
  1091. """Execute the fsck command.
  1092. Args:
  1093. args: Command line arguments
  1094. """
  1095. parser = argparse.ArgumentParser()
  1096. parser.parse_args(args)
  1097. for obj, msg in porcelain.fsck("."):
  1098. logger.info("%s: %s", obj.decode() if isinstance(obj, bytes) else obj, msg)
  1099. class cmd_log(Command):
  1100. """Show commit logs."""
  1101. def run(self, args: Sequence[str]) -> None:
  1102. """Execute the log command.
  1103. Args:
  1104. args: Command line arguments
  1105. """
  1106. parser = argparse.ArgumentParser()
  1107. parser.add_argument(
  1108. "--reverse",
  1109. action="store_true",
  1110. help="Reverse order in which entries are printed",
  1111. )
  1112. parser.add_argument(
  1113. "--name-status",
  1114. action="store_true",
  1115. help="Print name/status for each changed file",
  1116. )
  1117. parser.add_argument("paths", nargs="*", help="Paths to show log for")
  1118. parsed_args = parser.parse_args(args)
  1119. with Repo(".") as repo:
  1120. config = repo.get_config_stack()
  1121. with get_pager(config=config, cmd_name="log") as outstream:
  1122. porcelain.log(
  1123. repo,
  1124. paths=parsed_args.paths,
  1125. reverse=parsed_args.reverse,
  1126. name_status=parsed_args.name_status,
  1127. outstream=outstream,
  1128. )
  1129. class cmd_diff(Command):
  1130. """Show changes between commits, commit and working tree, etc."""
  1131. def run(self, args: Sequence[str]) -> None:
  1132. """Execute the diff command.
  1133. Args:
  1134. args: Command line arguments
  1135. """
  1136. parser = argparse.ArgumentParser()
  1137. parser.add_argument(
  1138. "committish", nargs="*", default=[], help="Commits or refs to compare"
  1139. )
  1140. parser.add_argument("--staged", action="store_true", help="Show staged changes")
  1141. parser.add_argument(
  1142. "--cached",
  1143. action="store_true",
  1144. help="Show staged changes (same as --staged)",
  1145. )
  1146. parser.add_argument(
  1147. "--color",
  1148. choices=["always", "never", "auto"],
  1149. default="auto",
  1150. help="Use colored output (requires rich)",
  1151. )
  1152. parser.add_argument(
  1153. "--patience",
  1154. action="store_true",
  1155. help="Use patience diff algorithm",
  1156. )
  1157. parser.add_argument(
  1158. "--diff-algorithm",
  1159. choices=["myers", "patience"],
  1160. default="myers",
  1161. help="Choose a diff algorithm",
  1162. )
  1163. parser.add_argument(
  1164. "--", dest="separator", action="store_true", help=argparse.SUPPRESS
  1165. )
  1166. parser.add_argument("paths", nargs="*", default=[], help="Paths to limit diff")
  1167. # Handle the -- separator for paths
  1168. if "--" in args:
  1169. sep_index = args.index("--")
  1170. parsed_args = parser.parse_args(args[:sep_index])
  1171. parsed_args.paths = args[sep_index + 1 :]
  1172. else:
  1173. parsed_args = parser.parse_args(args)
  1174. # Determine diff algorithm
  1175. diff_algorithm = parsed_args.diff_algorithm
  1176. if parsed_args.patience:
  1177. diff_algorithm = "patience"
  1178. # Determine if we should use color
  1179. def _should_use_color() -> bool:
  1180. if parsed_args.color == "always":
  1181. return True
  1182. elif parsed_args.color == "never":
  1183. return False
  1184. else: # auto
  1185. return sys.stdout.isatty()
  1186. def _create_output_stream(outstream: TextIO) -> BinaryIO:
  1187. """Create output stream, optionally with colorization."""
  1188. if not _should_use_color():
  1189. return outstream.buffer
  1190. from .diff import ColorizedDiffStream
  1191. if not ColorizedDiffStream.is_available():
  1192. if parsed_args.color == "always":
  1193. raise ImportError(
  1194. "Rich is required for colored output. Install with: pip install 'dulwich[colordiff]'"
  1195. )
  1196. else:
  1197. logging.warning(
  1198. "Rich not available, disabling colored output. Install with: pip install 'dulwich[colordiff]'"
  1199. )
  1200. return outstream.buffer
  1201. return ColorizedDiffStream(outstream.buffer)
  1202. with Repo(".") as repo:
  1203. config = repo.get_config_stack()
  1204. with get_pager(config=config, cmd_name="diff") as outstream:
  1205. output_stream = _create_output_stream(outstream)
  1206. try:
  1207. if len(parsed_args.committish) == 0:
  1208. # Show diff for working tree or staged changes
  1209. porcelain.diff(
  1210. repo,
  1211. staged=(parsed_args.staged or parsed_args.cached),
  1212. paths=parsed_args.paths or None,
  1213. outstream=output_stream,
  1214. diff_algorithm=diff_algorithm,
  1215. )
  1216. elif len(parsed_args.committish) == 1:
  1217. # Show diff between working tree and specified commit
  1218. if parsed_args.staged or parsed_args.cached:
  1219. parser.error(
  1220. "--staged/--cached cannot be used with commits"
  1221. )
  1222. porcelain.diff(
  1223. repo,
  1224. commit=parsed_args.committish[0],
  1225. staged=False,
  1226. paths=parsed_args.paths or None,
  1227. outstream=output_stream,
  1228. diff_algorithm=diff_algorithm,
  1229. )
  1230. elif len(parsed_args.committish) == 2:
  1231. # Show diff between two commits
  1232. porcelain.diff(
  1233. repo,
  1234. commit=parsed_args.committish[0],
  1235. commit2=parsed_args.committish[1],
  1236. paths=parsed_args.paths or None,
  1237. outstream=output_stream,
  1238. diff_algorithm=diff_algorithm,
  1239. )
  1240. else:
  1241. parser.error("Too many arguments - specify at most two commits")
  1242. except DiffAlgorithmNotAvailable as e:
  1243. sys.stderr.write(f"fatal: {e}\n")
  1244. sys.exit(1)
  1245. # Flush any remaining output
  1246. if hasattr(output_stream, "flush"):
  1247. output_stream.flush()
  1248. class cmd_dump_pack(Command):
  1249. """Dump the contents of a pack file for debugging."""
  1250. def run(self, args: Sequence[str]) -> None:
  1251. """Execute the dump-pack command.
  1252. Args:
  1253. args: Command line arguments
  1254. """
  1255. parser = argparse.ArgumentParser()
  1256. parser.add_argument("filename", help="Pack file to dump")
  1257. parsed_args = parser.parse_args(args)
  1258. basename, _ = os.path.splitext(parsed_args.filename)
  1259. x = Pack(basename)
  1260. logger.info("Object names checksum: %s", x.name().decode("ascii", "replace"))
  1261. logger.info("Checksum: %r", sha_to_hex(RawObjectID(x.get_stored_checksum())))
  1262. x.check()
  1263. logger.info("Length: %d", len(x))
  1264. for name in x:
  1265. try:
  1266. logger.info("\t%s", x[name])
  1267. except KeyError as k:
  1268. logger.error(
  1269. "\t%s: Unable to resolve base %r",
  1270. name.decode("ascii", "replace"),
  1271. k,
  1272. )
  1273. except ApplyDeltaError as e:
  1274. logger.error(
  1275. "\t%s: Unable to apply delta: %r",
  1276. name.decode("ascii", "replace"),
  1277. e,
  1278. )
  1279. class cmd_dump_index(Command):
  1280. """Show information about a pack index file."""
  1281. def run(self, args: Sequence[str]) -> None:
  1282. """Execute the dump-index command.
  1283. Args:
  1284. args: Command line arguments
  1285. """
  1286. parser = argparse.ArgumentParser()
  1287. parser.add_argument("filename", help="Index file to dump")
  1288. parsed_args = parser.parse_args(args)
  1289. idx = Index(parsed_args.filename)
  1290. for o in idx:
  1291. logger.info("%s %s", o, idx[o])
  1292. class cmd_interpret_trailers(Command):
  1293. """Add or parse structured information in commit messages."""
  1294. def run(self, args: Sequence[str]) -> None:
  1295. """Execute the interpret-trailers command.
  1296. Args:
  1297. args: Command line arguments
  1298. """
  1299. parser = argparse.ArgumentParser()
  1300. parser.add_argument(
  1301. "file",
  1302. nargs="?",
  1303. help="File to read message from. If not specified, reads from stdin.",
  1304. )
  1305. parser.add_argument(
  1306. "--trailer",
  1307. action="append",
  1308. dest="trailers",
  1309. metavar="<token>[(=|:)<value>]",
  1310. help="Trailer to add. Can be specified multiple times.",
  1311. )
  1312. parser.add_argument(
  1313. "--trim-empty",
  1314. action="store_true",
  1315. help="Remove trailers with empty values",
  1316. )
  1317. parser.add_argument(
  1318. "--only-trailers",
  1319. action="store_true",
  1320. help="Output only the trailers, not the message body",
  1321. )
  1322. parser.add_argument(
  1323. "--only-input",
  1324. action="store_true",
  1325. help="Don't add new trailers, only parse existing ones",
  1326. )
  1327. parser.add_argument(
  1328. "--unfold", action="store_true", help="Join multiline values into one line"
  1329. )
  1330. parser.add_argument(
  1331. "--parse",
  1332. action="store_true",
  1333. help="Shorthand for --only-trailers --only-input --unfold",
  1334. )
  1335. parser.add_argument(
  1336. "--where",
  1337. choices=["end", "start", "after", "before"],
  1338. default="end",
  1339. help="Where to place new trailers",
  1340. )
  1341. parser.add_argument(
  1342. "--if-exists",
  1343. choices=[
  1344. "add",
  1345. "replace",
  1346. "addIfDifferent",
  1347. "addIfDifferentNeighbor",
  1348. "doNothing",
  1349. ],
  1350. default="addIfDifferentNeighbor",
  1351. help="Action if trailer already exists",
  1352. )
  1353. parser.add_argument(
  1354. "--if-missing",
  1355. choices=["add", "doNothing"],
  1356. default="add",
  1357. help="Action if trailer is missing",
  1358. )
  1359. parsed_args = parser.parse_args(args)
  1360. # Read message from file or stdin
  1361. if parsed_args.file:
  1362. with open(parsed_args.file, "rb") as f:
  1363. message = f.read()
  1364. else:
  1365. message = sys.stdin.buffer.read()
  1366. # Parse trailer arguments
  1367. trailer_list = []
  1368. if parsed_args.trailers:
  1369. for trailer_spec in parsed_args.trailers:
  1370. # Parse "key:value" or "key=value" or just "key"
  1371. if ":" in trailer_spec:
  1372. key, value = trailer_spec.split(":", 1)
  1373. elif "=" in trailer_spec:
  1374. key, value = trailer_spec.split("=", 1)
  1375. else:
  1376. key = trailer_spec
  1377. value = ""
  1378. trailer_list.append((key.strip(), value.strip()))
  1379. # Call interpret_trailers
  1380. result = porcelain.interpret_trailers(
  1381. message,
  1382. trailers=trailer_list if trailer_list else None,
  1383. trim_empty=parsed_args.trim_empty,
  1384. only_trailers=parsed_args.only_trailers,
  1385. only_input=parsed_args.only_input,
  1386. unfold=parsed_args.unfold,
  1387. parse=parsed_args.parse,
  1388. where=parsed_args.where,
  1389. if_exists=parsed_args.if_exists,
  1390. if_missing=parsed_args.if_missing,
  1391. )
  1392. # Output result
  1393. sys.stdout.buffer.write(result)
  1394. class cmd_stripspace(Command):
  1395. """Remove unnecessary whitespace from text."""
  1396. def run(self, args: Sequence[str]) -> None:
  1397. """Execute the stripspace command.
  1398. Args:
  1399. args: Command line arguments
  1400. """
  1401. parser = argparse.ArgumentParser()
  1402. parser.add_argument(
  1403. "file",
  1404. nargs="?",
  1405. help="File to read text from. If not specified, reads from stdin.",
  1406. )
  1407. parser.add_argument(
  1408. "-s",
  1409. "--strip-comments",
  1410. action="store_true",
  1411. help="Strip lines that begin with comment character",
  1412. )
  1413. parser.add_argument(
  1414. "-c",
  1415. "--comment-lines",
  1416. action="store_true",
  1417. help="Prepend comment character to each line",
  1418. )
  1419. parser.add_argument(
  1420. "--comment-char",
  1421. default="#",
  1422. help="Comment character to use (default: #)",
  1423. )
  1424. parsed_args = parser.parse_args(args)
  1425. # Read text from file or stdin
  1426. if parsed_args.file:
  1427. with open(parsed_args.file, "rb") as f:
  1428. text = f.read()
  1429. else:
  1430. text = sys.stdin.buffer.read()
  1431. # Call stripspace
  1432. result = porcelain.stripspace(
  1433. text,
  1434. strip_comments=parsed_args.strip_comments,
  1435. comment_char=parsed_args.comment_char,
  1436. comment_lines=parsed_args.comment_lines,
  1437. )
  1438. # Output result
  1439. sys.stdout.buffer.write(result)
  1440. class cmd_column(Command):
  1441. """Display data in columns."""
  1442. def run(self, args: Sequence[str]) -> None:
  1443. """Execute the column command.
  1444. Args:
  1445. args: Command line arguments
  1446. """
  1447. parser = argparse.ArgumentParser(
  1448. description="Format input data into columns for better readability"
  1449. )
  1450. parser.add_argument(
  1451. "--mode",
  1452. default="column",
  1453. help=(
  1454. "Layout mode: 'column' (fill columns first), 'row' (fill rows first), "
  1455. "'plain' (one column). Add ',dense' for unequal column widths, "
  1456. "',nodense' for equal widths (default: column)"
  1457. ),
  1458. )
  1459. parser.add_argument(
  1460. "--width",
  1461. type=int,
  1462. help="Terminal width (default: auto-detect)",
  1463. )
  1464. parser.add_argument(
  1465. "--indent",
  1466. default="",
  1467. help="String to prepend to each line (default: empty)",
  1468. )
  1469. parser.add_argument(
  1470. "--nl",
  1471. default="\n",
  1472. help="String to append to each line, including newline (default: \\n)",
  1473. )
  1474. parser.add_argument(
  1475. "--padding",
  1476. type=int,
  1477. default=1,
  1478. help="Number of spaces between columns (default: 1)",
  1479. )
  1480. parsed_args = parser.parse_args(args)
  1481. # Read lines from stdin
  1482. lines = []
  1483. for line in sys.stdin:
  1484. # Strip the newline but keep the content
  1485. lines.append(line.rstrip("\n\r"))
  1486. # Format and output
  1487. result = format_columns(
  1488. lines,
  1489. width=parsed_args.width,
  1490. mode=parsed_args.mode,
  1491. padding=parsed_args.padding,
  1492. indent=parsed_args.indent,
  1493. nl=parsed_args.nl,
  1494. )
  1495. sys.stdout.write(result)
  1496. class cmd_init(Command):
  1497. """Create an empty Git repository or reinitialize an existing one."""
  1498. def run(self, args: Sequence[str]) -> None:
  1499. """Execute the init command.
  1500. Args:
  1501. args: Command line arguments
  1502. """
  1503. parser = argparse.ArgumentParser()
  1504. parser.add_argument(
  1505. "--bare", action="store_true", help="Create a bare repository"
  1506. )
  1507. parser.add_argument(
  1508. "path", nargs="?", default=os.getcwd(), help="Repository path"
  1509. )
  1510. parsed_args = parser.parse_args(args)
  1511. porcelain.init(parsed_args.path, bare=parsed_args.bare)
  1512. class cmd_clone(Command):
  1513. """Clone a repository into a new directory."""
  1514. def run(self, args: Sequence[str]) -> None:
  1515. """Execute the clone command.
  1516. Args:
  1517. args: Command line arguments
  1518. """
  1519. parser = argparse.ArgumentParser()
  1520. parser.add_argument(
  1521. "--bare",
  1522. help="Whether to create a bare repository.",
  1523. action="store_true",
  1524. )
  1525. parser.add_argument("--depth", type=int, help="Depth at which to fetch")
  1526. parser.add_argument(
  1527. "-b",
  1528. "--branch",
  1529. type=str,
  1530. help="Check out branch instead of branch pointed to by remote HEAD",
  1531. )
  1532. parser.add_argument(
  1533. "--refspec",
  1534. type=str,
  1535. help="References to fetch",
  1536. action="append",
  1537. )
  1538. parser.add_argument(
  1539. "--filter",
  1540. dest="filter_spec",
  1541. type=str,
  1542. help="git-rev-list-style object filter",
  1543. )
  1544. parser.add_argument(
  1545. "--protocol",
  1546. type=int,
  1547. help="Git protocol version to use",
  1548. )
  1549. parser.add_argument(
  1550. "--recurse-submodules",
  1551. action="store_true",
  1552. help="Initialize and clone submodules",
  1553. )
  1554. parser.add_argument("source", help="Repository to clone from")
  1555. parser.add_argument("target", nargs="?", help="Directory to clone into")
  1556. parsed_args = parser.parse_args(args)
  1557. try:
  1558. porcelain.clone(
  1559. parsed_args.source,
  1560. parsed_args.target,
  1561. bare=parsed_args.bare,
  1562. depth=parsed_args.depth,
  1563. branch=parsed_args.branch,
  1564. refspec=parsed_args.refspec,
  1565. filter_spec=parsed_args.filter_spec,
  1566. protocol_version=parsed_args.protocol,
  1567. recurse_submodules=parsed_args.recurse_submodules,
  1568. )
  1569. except GitProtocolError as e:
  1570. logging.exception(e)
  1571. def _get_commit_message_with_template(
  1572. initial_message: bytes | None,
  1573. repo: Repo | None = None,
  1574. commit: Commit | None = None,
  1575. ) -> bytes:
  1576. """Get commit message with an initial message template."""
  1577. # Start with the initial message
  1578. template = initial_message or b""
  1579. if template and not template.endswith(b"\n"):
  1580. template += b"\n"
  1581. template += b"\n"
  1582. template += b"# Please enter the commit message for your changes. Lines starting\n"
  1583. template += b"# with '#' will be ignored, and an empty message aborts the commit.\n"
  1584. template += b"#\n"
  1585. # Add branch info if repo is provided
  1586. if repo:
  1587. try:
  1588. ref_names, _ref_sha = repo.refs.follow(HEADREF)
  1589. ref_path = ref_names[-1] # Get the final reference
  1590. if ref_path.startswith(b"refs/heads/"):
  1591. branch = ref_path[11:] # Remove 'refs/heads/' prefix
  1592. else:
  1593. branch = ref_path
  1594. template += b"# On branch %s\n" % branch
  1595. except (KeyError, IndexError):
  1596. template += b"# On branch (unknown)\n"
  1597. template += b"#\n"
  1598. template += b"# Changes to be committed:\n"
  1599. # Launch editor
  1600. content = launch_editor(template)
  1601. # Remove comment lines and strip
  1602. lines = content.split(b"\n")
  1603. message_lines = [line for line in lines if not line.strip().startswith(b"#")]
  1604. message = b"\n".join(message_lines).strip()
  1605. if not message:
  1606. raise CommitMessageError("Aborting commit due to empty commit message")
  1607. return message
  1608. class cmd_config(Command):
  1609. """Get and set repository or global options."""
  1610. def run(self, args: Sequence[str]) -> int | None:
  1611. """Execute the config command.
  1612. Args:
  1613. args: Command line arguments
  1614. """
  1615. parser = argparse.ArgumentParser()
  1616. parser.add_argument(
  1617. "--global",
  1618. dest="global_config",
  1619. action="store_true",
  1620. help="Use global config file",
  1621. )
  1622. parser.add_argument(
  1623. "--local",
  1624. action="store_true",
  1625. help="Use repository config file (default)",
  1626. )
  1627. parser.add_argument(
  1628. "-l",
  1629. "--list",
  1630. action="store_true",
  1631. help="List all variables",
  1632. )
  1633. parser.add_argument(
  1634. "--unset",
  1635. action="store_true",
  1636. help="Remove a variable",
  1637. )
  1638. parser.add_argument(
  1639. "--unset-all",
  1640. action="store_true",
  1641. help="Remove all matches for a variable",
  1642. )
  1643. parser.add_argument(
  1644. "--get-all",
  1645. action="store_true",
  1646. help="Get all values for a multivar",
  1647. )
  1648. parser.add_argument(
  1649. "key",
  1650. nargs="?",
  1651. help="Config key (e.g., user.name)",
  1652. )
  1653. parser.add_argument(
  1654. "value",
  1655. nargs="?",
  1656. help="Config value to set",
  1657. )
  1658. parsed_args = parser.parse_args(args)
  1659. # Determine which config file to use
  1660. if parsed_args.global_config:
  1661. # Use global config file
  1662. config_path = os.path.expanduser("~/.gitconfig")
  1663. try:
  1664. from .config import ConfigFile
  1665. config = ConfigFile.from_path(config_path)
  1666. except FileNotFoundError:
  1667. from .config import ConfigFile
  1668. config = ConfigFile()
  1669. config.path = config_path
  1670. else:
  1671. # Use local repository config (default)
  1672. try:
  1673. repo = Repo(".")
  1674. config = repo.get_config()
  1675. except NotGitRepository:
  1676. logger.error("error: not a git repository")
  1677. return 1
  1678. # Handle --list
  1679. if parsed_args.list:
  1680. for section in config.sections():
  1681. for key, value in config.items(section):
  1682. section_str = ".".join(
  1683. s.decode("utf-8") if isinstance(s, bytes) else s
  1684. for s in section
  1685. )
  1686. key_str = key.decode("utf-8") if isinstance(key, bytes) else key
  1687. value_str = (
  1688. value.decode("utf-8") if isinstance(value, bytes) else value
  1689. )
  1690. print(f"{section_str}.{key_str}={value_str}")
  1691. return 0
  1692. # Handle --unset or --unset-all
  1693. if parsed_args.unset or parsed_args.unset_all:
  1694. if not parsed_args.key:
  1695. logger.error("error: key is required for --unset")
  1696. return 1
  1697. # Parse the key (e.g., "user.name" or "remote.origin.url")
  1698. parts = parsed_args.key.split(".")
  1699. if len(parts) < 2:
  1700. logger.error("error: invalid key format")
  1701. return 1
  1702. if len(parts) == 2:
  1703. section = (parts[0],)
  1704. name = parts[1]
  1705. else:
  1706. # For keys like "remote.origin.url", section is ("remote", "origin")
  1707. section = tuple(parts[:-1])
  1708. name = parts[-1]
  1709. try:
  1710. # Check if the key exists first
  1711. try:
  1712. config.get(section, name)
  1713. except KeyError:
  1714. logger.error(f"error: key '{parsed_args.key}' not found")
  1715. return 1
  1716. # Delete the configuration key using ConfigDict's delete method
  1717. section_bytes = tuple(
  1718. s.encode("utf-8") if isinstance(s, str) else s for s in section
  1719. )
  1720. name_bytes = name.encode("utf-8") if isinstance(name, str) else name
  1721. section_dict = config._values.get(section_bytes)
  1722. if section_dict:
  1723. del section_dict[name_bytes]
  1724. config.write_to_path()
  1725. else:
  1726. logger.error(f"error: key '{parsed_args.key}' not found")
  1727. return 1
  1728. except Exception as e:
  1729. logger.error(f"error: {e}")
  1730. return 1
  1731. return 0
  1732. # Handle --get-all
  1733. if parsed_args.get_all:
  1734. if not parsed_args.key:
  1735. logger.error("error: key is required for --get-all")
  1736. return 1
  1737. parts = parsed_args.key.split(".")
  1738. if len(parts) < 2:
  1739. logger.error("error: invalid key format")
  1740. return 1
  1741. if len(parts) == 2:
  1742. section = (parts[0],)
  1743. name = parts[1]
  1744. else:
  1745. section = tuple(parts[:-1])
  1746. name = parts[-1]
  1747. try:
  1748. for value in config.get_multivar(section, name):
  1749. value_str = (
  1750. value.decode("utf-8") if isinstance(value, bytes) else value
  1751. )
  1752. print(value_str)
  1753. return 0
  1754. except KeyError:
  1755. return 1
  1756. # Handle get (no value provided)
  1757. if parsed_args.key and not parsed_args.value:
  1758. parts = parsed_args.key.split(".")
  1759. if len(parts) < 2:
  1760. logger.error("error: invalid key format")
  1761. return 1
  1762. if len(parts) == 2:
  1763. section = (parts[0],)
  1764. name = parts[1]
  1765. else:
  1766. # For keys like "remote.origin.url", section is ("remote", "origin")
  1767. section = tuple(parts[:-1])
  1768. name = parts[-1]
  1769. try:
  1770. value = config.get(section, name)
  1771. value_str = value.decode("utf-8") if isinstance(value, bytes) else value
  1772. print(value_str)
  1773. return 0
  1774. except KeyError:
  1775. return 1
  1776. # Handle set (key and value provided)
  1777. if parsed_args.key and parsed_args.value:
  1778. parts = parsed_args.key.split(".")
  1779. if len(parts) < 2:
  1780. logger.error("error: invalid key format")
  1781. return 1
  1782. if len(parts) == 2:
  1783. section = (parts[0],)
  1784. name = parts[1]
  1785. else:
  1786. # For keys like "remote.origin.url", section is ("remote", "origin")
  1787. section = tuple(parts[:-1])
  1788. name = parts[-1]
  1789. config.set(section, name, parsed_args.value)
  1790. if parsed_args.global_config:
  1791. config.write_to_path()
  1792. else:
  1793. config.write_to_path()
  1794. return 0
  1795. # No action specified
  1796. parser.print_help()
  1797. return 1
  1798. class cmd_commit(Command):
  1799. """Record changes to the repository."""
  1800. def run(self, args: Sequence[str]) -> int | None:
  1801. """Execute the commit command.
  1802. Args:
  1803. args: Command line arguments
  1804. """
  1805. parser = argparse.ArgumentParser()
  1806. parser.add_argument("--message", "-m", help="Commit message")
  1807. parser.add_argument(
  1808. "-a",
  1809. "--all",
  1810. action="store_true",
  1811. help="Automatically stage all tracked files that have been modified",
  1812. )
  1813. parser.add_argument(
  1814. "--amend",
  1815. action="store_true",
  1816. help="Replace the tip of the current branch by creating a new commit",
  1817. )
  1818. parsed_args = parser.parse_args(args)
  1819. message: bytes | str | Callable[[Repo | None, Commit | None], bytes]
  1820. if parsed_args.message:
  1821. message = parsed_args.message
  1822. elif parsed_args.amend:
  1823. # For amend, create a callable that opens editor with original message pre-populated
  1824. def get_amend_message(repo: Repo | None, commit: Commit | None) -> bytes:
  1825. # Get the original commit message from current HEAD
  1826. assert repo is not None
  1827. try:
  1828. head_commit = repo[repo.head()]
  1829. assert isinstance(head_commit, Commit)
  1830. original_message = head_commit.message
  1831. except KeyError:
  1832. original_message = b""
  1833. # Open editor with original message
  1834. return _get_commit_message_with_template(original_message, repo, commit)
  1835. message = get_amend_message
  1836. else:
  1837. # For regular commits, use empty template
  1838. def get_regular_message(repo: Repo | None, commit: Commit | None) -> bytes:
  1839. return _get_commit_message_with_template(b"", repo, commit)
  1840. message = get_regular_message
  1841. try:
  1842. porcelain.commit(
  1843. ".", message=message, all=parsed_args.all, amend=parsed_args.amend
  1844. )
  1845. except CommitMessageError as e:
  1846. logging.exception(e)
  1847. return 1
  1848. return None
  1849. class cmd_commit_tree(Command):
  1850. """Create a new commit object from a tree."""
  1851. def run(self, args: Sequence[str]) -> None:
  1852. """Execute the commit-tree command.
  1853. Args:
  1854. args: Command line arguments
  1855. """
  1856. parser = argparse.ArgumentParser()
  1857. parser.add_argument("--message", "-m", required=True, help="Commit message")
  1858. parser.add_argument("tree", help="Tree SHA to commit")
  1859. parsed_args = parser.parse_args(args)
  1860. porcelain.commit_tree(".", tree=parsed_args.tree, message=parsed_args.message)
  1861. class cmd_update_server_info(Command):
  1862. """Update auxiliary info file to help dumb servers."""
  1863. def run(self, args: Sequence[str]) -> None:
  1864. """Execute the update-server-info command.
  1865. Args:
  1866. args: Command line arguments
  1867. """
  1868. porcelain.update_server_info(".")
  1869. class cmd_symbolic_ref(Command):
  1870. """Read, modify and delete symbolic refs."""
  1871. def run(self, args: Sequence[str]) -> int | None:
  1872. """Execute the symbolic-ref command.
  1873. Args:
  1874. args: Command line arguments
  1875. """
  1876. parser = argparse.ArgumentParser()
  1877. parser.add_argument("name", help="Symbolic reference name")
  1878. parser.add_argument("ref", nargs="?", help="Target reference")
  1879. parser.add_argument("--force", action="store_true", help="Force update")
  1880. parsed_args = parser.parse_args(args)
  1881. # If ref is provided, we're setting; otherwise we're reading
  1882. if parsed_args.ref:
  1883. # Set symbolic reference
  1884. from .repo import Repo
  1885. with Repo(".") as repo:
  1886. repo.refs.set_symbolic_ref(
  1887. parsed_args.name.encode(), parsed_args.ref.encode()
  1888. )
  1889. return 0
  1890. else:
  1891. # Read symbolic reference
  1892. from .repo import Repo
  1893. with Repo(".") as repo:
  1894. try:
  1895. target = repo.refs.read_ref(parsed_args.name.encode())
  1896. if target is None:
  1897. logger.error(
  1898. "fatal: ref '%s' is not a symbolic ref", parsed_args.name
  1899. )
  1900. return 1
  1901. elif target.startswith(b"ref: "):
  1902. logger.info(target[5:].decode())
  1903. else:
  1904. logger.info(target.decode())
  1905. return 0
  1906. except KeyError:
  1907. logger.error(
  1908. "fatal: ref '%s' is not a symbolic ref", parsed_args.name
  1909. )
  1910. return 1
  1911. class cmd_pack_refs(Command):
  1912. """Pack heads and tags for efficient repository access."""
  1913. def run(self, argv: Sequence[str]) -> None:
  1914. """Execute the pack-refs command.
  1915. Args:
  1916. argv: Command line arguments
  1917. """
  1918. parser = argparse.ArgumentParser()
  1919. parser.add_argument("--all", action="store_true")
  1920. # ignored, we never prune
  1921. parser.add_argument("--no-prune", action="store_true")
  1922. args = parser.parse_args(argv)
  1923. porcelain.pack_refs(".", all=args.all)
  1924. class cmd_var(Command):
  1925. """Display Git logical variables."""
  1926. def run(self, argv: Sequence[str]) -> int | None:
  1927. """Execute the var command.
  1928. Args:
  1929. argv: Command line arguments
  1930. """
  1931. parser = argparse.ArgumentParser()
  1932. parser.add_argument(
  1933. "variable",
  1934. nargs="?",
  1935. help="Variable to query (e.g., GIT_AUTHOR_IDENT)",
  1936. )
  1937. parser.add_argument(
  1938. "-l",
  1939. "--list",
  1940. action="store_true",
  1941. help="List all variables",
  1942. )
  1943. args = parser.parse_args(argv)
  1944. if args.list:
  1945. # List all variables
  1946. variables = porcelain.var_list(".")
  1947. for key, value in sorted(variables.items()):
  1948. print(f"{key}={value}")
  1949. return 0
  1950. elif args.variable:
  1951. # Query specific variable
  1952. try:
  1953. value = porcelain.var(".", variable=args.variable)
  1954. print(value)
  1955. return 0
  1956. except KeyError:
  1957. logger.error("error: variable '%s' has no value", args.variable)
  1958. return 1
  1959. else:
  1960. # No arguments - print error
  1961. logger.error("error: variable or -l is required")
  1962. parser.print_help()
  1963. return 1
  1964. class cmd_show(Command):
  1965. """Show various types of objects."""
  1966. def run(self, argv: Sequence[str]) -> None:
  1967. """Execute the show command.
  1968. Args:
  1969. argv: Command line arguments
  1970. """
  1971. parser = argparse.ArgumentParser()
  1972. parser.add_argument("objectish", type=str, nargs="*")
  1973. parser.add_argument(
  1974. "--color",
  1975. choices=["always", "never", "auto"],
  1976. default="auto",
  1977. help="Use colored output (requires rich)",
  1978. )
  1979. args = parser.parse_args(argv)
  1980. # Determine if we should use color
  1981. def _should_use_color() -> bool:
  1982. if args.color == "always":
  1983. return True
  1984. elif args.color == "never":
  1985. return False
  1986. else: # auto
  1987. return sys.stdout.isatty()
  1988. def _create_output_stream(outstream: TextIO) -> TextIO:
  1989. """Create output stream, optionally with colorization."""
  1990. if not _should_use_color():
  1991. return outstream
  1992. from .diff import ColorizedDiffStream
  1993. if not ColorizedDiffStream.is_available():
  1994. if args.color == "always":
  1995. raise ImportError(
  1996. "Rich is required for colored output. Install with: pip install 'dulwich[colordiff]'"
  1997. )
  1998. else:
  1999. logging.warning(
  2000. "Rich not available, disabling colored output. Install with: pip install 'dulwich[colordiff]'"
  2001. )
  2002. return outstream
  2003. # Wrap the ColorizedDiffStream (BinaryIO) back to TextIO
  2004. import io
  2005. colorized = ColorizedDiffStream(outstream.buffer)
  2006. return io.TextIOWrapper(colorized, encoding="utf-8", line_buffering=True)
  2007. with Repo(".") as repo:
  2008. config = repo.get_config_stack()
  2009. with get_pager(config=config, cmd_name="show") as outstream:
  2010. output_stream = _create_output_stream(outstream)
  2011. porcelain.show(repo, args.objectish or None, outstream=output_stream)
  2012. class cmd_show_ref(Command):
  2013. """List references in a local repository."""
  2014. def run(self, args: Sequence[str]) -> int | None:
  2015. """Execute the show-ref command.
  2016. Args:
  2017. args: Command line arguments
  2018. Returns:
  2019. Exit code (0 for success, 1 for error/no matches, 2 for missing ref with --exists)
  2020. """
  2021. parser = argparse.ArgumentParser()
  2022. parser.add_argument(
  2023. "--head",
  2024. action="store_true",
  2025. help="Show the HEAD reference",
  2026. )
  2027. parser.add_argument(
  2028. "--branches",
  2029. action="store_true",
  2030. help="Limit to local branches",
  2031. )
  2032. parser.add_argument(
  2033. "--tags",
  2034. action="store_true",
  2035. help="Limit to local tags",
  2036. )
  2037. parser.add_argument(
  2038. "-d",
  2039. "--dereference",
  2040. action="store_true",
  2041. help="Dereference tags into object IDs",
  2042. )
  2043. parser.add_argument(
  2044. "-s",
  2045. "--hash",
  2046. nargs="?",
  2047. const=40,
  2048. type=int,
  2049. metavar="n",
  2050. help="Only show the OID, not the reference name",
  2051. )
  2052. parser.add_argument(
  2053. "--abbrev",
  2054. nargs="?",
  2055. const=7,
  2056. type=int,
  2057. metavar="n",
  2058. help="Abbreviate the object name",
  2059. )
  2060. parser.add_argument(
  2061. "--verify",
  2062. action="store_true",
  2063. help="Enable stricter reference checking (exact path match)",
  2064. )
  2065. parser.add_argument(
  2066. "--exists",
  2067. action="store_true",
  2068. help="Check whether the given reference exists",
  2069. )
  2070. parser.add_argument(
  2071. "-q",
  2072. "--quiet",
  2073. action="store_true",
  2074. help="Do not print any results to stdout",
  2075. )
  2076. parser.add_argument(
  2077. "patterns",
  2078. nargs="*",
  2079. help="Show references matching patterns",
  2080. )
  2081. parsed_args = parser.parse_args(args)
  2082. # Handle --exists mode
  2083. if parsed_args.exists:
  2084. if not parsed_args.patterns or len(parsed_args.patterns) != 1:
  2085. logger.error("--exists requires exactly one reference argument")
  2086. return 1
  2087. try:
  2088. with Repo(".") as repo:
  2089. repo_refs = repo.get_refs()
  2090. pattern_bytes = os.fsencode(parsed_args.patterns[0])
  2091. if pattern_bytes in repo_refs:
  2092. return 0 # Reference exists
  2093. else:
  2094. return 2 # Reference missing
  2095. except (NotGitRepository, OSError, FileFormatException) as e:
  2096. logger.error(f"Error looking up reference: {e}")
  2097. return 1 # Error looking up reference
  2098. # Regular show-ref mode
  2099. try:
  2100. matched_refs = porcelain.show_ref(
  2101. ".",
  2102. patterns=parsed_args.patterns if parsed_args.patterns else None,
  2103. head=parsed_args.head,
  2104. branches=parsed_args.branches,
  2105. tags=parsed_args.tags,
  2106. dereference=parsed_args.dereference,
  2107. verify=parsed_args.verify,
  2108. )
  2109. except (NotGitRepository, OSError, FileFormatException) as e:
  2110. logger.error(f"Error: {e}")
  2111. return 1
  2112. # Return error if no matches found (unless quiet)
  2113. if not matched_refs:
  2114. if parsed_args.verify and not parsed_args.quiet:
  2115. logger.error("error: no matching refs found")
  2116. return 1
  2117. # Output results
  2118. if not parsed_args.quiet:
  2119. abbrev_len = parsed_args.abbrev if parsed_args.abbrev else 40
  2120. hash_only = parsed_args.hash is not None
  2121. if hash_only and parsed_args.hash:
  2122. abbrev_len = parsed_args.hash
  2123. for sha, ref in matched_refs:
  2124. sha_str = sha.decode()
  2125. if abbrev_len < 40:
  2126. sha_str = sha_str[:abbrev_len]
  2127. if hash_only:
  2128. logger.info(sha_str)
  2129. else:
  2130. logger.info(f"{sha_str} {ref.decode()}")
  2131. return 0
  2132. class cmd_show_branch(Command):
  2133. """Show branches and their commits."""
  2134. def run(self, args: Sequence[str]) -> int | None:
  2135. """Execute the show-branch command.
  2136. Args:
  2137. args: Command line arguments
  2138. Returns:
  2139. Exit code (0 for success, 1 for error)
  2140. """
  2141. parser = argparse.ArgumentParser()
  2142. parser.add_argument(
  2143. "-r",
  2144. "--remotes",
  2145. action="store_true",
  2146. help="Show remote-tracking branches",
  2147. )
  2148. parser.add_argument(
  2149. "-a",
  2150. "--all",
  2151. dest="all_branches",
  2152. action="store_true",
  2153. help="Show both remote-tracking and local branches",
  2154. )
  2155. parser.add_argument(
  2156. "--current",
  2157. action="store_true",
  2158. help="Include current branch if not given on command line",
  2159. )
  2160. parser.add_argument(
  2161. "--topo-order",
  2162. dest="topo_order",
  2163. action="store_true",
  2164. help="Show commits in topological order",
  2165. )
  2166. parser.add_argument(
  2167. "--date-order",
  2168. action="store_true",
  2169. help="Show commits in date order (default)",
  2170. )
  2171. parser.add_argument(
  2172. "--more",
  2173. type=int,
  2174. metavar="n",
  2175. help="Show n more commits beyond common ancestor",
  2176. )
  2177. parser.add_argument(
  2178. "--list",
  2179. dest="list_branches",
  2180. action="store_true",
  2181. help="Show only branch names and their tip commits",
  2182. )
  2183. parser.add_argument(
  2184. "--independent",
  2185. dest="independent_branches",
  2186. action="store_true",
  2187. help="Show only branches not reachable from any other",
  2188. )
  2189. parser.add_argument(
  2190. "--merge-base",
  2191. dest="merge_base",
  2192. action="store_true",
  2193. help="Show merge base of specified branches",
  2194. )
  2195. parser.add_argument(
  2196. "branches",
  2197. nargs="*",
  2198. help="Branches to show (default: all local branches)",
  2199. )
  2200. parsed_args = parser.parse_args(args)
  2201. try:
  2202. output_lines = porcelain.show_branch(
  2203. ".",
  2204. branches=parsed_args.branches if parsed_args.branches else None,
  2205. all_branches=parsed_args.all_branches,
  2206. remotes=parsed_args.remotes,
  2207. current=parsed_args.current,
  2208. topo_order=parsed_args.topo_order,
  2209. more=parsed_args.more,
  2210. list_branches=parsed_args.list_branches,
  2211. independent_branches=parsed_args.independent_branches,
  2212. merge_base=parsed_args.merge_base,
  2213. )
  2214. except (NotGitRepository, OSError, FileFormatException) as e:
  2215. logger.error(f"Error: {e}")
  2216. return 1
  2217. # Output results
  2218. for line in output_lines:
  2219. logger.info(line)
  2220. return 0
  2221. class cmd_diff_tree(Command):
  2222. """Compare the content and mode of trees."""
  2223. def run(self, args: Sequence[str]) -> None:
  2224. """Execute the diff-tree command.
  2225. Args:
  2226. args: Command line arguments
  2227. """
  2228. parser = argparse.ArgumentParser()
  2229. parser.add_argument("old_tree", help="Old tree SHA")
  2230. parser.add_argument("new_tree", help="New tree SHA")
  2231. parsed_args = parser.parse_args(args)
  2232. porcelain.diff_tree(".", parsed_args.old_tree, parsed_args.new_tree)
  2233. class cmd_rev_list(Command):
  2234. """List commit objects in reverse chronological order."""
  2235. def run(self, args: Sequence[str]) -> None:
  2236. """Execute the rev-list command.
  2237. Args:
  2238. args: Command line arguments
  2239. """
  2240. parser = argparse.ArgumentParser()
  2241. parser.add_argument("commits", nargs="+", help="Commit IDs to list")
  2242. parsed_args = parser.parse_args(args)
  2243. porcelain.rev_list(".", parsed_args.commits)
  2244. class cmd_tag(Command):
  2245. """Create, list, delete or verify a tag object."""
  2246. def run(self, args: Sequence[str]) -> None:
  2247. """Execute the tag command.
  2248. Args:
  2249. args: Command line arguments
  2250. """
  2251. parser = argparse.ArgumentParser()
  2252. parser.add_argument(
  2253. "-a",
  2254. "--annotated",
  2255. help="Create an annotated tag.",
  2256. action="store_true",
  2257. )
  2258. parser.add_argument(
  2259. "-s", "--sign", help="Sign the annotated tag.", action="store_true"
  2260. )
  2261. parser.add_argument("tag_name", help="Name of the tag to create")
  2262. parsed_args = parser.parse_args(args)
  2263. porcelain.tag_create(
  2264. ".",
  2265. parsed_args.tag_name,
  2266. annotated=parsed_args.annotated,
  2267. sign=parsed_args.sign,
  2268. )
  2269. class cmd_verify_commit(Command):
  2270. """Check the GPG signature of commits."""
  2271. def run(self, args: Sequence[str]) -> int | None:
  2272. """Execute the verify-commit command.
  2273. Args:
  2274. args: Command line arguments
  2275. Returns:
  2276. Exit code (1 on error, None on success)
  2277. """
  2278. parser = argparse.ArgumentParser()
  2279. parser.add_argument(
  2280. "-v",
  2281. "--verbose",
  2282. help="Print the contents of the commit object before validating it.",
  2283. action="store_true",
  2284. )
  2285. parser.add_argument(
  2286. "--raw",
  2287. help="Print the raw gpg status output to standard error.",
  2288. action="store_true",
  2289. )
  2290. parser.add_argument(
  2291. "commits",
  2292. nargs="*",
  2293. default=["HEAD"],
  2294. help="Commits to verify (defaults to HEAD)",
  2295. )
  2296. parsed_args = parser.parse_args(args)
  2297. exit_code = None
  2298. for commit in parsed_args.commits:
  2299. try:
  2300. if parsed_args.verbose:
  2301. # Show commit contents before verification
  2302. porcelain.show(
  2303. ".",
  2304. objects=[commit],
  2305. outstream=sys.stdout,
  2306. )
  2307. porcelain.verify_commit(".", commit)
  2308. if not parsed_args.raw:
  2309. print(f"gpg: Good signature from commit '{commit}'")
  2310. except Exception as e:
  2311. if not parsed_args.raw:
  2312. print(f"error: {commit}: {e}", file=sys.stderr)
  2313. else:
  2314. # In raw mode, let the exception propagate
  2315. raise
  2316. exit_code = 1
  2317. return exit_code
  2318. class cmd_verify_tag(Command):
  2319. """Check the GPG signature of tags."""
  2320. def run(self, args: Sequence[str]) -> int | None:
  2321. """Execute the verify-tag command.
  2322. Args:
  2323. args: Command line arguments
  2324. Returns:
  2325. Exit code (1 on error, None on success)
  2326. """
  2327. parser = argparse.ArgumentParser()
  2328. parser.add_argument(
  2329. "-v",
  2330. "--verbose",
  2331. help="Print the contents of the tag object before validating it.",
  2332. action="store_true",
  2333. )
  2334. parser.add_argument(
  2335. "--raw",
  2336. help="Print the raw gpg status output to standard error.",
  2337. action="store_true",
  2338. )
  2339. parser.add_argument("tags", nargs="+", help="Tags to verify")
  2340. parsed_args = parser.parse_args(args)
  2341. exit_code = None
  2342. for tag in parsed_args.tags:
  2343. try:
  2344. if parsed_args.verbose:
  2345. # Show tag contents before verification
  2346. porcelain.show(
  2347. ".",
  2348. objects=[tag],
  2349. outstream=sys.stdout,
  2350. )
  2351. porcelain.verify_tag(".", tag)
  2352. if not parsed_args.raw:
  2353. print(f"gpg: Good signature from tag '{tag}'")
  2354. except Exception as e:
  2355. if not parsed_args.raw:
  2356. print(f"error: {tag}: {e}", file=sys.stderr)
  2357. else:
  2358. # In raw mode, let the exception propagate
  2359. raise
  2360. exit_code = 1
  2361. return exit_code
  2362. class cmd_repack(Command):
  2363. """Pack unpacked objects in a repository."""
  2364. def run(self, args: Sequence[str]) -> None:
  2365. """Execute the repack command.
  2366. Args:
  2367. args: Command line arguments
  2368. """
  2369. parser = argparse.ArgumentParser()
  2370. parser.add_argument(
  2371. "--write-bitmap-index",
  2372. action="store_true",
  2373. help="write a bitmap index for packs",
  2374. )
  2375. parsed_args = parser.parse_args(args)
  2376. porcelain.repack(".", write_bitmaps=parsed_args.write_bitmap_index)
  2377. class cmd_reflog(Command):
  2378. """Manage reflog information."""
  2379. def run(self, args: Sequence[str]) -> None:
  2380. """Execute the reflog command.
  2381. Args:
  2382. args: Command line arguments
  2383. """
  2384. parser = argparse.ArgumentParser(prog="dulwich reflog")
  2385. subparsers = parser.add_subparsers(dest="subcommand", help="Subcommand")
  2386. # Show subcommand (default when no subcommand is specified)
  2387. show_parser = subparsers.add_parser(
  2388. "show", help="Show reflog entries (default)", add_help=False
  2389. )
  2390. show_parser.add_argument(
  2391. "ref", nargs="?", default="HEAD", help="Reference to show reflog for"
  2392. )
  2393. show_parser.add_argument(
  2394. "--all", action="store_true", help="Show reflogs for all refs"
  2395. )
  2396. # Expire subcommand
  2397. expire_parser = subparsers.add_parser("expire", help="Expire reflog entries")
  2398. expire_parser.add_argument(
  2399. "ref", nargs="?", help="Reference to expire reflog for"
  2400. )
  2401. expire_parser.add_argument(
  2402. "--all", action="store_true", help="Expire reflogs for all refs"
  2403. )
  2404. expire_parser.add_argument(
  2405. "--expire",
  2406. type=str,
  2407. help="Expire entries older than time (e.g., '90 days ago', 'all', 'never')",
  2408. )
  2409. expire_parser.add_argument(
  2410. "--expire-unreachable",
  2411. type=str,
  2412. help="Expire unreachable entries older than time",
  2413. )
  2414. expire_parser.add_argument(
  2415. "--dry-run", "-n", action="store_true", help="Show what would be expired"
  2416. )
  2417. # Delete subcommand
  2418. delete_parser = subparsers.add_parser(
  2419. "delete", help="Delete specific reflog entry"
  2420. )
  2421. delete_parser.add_argument(
  2422. "refspec", help="Reference specification (e.g., HEAD@{1})"
  2423. )
  2424. delete_parser.add_argument(
  2425. "--rewrite",
  2426. action="store_true",
  2427. help="Rewrite subsequent entries to maintain consistency",
  2428. )
  2429. # If no arguments or first arg is not a subcommand, treat as show
  2430. if not args or (args[0] not in ["show", "expire", "delete"]):
  2431. # Parse as show command
  2432. parsed_args = parser.parse_args(["show", *list(args)])
  2433. else:
  2434. parsed_args = parser.parse_args(args)
  2435. if parsed_args.subcommand == "expire":
  2436. self._run_expire(parsed_args)
  2437. elif parsed_args.subcommand == "delete":
  2438. self._run_delete(parsed_args)
  2439. else: # show or default
  2440. self._run_show(parsed_args)
  2441. def _run_show(self, parsed_args: argparse.Namespace) -> None:
  2442. """Show reflog entries."""
  2443. with Repo(".") as repo:
  2444. config = repo.get_config_stack()
  2445. with get_pager(config=config, cmd_name="reflog") as outstream:
  2446. if parsed_args.all:
  2447. # Show reflogs for all refs
  2448. for ref_bytes, entry in porcelain.reflog(repo, all=True):
  2449. ref_str = ref_bytes.decode("utf-8", "replace")
  2450. short_new = entry.new_sha[:8].decode("ascii")
  2451. outstream.write(
  2452. f"{short_new} {ref_str}: {entry.message.decode('utf-8', 'replace')}\n"
  2453. )
  2454. else:
  2455. ref = (
  2456. parsed_args.ref.encode("utf-8")
  2457. if isinstance(parsed_args.ref, str)
  2458. else parsed_args.ref
  2459. )
  2460. for i, entry in enumerate(porcelain.reflog(repo, ref)):
  2461. # Format similar to git reflog
  2462. from dulwich.reflog import Entry
  2463. assert isinstance(entry, Entry)
  2464. short_new = entry.new_sha[:8].decode("ascii")
  2465. message = (
  2466. entry.message.decode("utf-8", "replace")
  2467. if entry.message
  2468. else ""
  2469. )
  2470. outstream.write(
  2471. f"{short_new} {ref.decode('utf-8', 'replace')}@{{{i}}}: {message}\n"
  2472. )
  2473. def _run_expire(self, parsed_args: argparse.Namespace) -> None:
  2474. """Expire reflog entries."""
  2475. # Parse time specifications
  2476. expire_time = None
  2477. expire_unreachable_time = None
  2478. if parsed_args.expire:
  2479. expire_time = parse_time_to_timestamp(parsed_args.expire)
  2480. if parsed_args.expire_unreachable:
  2481. expire_unreachable_time = parse_time_to_timestamp(
  2482. parsed_args.expire_unreachable
  2483. )
  2484. # Execute expire
  2485. result = porcelain.reflog_expire(
  2486. repo=".",
  2487. ref=parsed_args.ref,
  2488. all=parsed_args.all,
  2489. expire_time=expire_time,
  2490. expire_unreachable_time=expire_unreachable_time,
  2491. dry_run=parsed_args.dry_run,
  2492. )
  2493. # Print results
  2494. for ref_name, count in result.items():
  2495. ref_str = ref_name.decode("utf-8", "replace")
  2496. if parsed_args.dry_run:
  2497. print(f"Would expire {count} entries from {ref_str}")
  2498. else:
  2499. print(f"Expired {count} entries from {ref_str}")
  2500. def _run_delete(self, parsed_args: argparse.Namespace) -> None:
  2501. """Delete a specific reflog entry."""
  2502. from dulwich.reflog import parse_reflog_spec
  2503. # Parse refspec (e.g., "HEAD@{1}" or "refs/heads/master@{2}")
  2504. ref, index = parse_reflog_spec(parsed_args.refspec)
  2505. # Execute delete
  2506. porcelain.reflog_delete(
  2507. repo=".",
  2508. ref=ref,
  2509. index=index,
  2510. rewrite=parsed_args.rewrite,
  2511. )
  2512. print(f"Deleted entry {ref.decode('utf-8', 'replace')}@{{{index}}}")
  2513. class cmd_reset(Command):
  2514. """Reset current HEAD to the specified state."""
  2515. def run(self, args: Sequence[str]) -> None:
  2516. """Execute the reset command.
  2517. Args:
  2518. args: Command line arguments
  2519. """
  2520. parser = argparse.ArgumentParser()
  2521. mode_group = parser.add_mutually_exclusive_group()
  2522. mode_group.add_argument(
  2523. "--hard", action="store_true", help="Reset working tree and index"
  2524. )
  2525. mode_group.add_argument("--soft", action="store_true", help="Reset only HEAD")
  2526. mode_group.add_argument(
  2527. "--mixed", action="store_true", help="Reset HEAD and index"
  2528. )
  2529. parser.add_argument("treeish", nargs="?", help="Commit/tree to reset to")
  2530. parsed_args = parser.parse_args(args)
  2531. if parsed_args.hard:
  2532. mode = "hard"
  2533. elif parsed_args.soft:
  2534. mode = "soft"
  2535. elif parsed_args.mixed:
  2536. mode = "mixed"
  2537. else:
  2538. # Default to mixed behavior
  2539. mode = "mixed"
  2540. # Use the porcelain.reset function for all modes
  2541. porcelain.reset(".", mode=mode, treeish=parsed_args.treeish)
  2542. class cmd_revert(Command):
  2543. """Revert some existing commits."""
  2544. def run(self, args: Sequence[str]) -> None:
  2545. """Execute the revert command.
  2546. Args:
  2547. args: Command line arguments
  2548. """
  2549. parser = argparse.ArgumentParser()
  2550. parser.add_argument(
  2551. "--no-commit",
  2552. "-n",
  2553. action="store_true",
  2554. help="Apply changes but don't create a commit",
  2555. )
  2556. parser.add_argument("-m", "--message", help="Custom commit message")
  2557. parser.add_argument("commits", nargs="+", help="Commits to revert")
  2558. parsed_args = parser.parse_args(args)
  2559. result = porcelain.revert(
  2560. ".",
  2561. commits=parsed_args.commits,
  2562. no_commit=parsed_args.no_commit,
  2563. message=parsed_args.message,
  2564. )
  2565. if result and not parsed_args.no_commit:
  2566. logger.info("[%s] Revert completed", result.decode("ascii")[:7])
  2567. class cmd_daemon(Command):
  2568. """Run a simple Git protocol server."""
  2569. def run(self, args: Sequence[str]) -> None:
  2570. """Execute the daemon command.
  2571. Args:
  2572. args: Command line arguments
  2573. """
  2574. from dulwich import log_utils
  2575. from .protocol import TCP_GIT_PORT
  2576. parser = argparse.ArgumentParser()
  2577. parser.add_argument(
  2578. "-l",
  2579. "--listen_address",
  2580. default="localhost",
  2581. help="Binding IP address.",
  2582. )
  2583. parser.add_argument(
  2584. "-p",
  2585. "--port",
  2586. type=int,
  2587. default=TCP_GIT_PORT,
  2588. help="Binding TCP port.",
  2589. )
  2590. parser.add_argument(
  2591. "gitdir", nargs="?", default=".", help="Git directory to serve"
  2592. )
  2593. parsed_args = parser.parse_args(args)
  2594. log_utils.default_logging_config()
  2595. porcelain.daemon(
  2596. parsed_args.gitdir,
  2597. address=parsed_args.listen_address,
  2598. port=parsed_args.port,
  2599. )
  2600. class cmd_web_daemon(Command):
  2601. """Run a simple HTTP server for Git repositories."""
  2602. def run(self, args: Sequence[str]) -> None:
  2603. """Execute the web-daemon command.
  2604. Args:
  2605. args: Command line arguments
  2606. """
  2607. from dulwich import log_utils
  2608. parser = argparse.ArgumentParser()
  2609. parser.add_argument(
  2610. "-l",
  2611. "--listen_address",
  2612. default="",
  2613. help="Binding IP address.",
  2614. )
  2615. parser.add_argument(
  2616. "-p",
  2617. "--port",
  2618. type=int,
  2619. default=8000,
  2620. help="Binding TCP port.",
  2621. )
  2622. parser.add_argument(
  2623. "gitdir", nargs="?", default=".", help="Git directory to serve"
  2624. )
  2625. parsed_args = parser.parse_args(args)
  2626. log_utils.default_logging_config()
  2627. porcelain.web_daemon(
  2628. parsed_args.gitdir,
  2629. address=parsed_args.listen_address,
  2630. port=parsed_args.port,
  2631. )
  2632. class cmd_write_tree(Command):
  2633. """Create a tree object from the current index."""
  2634. def run(self, args: Sequence[str]) -> None:
  2635. """Execute the write-tree command.
  2636. Args:
  2637. args: Command line arguments
  2638. """
  2639. parser = argparse.ArgumentParser()
  2640. parser.parse_args(args)
  2641. sys.stdout.write("{}\n".format(porcelain.write_tree(".").decode()))
  2642. class cmd_receive_pack(Command):
  2643. """Receive what is pushed into the repository."""
  2644. def run(self, args: Sequence[str]) -> None:
  2645. """Execute the receive-pack command.
  2646. Args:
  2647. args: Command line arguments
  2648. """
  2649. parser = argparse.ArgumentParser()
  2650. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  2651. parsed_args = parser.parse_args(args)
  2652. porcelain.receive_pack(parsed_args.gitdir)
  2653. class cmd_upload_pack(Command):
  2654. """Send objects packed back to git-fetch-pack."""
  2655. def run(self, args: Sequence[str]) -> None:
  2656. """Execute the upload-pack command.
  2657. Args:
  2658. args: Command line arguments
  2659. """
  2660. parser = argparse.ArgumentParser()
  2661. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  2662. parsed_args = parser.parse_args(args)
  2663. porcelain.upload_pack(parsed_args.gitdir)
  2664. class cmd_shortlog(Command):
  2665. """Show a shortlog of commits by author."""
  2666. def run(self, args: Sequence[str]) -> None:
  2667. """Execute the shortlog command with the given CLI arguments.
  2668. Args:
  2669. args: List of command line arguments.
  2670. """
  2671. parser = argparse.ArgumentParser()
  2672. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  2673. parser.add_argument("--summary", action="store_true", help="Show summary only")
  2674. parser.add_argument(
  2675. "--sort", action="store_true", help="Sort authors by commit count"
  2676. )
  2677. parsed_args = parser.parse_args(args)
  2678. shortlog_items: list[dict[str, str]] = porcelain.shortlog(
  2679. repo=parsed_args.gitdir,
  2680. summary_only=parsed_args.summary,
  2681. sort_by_commits=parsed_args.sort,
  2682. )
  2683. for item in shortlog_items:
  2684. author: str = item["author"]
  2685. messages: str = item["messages"]
  2686. if parsed_args.summary:
  2687. count = len(messages.splitlines())
  2688. sys.stdout.write(f"{count}\t{author}\n")
  2689. else:
  2690. sys.stdout.write(f"{author} ({len(messages.splitlines())}):\n")
  2691. for msg in messages.splitlines():
  2692. sys.stdout.write(f" {msg}\n")
  2693. sys.stdout.write("\n")
  2694. class cmd_status(Command):
  2695. """Show the working tree status."""
  2696. def run(self, args: Sequence[str]) -> None:
  2697. """Execute the status command.
  2698. Args:
  2699. args: Command line arguments
  2700. """
  2701. parser = argparse.ArgumentParser()
  2702. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  2703. parser.add_argument(
  2704. "--column",
  2705. action="store_true",
  2706. help="Display untracked files in columns",
  2707. )
  2708. parsed_args = parser.parse_args(args)
  2709. status = porcelain.status(parsed_args.gitdir)
  2710. if any(names for (kind, names) in status.staged.items()):
  2711. sys.stdout.write("Changes to be committed:\n\n")
  2712. for kind, names in status.staged.items():
  2713. for name in names:
  2714. sys.stdout.write(
  2715. f"\t{kind}: {name.decode(sys.getfilesystemencoding())}\n"
  2716. )
  2717. sys.stdout.write("\n")
  2718. if status.unstaged:
  2719. sys.stdout.write("Changes not staged for commit:\n\n")
  2720. for name in status.unstaged:
  2721. sys.stdout.write(f"\t{name.decode(sys.getfilesystemencoding())}\n")
  2722. sys.stdout.write("\n")
  2723. if status.untracked:
  2724. sys.stdout.write("Untracked files:\n\n")
  2725. if parsed_args.column:
  2726. # Format untracked files in columns
  2727. untracked_names = [name for name in status.untracked]
  2728. output = format_columns(untracked_names, mode="column", indent="\t")
  2729. sys.stdout.write(output)
  2730. else:
  2731. for name in status.untracked:
  2732. sys.stdout.write(f"\t{name}\n")
  2733. sys.stdout.write("\n")
  2734. class cmd_ls_remote(Command):
  2735. """List references in a remote repository."""
  2736. def run(self, args: Sequence[str]) -> None:
  2737. """Execute the ls-remote command.
  2738. Args:
  2739. args: Command line arguments
  2740. """
  2741. parser = argparse.ArgumentParser()
  2742. parser.add_argument(
  2743. "--symref", action="store_true", help="Show symbolic references"
  2744. )
  2745. parser.add_argument("url", help="Remote URL to list references from")
  2746. parsed_args = parser.parse_args(args)
  2747. result = porcelain.ls_remote(parsed_args.url)
  2748. if parsed_args.symref:
  2749. # Show symrefs first, like git does
  2750. for ref, target in sorted(result.symrefs.items()):
  2751. if target:
  2752. sys.stdout.write(f"ref: {target.decode()}\t{ref.decode()}\n")
  2753. # Show regular refs
  2754. for ref in sorted(result.refs):
  2755. sha = result.refs[ref]
  2756. if sha is not None:
  2757. sys.stdout.write(f"{sha.decode()}\t{ref.decode()}\n")
  2758. class cmd_ls_tree(Command):
  2759. """List the contents of a tree object."""
  2760. def run(self, args: Sequence[str]) -> None:
  2761. """Execute the ls-tree command.
  2762. Args:
  2763. args: Command line arguments
  2764. """
  2765. parser = argparse.ArgumentParser()
  2766. parser.add_argument(
  2767. "-r",
  2768. "--recursive",
  2769. action="store_true",
  2770. help="Recursively list tree contents.",
  2771. )
  2772. parser.add_argument(
  2773. "--name-only", action="store_true", help="Only display name."
  2774. )
  2775. parser.add_argument("treeish", nargs="?", help="Tree-ish to list")
  2776. parsed_args = parser.parse_args(args)
  2777. with Repo(".") as repo:
  2778. config = repo.get_config_stack()
  2779. with get_pager(config=config, cmd_name="ls-tree") as outstream:
  2780. porcelain.ls_tree(
  2781. repo,
  2782. parsed_args.treeish,
  2783. outstream=outstream,
  2784. recursive=parsed_args.recursive,
  2785. name_only=parsed_args.name_only,
  2786. )
  2787. class cmd_pack_objects(Command):
  2788. """Create a packed archive of objects."""
  2789. def run(self, args: Sequence[str]) -> None:
  2790. """Execute the pack-objects command.
  2791. Args:
  2792. args: Command line arguments
  2793. """
  2794. parser = argparse.ArgumentParser()
  2795. parser.add_argument(
  2796. "--stdout", action="store_true", help="Write pack to stdout"
  2797. )
  2798. parser.add_argument("--deltify", action="store_true", help="Create deltas")
  2799. parser.add_argument(
  2800. "--no-reuse-deltas", action="store_true", help="Don't reuse existing deltas"
  2801. )
  2802. parser.add_argument("basename", nargs="?", help="Base name for pack files")
  2803. parsed_args = parser.parse_args(args)
  2804. if not parsed_args.stdout and not parsed_args.basename:
  2805. parser.error("basename required when not using --stdout")
  2806. object_ids = [ObjectID(line.strip().encode()) for line in sys.stdin.readlines()]
  2807. deltify = parsed_args.deltify
  2808. reuse_deltas = not parsed_args.no_reuse_deltas
  2809. if parsed_args.stdout:
  2810. packf = getattr(sys.stdout, "buffer", sys.stdout)
  2811. assert isinstance(packf, BinaryIO)
  2812. idxf = None
  2813. close = []
  2814. else:
  2815. packf = open(parsed_args.basename + ".pack", "wb")
  2816. idxf = open(parsed_args.basename + ".idx", "wb")
  2817. close = [packf, idxf]
  2818. porcelain.pack_objects(
  2819. ".", object_ids, packf, idxf, deltify=deltify, reuse_deltas=reuse_deltas
  2820. )
  2821. for f in close:
  2822. f.close()
  2823. class cmd_unpack_objects(Command):
  2824. """Unpack objects from a packed archive."""
  2825. def run(self, args: Sequence[str]) -> None:
  2826. """Execute the unpack-objects command.
  2827. Args:
  2828. args: Command line arguments
  2829. """
  2830. parser = argparse.ArgumentParser()
  2831. parser.add_argument("pack_file", help="Pack file to unpack")
  2832. parsed_args = parser.parse_args(args)
  2833. count = porcelain.unpack_objects(parsed_args.pack_file)
  2834. logger.info("Unpacked %d objects", count)
  2835. class cmd_prune(Command):
  2836. """Prune all unreachable objects from the object database."""
  2837. def run(self, args: Sequence[str]) -> int | None:
  2838. """Execute the prune command.
  2839. Args:
  2840. args: Command line arguments
  2841. """
  2842. import datetime
  2843. import time
  2844. from dulwich.object_store import DEFAULT_TEMPFILE_GRACE_PERIOD
  2845. parser = argparse.ArgumentParser(
  2846. description="Remove temporary pack files left behind by interrupted operations"
  2847. )
  2848. parser.add_argument(
  2849. "--expire",
  2850. nargs="?",
  2851. const="2.weeks.ago",
  2852. help="Only prune files older than the specified date (default: 2.weeks.ago)",
  2853. )
  2854. parser.add_argument(
  2855. "--dry-run",
  2856. "-n",
  2857. action="store_true",
  2858. help="Only report what would be removed",
  2859. )
  2860. parser.add_argument(
  2861. "--verbose",
  2862. "-v",
  2863. action="store_true",
  2864. help="Report all actions",
  2865. )
  2866. parsed_args = parser.parse_args(args)
  2867. # Parse expire grace period
  2868. grace_period = DEFAULT_TEMPFILE_GRACE_PERIOD
  2869. if parsed_args.expire:
  2870. from .approxidate import parse_relative_time
  2871. try:
  2872. grace_period = parse_relative_time(parsed_args.expire)
  2873. except ValueError:
  2874. # Try to parse as absolute date
  2875. try:
  2876. date = datetime.datetime.strptime(parsed_args.expire, "%Y-%m-%d")
  2877. grace_period = int(time.time() - date.timestamp())
  2878. except ValueError:
  2879. logger.error("Invalid expire date: %s", parsed_args.expire)
  2880. return 1
  2881. # Progress callback
  2882. def progress(msg: str) -> None:
  2883. if parsed_args.verbose:
  2884. logger.info("%s", msg)
  2885. try:
  2886. porcelain.prune(
  2887. ".",
  2888. grace_period=grace_period,
  2889. dry_run=parsed_args.dry_run,
  2890. progress=progress if parsed_args.verbose else None,
  2891. )
  2892. return None
  2893. except porcelain.Error as e:
  2894. logger.error("%s", e)
  2895. return 1
  2896. class cmd_pull(Command):
  2897. """Fetch from and integrate with another repository or a local branch."""
  2898. def run(self, args: Sequence[str]) -> None:
  2899. """Execute the pull command.
  2900. Args:
  2901. args: Command line arguments
  2902. """
  2903. parser = argparse.ArgumentParser()
  2904. parser.add_argument("from_location", type=str)
  2905. parser.add_argument("refspec", type=str, nargs="*")
  2906. parser.add_argument("--filter", type=str, nargs=1)
  2907. parser.add_argument("--protocol", type=int)
  2908. parsed_args = parser.parse_args(args)
  2909. porcelain.pull(
  2910. ".",
  2911. remote_location=parsed_args.from_location or None,
  2912. refspecs=parsed_args.refspec or None,
  2913. filter_spec=parsed_args.filter,
  2914. protocol_version=parsed_args.protocol or None,
  2915. )
  2916. class cmd_push(Command):
  2917. """Update remote refs along with associated objects."""
  2918. def run(self, argv: Sequence[str]) -> int | None:
  2919. """Execute the push command.
  2920. Args:
  2921. argv: Command line arguments
  2922. """
  2923. parser = argparse.ArgumentParser()
  2924. parser.add_argument("-f", "--force", action="store_true", help="Force")
  2925. parser.add_argument("to_location", type=str)
  2926. parser.add_argument("refspec", type=str, nargs="*")
  2927. args = parser.parse_args(argv)
  2928. try:
  2929. porcelain.push(
  2930. ".", args.to_location, args.refspec or None, force=args.force
  2931. )
  2932. except porcelain.DivergedBranches:
  2933. sys.stderr.write("Diverged branches; specify --force to override")
  2934. return 1
  2935. return None
  2936. class cmd_remote_add(Command):
  2937. """Add a remote repository."""
  2938. def run(self, args: Sequence[str]) -> None:
  2939. """Execute the remote-add command.
  2940. Args:
  2941. args: Command line arguments
  2942. """
  2943. parser = argparse.ArgumentParser()
  2944. parser.add_argument("name", help="Name of the remote")
  2945. parser.add_argument("url", help="URL of the remote")
  2946. parsed_args = parser.parse_args(args)
  2947. porcelain.remote_add(".", parsed_args.name, parsed_args.url)
  2948. class SuperCommand(Command):
  2949. """Base class for commands that have subcommands."""
  2950. subcommands: ClassVar[dict[str, type[Command]]] = {}
  2951. default_command: ClassVar[type[Command] | None] = None
  2952. def run(self, args: Sequence[str]) -> int | None:
  2953. """Execute the subcommand command.
  2954. Args:
  2955. args: Command line arguments
  2956. """
  2957. if not args:
  2958. if self.default_command:
  2959. return self.default_command().run(args)
  2960. else:
  2961. logger.info(
  2962. "Supported subcommands: %s", ", ".join(self.subcommands.keys())
  2963. )
  2964. return False
  2965. cmd = args[0]
  2966. try:
  2967. cmd_kls = self.subcommands[cmd]
  2968. except KeyError:
  2969. logger.error("No such subcommand: %s", args[0])
  2970. sys.exit(1)
  2971. return cmd_kls().run(args[1:])
  2972. class cmd_remote(SuperCommand):
  2973. """Manage set of tracked repositories."""
  2974. subcommands: ClassVar[dict[str, type[Command]]] = {
  2975. "add": cmd_remote_add,
  2976. }
  2977. class cmd_submodule_list(Command):
  2978. """List submodules."""
  2979. def run(self, argv: Sequence[str]) -> None:
  2980. """Execute the submodule-list command.
  2981. Args:
  2982. argv: Command line arguments
  2983. """
  2984. parser = argparse.ArgumentParser()
  2985. parser.parse_args(argv)
  2986. for path, sha in porcelain.submodule_list("."):
  2987. sys.stdout.write(f" {sha} {path}\n")
  2988. class cmd_submodule_init(Command):
  2989. """Initialize submodules."""
  2990. def run(self, argv: Sequence[str]) -> None:
  2991. """Execute the submodule-init command.
  2992. Args:
  2993. argv: Command line arguments
  2994. """
  2995. parser = argparse.ArgumentParser()
  2996. parser.parse_args(argv)
  2997. porcelain.submodule_init(".")
  2998. class cmd_submodule_add(Command):
  2999. """Add a submodule."""
  3000. def run(self, argv: Sequence[str]) -> None:
  3001. """Execute the submodule-add command.
  3002. Args:
  3003. argv: Command line arguments
  3004. """
  3005. parser = argparse.ArgumentParser()
  3006. parser.add_argument("url", help="URL of repository to add as submodule")
  3007. parser.add_argument("path", nargs="?", help="Path where submodule should live")
  3008. parser.add_argument("--name", help="Name for the submodule")
  3009. args = parser.parse_args(argv)
  3010. porcelain.submodule_add(".", args.url, args.path, args.name)
  3011. class cmd_submodule_update(Command):
  3012. """Update submodules."""
  3013. def run(self, argv: Sequence[str]) -> None:
  3014. """Execute the submodule-update command.
  3015. Args:
  3016. argv: Command line arguments
  3017. """
  3018. parser = argparse.ArgumentParser()
  3019. parser.add_argument(
  3020. "--init", action="store_true", help="Initialize submodules first"
  3021. )
  3022. parser.add_argument(
  3023. "--force",
  3024. action="store_true",
  3025. help="Force update even if local changes exist",
  3026. )
  3027. parser.add_argument(
  3028. "--recursive",
  3029. action="store_true",
  3030. help="Recursively update nested submodules",
  3031. )
  3032. parser.add_argument(
  3033. "paths", nargs="*", help="Specific submodule paths to update"
  3034. )
  3035. args = parser.parse_args(argv)
  3036. paths = args.paths if args.paths else None
  3037. porcelain.submodule_update(
  3038. ".", paths=paths, init=args.init, force=args.force, recursive=args.recursive
  3039. )
  3040. class cmd_submodule(SuperCommand):
  3041. """Initialize, update or inspect submodules."""
  3042. subcommands: ClassVar[dict[str, type[Command]]] = {
  3043. "add": cmd_submodule_add,
  3044. "init": cmd_submodule_init,
  3045. "list": cmd_submodule_list,
  3046. "update": cmd_submodule_update,
  3047. }
  3048. default_command = cmd_submodule_list
  3049. class cmd_check_ignore(Command):
  3050. """Check whether files are excluded by gitignore."""
  3051. def run(self, args: Sequence[str]) -> int:
  3052. """Execute the check-ignore command.
  3053. Args:
  3054. args: Command line arguments
  3055. """
  3056. parser = argparse.ArgumentParser()
  3057. parser.add_argument("paths", nargs="+", help="Paths to check")
  3058. parsed_args = parser.parse_args(args)
  3059. ret = 1
  3060. for path in porcelain.check_ignore(".", parsed_args.paths):
  3061. logger.info(path)
  3062. ret = 0
  3063. return ret
  3064. class cmd_check_mailmap(Command):
  3065. """Show canonical names and email addresses of contacts."""
  3066. def run(self, args: Sequence[str]) -> None:
  3067. """Execute the check-mailmap command.
  3068. Args:
  3069. args: Command line arguments
  3070. """
  3071. parser = argparse.ArgumentParser()
  3072. parser.add_argument("identities", nargs="+", help="Identities to check")
  3073. parsed_args = parser.parse_args(args)
  3074. for identity in parsed_args.identities:
  3075. canonical_identity = porcelain.check_mailmap(".", identity)
  3076. logger.info(canonical_identity)
  3077. class cmd_branch(Command):
  3078. """List, create, or delete branches."""
  3079. def run(self, args: Sequence[str]) -> int | None:
  3080. """Execute the branch command.
  3081. Args:
  3082. args: Command line arguments
  3083. """
  3084. parser = argparse.ArgumentParser()
  3085. parser.add_argument(
  3086. "branch",
  3087. type=str,
  3088. nargs="?",
  3089. help="Name of the branch",
  3090. )
  3091. parser.add_argument(
  3092. "-d",
  3093. "--delete",
  3094. action="store_true",
  3095. help="Delete branch",
  3096. )
  3097. parser.add_argument("--all", action="store_true", help="List all branches")
  3098. parser.add_argument(
  3099. "--merged", action="store_true", help="List merged into current branch"
  3100. )
  3101. parser.add_argument(
  3102. "--no-merged",
  3103. action="store_true",
  3104. help="List branches not merged into current branch",
  3105. )
  3106. parser.add_argument(
  3107. "--remotes", action="store_true", help="List remotes branches"
  3108. )
  3109. parser.add_argument(
  3110. "--contains",
  3111. nargs="?",
  3112. const="HEAD",
  3113. help="List branches that contain a specific commit",
  3114. )
  3115. parser.add_argument(
  3116. "--column", action="store_true", help="Display branch list in columns"
  3117. )
  3118. parser.add_argument(
  3119. "--list",
  3120. nargs="?",
  3121. const=None,
  3122. help="List branches matching a pattern",
  3123. )
  3124. parsed_args = parser.parse_args(args)
  3125. def print_branches(
  3126. branches: Iterator[bytes] | Sequence[bytes], use_columns: bool = False
  3127. ) -> None:
  3128. if use_columns:
  3129. branch_names = [branch.decode() for branch in branches]
  3130. output = format_columns(branch_names, mode="column")
  3131. sys.stdout.write(output)
  3132. else:
  3133. for branch in branches:
  3134. sys.stdout.write(f"{branch.decode()}\n")
  3135. branches: Iterator[bytes] | list[bytes] | None = None
  3136. try:
  3137. if parsed_args.all:
  3138. branches = porcelain.branch_list(".") + porcelain.branch_remotes_list(
  3139. "."
  3140. )
  3141. elif parsed_args.remotes:
  3142. branches = porcelain.branch_remotes_list(".")
  3143. elif parsed_args.merged:
  3144. branches = porcelain.merged_branches(".")
  3145. elif parsed_args.no_merged:
  3146. branches = porcelain.no_merged_branches(".")
  3147. elif parsed_args.contains:
  3148. try:
  3149. branches = list(
  3150. porcelain.branches_containing(".", commit=parsed_args.contains)
  3151. )
  3152. except KeyError as e:
  3153. sys.stderr.write(
  3154. f"error: object name {e.args[0].decode()} not found\n"
  3155. )
  3156. return 1
  3157. except porcelain.Error as e:
  3158. sys.stderr.write(f"{e}")
  3159. return 1
  3160. pattern = parsed_args.list
  3161. if pattern is not None and branches:
  3162. branches = porcelain.filter_branches_by_pattern(branches, pattern)
  3163. if branches is not None:
  3164. print_branches(branches, parsed_args.column)
  3165. return 0
  3166. if not parsed_args.branch:
  3167. logger.error("Usage: dulwich branch [-d] BRANCH_NAME")
  3168. return 1
  3169. if parsed_args.delete:
  3170. porcelain.branch_delete(".", name=parsed_args.branch)
  3171. else:
  3172. try:
  3173. porcelain.branch_create(".", name=parsed_args.branch)
  3174. except porcelain.Error as e:
  3175. sys.stderr.write(f"{e}")
  3176. return 1
  3177. return 0
  3178. class cmd_checkout(Command):
  3179. """Switch branches or restore working tree files."""
  3180. def run(self, args: Sequence[str]) -> int | None:
  3181. """Execute the checkout command.
  3182. Args:
  3183. args: Command line arguments
  3184. """
  3185. parser = argparse.ArgumentParser()
  3186. parser.add_argument(
  3187. "target",
  3188. type=str,
  3189. help="Name of the branch, tag, or commit to checkout",
  3190. )
  3191. parser.add_argument(
  3192. "-f",
  3193. "--force",
  3194. action="store_true",
  3195. help="Force checkout",
  3196. )
  3197. parser.add_argument(
  3198. "-b",
  3199. "--new-branch",
  3200. type=str,
  3201. help="Create a new branch at the target and switch to it",
  3202. )
  3203. parsed_args = parser.parse_args(args)
  3204. if not parsed_args.target:
  3205. logger.error("Usage: dulwich checkout TARGET [--force] [-b NEW_BRANCH]")
  3206. return 1
  3207. try:
  3208. porcelain.checkout(
  3209. ".",
  3210. target=parsed_args.target,
  3211. force=parsed_args.force,
  3212. new_branch=parsed_args.new_branch,
  3213. )
  3214. except porcelain.CheckoutError as e:
  3215. sys.stderr.write(f"{e}\n")
  3216. return 1
  3217. return 0
  3218. class cmd_restore(Command):
  3219. """Restore working tree files."""
  3220. def run(self, args: Sequence[str]) -> int | None:
  3221. """Execute the restore command.
  3222. Args:
  3223. args: Command line arguments
  3224. """
  3225. parser = argparse.ArgumentParser()
  3226. parser.add_argument(
  3227. "paths",
  3228. nargs="+",
  3229. type=str,
  3230. help="Paths to restore",
  3231. )
  3232. parser.add_argument(
  3233. "-s",
  3234. "--source",
  3235. type=str,
  3236. help="Restore from a specific commit (default: HEAD for --staged, index for worktree)",
  3237. )
  3238. parser.add_argument(
  3239. "--staged",
  3240. action="store_true",
  3241. help="Restore files in the index",
  3242. )
  3243. parser.add_argument(
  3244. "--worktree",
  3245. action="store_true",
  3246. help="Restore files in the working tree",
  3247. )
  3248. parsed_args = parser.parse_args(args)
  3249. # If neither --staged nor --worktree is specified, default to --worktree
  3250. if not parsed_args.staged and not parsed_args.worktree:
  3251. worktree = True
  3252. staged = False
  3253. else:
  3254. worktree = parsed_args.worktree
  3255. staged = parsed_args.staged
  3256. try:
  3257. porcelain.restore(
  3258. ".",
  3259. paths=parsed_args.paths,
  3260. source=parsed_args.source,
  3261. staged=staged,
  3262. worktree=worktree,
  3263. )
  3264. except porcelain.CheckoutError as e:
  3265. sys.stderr.write(f"{e}\n")
  3266. return 1
  3267. return 0
  3268. class cmd_switch(Command):
  3269. """Switch branches."""
  3270. def run(self, args: Sequence[str]) -> int | None:
  3271. """Execute the switch command.
  3272. Args:
  3273. args: Command line arguments
  3274. """
  3275. parser = argparse.ArgumentParser()
  3276. parser.add_argument(
  3277. "target",
  3278. type=str,
  3279. help="Branch or commit to switch to",
  3280. )
  3281. parser.add_argument(
  3282. "-c",
  3283. "--create",
  3284. type=str,
  3285. help="Create a new branch at the target and switch to it",
  3286. )
  3287. parser.add_argument(
  3288. "-f",
  3289. "--force",
  3290. action="store_true",
  3291. help="Force switch even if there are local changes",
  3292. )
  3293. parser.add_argument(
  3294. "-d",
  3295. "--detach",
  3296. action="store_true",
  3297. help="Switch to a commit in detached HEAD state",
  3298. )
  3299. parsed_args = parser.parse_args(args)
  3300. if not parsed_args.target:
  3301. logger.error(
  3302. "Usage: dulwich switch TARGET [-c NEW_BRANCH] [--force] [--detach]"
  3303. )
  3304. return 1
  3305. try:
  3306. porcelain.switch(
  3307. ".",
  3308. target=parsed_args.target,
  3309. create=parsed_args.create,
  3310. force=parsed_args.force,
  3311. detach=parsed_args.detach,
  3312. )
  3313. except porcelain.CheckoutError as e:
  3314. sys.stderr.write(f"{e}\n")
  3315. return 1
  3316. return 0
  3317. class cmd_stash_list(Command):
  3318. """List stash entries."""
  3319. def run(self, args: Sequence[str]) -> None:
  3320. """Execute the stash-list command.
  3321. Args:
  3322. args: Command line arguments
  3323. """
  3324. parser = argparse.ArgumentParser()
  3325. parser.parse_args(args)
  3326. from .repo import Repo
  3327. from .stash import Stash
  3328. with Repo(".") as r:
  3329. stash = Stash.from_repo(r)
  3330. for i, entry in enumerate(stash.stashes()):
  3331. logger.info(
  3332. "stash@{%d}: %s",
  3333. i,
  3334. entry.message.decode("utf-8", "replace").rstrip("\n"),
  3335. )
  3336. class cmd_stash_push(Command):
  3337. """Save your local modifications to a new stash."""
  3338. def run(self, args: Sequence[str]) -> None:
  3339. """Execute the stash-push command.
  3340. Args:
  3341. args: Command line arguments
  3342. """
  3343. parser = argparse.ArgumentParser()
  3344. parser.parse_args(args)
  3345. porcelain.stash_push(".")
  3346. logger.info("Saved working directory and index state")
  3347. class cmd_stash_pop(Command):
  3348. """Apply a stash and remove it from the stash list."""
  3349. def run(self, args: Sequence[str]) -> None:
  3350. """Execute the stash-pop command.
  3351. Args:
  3352. args: Command line arguments
  3353. """
  3354. parser = argparse.ArgumentParser()
  3355. parser.parse_args(args)
  3356. porcelain.stash_pop(".")
  3357. logger.info("Restored working directory and index state")
  3358. class cmd_bisect(SuperCommand):
  3359. """Use binary search to find the commit that introduced a bug."""
  3360. subcommands: ClassVar[dict[str, type[Command]]] = {}
  3361. def run(self, args: Sequence[str]) -> int | None:
  3362. """Execute the bisect command.
  3363. Args:
  3364. args: Command line arguments
  3365. """
  3366. parser = argparse.ArgumentParser(prog="dulwich bisect")
  3367. subparsers = parser.add_subparsers(dest="subcommand", help="bisect subcommands")
  3368. # bisect start
  3369. start_parser = subparsers.add_parser("start", help="Start a new bisect session")
  3370. start_parser.add_argument("bad", nargs="?", help="Bad commit")
  3371. start_parser.add_argument("good", nargs="*", help="Good commit(s)")
  3372. start_parser.add_argument(
  3373. "--no-checkout",
  3374. action="store_true",
  3375. help="Don't checkout commits during bisect",
  3376. )
  3377. start_parser.add_argument(
  3378. "--term-bad", default="bad", help="Term to use for bad commits"
  3379. )
  3380. start_parser.add_argument(
  3381. "--term-good", default="good", help="Term to use for good commits"
  3382. )
  3383. start_parser.add_argument(
  3384. "--", dest="paths", nargs="*", help="Paths to limit bisect to"
  3385. )
  3386. # bisect bad
  3387. bad_parser = subparsers.add_parser("bad", help="Mark a commit as bad")
  3388. bad_parser.add_argument("rev", nargs="?", help="Commit to mark as bad")
  3389. # bisect good
  3390. good_parser = subparsers.add_parser("good", help="Mark a commit as good")
  3391. good_parser.add_argument("rev", nargs="?", help="Commit to mark as good")
  3392. # bisect skip
  3393. skip_parser = subparsers.add_parser("skip", help="Skip commits")
  3394. skip_parser.add_argument("revs", nargs="*", help="Commits to skip")
  3395. # bisect reset
  3396. reset_parser = subparsers.add_parser("reset", help="Reset bisect state")
  3397. reset_parser.add_argument("commit", nargs="?", help="Commit to reset to")
  3398. # bisect log
  3399. subparsers.add_parser("log", help="Show bisect log")
  3400. # bisect replay
  3401. replay_parser = subparsers.add_parser("replay", help="Replay bisect log")
  3402. replay_parser.add_argument("logfile", help="Log file to replay")
  3403. # bisect help
  3404. subparsers.add_parser("help", help="Show help")
  3405. parsed_args = parser.parse_args(args)
  3406. if not parsed_args.subcommand:
  3407. parser.print_help()
  3408. return 1
  3409. try:
  3410. if parsed_args.subcommand == "start":
  3411. next_sha = porcelain.bisect_start(
  3412. bad=parsed_args.bad,
  3413. good=parsed_args.good if parsed_args.good else None,
  3414. paths=parsed_args.paths,
  3415. no_checkout=parsed_args.no_checkout,
  3416. term_bad=parsed_args.term_bad,
  3417. term_good=parsed_args.term_good,
  3418. )
  3419. if next_sha:
  3420. logger.info(
  3421. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  3422. )
  3423. elif parsed_args.subcommand == "bad":
  3424. next_sha = porcelain.bisect_bad(rev=parsed_args.rev)
  3425. if next_sha:
  3426. logger.info(
  3427. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  3428. )
  3429. else:
  3430. # Bisect complete - find the first bad commit
  3431. with porcelain.open_repo_closing(".") as r:
  3432. bad_ref = os.path.join(r.controldir(), "refs", "bisect", "bad")
  3433. with open(bad_ref, "rb") as f:
  3434. bad_sha = ObjectID(f.read().strip())
  3435. commit = r.object_store[bad_sha]
  3436. assert isinstance(commit, Commit)
  3437. message = commit.message.decode(
  3438. "utf-8", errors="replace"
  3439. ).split("\n")[0]
  3440. logger.info(
  3441. "%s is the first bad commit", bad_sha.decode("ascii")
  3442. )
  3443. logger.info("commit %s", bad_sha.decode("ascii"))
  3444. logger.info(" %s", message)
  3445. elif parsed_args.subcommand == "good":
  3446. next_sha = porcelain.bisect_good(rev=parsed_args.rev)
  3447. if next_sha:
  3448. logger.info(
  3449. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  3450. )
  3451. elif parsed_args.subcommand == "skip":
  3452. next_sha = porcelain.bisect_skip(
  3453. revs=parsed_args.revs if parsed_args.revs else None
  3454. )
  3455. if next_sha:
  3456. logger.info(
  3457. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  3458. )
  3459. elif parsed_args.subcommand == "reset":
  3460. porcelain.bisect_reset(commit=parsed_args.commit)
  3461. logger.info("Bisect reset")
  3462. elif parsed_args.subcommand == "log":
  3463. log = porcelain.bisect_log()
  3464. logger.info(log.rstrip())
  3465. elif parsed_args.subcommand == "replay":
  3466. porcelain.bisect_replay(".", log_file=parsed_args.logfile)
  3467. logger.info("Replayed bisect log from %s", parsed_args.logfile)
  3468. elif parsed_args.subcommand == "help":
  3469. parser.print_help()
  3470. except porcelain.Error as e:
  3471. logger.error("%s", e)
  3472. return 1
  3473. except ValueError as e:
  3474. logger.error("%s", e)
  3475. return 1
  3476. return 0
  3477. class cmd_stash(SuperCommand):
  3478. """Stash the changes in a dirty working directory away."""
  3479. subcommands: ClassVar[dict[str, type[Command]]] = {
  3480. "list": cmd_stash_list,
  3481. "pop": cmd_stash_pop,
  3482. "push": cmd_stash_push,
  3483. }
  3484. class cmd_ls_files(Command):
  3485. """Show information about files in the index and working tree."""
  3486. def run(self, args: Sequence[str]) -> None:
  3487. """Execute the ls-files command.
  3488. Args:
  3489. args: Command line arguments
  3490. """
  3491. parser = argparse.ArgumentParser()
  3492. parser.parse_args(args)
  3493. for name in porcelain.ls_files("."):
  3494. logger.info(name)
  3495. class cmd_describe(Command):
  3496. """Give an object a human readable name based on an available ref."""
  3497. def run(self, args: Sequence[str]) -> None:
  3498. """Execute the describe command.
  3499. Args:
  3500. args: Command line arguments
  3501. """
  3502. parser = argparse.ArgumentParser()
  3503. parser.parse_args(args)
  3504. logger.info(porcelain.describe("."))
  3505. class cmd_diagnose(Command):
  3506. """Display diagnostic information about the Python environment."""
  3507. def run(self, args: Sequence[str]) -> None:
  3508. """Execute the diagnose command.
  3509. Args:
  3510. args: Command line arguments
  3511. """
  3512. # TODO: Support creating zip files with diagnostic information
  3513. parser = argparse.ArgumentParser()
  3514. parser.parse_args(args)
  3515. # Python version and executable
  3516. logger.info("Python version: %s", sys.version)
  3517. logger.info("Python executable: %s", sys.executable)
  3518. # PYTHONPATH
  3519. pythonpath = os.environ.get("PYTHONPATH", "")
  3520. if pythonpath:
  3521. logger.info("PYTHONPATH: %s", pythonpath)
  3522. else:
  3523. logger.info("PYTHONPATH: (not set)")
  3524. # sys.path
  3525. logger.info("sys.path:")
  3526. for path_entry in sys.path:
  3527. logger.info(" %s", path_entry)
  3528. # Dulwich version
  3529. try:
  3530. import dulwich
  3531. logger.info("Dulwich version: %s", dulwich.__version__)
  3532. except AttributeError:
  3533. logger.info("Dulwich version: (unknown)")
  3534. # List installed dependencies and their versions
  3535. logger.info("Installed dependencies:")
  3536. # Core dependencies
  3537. dependencies = [
  3538. ("urllib3", "core"),
  3539. ("typing_extensions", "core (Python < 3.12)"),
  3540. ]
  3541. # Optional dependencies
  3542. optional_dependencies = [
  3543. ("fastimport", "fastimport"),
  3544. ("gpg", "pgp"),
  3545. ("paramiko", "paramiko"),
  3546. ("rich", "colordiff"),
  3547. ("merge3", "merge"),
  3548. ("patiencediff", "patiencediff"),
  3549. ("atheris", "fuzzing"),
  3550. ]
  3551. for dep, dep_type in dependencies + optional_dependencies:
  3552. try:
  3553. module = __import__(dep)
  3554. version = getattr(module, "__version__", "(unknown)")
  3555. logger.info(" %s: %s [%s]", dep, version, dep_type)
  3556. except ImportError:
  3557. logger.info(" %s: (not installed) [%s]", dep, dep_type)
  3558. class cmd_merge(Command):
  3559. """Join two or more development histories together."""
  3560. def run(self, args: Sequence[str]) -> int | None:
  3561. """Execute the merge command.
  3562. Args:
  3563. args: Command line arguments
  3564. """
  3565. parser = argparse.ArgumentParser()
  3566. parser.add_argument("commit", type=str, nargs="+", help="Commit(s) to merge")
  3567. parser.add_argument(
  3568. "--no-commit", action="store_true", help="Do not create a merge commit"
  3569. )
  3570. parser.add_argument(
  3571. "--no-ff", action="store_true", help="Force create a merge commit"
  3572. )
  3573. parser.add_argument("-m", "--message", type=str, help="Merge commit message")
  3574. parsed_args = parser.parse_args(args)
  3575. try:
  3576. # If multiple commits are provided, pass them as a list
  3577. # If only one commit is provided, pass it as a string
  3578. if len(parsed_args.commit) == 1:
  3579. committish = parsed_args.commit[0]
  3580. else:
  3581. committish = parsed_args.commit
  3582. merge_commit_id, conflicts = porcelain.merge(
  3583. ".",
  3584. committish,
  3585. no_commit=parsed_args.no_commit,
  3586. no_ff=parsed_args.no_ff,
  3587. message=parsed_args.message,
  3588. )
  3589. if conflicts:
  3590. logger.warning("Merge conflicts in %d file(s):", len(conflicts))
  3591. for conflict_path in conflicts:
  3592. logger.warning(" %s", conflict_path.decode())
  3593. if len(parsed_args.commit) > 1:
  3594. logger.error(
  3595. "Octopus merge failed; refusing to merge with conflicts."
  3596. )
  3597. else:
  3598. logger.error(
  3599. "Automatic merge failed; fix conflicts and then commit the result."
  3600. )
  3601. return 1
  3602. elif merge_commit_id is None and not parsed_args.no_commit:
  3603. logger.info("Already up to date.")
  3604. elif parsed_args.no_commit:
  3605. logger.info("Automatic merge successful; not committing as requested.")
  3606. else:
  3607. assert merge_commit_id is not None
  3608. if len(parsed_args.commit) > 1:
  3609. logger.info(
  3610. "Octopus merge successful. Created merge commit %s",
  3611. merge_commit_id.decode(),
  3612. )
  3613. else:
  3614. logger.info(
  3615. "Merge successful. Created merge commit %s",
  3616. merge_commit_id.decode(),
  3617. )
  3618. return 0
  3619. except porcelain.Error as e:
  3620. logger.error("%s", e)
  3621. return 1
  3622. class cmd_merge_base(Command):
  3623. """Find the best common ancestor between commits."""
  3624. def run(self, args: Sequence[str]) -> int | None:
  3625. """Execute the merge-base command.
  3626. Args:
  3627. args: Command line arguments
  3628. """
  3629. parser = argparse.ArgumentParser(
  3630. description="Find the best common ancestor between commits",
  3631. prog="dulwich merge-base",
  3632. )
  3633. parser.add_argument("commits", nargs="+", help="Commits to find merge base for")
  3634. parser.add_argument("--all", action="store_true", help="Output all merge bases")
  3635. parser.add_argument(
  3636. "--octopus",
  3637. action="store_true",
  3638. help="Compute common ancestor of all commits",
  3639. )
  3640. parser.add_argument(
  3641. "--is-ancestor",
  3642. action="store_true",
  3643. help="Check if first commit is ancestor of second",
  3644. )
  3645. parser.add_argument(
  3646. "--independent",
  3647. action="store_true",
  3648. help="List commits not reachable from others",
  3649. )
  3650. parsed_args = parser.parse_args(args)
  3651. try:
  3652. if parsed_args.is_ancestor:
  3653. if len(parsed_args.commits) != 2:
  3654. logger.error("--is-ancestor requires exactly two commits")
  3655. return 1
  3656. is_anc = porcelain.is_ancestor(
  3657. ".",
  3658. ancestor=parsed_args.commits[0],
  3659. descendant=parsed_args.commits[1],
  3660. )
  3661. return 0 if is_anc else 1
  3662. elif parsed_args.independent:
  3663. commits = porcelain.independent_commits(".", parsed_args.commits)
  3664. for commit_id in commits:
  3665. print(commit_id.decode())
  3666. return 0
  3667. else:
  3668. if len(parsed_args.commits) < 2:
  3669. logger.error("At least two commits are required")
  3670. return 1
  3671. merge_bases = porcelain.merge_base(
  3672. ".",
  3673. parsed_args.commits,
  3674. all=parsed_args.all,
  3675. octopus=parsed_args.octopus,
  3676. )
  3677. for commit_id in merge_bases:
  3678. print(commit_id.decode())
  3679. return 0
  3680. except (ValueError, KeyError) as e:
  3681. logger.error("%s", e)
  3682. return 1
  3683. class cmd_notes_add(Command):
  3684. """Add notes to a commit."""
  3685. def run(self, args: Sequence[str]) -> None:
  3686. """Execute the notes-add command.
  3687. Args:
  3688. args: Command line arguments
  3689. """
  3690. parser = argparse.ArgumentParser()
  3691. parser.add_argument("object", help="Object to annotate")
  3692. parser.add_argument("-m", "--message", help="Note message", required=True)
  3693. parser.add_argument(
  3694. "--ref", default="commits", help="Notes ref (default: commits)"
  3695. )
  3696. parsed_args = parser.parse_args(args)
  3697. porcelain.notes_add(
  3698. ".", parsed_args.object, parsed_args.message, ref=parsed_args.ref
  3699. )
  3700. class cmd_notes_show(Command):
  3701. """Show notes for a commit."""
  3702. def run(self, args: Sequence[str]) -> None:
  3703. """Execute the notes-show command.
  3704. Args:
  3705. args: Command line arguments
  3706. """
  3707. parser = argparse.ArgumentParser()
  3708. parser.add_argument("object", help="Object to show notes for")
  3709. parser.add_argument(
  3710. "--ref", default="commits", help="Notes ref (default: commits)"
  3711. )
  3712. parsed_args = parser.parse_args(args)
  3713. note = porcelain.notes_show(".", parsed_args.object, ref=parsed_args.ref)
  3714. if note:
  3715. sys.stdout.buffer.write(note)
  3716. else:
  3717. logger.info("No notes found for object %s", parsed_args.object)
  3718. class cmd_notes_remove(Command):
  3719. """Remove notes for a commit."""
  3720. def run(self, args: Sequence[str]) -> None:
  3721. """Execute the notes-remove command.
  3722. Args:
  3723. args: Command line arguments
  3724. """
  3725. parser = argparse.ArgumentParser()
  3726. parser.add_argument("object", help="Object to remove notes from")
  3727. parser.add_argument(
  3728. "--ref", default="commits", help="Notes ref (default: commits)"
  3729. )
  3730. parsed_args = parser.parse_args(args)
  3731. result = porcelain.notes_remove(".", parsed_args.object, ref=parsed_args.ref)
  3732. if result:
  3733. logger.info("Removed notes for object %s", parsed_args.object)
  3734. else:
  3735. logger.info("No notes found for object %s", parsed_args.object)
  3736. class cmd_notes_list(Command):
  3737. """List all note objects."""
  3738. def run(self, args: Sequence[str]) -> None:
  3739. """Execute the notes-list command.
  3740. Args:
  3741. args: Command line arguments
  3742. """
  3743. parser = argparse.ArgumentParser()
  3744. parser.add_argument(
  3745. "--ref", default="commits", help="Notes ref (default: commits)"
  3746. )
  3747. parsed_args = parser.parse_args(args)
  3748. notes = porcelain.notes_list(".", ref=parsed_args.ref)
  3749. for object_sha, note_content in notes:
  3750. logger.info(object_sha.hex())
  3751. class cmd_notes(SuperCommand):
  3752. """Add or inspect object notes."""
  3753. subcommands: ClassVar[dict[str, type[Command]]] = {
  3754. "add": cmd_notes_add,
  3755. "show": cmd_notes_show,
  3756. "remove": cmd_notes_remove,
  3757. "list": cmd_notes_list,
  3758. }
  3759. default_command = cmd_notes_list
  3760. class cmd_replace_list(Command):
  3761. """List all replacement refs."""
  3762. def run(self, args: Sequence[str]) -> None:
  3763. """Execute the replace-list command.
  3764. Args:
  3765. args: Command line arguments
  3766. """
  3767. parser = argparse.ArgumentParser()
  3768. parser.parse_args(args)
  3769. replacements = porcelain.replace_list(".")
  3770. for object_sha, replacement_sha in replacements:
  3771. sys.stdout.write(
  3772. f"{object_sha.decode('ascii')} -> {replacement_sha.decode('ascii')}\n"
  3773. )
  3774. class cmd_replace_delete(Command):
  3775. """Delete a replacement ref."""
  3776. def run(self, args: Sequence[str]) -> int | None:
  3777. """Execute the replace-delete command.
  3778. Args:
  3779. args: Command line arguments
  3780. Returns:
  3781. Exit code (0 for success, 1 for error)
  3782. """
  3783. parser = argparse.ArgumentParser()
  3784. parser.add_argument("object", help="Object whose replacement should be removed")
  3785. parsed_args = parser.parse_args(args)
  3786. try:
  3787. porcelain.replace_delete(".", parsed_args.object)
  3788. logger.info("Deleted replacement for %s", parsed_args.object)
  3789. return None
  3790. except KeyError as e:
  3791. logger.error(str(e))
  3792. return 1
  3793. class cmd_replace(SuperCommand):
  3794. """Create, list, and delete replacement refs."""
  3795. subcommands: ClassVar[dict[str, type[Command]]] = {
  3796. "list": cmd_replace_list,
  3797. "delete": cmd_replace_delete,
  3798. }
  3799. default_command = cmd_replace_list
  3800. def run(self, args: Sequence[str]) -> int | None:
  3801. """Execute the replace command.
  3802. Args:
  3803. args: Command line arguments
  3804. Returns:
  3805. Exit code (0 for success, 1 for error)
  3806. """
  3807. # Special case: if we have exactly 2 args and no subcommand, treat as create
  3808. if len(args) == 2 and args[0] not in self.subcommands:
  3809. # This is the create form: git replace <object> <replacement>
  3810. parser = argparse.ArgumentParser()
  3811. parser.add_argument("object", help="Object to replace")
  3812. parser.add_argument("replacement", help="Replacement object")
  3813. parsed_args = parser.parse_args(args)
  3814. porcelain.replace_create(".", parsed_args.object, parsed_args.replacement)
  3815. logger.info(
  3816. "Created replacement: %s -> %s",
  3817. parsed_args.object,
  3818. parsed_args.replacement,
  3819. )
  3820. return None
  3821. # Otherwise, delegate to supercommand handling
  3822. return super().run(args)
  3823. class cmd_cherry(Command):
  3824. """Find commits not merged upstream."""
  3825. def run(self, args: Sequence[str]) -> int | None:
  3826. """Execute the cherry command.
  3827. Args:
  3828. args: Command line arguments
  3829. Returns:
  3830. Exit code (0 for success, 1 for error)
  3831. """
  3832. parser = argparse.ArgumentParser(description="Find commits not merged upstream")
  3833. parser.add_argument(
  3834. "-v",
  3835. "--verbose",
  3836. action="store_true",
  3837. help="Show commit messages",
  3838. )
  3839. parser.add_argument(
  3840. "upstream",
  3841. nargs="?",
  3842. help="Upstream branch (default: tracking branch or HEAD^)",
  3843. )
  3844. parser.add_argument(
  3845. "head",
  3846. nargs="?",
  3847. help="Head branch (default: HEAD)",
  3848. )
  3849. parser.add_argument(
  3850. "limit",
  3851. nargs="?",
  3852. help="Limit commits to those after this ref",
  3853. )
  3854. parsed_args = parser.parse_args(args)
  3855. try:
  3856. results = porcelain.cherry(
  3857. ".",
  3858. upstream=parsed_args.upstream,
  3859. head=parsed_args.head,
  3860. limit=parsed_args.limit,
  3861. verbose=parsed_args.verbose,
  3862. )
  3863. except (NotGitRepository, OSError, FileFormatException, ValueError) as e:
  3864. logger.error(f"Error: {e}")
  3865. return 1
  3866. # Output results
  3867. for status, commit_sha, message in results:
  3868. # Convert commit_sha to hex string
  3869. if isinstance(commit_sha, bytes):
  3870. commit_hex = commit_sha.hex()
  3871. else:
  3872. commit_hex = commit_sha
  3873. if parsed_args.verbose and message:
  3874. message_str = message.decode("utf-8", errors="replace")
  3875. logger.info(f"{status} {commit_hex} {message_str}")
  3876. else:
  3877. logger.info(f"{status} {commit_hex}")
  3878. return 0
  3879. class cmd_cherry_pick(Command):
  3880. """Apply the changes introduced by some existing commits."""
  3881. def run(self, args: Sequence[str]) -> int | None:
  3882. """Execute the cherry-pick command.
  3883. Args:
  3884. args: Command line arguments
  3885. """
  3886. parser = argparse.ArgumentParser(
  3887. description="Apply the changes introduced by some existing commits"
  3888. )
  3889. parser.add_argument("commit", nargs="?", help="Commit to cherry-pick")
  3890. parser.add_argument(
  3891. "-n",
  3892. "--no-commit",
  3893. action="store_true",
  3894. help="Apply changes without making a commit",
  3895. )
  3896. parser.add_argument(
  3897. "--continue",
  3898. dest="continue_",
  3899. action="store_true",
  3900. help="Continue after resolving conflicts",
  3901. )
  3902. parser.add_argument(
  3903. "--abort",
  3904. action="store_true",
  3905. help="Abort the current cherry-pick operation",
  3906. )
  3907. parsed_args = parser.parse_args(args)
  3908. # Check argument validity
  3909. if parsed_args.continue_ or parsed_args.abort:
  3910. if parsed_args.commit is not None:
  3911. parser.error("Cannot specify commit with --continue or --abort")
  3912. return 1
  3913. else:
  3914. if parsed_args.commit is None:
  3915. parser.error("Commit argument is required")
  3916. return 1
  3917. try:
  3918. commit_arg = parsed_args.commit
  3919. result = porcelain.cherry_pick(
  3920. ".",
  3921. commit_arg,
  3922. no_commit=parsed_args.no_commit,
  3923. continue_=parsed_args.continue_,
  3924. abort=parsed_args.abort,
  3925. )
  3926. if parsed_args.abort:
  3927. logger.info("Cherry-pick aborted.")
  3928. elif parsed_args.continue_:
  3929. if result:
  3930. logger.info("Cherry-pick completed: %s", result.decode())
  3931. else:
  3932. logger.info("Cherry-pick completed.")
  3933. elif result is None:
  3934. if parsed_args.no_commit:
  3935. logger.info("Cherry-pick applied successfully (no commit created).")
  3936. else:
  3937. # This shouldn't happen unless there were conflicts
  3938. logger.warning("Cherry-pick resulted in conflicts.")
  3939. else:
  3940. logger.info("Cherry-pick successful: %s", result.decode())
  3941. return None
  3942. except porcelain.Error as e:
  3943. logger.error("%s", e)
  3944. return 1
  3945. class cmd_merge_tree(Command):
  3946. """Show three-way merge without touching index."""
  3947. def run(self, args: Sequence[str]) -> int | None:
  3948. """Execute the merge-tree command.
  3949. Args:
  3950. args: Command line arguments
  3951. """
  3952. parser = argparse.ArgumentParser(
  3953. description="Perform a tree-level merge without touching the working directory"
  3954. )
  3955. parser.add_argument(
  3956. "base_tree",
  3957. nargs="?",
  3958. help="The common ancestor tree (optional, defaults to empty tree)",
  3959. )
  3960. parser.add_argument("our_tree", help="Our side of the merge")
  3961. parser.add_argument("their_tree", help="Their side of the merge")
  3962. parser.add_argument(
  3963. "-z",
  3964. "--name-only",
  3965. action="store_true",
  3966. help="Output only conflict paths, null-terminated",
  3967. )
  3968. parsed_args = parser.parse_args(args)
  3969. try:
  3970. # Determine base tree - if only two parsed_args provided, base is None
  3971. if parsed_args.base_tree is None:
  3972. # Only two arguments provided
  3973. base_tree = None
  3974. our_tree = parsed_args.our_tree
  3975. their_tree = parsed_args.their_tree
  3976. else:
  3977. # Three arguments provided
  3978. base_tree = parsed_args.base_tree
  3979. our_tree = parsed_args.our_tree
  3980. their_tree = parsed_args.their_tree
  3981. merged_tree_id, conflicts = porcelain.merge_tree(
  3982. ".", base_tree, our_tree, their_tree
  3983. )
  3984. if parsed_args.name_only:
  3985. # Output only conflict paths, null-terminated
  3986. for conflict_path in conflicts:
  3987. sys.stdout.buffer.write(conflict_path)
  3988. sys.stdout.buffer.write(b"\0")
  3989. else:
  3990. # Output the merged tree SHA
  3991. logger.info(merged_tree_id.decode("ascii"))
  3992. # Output conflict information
  3993. if conflicts:
  3994. logger.warning("\nConflicts in %d file(s):", len(conflicts))
  3995. for conflict_path in conflicts:
  3996. logger.warning(" %s", conflict_path.decode())
  3997. return None
  3998. except porcelain.Error as e:
  3999. logger.error("%s", e)
  4000. return 1
  4001. except KeyError as e:
  4002. logger.error("Object not found: %s", e)
  4003. return 1
  4004. class cmd_gc(Command):
  4005. """Cleanup unnecessary files and optimize the local repository."""
  4006. def run(self, args: Sequence[str]) -> int | None:
  4007. """Execute the gc command.
  4008. Args:
  4009. args: Command line arguments
  4010. """
  4011. import datetime
  4012. import time
  4013. parser = argparse.ArgumentParser()
  4014. parser.add_argument(
  4015. "--auto",
  4016. action="store_true",
  4017. help="Only run gc if needed",
  4018. )
  4019. parser.add_argument(
  4020. "--aggressive",
  4021. action="store_true",
  4022. help="Use more aggressive settings",
  4023. )
  4024. parser.add_argument(
  4025. "--no-prune",
  4026. action="store_true",
  4027. help="Do not prune unreachable objects",
  4028. )
  4029. parser.add_argument(
  4030. "--prune",
  4031. nargs="?",
  4032. const="now",
  4033. help="Prune unreachable objects older than date (default: 2 weeks ago)",
  4034. )
  4035. parser.add_argument(
  4036. "--dry-run",
  4037. "-n",
  4038. action="store_true",
  4039. help="Only report what would be done",
  4040. )
  4041. parser.add_argument(
  4042. "--quiet",
  4043. "-q",
  4044. action="store_true",
  4045. help="Only report errors",
  4046. )
  4047. parsed_args = parser.parse_args(args)
  4048. # Parse prune grace period
  4049. grace_period = None
  4050. if parsed_args.prune:
  4051. from .approxidate import parse_relative_time
  4052. try:
  4053. grace_period = parse_relative_time(parsed_args.prune)
  4054. except ValueError:
  4055. # Try to parse as absolute date
  4056. try:
  4057. date = datetime.datetime.strptime(parsed_args.prune, "%Y-%m-%d")
  4058. grace_period = int(time.time() - date.timestamp())
  4059. except ValueError:
  4060. logger.error("Invalid prune date: %s", parsed_args.prune)
  4061. return 1
  4062. elif not parsed_args.no_prune:
  4063. # Default to 2 weeks
  4064. grace_period = 1209600
  4065. # Progress callback
  4066. def progress(msg: str) -> None:
  4067. if not parsed_args.quiet:
  4068. logger.info(msg)
  4069. try:
  4070. stats = porcelain.gc(
  4071. ".",
  4072. auto=parsed_args.auto,
  4073. aggressive=parsed_args.aggressive,
  4074. prune=not parsed_args.no_prune,
  4075. grace_period=grace_period,
  4076. dry_run=parsed_args.dry_run,
  4077. progress=progress if not parsed_args.quiet else None,
  4078. )
  4079. # Report results
  4080. if not parsed_args.quiet:
  4081. if parsed_args.dry_run:
  4082. logger.info("\nDry run results:")
  4083. if not parsed_args.quiet:
  4084. if parsed_args.dry_run:
  4085. print("\nDry run results:")
  4086. else:
  4087. logger.info("\nGarbage collection complete:")
  4088. if stats.pruned_objects:
  4089. logger.info(
  4090. " Pruned %d unreachable objects", len(stats.pruned_objects)
  4091. )
  4092. logger.info(" Freed %s", format_bytes(stats.bytes_freed))
  4093. if stats.packs_before != stats.packs_after:
  4094. logger.info(
  4095. " Reduced pack files from %d to %d",
  4096. stats.packs_before,
  4097. stats.packs_after,
  4098. )
  4099. except porcelain.Error as e:
  4100. logger.error("%s", e)
  4101. return 1
  4102. return None
  4103. class cmd_maintenance(Command):
  4104. """Run tasks to optimize Git repository data."""
  4105. def run(self, args: Sequence[str]) -> int | None:
  4106. """Execute the maintenance command.
  4107. Args:
  4108. args: Command line arguments
  4109. """
  4110. parser = argparse.ArgumentParser(
  4111. description="Run tasks to optimize Git repository data"
  4112. )
  4113. subparsers = parser.add_subparsers(
  4114. dest="subcommand", help="Maintenance subcommand"
  4115. )
  4116. # maintenance run subcommand
  4117. run_parser = subparsers.add_parser("run", help="Run maintenance tasks")
  4118. run_parser.add_argument(
  4119. "--task",
  4120. action="append",
  4121. dest="tasks",
  4122. help="Run a specific task (can be specified multiple times)",
  4123. )
  4124. run_parser.add_argument(
  4125. "--auto",
  4126. action="store_true",
  4127. help="Only run tasks if needed",
  4128. )
  4129. run_parser.add_argument(
  4130. "--quiet",
  4131. "-q",
  4132. action="store_true",
  4133. help="Only report errors",
  4134. )
  4135. # maintenance start subcommand (placeholder)
  4136. subparsers.add_parser("start", help="Start background maintenance")
  4137. # maintenance stop subcommand (placeholder)
  4138. subparsers.add_parser("stop", help="Stop background maintenance")
  4139. # maintenance register subcommand
  4140. subparsers.add_parser("register", help="Register repository for maintenance")
  4141. # maintenance unregister subcommand
  4142. unregister_parser = subparsers.add_parser(
  4143. "unregister", help="Unregister repository from maintenance"
  4144. )
  4145. unregister_parser.add_argument(
  4146. "--force",
  4147. action="store_true",
  4148. help="Don't error if repository is not registered",
  4149. )
  4150. parsed_args = parser.parse_args(args)
  4151. if not parsed_args.subcommand:
  4152. parser.print_help()
  4153. return 1
  4154. if parsed_args.subcommand == "run":
  4155. # Progress callback
  4156. def progress(msg: str) -> None:
  4157. if not parsed_args.quiet:
  4158. logger.info(msg)
  4159. try:
  4160. result = porcelain.maintenance_run(
  4161. ".",
  4162. tasks=parsed_args.tasks,
  4163. auto=parsed_args.auto,
  4164. progress=progress if not parsed_args.quiet else None,
  4165. )
  4166. # Report results
  4167. if not parsed_args.quiet:
  4168. if result.tasks_succeeded:
  4169. logger.info("\nSuccessfully completed tasks:")
  4170. for task in result.tasks_succeeded:
  4171. logger.info(f" - {task}")
  4172. if result.tasks_failed:
  4173. logger.error("\nFailed tasks:")
  4174. for task in result.tasks_failed:
  4175. error_msg = result.errors.get(task, "Unknown error")
  4176. logger.error(f" - {task}: {error_msg}")
  4177. return 1
  4178. except porcelain.Error as e:
  4179. logger.error("%s", e)
  4180. return 1
  4181. elif parsed_args.subcommand == "register":
  4182. porcelain.maintenance_register(".")
  4183. logger.info("Repository registered for background maintenance")
  4184. elif parsed_args.subcommand == "unregister":
  4185. try:
  4186. force = getattr(parsed_args, "force", False)
  4187. porcelain.maintenance_unregister(".", force=force)
  4188. except ValueError as e:
  4189. logger.error(str(e))
  4190. return 1
  4191. logger.info("Repository unregistered from background maintenance")
  4192. elif parsed_args.subcommand in ("start", "stop"):
  4193. # TODO: Implement background maintenance scheduling
  4194. logger.error(
  4195. f"The '{parsed_args.subcommand}' subcommand is not yet implemented"
  4196. )
  4197. return 1
  4198. else:
  4199. parser.print_help()
  4200. return 1
  4201. return None
  4202. class cmd_grep(Command):
  4203. """Search for patterns in tracked files."""
  4204. def run(self, args: Sequence[str]) -> None:
  4205. """Execute the grep command.
  4206. Args:
  4207. args: Command line arguments
  4208. """
  4209. parser = argparse.ArgumentParser()
  4210. parser.add_argument("pattern", help="Regular expression pattern to search for")
  4211. parser.add_argument(
  4212. "revision",
  4213. nargs="?",
  4214. default=None,
  4215. help="Revision to search (defaults to HEAD)",
  4216. )
  4217. parser.add_argument(
  4218. "pathspecs",
  4219. nargs="*",
  4220. help="Path patterns to limit search",
  4221. )
  4222. parser.add_argument(
  4223. "-i",
  4224. "--ignore-case",
  4225. action="store_true",
  4226. help="Perform case-insensitive matching",
  4227. )
  4228. parser.add_argument(
  4229. "-n",
  4230. "--line-number",
  4231. action="store_true",
  4232. help="Show line numbers for matches",
  4233. )
  4234. parser.add_argument(
  4235. "--max-depth",
  4236. type=int,
  4237. default=None,
  4238. help="Maximum directory depth to search",
  4239. )
  4240. parser.add_argument(
  4241. "--no-ignore",
  4242. action="store_true",
  4243. help="Do not respect .gitignore patterns",
  4244. )
  4245. parsed_args = parser.parse_args(args)
  4246. # Handle the case where revision might be a pathspec
  4247. revision = parsed_args.revision
  4248. pathspecs = parsed_args.pathspecs
  4249. # If revision looks like a pathspec (contains wildcards or slashes),
  4250. # treat it as a pathspec instead
  4251. if revision and ("*" in revision or "/" in revision or "." in revision):
  4252. pathspecs = [revision, *pathspecs]
  4253. revision = None
  4254. with Repo(".") as repo:
  4255. config = repo.get_config_stack()
  4256. with get_pager(config=config, cmd_name="grep") as outstream:
  4257. porcelain.grep(
  4258. repo,
  4259. parsed_args.pattern,
  4260. outstream=outstream,
  4261. rev=revision,
  4262. pathspecs=pathspecs if pathspecs else None,
  4263. ignore_case=parsed_args.ignore_case,
  4264. line_number=parsed_args.line_number,
  4265. max_depth=parsed_args.max_depth,
  4266. respect_ignores=not parsed_args.no_ignore,
  4267. )
  4268. class cmd_count_objects(Command):
  4269. """Count unpacked number of objects and their disk consumption."""
  4270. def run(self, args: Sequence[str]) -> None:
  4271. """Execute the count-objects command.
  4272. Args:
  4273. args: Command line arguments
  4274. """
  4275. parser = argparse.ArgumentParser()
  4276. parser.add_argument(
  4277. "-v",
  4278. "--verbose",
  4279. action="store_true",
  4280. help="Display verbose information.",
  4281. )
  4282. parsed_args = parser.parse_args(args)
  4283. if parsed_args.verbose:
  4284. stats = porcelain.count_objects(".", verbose=True)
  4285. # Display verbose output
  4286. logger.info("count: %d", stats.count)
  4287. logger.info("size: %d", stats.size // 1024) # Size in KiB
  4288. assert stats.in_pack is not None
  4289. logger.info("in-pack: %d", stats.in_pack)
  4290. assert stats.packs is not None
  4291. logger.info("packs: %d", stats.packs)
  4292. assert stats.size_pack is not None
  4293. logger.info("size-pack: %d", stats.size_pack // 1024) # Size in KiB
  4294. else:
  4295. # Simple output
  4296. stats = porcelain.count_objects(".", verbose=False)
  4297. logger.info("%d objects, %d kilobytes", stats.count, stats.size // 1024)
  4298. class cmd_rebase(Command):
  4299. """Reapply commits on top of another base tip."""
  4300. def run(self, args: Sequence[str]) -> int:
  4301. """Execute the rebase command.
  4302. Args:
  4303. args: Command line arguments
  4304. """
  4305. parser = argparse.ArgumentParser()
  4306. parser.add_argument(
  4307. "upstream", nargs="?", help="Upstream branch to rebase onto"
  4308. )
  4309. parser.add_argument("--onto", type=str, help="Rebase onto specific commit")
  4310. parser.add_argument(
  4311. "--branch", type=str, help="Branch to rebase (default: current)"
  4312. )
  4313. parser.add_argument(
  4314. "-i", "--interactive", action="store_true", help="Interactive rebase"
  4315. )
  4316. parser.add_argument(
  4317. "--edit-todo",
  4318. action="store_true",
  4319. help="Edit the todo list during an interactive rebase",
  4320. )
  4321. parser.add_argument(
  4322. "--abort", action="store_true", help="Abort an in-progress rebase"
  4323. )
  4324. parser.add_argument(
  4325. "--continue",
  4326. dest="continue_rebase",
  4327. action="store_true",
  4328. help="Continue an in-progress rebase",
  4329. )
  4330. parser.add_argument(
  4331. "--skip", action="store_true", help="Skip current commit and continue"
  4332. )
  4333. parsed_args = parser.parse_args(args)
  4334. # Handle abort/continue/skip first
  4335. if parsed_args.abort:
  4336. try:
  4337. porcelain.rebase(".", parsed_args.upstream or "HEAD", abort=True)
  4338. logger.info("Rebase aborted.")
  4339. except porcelain.Error as e:
  4340. logger.error("%s", e)
  4341. return 1
  4342. return 0
  4343. if parsed_args.continue_rebase:
  4344. try:
  4345. # Check if interactive rebase is in progress
  4346. if porcelain.is_interactive_rebase("."):
  4347. result = porcelain.rebase(
  4348. ".",
  4349. parsed_args.upstream or "HEAD",
  4350. continue_rebase=True,
  4351. interactive=True,
  4352. )
  4353. if result:
  4354. logger.info("Rebase complete.")
  4355. else:
  4356. logger.info("Rebase paused. Use --continue to resume.")
  4357. else:
  4358. new_shas = porcelain.rebase(
  4359. ".", parsed_args.upstream or "HEAD", continue_rebase=True
  4360. )
  4361. logger.info("Rebase complete.")
  4362. except porcelain.Error as e:
  4363. logger.error("%s", e)
  4364. return 1
  4365. return 0
  4366. if parsed_args.edit_todo:
  4367. # Edit todo list for interactive rebase
  4368. try:
  4369. porcelain.rebase(".", parsed_args.upstream or "HEAD", edit_todo=True)
  4370. logger.info("Todo list updated.")
  4371. except porcelain.Error as e:
  4372. logger.error("%s", e)
  4373. return 1
  4374. return 0
  4375. # Normal rebase requires upstream
  4376. if not parsed_args.upstream:
  4377. logger.error("Missing required argument 'upstream'")
  4378. return 1
  4379. try:
  4380. if parsed_args.interactive:
  4381. # Interactive rebase
  4382. result = porcelain.rebase(
  4383. ".",
  4384. parsed_args.upstream,
  4385. onto=parsed_args.onto,
  4386. branch=parsed_args.branch,
  4387. interactive=True,
  4388. )
  4389. if result:
  4390. logger.info(
  4391. "Interactive rebase started. Edit the todo list and save."
  4392. )
  4393. else:
  4394. logger.info("No commits to rebase.")
  4395. else:
  4396. # Regular rebase
  4397. new_shas = porcelain.rebase(
  4398. ".",
  4399. parsed_args.upstream,
  4400. onto=parsed_args.onto,
  4401. branch=parsed_args.branch,
  4402. )
  4403. if new_shas:
  4404. logger.info("Successfully rebased %d commits.", len(new_shas))
  4405. else:
  4406. logger.info("Already up to date.")
  4407. return 0
  4408. except porcelain.Error as e:
  4409. logger.error("%s", e)
  4410. return 1
  4411. class cmd_filter_branch(Command):
  4412. """Rewrite branches."""
  4413. def run(self, args: Sequence[str]) -> int | None:
  4414. """Execute the filter-branch command.
  4415. Args:
  4416. args: Command line arguments
  4417. """
  4418. import subprocess
  4419. parser = argparse.ArgumentParser(description="Rewrite branches")
  4420. # Supported Git-compatible options
  4421. parser.add_argument(
  4422. "--subdirectory-filter",
  4423. type=str,
  4424. help="Only include history for subdirectory",
  4425. )
  4426. parser.add_argument("--env-filter", type=str, help="Environment filter command")
  4427. parser.add_argument("--tree-filter", type=str, help="Tree filter command")
  4428. parser.add_argument("--index-filter", type=str, help="Index filter command")
  4429. parser.add_argument("--parent-filter", type=str, help="Parent filter command")
  4430. parser.add_argument("--msg-filter", type=str, help="Message filter command")
  4431. parser.add_argument("--commit-filter", type=str, help="Commit filter command")
  4432. parser.add_argument(
  4433. "--tag-name-filter", type=str, help="Tag name filter command"
  4434. )
  4435. parser.add_argument(
  4436. "--prune-empty", action="store_true", help="Remove empty commits"
  4437. )
  4438. parser.add_argument(
  4439. "--original",
  4440. type=str,
  4441. default="refs/original",
  4442. help="Namespace for original refs",
  4443. )
  4444. parser.add_argument(
  4445. "-f",
  4446. "--force",
  4447. action="store_true",
  4448. help="Force operation even if refs/original/* exists",
  4449. )
  4450. # Branch/ref to rewrite (defaults to HEAD)
  4451. parser.add_argument(
  4452. "branch", nargs="?", default="HEAD", help="Branch or ref to rewrite"
  4453. )
  4454. parsed_args = parser.parse_args(args)
  4455. # Track if any filter fails
  4456. filter_error = False
  4457. # Setup environment for filters
  4458. env = os.environ.copy()
  4459. # Helper function to run shell commands
  4460. def run_filter(
  4461. cmd: str,
  4462. input_data: bytes | None = None,
  4463. cwd: str | None = None,
  4464. extra_env: dict[str, str] | None = None,
  4465. ) -> bytes | None:
  4466. nonlocal filter_error
  4467. filter_env = env.copy()
  4468. if extra_env:
  4469. filter_env.update(extra_env)
  4470. result = subprocess.run(
  4471. cmd,
  4472. shell=True,
  4473. input=input_data,
  4474. cwd=cwd,
  4475. env=filter_env,
  4476. capture_output=True,
  4477. )
  4478. if result.returncode != 0:
  4479. filter_error = True
  4480. return None
  4481. return result.stdout
  4482. # Create filter functions based on arguments
  4483. filter_message = None
  4484. if parsed_args.msg_filter:
  4485. def filter_message(message: bytes) -> bytes:
  4486. result = run_filter(parsed_args.msg_filter, input_data=message)
  4487. return result if result is not None else message
  4488. tree_filter = None
  4489. if parsed_args.tree_filter:
  4490. def tree_filter(tree_sha: ObjectID, tmpdir: str) -> ObjectID:
  4491. from dulwich.objects import Blob, Tree
  4492. # Export tree to tmpdir
  4493. with Repo(".") as r:
  4494. tree = r.object_store[tree_sha]
  4495. assert isinstance(tree, Tree)
  4496. for entry in tree.iteritems():
  4497. assert entry.path is not None
  4498. assert entry.sha is not None
  4499. path = Path(tmpdir) / entry.path.decode()
  4500. obj = r.object_store[entry.sha]
  4501. if isinstance(obj, Tree):
  4502. path.mkdir(exist_ok=True)
  4503. else:
  4504. assert isinstance(obj, Blob)
  4505. path.write_bytes(obj.data)
  4506. # Run the filter command in the temp directory
  4507. run_filter(parsed_args.tree_filter, cwd=tmpdir)
  4508. # Rebuild tree from modified temp directory
  4509. def build_tree_from_dir(dir_path: str) -> ObjectID:
  4510. tree = Tree()
  4511. for name in sorted(os.listdir(dir_path)):
  4512. if name.startswith("."):
  4513. continue
  4514. path = os.path.join(dir_path, name)
  4515. if os.path.isdir(path):
  4516. subtree_sha = build_tree_from_dir(path)
  4517. tree.add(name.encode(), 0o040000, subtree_sha)
  4518. else:
  4519. with open(path, "rb") as f:
  4520. data = f.read()
  4521. blob = Blob.from_string(data)
  4522. r.object_store.add_object(blob)
  4523. # Use appropriate file mode
  4524. mode = os.stat(path).st_mode
  4525. if mode & 0o100:
  4526. file_mode = 0o100755
  4527. else:
  4528. file_mode = 0o100644
  4529. tree.add(name.encode(), file_mode, blob.id)
  4530. r.object_store.add_object(tree)
  4531. return tree.id
  4532. return build_tree_from_dir(tmpdir)
  4533. index_filter = None
  4534. if parsed_args.index_filter:
  4535. def index_filter(tree_sha: ObjectID, index_path: str) -> ObjectID | None:
  4536. run_filter(
  4537. parsed_args.index_filter, extra_env={"GIT_INDEX_FILE": index_path}
  4538. )
  4539. return None # Read back from index
  4540. parent_filter = None
  4541. if parsed_args.parent_filter:
  4542. def parent_filter(parents: Sequence[ObjectID]) -> list[ObjectID]:
  4543. parent_str = " ".join(p.hex() for p in parents)
  4544. result = run_filter(
  4545. parsed_args.parent_filter, input_data=parent_str.encode()
  4546. )
  4547. if result is None:
  4548. return list(parents)
  4549. output = result.decode().strip()
  4550. if not output:
  4551. return []
  4552. new_parents = []
  4553. for sha in output.split():
  4554. sha_bytes = sha.encode()
  4555. if valid_hexsha(sha_bytes):
  4556. new_parents.append(ObjectID(sha_bytes))
  4557. return new_parents
  4558. commit_filter = None
  4559. if parsed_args.commit_filter:
  4560. def commit_filter(
  4561. commit_obj: Commit, tree_sha: ObjectID
  4562. ) -> ObjectID | None:
  4563. # The filter receives: tree parent1 parent2...
  4564. cmd_input = tree_sha.hex()
  4565. for parent in commit_obj.parents:
  4566. cmd_input += " " + parent.hex()
  4567. result = run_filter(
  4568. parsed_args.commit_filter,
  4569. input_data=cmd_input.encode(),
  4570. extra_env={"GIT_COMMIT": commit_obj.id.hex()},
  4571. )
  4572. if result is None:
  4573. return None
  4574. output = result.decode().strip()
  4575. if not output:
  4576. return None # Skip commit
  4577. if valid_hexsha(output):
  4578. return ObjectID(output.encode())
  4579. return None
  4580. tag_name_filter = None
  4581. if parsed_args.tag_name_filter:
  4582. def tag_name_filter(tag_name: bytes) -> bytes:
  4583. result = run_filter(parsed_args.tag_name_filter, input_data=tag_name)
  4584. return result if result is not None else tag_name
  4585. # Open repo once
  4586. with Repo(".") as r:
  4587. # Check for refs/original if not forcing
  4588. if not parsed_args.force:
  4589. original_prefix = parsed_args.original.encode() + b"/"
  4590. for ref in r.refs.allkeys():
  4591. if ref.startswith(original_prefix):
  4592. logger.error("Cannot create a new backup.")
  4593. logger.error(
  4594. "A previous backup already exists in %s/",
  4595. parsed_args.original,
  4596. )
  4597. logger.error("Force overwriting the backup with -f")
  4598. print("Cannot create a new backup.")
  4599. print(
  4600. f"A previous backup already exists in {parsed_args.original}/"
  4601. )
  4602. print("Force overwriting the backup with -f")
  4603. return 1
  4604. try:
  4605. # Call porcelain.filter_branch with the repo object
  4606. result = porcelain.filter_branch(
  4607. r,
  4608. parsed_args.branch,
  4609. filter_message=filter_message,
  4610. tree_filter=tree_filter if parsed_args.tree_filter else None,
  4611. index_filter=index_filter if parsed_args.index_filter else None,
  4612. parent_filter=parent_filter if parsed_args.parent_filter else None,
  4613. commit_filter=commit_filter if parsed_args.commit_filter else None,
  4614. subdirectory_filter=parsed_args.subdirectory_filter,
  4615. prune_empty=parsed_args.prune_empty,
  4616. tag_name_filter=tag_name_filter
  4617. if parsed_args.tag_name_filter
  4618. else None,
  4619. force=parsed_args.force,
  4620. keep_original=True, # Always keep original with git
  4621. )
  4622. # Check if any filter failed
  4623. if filter_error:
  4624. logger.error("Filter command failed")
  4625. return 1
  4626. # Git filter-branch shows progress
  4627. if result:
  4628. logger.info(
  4629. "Rewrite %s (%d commits)", parsed_args.branch, len(result)
  4630. )
  4631. # Git shows: Ref 'refs/heads/branch' was rewritten
  4632. if parsed_args.branch != "HEAD":
  4633. ref_name = (
  4634. parsed_args.branch
  4635. if parsed_args.branch.startswith("refs/")
  4636. else f"refs/heads/{parsed_args.branch}"
  4637. )
  4638. logger.info("Ref '%s' was rewritten", ref_name)
  4639. return 0
  4640. except porcelain.Error as e:
  4641. logger.error("%s", e)
  4642. return 1
  4643. class cmd_lfs(Command):
  4644. """Git Large File Storage management."""
  4645. """Git LFS management commands."""
  4646. def run(self, argv: Sequence[str]) -> None:
  4647. """Execute the lfs command.
  4648. Args:
  4649. argv: Command line arguments
  4650. """
  4651. parser = argparse.ArgumentParser(prog="dulwich lfs")
  4652. subparsers = parser.add_subparsers(dest="subcommand", help="LFS subcommands")
  4653. # lfs init
  4654. subparsers.add_parser("init", help="Initialize Git LFS")
  4655. # lfs track
  4656. parser_track = subparsers.add_parser(
  4657. "track", help="Track file patterns with LFS"
  4658. )
  4659. parser_track.add_argument("patterns", nargs="*", help="File patterns to track")
  4660. # lfs untrack
  4661. parser_untrack = subparsers.add_parser(
  4662. "untrack", help="Untrack file patterns from LFS"
  4663. )
  4664. parser_untrack.add_argument(
  4665. "patterns", nargs="+", help="File patterns to untrack"
  4666. )
  4667. # lfs ls-files
  4668. parser_ls = subparsers.add_parser("ls-files", help="List LFS files")
  4669. parser_ls.add_argument("--ref", help="Git ref to check (defaults to HEAD)")
  4670. # lfs migrate
  4671. parser_migrate = subparsers.add_parser("migrate", help="Migrate files to LFS")
  4672. parser_migrate.add_argument("--include", nargs="+", help="Patterns to include")
  4673. parser_migrate.add_argument("--exclude", nargs="+", help="Patterns to exclude")
  4674. parser_migrate.add_argument(
  4675. "--everything", action="store_true", help="Migrate all files above 100MB"
  4676. )
  4677. # lfs pointer
  4678. parser_pointer = subparsers.add_parser("pointer", help="Check LFS pointers")
  4679. parser_pointer.add_argument(
  4680. "--check", nargs="*", dest="paths", help="Check if files are LFS pointers"
  4681. )
  4682. # lfs clean
  4683. parser_clean = subparsers.add_parser("clean", help="Clean file to LFS pointer")
  4684. parser_clean.add_argument("path", help="File path to clean")
  4685. # lfs smudge
  4686. parser_smudge = subparsers.add_parser(
  4687. "smudge", help="Smudge LFS pointer to content"
  4688. )
  4689. parser_smudge.add_argument(
  4690. "--stdin", action="store_true", help="Read pointer from stdin"
  4691. )
  4692. # lfs fetch
  4693. parser_fetch = subparsers.add_parser(
  4694. "fetch", help="Fetch LFS objects from remote"
  4695. )
  4696. parser_fetch.add_argument(
  4697. "--remote", default="origin", help="Remote to fetch from"
  4698. )
  4699. parser_fetch.add_argument("refs", nargs="*", help="Specific refs to fetch")
  4700. # lfs pull
  4701. parser_pull = subparsers.add_parser(
  4702. "pull", help="Pull LFS objects for current checkout"
  4703. )
  4704. parser_pull.add_argument(
  4705. "--remote", default="origin", help="Remote to pull from"
  4706. )
  4707. # lfs push
  4708. parser_push = subparsers.add_parser("push", help="Push LFS objects to remote")
  4709. parser_push.add_argument("--remote", default="origin", help="Remote to push to")
  4710. parser_push.add_argument("refs", nargs="*", help="Specific refs to push")
  4711. # lfs status
  4712. subparsers.add_parser("status", help="Show status of LFS files")
  4713. args = parser.parse_args(argv)
  4714. if args.subcommand == "init":
  4715. porcelain.lfs_init()
  4716. logger.info("Git LFS initialized.")
  4717. elif args.subcommand == "track":
  4718. if args.patterns:
  4719. tracked = porcelain.lfs_track(patterns=args.patterns)
  4720. logger.info("Tracking patterns:")
  4721. else:
  4722. tracked = porcelain.lfs_track()
  4723. logger.info("Currently tracked patterns:")
  4724. for pattern in tracked:
  4725. logger.info(" %s", pattern)
  4726. elif args.subcommand == "untrack":
  4727. tracked = porcelain.lfs_untrack(patterns=args.patterns)
  4728. logger.info("Remaining tracked patterns:")
  4729. for pattern in tracked:
  4730. logger.info(" %s", to_display_str(pattern))
  4731. elif args.subcommand == "ls-files":
  4732. files = porcelain.lfs_ls_files(ref=args.ref)
  4733. for path, oid, size in files:
  4734. logger.info(
  4735. "%s * %s (%s)",
  4736. to_display_str(oid[:12]),
  4737. to_display_str(path),
  4738. format_bytes(size),
  4739. )
  4740. elif args.subcommand == "migrate":
  4741. count = porcelain.lfs_migrate(
  4742. include=args.include, exclude=args.exclude, everything=args.everything
  4743. )
  4744. logger.info("Migrated %d file(s) to Git LFS.", count)
  4745. elif args.subcommand == "pointer":
  4746. if args.paths is not None:
  4747. results = porcelain.lfs_pointer_check(paths=args.paths or None)
  4748. for file_path, pointer in results.items():
  4749. if pointer:
  4750. logger.info(
  4751. "%s: LFS pointer (oid: %s, size: %s)",
  4752. to_display_str(file_path),
  4753. to_display_str(pointer.oid[:12]),
  4754. format_bytes(pointer.size),
  4755. )
  4756. else:
  4757. logger.warning(
  4758. "%s: Not an LFS pointer", to_display_str(file_path)
  4759. )
  4760. elif args.subcommand == "clean":
  4761. pointer = porcelain.lfs_clean(path=args.path)
  4762. sys.stdout.buffer.write(pointer)
  4763. elif args.subcommand == "smudge":
  4764. if args.stdin:
  4765. pointer_content = sys.stdin.buffer.read()
  4766. content = porcelain.lfs_smudge(pointer_content=pointer_content)
  4767. sys.stdout.buffer.write(content)
  4768. else:
  4769. logger.error("--stdin required for smudge command")
  4770. sys.exit(1)
  4771. elif args.subcommand == "fetch":
  4772. refs = args.refs or None
  4773. count = porcelain.lfs_fetch(remote=args.remote, refs=refs)
  4774. logger.info("Fetched %d LFS object(s).", count)
  4775. elif args.subcommand == "pull":
  4776. count = porcelain.lfs_pull(remote=args.remote)
  4777. logger.info("Pulled %d LFS object(s).", count)
  4778. elif args.subcommand == "push":
  4779. refs = args.refs or None
  4780. count = porcelain.lfs_push(remote=args.remote, refs=refs)
  4781. logger.info("Pushed %d LFS object(s).", count)
  4782. elif args.subcommand == "status":
  4783. status = porcelain.lfs_status()
  4784. if status["tracked"]:
  4785. logger.info("LFS tracked files: %d", len(status["tracked"]))
  4786. if status["missing"]:
  4787. logger.warning("\nMissing LFS objects:")
  4788. for file_path in status["missing"]:
  4789. logger.warning(" %s", to_display_str(file_path))
  4790. if status["not_staged"]:
  4791. logger.info("\nModified LFS files not staged:")
  4792. for file_path in status["not_staged"]:
  4793. logger.warning(" %s", to_display_str(file_path))
  4794. if not any(status.values()):
  4795. logger.info("No LFS files found.")
  4796. else:
  4797. parser.print_help()
  4798. sys.exit(1)
  4799. class cmd_help(Command):
  4800. """Display help information about git."""
  4801. def run(self, args: Sequence[str]) -> None:
  4802. """Execute the help command.
  4803. Args:
  4804. args: Command line arguments
  4805. """
  4806. parser = argparse.ArgumentParser()
  4807. parser.add_argument(
  4808. "-a",
  4809. "--all",
  4810. action="store_true",
  4811. help="List all commands.",
  4812. )
  4813. parsed_args = parser.parse_args(args)
  4814. if parsed_args.all:
  4815. logger.info("Available commands:")
  4816. for cmd in sorted(commands):
  4817. logger.info(" %s", cmd)
  4818. else:
  4819. logger.info(
  4820. "The dulwich command line tool is currently a very basic frontend for the\n"
  4821. "Dulwich python module. For full functionality, please see the API reference.\n"
  4822. "\n"
  4823. "For a list of supported commands, see 'dulwich help -a'."
  4824. )
  4825. class cmd_format_patch(Command):
  4826. """Prepare patches for e-mail submission."""
  4827. def run(self, args: Sequence[str]) -> None:
  4828. """Execute the format-patch command.
  4829. Args:
  4830. args: Command line arguments
  4831. """
  4832. parser = argparse.ArgumentParser()
  4833. parser.add_argument(
  4834. "committish",
  4835. nargs="?",
  4836. help="Commit or commit range (e.g., HEAD~3..HEAD or origin/master..HEAD)",
  4837. )
  4838. parser.add_argument(
  4839. "-n",
  4840. "--numbered",
  4841. type=int,
  4842. default=1,
  4843. help="Number of commits to format (default: 1)",
  4844. )
  4845. parser.add_argument(
  4846. "-o",
  4847. "--output-directory",
  4848. dest="outdir",
  4849. help="Output directory for patches",
  4850. )
  4851. parser.add_argument(
  4852. "--stdout",
  4853. action="store_true",
  4854. help="Output patches to stdout",
  4855. )
  4856. parsed_args = parser.parse_args(args)
  4857. # Parse committish using the new function
  4858. committish: ObjectID | tuple[ObjectID, ObjectID] | None = None
  4859. if parsed_args.committish:
  4860. with Repo(".") as r:
  4861. range_result = parse_commit_range(r, parsed_args.committish)
  4862. if range_result:
  4863. # Convert Commit objects to their SHAs
  4864. committish = (range_result[0].id, range_result[1].id)
  4865. else:
  4866. committish = ObjectID(
  4867. parsed_args.committish.encode()
  4868. if isinstance(parsed_args.committish, str)
  4869. else parsed_args.committish
  4870. )
  4871. filenames = porcelain.format_patch(
  4872. ".",
  4873. committish=committish,
  4874. outstream=sys.stdout,
  4875. outdir=parsed_args.outdir,
  4876. n=parsed_args.numbered,
  4877. stdout=parsed_args.stdout,
  4878. )
  4879. if not parsed_args.stdout:
  4880. for filename in filenames:
  4881. logger.info(filename)
  4882. class cmd_mailsplit(Command):
  4883. """Split mbox or Maildir into individual message files."""
  4884. def run(self, args: Sequence[str]) -> None:
  4885. """Execute the mailsplit command.
  4886. Args:
  4887. args: Command line arguments
  4888. """
  4889. parser = argparse.ArgumentParser()
  4890. parser.add_argument(
  4891. "mbox",
  4892. nargs="?",
  4893. help="Path to mbox file or Maildir. If not specified, reads from stdin.",
  4894. )
  4895. parser.add_argument(
  4896. "-o",
  4897. "--output-directory",
  4898. dest="output_dir",
  4899. required=True,
  4900. help="Directory in which to place the individual messages",
  4901. )
  4902. parser.add_argument(
  4903. "-b",
  4904. action="store_true",
  4905. dest="single_mail",
  4906. help="If any file doesn't begin with a From line, assume it is a single mail message",
  4907. )
  4908. parser.add_argument(
  4909. "-d",
  4910. dest="precision",
  4911. type=int,
  4912. default=4,
  4913. help="Number of digits for generated filenames (default: 4)",
  4914. )
  4915. parser.add_argument(
  4916. "-f",
  4917. dest="start_number",
  4918. type=int,
  4919. default=1,
  4920. help="Skip the first <nn> numbers (default: 1)",
  4921. )
  4922. parser.add_argument(
  4923. "--keep-cr",
  4924. action="store_true",
  4925. help="Do not remove \\r from lines ending with \\r\\n",
  4926. )
  4927. parser.add_argument(
  4928. "--mboxrd",
  4929. action="store_true",
  4930. help='Input is of the "mboxrd" format and "^>+From " line escaping is reversed',
  4931. )
  4932. parsed_args = parser.parse_args(args)
  4933. # Determine if input is a Maildir
  4934. is_maildir = False
  4935. if parsed_args.mbox:
  4936. input_path = Path(parsed_args.mbox)
  4937. if input_path.is_dir():
  4938. # Check if it's a Maildir (has cur, tmp, new subdirectories)
  4939. if (
  4940. (input_path / "cur").exists()
  4941. and (input_path / "tmp").exists()
  4942. and (input_path / "new").exists()
  4943. ):
  4944. is_maildir = True
  4945. else:
  4946. input_path = None
  4947. # Call porcelain function
  4948. output_files = porcelain.mailsplit(
  4949. input_path=input_path,
  4950. output_dir=parsed_args.output_dir,
  4951. start_number=parsed_args.start_number,
  4952. precision=parsed_args.precision,
  4953. keep_cr=parsed_args.keep_cr,
  4954. mboxrd=parsed_args.mboxrd,
  4955. is_maildir=is_maildir,
  4956. )
  4957. # Print information about the split
  4958. logger.info(
  4959. "Split %d messages into %s", len(output_files), parsed_args.output_dir
  4960. )
  4961. class cmd_mailinfo(Command):
  4962. """Extract patch information from an email message."""
  4963. def run(self, args: Sequence[str]) -> None:
  4964. """Execute the mailinfo command.
  4965. Args:
  4966. args: Command line arguments
  4967. """
  4968. parser = argparse.ArgumentParser()
  4969. parser.add_argument(
  4970. "msg",
  4971. help="File to write commit message",
  4972. )
  4973. parser.add_argument(
  4974. "patch",
  4975. help="File to write patch content",
  4976. )
  4977. parser.add_argument(
  4978. "mail",
  4979. nargs="?",
  4980. help="Path to email file. If not specified, reads from stdin.",
  4981. )
  4982. parser.add_argument(
  4983. "-k",
  4984. action="store_true",
  4985. dest="keep_subject",
  4986. help="Pass -k flag to git mailinfo (keeps [PATCH] and other subject tags)",
  4987. )
  4988. parser.add_argument(
  4989. "-b",
  4990. action="store_true",
  4991. dest="keep_non_patch",
  4992. help="Pass -b flag to git mailinfo (only strip [PATCH] tags)",
  4993. )
  4994. parser.add_argument(
  4995. "--encoding",
  4996. dest="encoding",
  4997. help="Character encoding to use (default: detect from message)",
  4998. )
  4999. parser.add_argument(
  5000. "--scissors",
  5001. action="store_true",
  5002. help="Remove everything before scissors line",
  5003. )
  5004. parser.add_argument(
  5005. "-m",
  5006. "--message-id",
  5007. action="store_true",
  5008. dest="message_id",
  5009. help="Copy Message-ID to the end of the commit message",
  5010. )
  5011. parsed_args = parser.parse_args(args)
  5012. # Call porcelain function
  5013. result = porcelain.mailinfo(
  5014. input_path=parsed_args.mail,
  5015. msg_file=parsed_args.msg,
  5016. patch_file=parsed_args.patch,
  5017. keep_subject=parsed_args.keep_subject,
  5018. keep_non_patch=parsed_args.keep_non_patch,
  5019. encoding=parsed_args.encoding,
  5020. scissors=parsed_args.scissors,
  5021. message_id=parsed_args.message_id,
  5022. )
  5023. # Print author info to stdout (as git mailinfo does)
  5024. print(f"Author: {result.author_name}")
  5025. print(f"Email: {result.author_email}")
  5026. print(f"Subject: {result.subject}")
  5027. if result.author_date:
  5028. print(f"Date: {result.author_date}")
  5029. class cmd_bundle(Command):
  5030. """Create, unpack, and manipulate bundle files."""
  5031. def run(self, args: Sequence[str]) -> int:
  5032. """Execute the bundle command.
  5033. Args:
  5034. args: Command line arguments
  5035. """
  5036. if not args:
  5037. logger.error("Usage: bundle <create|verify|list-heads|unbundle> <options>")
  5038. return 1
  5039. subcommand = args[0]
  5040. subargs = args[1:]
  5041. if subcommand == "create":
  5042. return self._create(subargs)
  5043. elif subcommand == "verify":
  5044. return self._verify(subargs)
  5045. elif subcommand == "list-heads":
  5046. return self._list_heads(subargs)
  5047. elif subcommand == "unbundle":
  5048. return self._unbundle(subargs)
  5049. else:
  5050. logger.error("Unknown bundle subcommand: %s", subcommand)
  5051. return 1
  5052. def _create(self, args: Sequence[str]) -> int:
  5053. parser = argparse.ArgumentParser(prog="bundle create")
  5054. parser.add_argument(
  5055. "-q", "--quiet", action="store_true", help="Suppress progress"
  5056. )
  5057. parser.add_argument("--progress", action="store_true", help="Show progress")
  5058. parser.add_argument(
  5059. "--version", type=int, choices=[2, 3], help="Bundle version"
  5060. )
  5061. parser.add_argument("--all", action="store_true", help="Include all refs")
  5062. parser.add_argument("--stdin", action="store_true", help="Read refs from stdin")
  5063. parser.add_argument("file", help="Output bundle file (use - for stdout)")
  5064. parser.add_argument("refs", nargs="*", help="References or rev-list args")
  5065. parsed_args = parser.parse_args(args)
  5066. repo = Repo(".")
  5067. progress = None
  5068. if parsed_args.progress and not parsed_args.quiet:
  5069. def progress(*args: str | int) -> None:
  5070. # Handle both progress(msg) and progress(count, msg) signatures
  5071. if len(args) == 1:
  5072. msg = args[0]
  5073. elif len(args) == 2:
  5074. _count, msg = args
  5075. else:
  5076. msg = str(args)
  5077. # Convert bytes to string if needed
  5078. if isinstance(msg, bytes):
  5079. msg = msg.decode("utf-8", "replace")
  5080. logger.error("%s", msg)
  5081. refs_to_include: list[Ref] = []
  5082. prerequisites = []
  5083. if parsed_args.all:
  5084. refs_to_include = list(repo.refs.keys())
  5085. elif parsed_args.stdin:
  5086. for line in sys.stdin:
  5087. ref = line.strip().encode("utf-8")
  5088. if ref:
  5089. refs_to_include.append(Ref(ref))
  5090. elif parsed_args.refs:
  5091. for ref_arg in parsed_args.refs:
  5092. if ".." in ref_arg:
  5093. range_result = parse_commit_range(repo, ref_arg)
  5094. if range_result:
  5095. start_commit, _end_commit = range_result
  5096. prerequisites.append(start_commit.id)
  5097. # For ranges like A..B, we need to include B if it's a ref
  5098. # Split the range to get the end part
  5099. end_part = ref_arg.split("..")[1]
  5100. if end_part: # Not empty (not "A..")
  5101. end_ref = Ref(end_part.encode("utf-8"))
  5102. if end_ref in repo.refs:
  5103. refs_to_include.append(end_ref)
  5104. else:
  5105. sha = repo.refs[Ref(ref_arg.encode("utf-8"))]
  5106. refs_to_include.append(Ref(ref_arg.encode("utf-8")))
  5107. else:
  5108. if ref_arg.startswith("^"):
  5109. sha = repo.refs[Ref(ref_arg[1:].encode("utf-8"))]
  5110. prerequisites.append(sha)
  5111. else:
  5112. sha = repo.refs[Ref(ref_arg.encode("utf-8"))]
  5113. refs_to_include.append(Ref(ref_arg.encode("utf-8")))
  5114. else:
  5115. logger.error("No refs specified. Use --all, --stdin, or specify refs")
  5116. return 1
  5117. if not refs_to_include:
  5118. logger.error("fatal: Refusing to create empty bundle.")
  5119. return 1
  5120. bundle = create_bundle_from_repo(
  5121. repo,
  5122. refs=refs_to_include,
  5123. prerequisites=prerequisites,
  5124. version=parsed_args.version,
  5125. progress=progress,
  5126. )
  5127. if parsed_args.file == "-":
  5128. write_bundle(sys.stdout.buffer, bundle)
  5129. else:
  5130. with open(parsed_args.file, "wb") as f:
  5131. write_bundle(f, bundle)
  5132. return 0
  5133. def _verify(self, args: Sequence[str]) -> int:
  5134. parser = argparse.ArgumentParser(prog="bundle verify")
  5135. parser.add_argument(
  5136. "-q", "--quiet", action="store_true", help="Suppress output"
  5137. )
  5138. parser.add_argument("file", help="Bundle file to verify (use - for stdin)")
  5139. parsed_args = parser.parse_args(args)
  5140. repo = Repo(".")
  5141. def verify_bundle(bundle: Bundle) -> int:
  5142. missing_prereqs = []
  5143. for prereq_sha, comment in bundle.prerequisites:
  5144. try:
  5145. repo.object_store[prereq_sha]
  5146. except KeyError:
  5147. missing_prereqs.append(prereq_sha)
  5148. if missing_prereqs:
  5149. if not parsed_args.quiet:
  5150. logger.info("The bundle requires these prerequisite commits:")
  5151. for sha in missing_prereqs:
  5152. logger.info(" %s", sha.decode())
  5153. return 1
  5154. else:
  5155. if not parsed_args.quiet:
  5156. logger.info(
  5157. "The bundle is valid and can be applied to the current repository"
  5158. )
  5159. return 0
  5160. if parsed_args.file == "-":
  5161. bundle = read_bundle(sys.stdin.buffer)
  5162. return verify_bundle(bundle)
  5163. else:
  5164. with open(parsed_args.file, "rb") as f:
  5165. bundle = read_bundle(f)
  5166. return verify_bundle(bundle)
  5167. def _list_heads(self, args: Sequence[str]) -> int:
  5168. parser = argparse.ArgumentParser(prog="bundle list-heads")
  5169. parser.add_argument("file", help="Bundle file (use - for stdin)")
  5170. parser.add_argument("refnames", nargs="*", help="Only show these refs")
  5171. parsed_args = parser.parse_args(args)
  5172. def list_heads(bundle: Bundle) -> None:
  5173. for ref, sha in bundle.references.items():
  5174. if not parsed_args.refnames or ref.decode() in parsed_args.refnames:
  5175. logger.info("%s %s", sha.decode(), ref.decode())
  5176. if parsed_args.file == "-":
  5177. bundle = read_bundle(sys.stdin.buffer)
  5178. list_heads(bundle)
  5179. else:
  5180. with open(parsed_args.file, "rb") as f:
  5181. bundle = read_bundle(f)
  5182. list_heads(bundle)
  5183. return 0
  5184. def _unbundle(self, args: Sequence[str]) -> int:
  5185. parser = argparse.ArgumentParser(prog="bundle unbundle")
  5186. parser.add_argument("--progress", action="store_true", help="Show progress")
  5187. parser.add_argument("file", help="Bundle file (use - for stdin)")
  5188. parser.add_argument("refnames", nargs="*", help="Only unbundle these refs")
  5189. parsed_args = parser.parse_args(args)
  5190. repo = Repo(".")
  5191. progress = None
  5192. if parsed_args.progress:
  5193. def progress(*args: str | int | bytes) -> None:
  5194. # Handle both progress(msg) and progress(count, msg) signatures
  5195. if len(args) == 1:
  5196. msg = args[0]
  5197. elif len(args) == 2:
  5198. _count, msg = args
  5199. else:
  5200. msg = str(args)
  5201. # Convert bytes to string if needed
  5202. if isinstance(msg, bytes):
  5203. msg = msg.decode("utf-8", "replace")
  5204. elif not isinstance(msg, str):
  5205. msg = str(msg)
  5206. logger.error("%s", msg)
  5207. if parsed_args.file == "-":
  5208. bundle = read_bundle(sys.stdin.buffer)
  5209. # Process the bundle while file is still available via stdin
  5210. bundle.store_objects(repo.object_store, progress=progress)
  5211. else:
  5212. # Keep the file open during bundle processing
  5213. with open(parsed_args.file, "rb") as f:
  5214. bundle = read_bundle(f)
  5215. # Process pack data while file is still open
  5216. bundle.store_objects(repo.object_store, progress=progress)
  5217. for ref, sha in bundle.references.items():
  5218. if not parsed_args.refnames or ref.decode() in parsed_args.refnames:
  5219. logger.info(ref.decode())
  5220. return 0
  5221. class cmd_worktree_add(Command):
  5222. """Create a new worktree."""
  5223. """Add a new worktree to the repository."""
  5224. def run(self, args: Sequence[str]) -> int | None:
  5225. """Execute the worktree-add command.
  5226. Args:
  5227. args: Command line arguments
  5228. """
  5229. parser = argparse.ArgumentParser(
  5230. description="Add a new worktree", prog="dulwich worktree add"
  5231. )
  5232. parser.add_argument("path", help="Path for the new worktree")
  5233. parser.add_argument("committish", nargs="?", help="Commit-ish to checkout")
  5234. parser.add_argument("-b", "--create-branch", help="Create a new branch")
  5235. parser.add_argument(
  5236. "-B", "--force-create-branch", help="Create or reset a branch"
  5237. )
  5238. parser.add_argument(
  5239. "--detach", action="store_true", help="Detach HEAD in new worktree"
  5240. )
  5241. parser.add_argument("--force", action="store_true", help="Force creation")
  5242. parsed_args = parser.parse_args(args)
  5243. from dulwich import porcelain
  5244. branch = None
  5245. commit = None
  5246. if parsed_args.create_branch or parsed_args.force_create_branch:
  5247. branch = (
  5248. parsed_args.create_branch or parsed_args.force_create_branch
  5249. ).encode()
  5250. elif parsed_args.committish and not parsed_args.detach:
  5251. # If committish is provided and not detaching, treat as branch
  5252. branch = parsed_args.committish.encode()
  5253. elif parsed_args.committish:
  5254. # If committish is provided and detaching, treat as commit
  5255. commit = parsed_args.committish.encode()
  5256. worktree_path = porcelain.worktree_add(
  5257. repo=".",
  5258. path=parsed_args.path,
  5259. branch=branch,
  5260. commit=commit,
  5261. detach=parsed_args.detach,
  5262. force=parsed_args.force or bool(parsed_args.force_create_branch),
  5263. )
  5264. logger.info("Worktree added: %s", worktree_path)
  5265. return 0
  5266. class cmd_worktree_list(Command):
  5267. """List worktrees."""
  5268. """List details of each worktree."""
  5269. def run(self, args: Sequence[str]) -> int | None:
  5270. """Execute the worktree-list command.
  5271. Args:
  5272. args: Command line arguments
  5273. """
  5274. parser = argparse.ArgumentParser(
  5275. description="List worktrees", prog="dulwich worktree list"
  5276. )
  5277. parser.add_argument(
  5278. "-v", "--verbose", action="store_true", help="Show additional information"
  5279. )
  5280. parser.add_argument(
  5281. "--porcelain", action="store_true", help="Machine-readable output"
  5282. )
  5283. parsed_args = parser.parse_args(args)
  5284. from dulwich import porcelain
  5285. worktrees = porcelain.worktree_list(repo=".")
  5286. for wt in worktrees:
  5287. path = wt.path
  5288. if wt.bare:
  5289. status = "(bare)"
  5290. elif wt.detached:
  5291. status = (
  5292. f"(detached HEAD {wt.head[:7].decode() if wt.head else 'unknown'})"
  5293. )
  5294. elif wt.branch:
  5295. branch_name = wt.branch.decode().replace("refs/heads/", "")
  5296. status = f"[{branch_name}]"
  5297. else:
  5298. status = "(unknown)"
  5299. if parsed_args.porcelain:
  5300. locked = "locked" if wt.locked else "unlocked"
  5301. prunable = "prunable" if wt.prunable else "unprunable"
  5302. logger.info(
  5303. "%s %s %s %s %s",
  5304. path,
  5305. wt.head.decode() if wt.head else "unknown",
  5306. status,
  5307. locked,
  5308. prunable,
  5309. )
  5310. else:
  5311. line = f"{path} {status}"
  5312. if wt.locked:
  5313. line += " locked"
  5314. if wt.prunable:
  5315. line += " prunable"
  5316. logger.info(line)
  5317. return 0
  5318. class cmd_worktree_remove(Command):
  5319. """Remove a worktree."""
  5320. """Remove a worktree."""
  5321. def run(self, args: Sequence[str]) -> int | None:
  5322. """Execute the worktree-remove command.
  5323. Args:
  5324. args: Command line arguments
  5325. """
  5326. parser = argparse.ArgumentParser(
  5327. description="Remove a worktree", prog="dulwich worktree remove"
  5328. )
  5329. parser.add_argument("worktree", help="Path to worktree to remove")
  5330. parser.add_argument("--force", action="store_true", help="Force removal")
  5331. parsed_args = parser.parse_args(args)
  5332. from dulwich import porcelain
  5333. porcelain.worktree_remove(
  5334. repo=".", path=parsed_args.worktree, force=parsed_args.force
  5335. )
  5336. logger.info("Worktree removed: %s", parsed_args.worktree)
  5337. return 0
  5338. class cmd_worktree_prune(Command):
  5339. """Prune worktree information."""
  5340. """Prune worktree information."""
  5341. def run(self, args: Sequence[str]) -> int | None:
  5342. """Execute the worktree-prune command.
  5343. Args:
  5344. args: Command line arguments
  5345. """
  5346. parser = argparse.ArgumentParser(
  5347. description="Prune worktree information", prog="dulwich worktree prune"
  5348. )
  5349. parser.add_argument(
  5350. "--dry-run", action="store_true", help="Do not remove anything"
  5351. )
  5352. parser.add_argument(
  5353. "-v", "--verbose", action="store_true", help="Report all removals"
  5354. )
  5355. parser.add_argument(
  5356. "--expire", type=int, help="Expire worktrees older than time (seconds)"
  5357. )
  5358. parsed_args = parser.parse_args(args)
  5359. from dulwich import porcelain
  5360. pruned = porcelain.worktree_prune(
  5361. repo=".", dry_run=parsed_args.dry_run, expire=parsed_args.expire
  5362. )
  5363. if pruned:
  5364. if parsed_args.dry_run:
  5365. logger.info("Would prune worktrees:")
  5366. elif parsed_args.verbose:
  5367. logger.info("Pruned worktrees:")
  5368. for wt_id in pruned:
  5369. logger.info(" %s", wt_id)
  5370. elif parsed_args.verbose:
  5371. logger.info("No worktrees to prune")
  5372. return 0
  5373. class cmd_worktree_lock(Command):
  5374. """Lock a worktree to prevent it from being pruned."""
  5375. """Lock a worktree."""
  5376. def run(self, args: Sequence[str]) -> int | None:
  5377. """Execute the worktree-lock command.
  5378. Args:
  5379. args: Command line arguments
  5380. """
  5381. parser = argparse.ArgumentParser(
  5382. description="Lock a worktree", prog="dulwich worktree lock"
  5383. )
  5384. parser.add_argument("worktree", help="Path to worktree to lock")
  5385. parser.add_argument("--reason", help="Reason for locking")
  5386. parsed_args = parser.parse_args(args)
  5387. from dulwich import porcelain
  5388. porcelain.worktree_lock(
  5389. repo=".", path=parsed_args.worktree, reason=parsed_args.reason
  5390. )
  5391. logger.info("Worktree locked: %s", parsed_args.worktree)
  5392. return 0
  5393. class cmd_worktree_unlock(Command):
  5394. """Unlock a locked worktree."""
  5395. """Unlock a worktree."""
  5396. def run(self, args: Sequence[str]) -> int | None:
  5397. """Execute the worktree-unlock command.
  5398. Args:
  5399. args: Command line arguments
  5400. """
  5401. parser = argparse.ArgumentParser(
  5402. description="Unlock a worktree", prog="dulwich worktree unlock"
  5403. )
  5404. parser.add_argument("worktree", help="Path to worktree to unlock")
  5405. parsed_args = parser.parse_args(args)
  5406. from dulwich import porcelain
  5407. porcelain.worktree_unlock(repo=".", path=parsed_args.worktree)
  5408. logger.info("Worktree unlocked: %s", parsed_args.worktree)
  5409. return 0
  5410. class cmd_worktree_move(Command):
  5411. """Move a worktree to a new location."""
  5412. """Move a worktree."""
  5413. def run(self, args: Sequence[str]) -> int | None:
  5414. """Execute the worktree-move command.
  5415. Args:
  5416. args: Command line arguments
  5417. """
  5418. parser = argparse.ArgumentParser(
  5419. description="Move a worktree", prog="dulwich worktree move"
  5420. )
  5421. parser.add_argument("worktree", help="Path to worktree to move")
  5422. parser.add_argument("new_path", help="New path for the worktree")
  5423. parsed_args = parser.parse_args(args)
  5424. from dulwich import porcelain
  5425. porcelain.worktree_move(
  5426. repo=".", old_path=parsed_args.worktree, new_path=parsed_args.new_path
  5427. )
  5428. logger.info(
  5429. "Worktree moved: %s -> %s", parsed_args.worktree, parsed_args.new_path
  5430. )
  5431. return 0
  5432. class cmd_worktree_repair(Command):
  5433. """Repair worktree administrative files."""
  5434. """Repair worktree administrative files."""
  5435. def run(self, args: Sequence[str]) -> int | None:
  5436. """Execute the worktree-repair command.
  5437. Args:
  5438. args: Command line arguments
  5439. """
  5440. parser = argparse.ArgumentParser(
  5441. description="Repair worktree administrative files",
  5442. prog="dulwich worktree repair",
  5443. )
  5444. parser.add_argument(
  5445. "path",
  5446. nargs="*",
  5447. help="Paths to worktrees to repair (if not specified, repairs all)",
  5448. )
  5449. parsed_args = parser.parse_args(args)
  5450. from dulwich import porcelain
  5451. paths = parsed_args.path if parsed_args.path else None
  5452. repaired = porcelain.worktree_repair(repo=".", paths=paths)
  5453. if repaired:
  5454. for path in repaired:
  5455. logger.info("Repaired worktree: %s", path)
  5456. else:
  5457. logger.info("No worktrees needed repair")
  5458. return 0
  5459. class cmd_worktree(SuperCommand):
  5460. """Manage multiple working trees."""
  5461. """Manage multiple working trees."""
  5462. subcommands: ClassVar[dict[str, type[Command]]] = {
  5463. "add": cmd_worktree_add,
  5464. "list": cmd_worktree_list,
  5465. "remove": cmd_worktree_remove,
  5466. "prune": cmd_worktree_prune,
  5467. "lock": cmd_worktree_lock,
  5468. "unlock": cmd_worktree_unlock,
  5469. "move": cmd_worktree_move,
  5470. "repair": cmd_worktree_repair,
  5471. }
  5472. default_command = cmd_worktree_list
  5473. class cmd_rerere(Command):
  5474. """Record and reuse recorded conflict resolutions."""
  5475. def run(self, args: Sequence[str]) -> None:
  5476. """Execute the rerere command.
  5477. Args:
  5478. args: Command line arguments
  5479. """
  5480. parser = argparse.ArgumentParser()
  5481. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  5482. parser.add_argument(
  5483. "subcommand",
  5484. nargs="?",
  5485. default=None,
  5486. choices=["status", "diff", "forget", "clear", "gc"],
  5487. help="Subcommand to execute (default: record conflicts)",
  5488. )
  5489. parser.add_argument(
  5490. "pathspec", nargs="?", help="Path specification (for forget subcommand)"
  5491. )
  5492. parser.add_argument(
  5493. "--max-age-days",
  5494. type=int,
  5495. default=60,
  5496. help="Maximum age in days for gc (default: 60)",
  5497. )
  5498. parsed_args = parser.parse_args(args)
  5499. if parsed_args.subcommand is None:
  5500. # Record current conflicts
  5501. recorded, resolved = porcelain.rerere(parsed_args.gitdir)
  5502. if not recorded:
  5503. sys.stdout.write("No conflicts to record.\n")
  5504. else:
  5505. for path, conflict_id in recorded:
  5506. sys.stdout.write(
  5507. f"Recorded resolution for {path.decode('utf-8')}: {conflict_id}\n"
  5508. )
  5509. if resolved:
  5510. sys.stdout.write("\nAutomatically resolved:\n")
  5511. for path in resolved:
  5512. sys.stdout.write(f" {path.decode('utf-8')}\n")
  5513. elif parsed_args.subcommand == "status":
  5514. status_list = porcelain.rerere_status(parsed_args.gitdir)
  5515. if not status_list:
  5516. sys.stdout.write("No recorded resolutions.\n")
  5517. else:
  5518. for conflict_id, has_resolution in status_list:
  5519. status = "resolved" if has_resolution else "unresolved"
  5520. sys.stdout.write(f"{conflict_id}\t{status}\n")
  5521. elif parsed_args.subcommand == "diff":
  5522. diff_list = porcelain.rerere_diff(parsed_args.gitdir)
  5523. if not diff_list:
  5524. sys.stdout.write("No recorded conflicts.\n")
  5525. else:
  5526. for conflict_id, preimage, postimage in diff_list:
  5527. sys.stdout.write(f"--- {conflict_id} (preimage)\n")
  5528. sys.stdout.buffer.write(preimage)
  5529. sys.stdout.write("\n")
  5530. if postimage:
  5531. sys.stdout.write(f"+++ {conflict_id} (postimage)\n")
  5532. sys.stdout.buffer.write(postimage)
  5533. sys.stdout.write("\n")
  5534. elif parsed_args.subcommand == "forget":
  5535. porcelain.rerere_forget(parsed_args.gitdir, parsed_args.pathspec)
  5536. if parsed_args.pathspec:
  5537. sys.stdout.write(f"Forgot resolution for {parsed_args.pathspec}\n")
  5538. else:
  5539. sys.stdout.write("Forgot all resolutions\n")
  5540. elif parsed_args.subcommand == "clear":
  5541. porcelain.rerere_clear(parsed_args.gitdir)
  5542. sys.stdout.write("Cleared all rerere resolutions\n")
  5543. elif parsed_args.subcommand == "gc":
  5544. porcelain.rerere_gc(parsed_args.gitdir, parsed_args.max_age_days)
  5545. sys.stdout.write(
  5546. f"Cleaned up resolutions older than {parsed_args.max_age_days} days\n"
  5547. )
  5548. commands = {
  5549. "add": cmd_add,
  5550. "annotate": cmd_annotate,
  5551. "archive": cmd_archive,
  5552. "bisect": cmd_bisect,
  5553. "blame": cmd_blame,
  5554. "branch": cmd_branch,
  5555. "bundle": cmd_bundle,
  5556. "check-ignore": cmd_check_ignore,
  5557. "check-mailmap": cmd_check_mailmap,
  5558. "checkout": cmd_checkout,
  5559. "cherry": cmd_cherry,
  5560. "cherry-pick": cmd_cherry_pick,
  5561. "clone": cmd_clone,
  5562. "column": cmd_column,
  5563. "commit": cmd_commit,
  5564. "commit-tree": cmd_commit_tree,
  5565. "config": cmd_config,
  5566. "count-objects": cmd_count_objects,
  5567. "describe": cmd_describe,
  5568. "diagnose": cmd_diagnose,
  5569. "daemon": cmd_daemon,
  5570. "diff": cmd_diff,
  5571. "diff-tree": cmd_diff_tree,
  5572. "dump-pack": cmd_dump_pack,
  5573. "dump-index": cmd_dump_index,
  5574. "fetch-pack": cmd_fetch_pack,
  5575. "fetch": cmd_fetch,
  5576. "filter-branch": cmd_filter_branch,
  5577. "for-each-ref": cmd_for_each_ref,
  5578. "format-patch": cmd_format_patch,
  5579. "fsck": cmd_fsck,
  5580. "gc": cmd_gc,
  5581. "grep": cmd_grep,
  5582. "help": cmd_help,
  5583. "init": cmd_init,
  5584. "interpret-trailers": cmd_interpret_trailers,
  5585. "lfs": cmd_lfs,
  5586. "log": cmd_log,
  5587. "ls-files": cmd_ls_files,
  5588. "ls-remote": cmd_ls_remote,
  5589. "ls-tree": cmd_ls_tree,
  5590. "maintenance": cmd_maintenance,
  5591. "mailinfo": cmd_mailinfo,
  5592. "mailsplit": cmd_mailsplit,
  5593. "merge": cmd_merge,
  5594. "merge-base": cmd_merge_base,
  5595. "merge-tree": cmd_merge_tree,
  5596. "notes": cmd_notes,
  5597. "pack-objects": cmd_pack_objects,
  5598. "pack-refs": cmd_pack_refs,
  5599. "prune": cmd_prune,
  5600. "pull": cmd_pull,
  5601. "push": cmd_push,
  5602. "rebase": cmd_rebase,
  5603. "receive-pack": cmd_receive_pack,
  5604. "reflog": cmd_reflog,
  5605. "rerere": cmd_rerere,
  5606. "remote": cmd_remote,
  5607. "repack": cmd_repack,
  5608. "replace": cmd_replace,
  5609. "reset": cmd_reset,
  5610. "restore": cmd_restore,
  5611. "revert": cmd_revert,
  5612. "rev-list": cmd_rev_list,
  5613. "rm": cmd_rm,
  5614. "mv": cmd_mv,
  5615. "show": cmd_show,
  5616. "show-branch": cmd_show_branch,
  5617. "show-ref": cmd_show_ref,
  5618. "stash": cmd_stash,
  5619. "status": cmd_status,
  5620. "stripspace": cmd_stripspace,
  5621. "shortlog": cmd_shortlog,
  5622. "switch": cmd_switch,
  5623. "symbolic-ref": cmd_symbolic_ref,
  5624. "submodule": cmd_submodule,
  5625. "tag": cmd_tag,
  5626. "unpack-objects": cmd_unpack_objects,
  5627. "update-server-info": cmd_update_server_info,
  5628. "upload-pack": cmd_upload_pack,
  5629. "var": cmd_var,
  5630. "verify-commit": cmd_verify_commit,
  5631. "verify-tag": cmd_verify_tag,
  5632. "web-daemon": cmd_web_daemon,
  5633. "worktree": cmd_worktree,
  5634. "write-tree": cmd_write_tree,
  5635. }
  5636. def main(argv: Sequence[str] | None = None) -> int | None:
  5637. """Main entry point for the Dulwich CLI.
  5638. Args:
  5639. argv: Command line arguments (defaults to sys.argv[1:])
  5640. Returns:
  5641. Exit code or None
  5642. """
  5643. # Wrap stdout and stderr to respect GIT_FLUSH environment variable
  5644. sys.stdout = AutoFlushTextIOWrapper.env(sys.stdout)
  5645. sys.stderr = AutoFlushTextIOWrapper.env(sys.stderr)
  5646. if argv is None:
  5647. argv = sys.argv[1:]
  5648. # Parse only the global options and command, stop at first positional
  5649. parser = argparse.ArgumentParser(
  5650. prog="dulwich",
  5651. description="Simple command-line interface to Dulwich",
  5652. add_help=False, # We'll handle help ourselves
  5653. )
  5654. parser.add_argument("--no-pager", action="store_true", help="Disable pager")
  5655. parser.add_argument("--pager", action="store_true", help="Force enable pager")
  5656. parser.add_argument("--help", "-h", action="store_true", help="Show help")
  5657. # Parse known args to separate global options from command args
  5658. global_args, remaining = parser.parse_known_args(argv)
  5659. # Apply global pager settings
  5660. if global_args.no_pager:
  5661. disable_pager()
  5662. elif global_args.pager:
  5663. enable_pager()
  5664. # Handle help
  5665. if global_args.help or not remaining:
  5666. parser = argparse.ArgumentParser(
  5667. prog="dulwich", description="Simple command-line interface to Dulwich"
  5668. )
  5669. parser.add_argument("--no-pager", action="store_true", help="Disable pager")
  5670. parser.add_argument("--pager", action="store_true", help="Force enable pager")
  5671. parser.add_argument(
  5672. "command",
  5673. nargs="?",
  5674. help=f"Command to run. Available: {', '.join(sorted(commands.keys()))}",
  5675. )
  5676. parser.print_help()
  5677. return 1
  5678. # Try to configure from GIT_TRACE, fall back to default if it fails
  5679. if not _configure_logging_from_trace():
  5680. logging.basicConfig(
  5681. level=logging.INFO,
  5682. format="%(message)s",
  5683. )
  5684. # First remaining arg is the command
  5685. cmd = remaining[0]
  5686. cmd_args = remaining[1:]
  5687. try:
  5688. cmd_kls = commands[cmd]
  5689. except KeyError:
  5690. logging.fatal("No such subcommand: %s", cmd)
  5691. return 1
  5692. # TODO(jelmer): Return non-0 on errors
  5693. return cmd_kls().run(cmd_args)
  5694. def _main() -> None:
  5695. if "DULWICH_PDB" in os.environ and getattr(signal, "SIGQUIT", None):
  5696. signal.signal(signal.SIGQUIT, signal_quit) # type: ignore[attr-defined,unused-ignore]
  5697. signal.signal(signal.SIGINT, signal_int)
  5698. sys.exit(main())
  5699. if __name__ == "__main__":
  5700. _main()