cli.py 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245
  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. import argparse
  29. import logging
  30. import os
  31. import shutil
  32. import signal
  33. import subprocess
  34. import sys
  35. import tempfile
  36. from collections.abc import Iterator
  37. from pathlib import Path
  38. from typing import BinaryIO, Callable, ClassVar, Optional, TextIO, Union
  39. from dulwich import porcelain
  40. from .bundle import create_bundle_from_repo, read_bundle, write_bundle
  41. from .client import GitProtocolError, get_transport_and_path
  42. from .errors import ApplyDeltaError
  43. from .index import Index
  44. from .objects import Commit, valid_hexsha
  45. from .objectspec import parse_commit_range
  46. from .pack import Pack, sha_to_hex
  47. from .patch import DiffAlgorithmNotAvailable
  48. from .repo import Repo
  49. logger = logging.getLogger(__name__)
  50. def to_display_str(value: Union[bytes, str]) -> str:
  51. """Convert a bytes or string value to a display string.
  52. Args:
  53. value: The value to convert (bytes or str)
  54. Returns:
  55. A string suitable for display
  56. """
  57. if isinstance(value, bytes):
  58. return value.decode("utf-8", "replace")
  59. return value
  60. class CommitMessageError(Exception):
  61. """Raised when there's an issue with the commit message."""
  62. def signal_int(signal: int, frame) -> None:
  63. """Handle interrupt signal by exiting.
  64. Args:
  65. signal: Signal number
  66. frame: Current stack frame
  67. """
  68. sys.exit(1)
  69. def signal_quit(signal: int, frame) -> None:
  70. """Handle quit signal by entering debugger.
  71. Args:
  72. signal: Signal number
  73. frame: Current stack frame
  74. """
  75. import pdb
  76. pdb.set_trace()
  77. def parse_relative_time(time_str: str) -> int:
  78. """Parse a relative time string like '2 weeks ago' into seconds.
  79. Args:
  80. time_str: String like '2 weeks ago' or 'now'
  81. Returns:
  82. Number of seconds
  83. Raises:
  84. ValueError: If the time string cannot be parsed
  85. """
  86. if time_str == "now":
  87. return 0
  88. if not time_str.endswith(" ago"):
  89. raise ValueError(f"Invalid relative time format: {time_str}")
  90. parts = time_str[:-4].split()
  91. if len(parts) != 2:
  92. raise ValueError(f"Invalid relative time format: {time_str}")
  93. try:
  94. num = int(parts[0])
  95. unit = parts[1]
  96. multipliers = {
  97. "second": 1,
  98. "seconds": 1,
  99. "minute": 60,
  100. "minutes": 60,
  101. "hour": 3600,
  102. "hours": 3600,
  103. "day": 86400,
  104. "days": 86400,
  105. "week": 604800,
  106. "weeks": 604800,
  107. }
  108. if unit in multipliers:
  109. return num * multipliers[unit]
  110. else:
  111. raise ValueError(f"Unknown time unit: {unit}")
  112. except ValueError as e:
  113. if "invalid literal" in str(e):
  114. raise ValueError(f"Invalid number in relative time: {parts[0]}")
  115. raise
  116. def format_bytes(bytes: float) -> str:
  117. """Format bytes as human-readable string.
  118. Args:
  119. bytes: Number of bytes
  120. Returns:
  121. Human-readable string like "1.5 MB"
  122. """
  123. for unit in ["B", "KB", "MB", "GB"]:
  124. if bytes < 1024.0:
  125. return f"{bytes:.1f} {unit}"
  126. bytes /= 1024.0
  127. return f"{bytes:.1f} TB"
  128. def launch_editor(template_content: bytes = b"") -> bytes:
  129. """Launch an editor for the user to enter text.
  130. Args:
  131. template_content: Initial content for the editor
  132. Returns:
  133. The edited content as bytes
  134. """
  135. # Determine which editor to use
  136. editor = os.environ.get("GIT_EDITOR") or os.environ.get("EDITOR") or "vi"
  137. # Create a temporary file
  138. with tempfile.NamedTemporaryFile(mode="wb", delete=False, suffix=".txt") as f:
  139. temp_file = f.name
  140. f.write(template_content)
  141. try:
  142. # Launch the editor
  143. subprocess.run([editor, temp_file], check=True)
  144. # Read the edited content
  145. with open(temp_file, "rb") as f:
  146. content = f.read()
  147. return content
  148. finally:
  149. # Clean up the temporary file
  150. os.unlink(temp_file)
  151. def detect_terminal_width() -> int:
  152. """Detect the width of the terminal.
  153. Returns:
  154. Width of the terminal in characters, or 80 if it cannot be determined
  155. """
  156. try:
  157. return os.get_terminal_size().columns
  158. except OSError:
  159. return 80
  160. def write_columns(
  161. items: Union[Iterator[bytes], list[bytes]], out: TextIO, width: Optional[int] = None
  162. ) -> None:
  163. """Display items in formatted columns based on terminal width.
  164. Args:
  165. items: List or iterator of bytes objects to display in columns
  166. out: Output stream to write to
  167. width: Optional width of the terminal (if None, auto-detect)
  168. The function calculates the optimal number of columns to fit the terminal
  169. width and displays the items in a formatted column layout with proper
  170. padding and alignment.
  171. """
  172. if width is None:
  173. ter_width = detect_terminal_width()
  174. else:
  175. ter_width = width
  176. item_names = [item.decode() for item in items]
  177. def columns(names, width, num_cols):
  178. if num_cols <= 0:
  179. return False, []
  180. num_rows = (len(names) + num_cols - 1) // num_cols
  181. col_widths = []
  182. for col in range(num_cols):
  183. max_width = 0
  184. for row in range(num_rows):
  185. idx = row + col * num_rows
  186. if idx < len(names):
  187. max_width = max(max_width, len(names[idx]))
  188. col_widths.append(max_width + 2) # add padding
  189. total_width = sum(col_widths)
  190. if total_width <= width:
  191. return True, col_widths
  192. return False, []
  193. best_cols = 1
  194. best_widths = []
  195. for num_cols in range(min(8, len(item_names)), 0, -1):
  196. fits, widths = columns(item_names, ter_width, num_cols)
  197. if fits:
  198. best_cols = num_cols
  199. best_widths = widths
  200. break
  201. if not best_widths:
  202. best_cols = 1
  203. best_widths = [max(len(name) for name in item_names) + 2]
  204. num_rows = (len(item_names) + best_cols - 1) // best_cols
  205. for row in range(num_rows):
  206. lines = []
  207. for col in range(best_cols):
  208. idx = row + col * num_rows
  209. if idx < len(item_names):
  210. branch_name = item_names[idx]
  211. if col < len(best_widths):
  212. lines.append(branch_name.ljust(best_widths[col]))
  213. else:
  214. lines.append(branch_name)
  215. if lines:
  216. out.write("".join(lines).rstrip() + "\n")
  217. class PagerBuffer:
  218. """Binary buffer wrapper for Pager to mimic sys.stdout.buffer."""
  219. def __init__(self, pager: "Pager") -> None:
  220. """Initialize PagerBuffer.
  221. Args:
  222. pager: Pager instance to wrap
  223. """
  224. self.pager = pager
  225. def write(self, data: bytes) -> int:
  226. """Write bytes to pager."""
  227. if isinstance(data, bytes):
  228. text = data.decode("utf-8", errors="replace")
  229. return self.pager.write(text)
  230. return self.pager.write(data)
  231. def flush(self) -> None:
  232. """Flush the pager."""
  233. return self.pager.flush()
  234. def writelines(self, lines) -> None:
  235. """Write multiple lines to pager."""
  236. for line in lines:
  237. self.write(line)
  238. def readable(self) -> bool:
  239. """Return whether the buffer is readable (it's not)."""
  240. return False
  241. def writable(self) -> bool:
  242. """Return whether the buffer is writable."""
  243. return not self.pager._closed
  244. def seekable(self) -> bool:
  245. """Return whether the buffer is seekable (it's not)."""
  246. return False
  247. def close(self) -> None:
  248. """Close the pager."""
  249. return self.pager.close()
  250. @property
  251. def closed(self) -> bool:
  252. """Return whether the buffer is closed."""
  253. return self.pager.closed
  254. class Pager:
  255. """File-like object that pages output through external pager programs."""
  256. def __init__(self, pager_cmd: str = "cat") -> None:
  257. """Initialize Pager.
  258. Args:
  259. pager_cmd: Command to use for paging (default: "cat")
  260. """
  261. self.pager_process: Optional[subprocess.Popen] = None
  262. self.buffer = PagerBuffer(self)
  263. self._closed = False
  264. self.pager_cmd = pager_cmd
  265. self._pager_died = False
  266. def _get_pager_command(self) -> str:
  267. """Get the pager command to use."""
  268. return self.pager_cmd
  269. def _ensure_pager_started(self) -> None:
  270. """Start the pager process if not already started."""
  271. if self.pager_process is None and not self._closed:
  272. try:
  273. pager_cmd = self._get_pager_command()
  274. self.pager_process = subprocess.Popen(
  275. pager_cmd,
  276. shell=True,
  277. stdin=subprocess.PIPE,
  278. stdout=sys.stdout,
  279. stderr=sys.stderr,
  280. text=True,
  281. )
  282. except (OSError, subprocess.SubprocessError):
  283. # Pager failed to start, fall back to direct output
  284. self.pager_process = None
  285. def write(self, text: str) -> int:
  286. """Write text to the pager."""
  287. if self._closed:
  288. raise ValueError("I/O operation on closed file")
  289. # If pager died (user quit), stop writing output
  290. if self._pager_died:
  291. return len(text)
  292. self._ensure_pager_started()
  293. if self.pager_process and self.pager_process.stdin:
  294. try:
  295. return self.pager_process.stdin.write(text)
  296. except (OSError, subprocess.SubprocessError, BrokenPipeError):
  297. # Pager died (user quit), stop writing output
  298. self._pager_died = True
  299. return len(text)
  300. else:
  301. # No pager available, write directly to stdout
  302. return sys.stdout.write(text)
  303. def flush(self) -> None:
  304. """Flush the pager."""
  305. if self._closed or self._pager_died:
  306. return
  307. if self.pager_process and self.pager_process.stdin:
  308. try:
  309. self.pager_process.stdin.flush()
  310. except (OSError, subprocess.SubprocessError, BrokenPipeError):
  311. self._pager_died = True
  312. else:
  313. sys.stdout.flush()
  314. def close(self) -> None:
  315. """Close the pager."""
  316. if self._closed:
  317. return
  318. self._closed = True
  319. if self.pager_process:
  320. try:
  321. if self.pager_process.stdin:
  322. self.pager_process.stdin.close()
  323. self.pager_process.wait()
  324. except (OSError, subprocess.SubprocessError):
  325. pass
  326. self.pager_process = None
  327. def __enter__(self) -> "Pager":
  328. """Context manager entry."""
  329. return self
  330. def __exit__(self, exc_type, exc_val, exc_tb) -> None:
  331. """Context manager exit."""
  332. self.close()
  333. # Additional file-like methods for compatibility
  334. def writelines(self, lines) -> None:
  335. """Write a list of lines to the pager."""
  336. if self._pager_died:
  337. return
  338. for line in lines:
  339. self.write(line)
  340. @property
  341. def closed(self) -> bool:
  342. """Return whether the pager is closed."""
  343. return self._closed
  344. def readable(self) -> bool:
  345. """Return whether the pager is readable (it's not)."""
  346. return False
  347. def writable(self) -> bool:
  348. """Return whether the pager is writable."""
  349. return not self._closed
  350. def seekable(self) -> bool:
  351. """Return whether the pager is seekable (it's not)."""
  352. return False
  353. class _StreamContextAdapter:
  354. """Adapter to make streams work with context manager protocol."""
  355. def __init__(self, stream) -> None:
  356. self.stream = stream
  357. # Expose buffer if it exists
  358. if hasattr(stream, "buffer"):
  359. self.buffer = stream.buffer
  360. else:
  361. self.buffer = stream
  362. def __enter__(self):
  363. return self.stream
  364. def __exit__(self, exc_type, exc_val, exc_tb) -> None:
  365. # For stdout/stderr, we don't close them
  366. pass
  367. def __getattr__(self, name: str):
  368. return getattr(self.stream, name)
  369. def get_pager(config=None, cmd_name: Optional[str] = None):
  370. """Get a pager instance if paging should be used, otherwise return sys.stdout.
  371. Args:
  372. config: Optional config instance (e.g., StackedConfig) to read settings from
  373. cmd_name: Optional command name for per-command pager settings
  374. Returns:
  375. Either a wrapped sys.stdout or a Pager instance (both context managers)
  376. """
  377. # Check global pager disable flag
  378. if getattr(get_pager, "_disabled", False):
  379. return _StreamContextAdapter(sys.stdout)
  380. # Don't page if stdout is not a terminal
  381. if not sys.stdout.isatty():
  382. return _StreamContextAdapter(sys.stdout)
  383. # Priority order for pager command (following git's behavior):
  384. # 1. Check pager.<cmd> config (if cmd_name provided)
  385. # 2. Check environment variables: DULWICH_PAGER, GIT_PAGER, PAGER
  386. # 3. Check core.pager config
  387. # 4. Fallback to common pagers
  388. pager_cmd = None
  389. # 1. Check per-command pager config (pager.<cmd>)
  390. if config and cmd_name:
  391. try:
  392. pager_value = config.get(
  393. ("pager",), cmd_name.encode() if isinstance(cmd_name, str) else cmd_name
  394. )
  395. except KeyError:
  396. pass
  397. else:
  398. if pager_value == b"false":
  399. return _StreamContextAdapter(sys.stdout)
  400. elif pager_value != b"true":
  401. # It's a custom pager command
  402. pager_cmd = (
  403. pager_value.decode()
  404. if isinstance(pager_value, bytes)
  405. else pager_value
  406. )
  407. # 2. Check environment variables
  408. if not pager_cmd:
  409. for env_var in ["DULWICH_PAGER", "GIT_PAGER", "PAGER"]:
  410. pager = os.environ.get(env_var)
  411. if pager:
  412. if pager == "false":
  413. return _StreamContextAdapter(sys.stdout)
  414. pager_cmd = pager
  415. break
  416. # 3. Check core.pager config
  417. if not pager_cmd and config:
  418. try:
  419. core_pager = config.get(("core",), b"pager")
  420. except KeyError:
  421. pass
  422. else:
  423. if core_pager == b"false" or core_pager == b"":
  424. return _StreamContextAdapter(sys.stdout)
  425. pager_cmd = (
  426. core_pager.decode() if isinstance(core_pager, bytes) else core_pager
  427. )
  428. # 4. Fallback to common pagers
  429. if not pager_cmd:
  430. for pager in ["less", "more", "cat"]:
  431. if shutil.which(pager):
  432. if pager == "less":
  433. pager_cmd = "less -FRX" # -F: quit if one screen, -R: raw control chars, -X: no init/deinit
  434. else:
  435. pager_cmd = pager
  436. break
  437. else:
  438. pager_cmd = "cat" # Ultimate fallback
  439. return Pager(pager_cmd)
  440. def disable_pager() -> None:
  441. """Disable pager for this session."""
  442. get_pager._disabled = True # type: ignore[attr-defined]
  443. def enable_pager() -> None:
  444. """Enable pager for this session."""
  445. get_pager._disabled = False # type: ignore[attr-defined]
  446. class Command:
  447. """A Dulwich subcommand."""
  448. def run(self, args) -> Optional[int]:
  449. """Run the command."""
  450. raise NotImplementedError(self.run)
  451. class cmd_archive(Command):
  452. """Create an archive of files from a named tree."""
  453. def run(self, args) -> None:
  454. """Execute the archive command.
  455. Args:
  456. args: Command line arguments
  457. """
  458. parser = argparse.ArgumentParser()
  459. parser.add_argument(
  460. "--remote",
  461. type=str,
  462. help="Retrieve archive from specified remote repo",
  463. )
  464. parser.add_argument("committish", type=str, nargs="?")
  465. args = parser.parse_args(args)
  466. if args.remote:
  467. client, path = get_transport_and_path(args.remote)
  468. client.archive(
  469. path,
  470. args.committish,
  471. sys.stdout.write,
  472. write_error=sys.stderr.write,
  473. )
  474. else:
  475. # Use binary buffer for archive output
  476. outstream: BinaryIO = sys.stdout.buffer
  477. errstream: BinaryIO = sys.stderr.buffer
  478. porcelain.archive(
  479. ".",
  480. args.committish,
  481. outstream=outstream,
  482. errstream=errstream,
  483. )
  484. class cmd_add(Command):
  485. """Add file contents to the index."""
  486. def run(self, argv) -> None:
  487. """Execute the add command.
  488. Args:
  489. argv: Command line arguments
  490. """
  491. parser = argparse.ArgumentParser()
  492. parser.add_argument("path", nargs="+")
  493. args = parser.parse_args(argv)
  494. # Convert '.' to None to add all files
  495. paths = args.path
  496. if len(paths) == 1 and paths[0] == ".":
  497. paths = None
  498. porcelain.add(".", paths=paths)
  499. class cmd_annotate(Command):
  500. """Annotate each line in a file with commit information."""
  501. def run(self, argv) -> None:
  502. """Execute the annotate command.
  503. Args:
  504. argv: Command line arguments
  505. """
  506. parser = argparse.ArgumentParser()
  507. parser.add_argument("path", help="Path to file to annotate")
  508. parser.add_argument("committish", nargs="?", help="Commit to start from")
  509. args = parser.parse_args(argv)
  510. with Repo(".") as repo:
  511. config = repo.get_config_stack()
  512. with get_pager(config=config, cmd_name="annotate") as outstream:
  513. results = porcelain.annotate(repo, args.path, args.committish)
  514. for (commit, entry), line in results:
  515. # Show shortened commit hash and line content
  516. commit_hash = commit.id[:8]
  517. outstream.write(f"{commit_hash.decode()} {line.decode()}\n")
  518. class cmd_blame(Command):
  519. """Show what revision and author last modified each line of a file."""
  520. def run(self, argv) -> None:
  521. """Execute the blame command.
  522. Args:
  523. argv: Command line arguments
  524. """
  525. # blame is an alias for annotate
  526. cmd_annotate().run(argv)
  527. class cmd_rm(Command):
  528. """Remove files from the working tree and from the index."""
  529. def run(self, argv) -> None:
  530. """Execute the rm command.
  531. Args:
  532. argv: Command line arguments
  533. """
  534. parser = argparse.ArgumentParser()
  535. parser.add_argument(
  536. "--cached", action="store_true", help="Remove from index only"
  537. )
  538. parser.add_argument("path", type=Path, nargs="+")
  539. args = parser.parse_args(argv)
  540. porcelain.remove(".", paths=args.path, cached=args.cached)
  541. class cmd_mv(Command):
  542. """Move or rename a file, a directory, or a symlink."""
  543. def run(self, argv) -> None:
  544. """Execute the mv command.
  545. Args:
  546. argv: Command line arguments
  547. """
  548. parser = argparse.ArgumentParser()
  549. parser.add_argument(
  550. "-f",
  551. "--force",
  552. action="store_true",
  553. help="Force move even if destination exists",
  554. )
  555. parser.add_argument("source", type=Path)
  556. parser.add_argument("destination", type=Path)
  557. args = parser.parse_args(argv)
  558. porcelain.mv(".", args.source, args.destination, force=args.force)
  559. class cmd_fetch_pack(Command):
  560. """Receive missing objects from another repository."""
  561. def run(self, argv) -> None:
  562. """Execute the fetch-pack command.
  563. Args:
  564. argv: Command line arguments
  565. """
  566. parser = argparse.ArgumentParser()
  567. parser.add_argument("--all", action="store_true")
  568. parser.add_argument("location", nargs="?", type=str)
  569. parser.add_argument("refs", nargs="*", type=str)
  570. args = parser.parse_args(argv)
  571. client, path = get_transport_and_path(args.location)
  572. r = Repo(".")
  573. if args.all:
  574. determine_wants = r.object_store.determine_wants_all
  575. else:
  576. def determine_wants(refs, depth: Optional[int] = None):
  577. return [y.encode("utf-8") for y in args.refs if y not in r.object_store]
  578. client.fetch(path, r, determine_wants)
  579. class cmd_fetch(Command):
  580. """Download objects and refs from another repository."""
  581. def run(self, args) -> None:
  582. """Execute the fetch command.
  583. Args:
  584. args: Command line arguments
  585. """
  586. parser = argparse.ArgumentParser()
  587. parser.add_argument("location", help="Remote location to fetch from")
  588. args = parser.parse_args(args)
  589. client, path = get_transport_and_path(args.location)
  590. r = Repo(".")
  591. def progress(msg: bytes) -> None:
  592. sys.stdout.buffer.write(msg)
  593. result = client.fetch(path, r, progress=progress)
  594. logger.info("Remote refs:")
  595. for ref, sha in result.refs.items():
  596. if sha is not None:
  597. logger.info("%s → %s", ref.decode(), sha.decode())
  598. class cmd_for_each_ref(Command):
  599. """Output information on each ref."""
  600. def run(self, args) -> None:
  601. """Execute the for-each-ref command.
  602. Args:
  603. args: Command line arguments
  604. """
  605. parser = argparse.ArgumentParser()
  606. parser.add_argument("pattern", type=str, nargs="?")
  607. args = parser.parse_args(args)
  608. for sha, object_type, ref in porcelain.for_each_ref(".", args.pattern):
  609. logger.info("%s %s\t%s", sha.decode(), object_type.decode(), ref.decode())
  610. class cmd_fsck(Command):
  611. """Verify the connectivity and validity of objects in the database."""
  612. def run(self, args) -> None:
  613. """Execute the fsck command.
  614. Args:
  615. args: Command line arguments
  616. """
  617. parser = argparse.ArgumentParser()
  618. parser.parse_args(args)
  619. for obj, msg in porcelain.fsck("."):
  620. logger.info("%s: %s", obj.decode() if isinstance(obj, bytes) else obj, msg)
  621. class cmd_log(Command):
  622. """Show commit logs."""
  623. def run(self, args) -> None:
  624. """Execute the log command.
  625. Args:
  626. args: Command line arguments
  627. """
  628. parser = argparse.ArgumentParser()
  629. parser.add_argument(
  630. "--reverse",
  631. action="store_true",
  632. help="Reverse order in which entries are printed",
  633. )
  634. parser.add_argument(
  635. "--name-status",
  636. action="store_true",
  637. help="Print name/status for each changed file",
  638. )
  639. parser.add_argument("paths", nargs="*", help="Paths to show log for")
  640. args = parser.parse_args(args)
  641. with Repo(".") as repo:
  642. config = repo.get_config_stack()
  643. with get_pager(config=config, cmd_name="log") as outstream:
  644. porcelain.log(
  645. repo,
  646. paths=args.paths,
  647. reverse=args.reverse,
  648. name_status=args.name_status,
  649. outstream=outstream,
  650. )
  651. class cmd_diff(Command):
  652. """Show changes between commits, commit and working tree, etc."""
  653. def run(self, args) -> None:
  654. """Execute the diff command.
  655. Args:
  656. args: Command line arguments
  657. """
  658. parser = argparse.ArgumentParser()
  659. parser.add_argument(
  660. "committish", nargs="*", default=[], help="Commits or refs to compare"
  661. )
  662. parser.add_argument("--staged", action="store_true", help="Show staged changes")
  663. parser.add_argument(
  664. "--cached",
  665. action="store_true",
  666. help="Show staged changes (same as --staged)",
  667. )
  668. parser.add_argument(
  669. "--color",
  670. choices=["always", "never", "auto"],
  671. default="auto",
  672. help="Use colored output (requires rich)",
  673. )
  674. parser.add_argument(
  675. "--patience",
  676. action="store_true",
  677. help="Use patience diff algorithm",
  678. )
  679. parser.add_argument(
  680. "--diff-algorithm",
  681. choices=["myers", "patience"],
  682. default="myers",
  683. help="Choose a diff algorithm",
  684. )
  685. parser.add_argument(
  686. "--", dest="separator", action="store_true", help=argparse.SUPPRESS
  687. )
  688. parser.add_argument("paths", nargs="*", default=[], help="Paths to limit diff")
  689. # Handle the -- separator for paths
  690. if "--" in args:
  691. sep_index = args.index("--")
  692. parsed_args = parser.parse_args(args[:sep_index])
  693. parsed_args.paths = args[sep_index + 1 :]
  694. else:
  695. parsed_args = parser.parse_args(args)
  696. args = parsed_args
  697. # Determine diff algorithm
  698. diff_algorithm = args.diff_algorithm
  699. if args.patience:
  700. diff_algorithm = "patience"
  701. # Determine if we should use color
  702. def _should_use_color():
  703. if args.color == "always":
  704. return True
  705. elif args.color == "never":
  706. return False
  707. else: # auto
  708. return sys.stdout.isatty()
  709. def _create_output_stream(outstream):
  710. """Create output stream, optionally with colorization."""
  711. if not _should_use_color():
  712. return outstream.buffer
  713. from .diff import ColorizedDiffStream
  714. if not ColorizedDiffStream.is_available():
  715. if args.color == "always":
  716. raise ImportError(
  717. "Rich is required for colored output. Install with: pip install 'dulwich[colordiff]'"
  718. )
  719. else:
  720. logging.warning(
  721. "Rich not available, disabling colored output. Install with: pip install 'dulwich[colordiff]'"
  722. )
  723. return outstream.buffer
  724. return ColorizedDiffStream(outstream.buffer)
  725. with Repo(".") as repo:
  726. config = repo.get_config_stack()
  727. with get_pager(config=config, cmd_name="diff") as outstream:
  728. output_stream = _create_output_stream(outstream)
  729. try:
  730. if len(args.committish) == 0:
  731. # Show diff for working tree or staged changes
  732. porcelain.diff(
  733. repo,
  734. staged=(args.staged or args.cached),
  735. paths=args.paths or None,
  736. outstream=output_stream,
  737. diff_algorithm=diff_algorithm,
  738. )
  739. elif len(args.committish) == 1:
  740. # Show diff between working tree and specified commit
  741. if args.staged or args.cached:
  742. parser.error(
  743. "--staged/--cached cannot be used with commits"
  744. )
  745. porcelain.diff(
  746. repo,
  747. commit=args.committish[0],
  748. staged=False,
  749. paths=args.paths or None,
  750. outstream=output_stream,
  751. diff_algorithm=diff_algorithm,
  752. )
  753. elif len(args.committish) == 2:
  754. # Show diff between two commits
  755. porcelain.diff(
  756. repo,
  757. commit=args.committish[0],
  758. commit2=args.committish[1],
  759. paths=args.paths or None,
  760. outstream=output_stream,
  761. diff_algorithm=diff_algorithm,
  762. )
  763. else:
  764. parser.error("Too many arguments - specify at most two commits")
  765. except DiffAlgorithmNotAvailable as e:
  766. sys.stderr.write(f"fatal: {e}\n")
  767. sys.exit(1)
  768. # Flush any remaining output
  769. if hasattr(output_stream, "flush"):
  770. output_stream.flush()
  771. class cmd_dump_pack(Command):
  772. """Dump the contents of a pack file for debugging."""
  773. def run(self, args) -> None:
  774. """Execute the dump-pack command.
  775. Args:
  776. args: Command line arguments
  777. """
  778. parser = argparse.ArgumentParser()
  779. parser.add_argument("filename", help="Pack file to dump")
  780. args = parser.parse_args(args)
  781. basename, _ = os.path.splitext(args.filename)
  782. x = Pack(basename)
  783. logger.info("Object names checksum: %s", x.name().decode("ascii", "replace"))
  784. logger.info("Checksum: %r", sha_to_hex(x.get_stored_checksum()))
  785. x.check()
  786. logger.info("Length: %d", len(x))
  787. for name in x:
  788. try:
  789. logger.info("\t%s", x[name])
  790. except KeyError as k:
  791. logger.error(
  792. "\t%s: Unable to resolve base %r",
  793. name.decode("ascii", "replace"),
  794. k,
  795. )
  796. except ApplyDeltaError as e:
  797. logger.error(
  798. "\t%s: Unable to apply delta: %r",
  799. name.decode("ascii", "replace"),
  800. e,
  801. )
  802. class cmd_dump_index(Command):
  803. """Show information about a pack index file."""
  804. def run(self, args) -> None:
  805. """Execute the dump-index command.
  806. Args:
  807. args: Command line arguments
  808. """
  809. parser = argparse.ArgumentParser()
  810. parser.add_argument("filename", help="Index file to dump")
  811. args = parser.parse_args(args)
  812. idx = Index(args.filename)
  813. for o in idx:
  814. logger.info("%s %s", o, idx[o])
  815. class cmd_init(Command):
  816. """Create an empty Git repository or reinitialize an existing one."""
  817. def run(self, args) -> None:
  818. """Execute the init command.
  819. Args:
  820. args: Command line arguments
  821. """
  822. parser = argparse.ArgumentParser()
  823. parser.add_argument(
  824. "--bare", action="store_true", help="Create a bare repository"
  825. )
  826. parser.add_argument(
  827. "path", nargs="?", default=os.getcwd(), help="Repository path"
  828. )
  829. args = parser.parse_args(args)
  830. porcelain.init(args.path, bare=args.bare)
  831. class cmd_clone(Command):
  832. """Clone a repository into a new directory."""
  833. def run(self, args) -> None:
  834. """Execute the clone command.
  835. Args:
  836. args: Command line arguments
  837. """
  838. parser = argparse.ArgumentParser()
  839. parser.add_argument(
  840. "--bare",
  841. help="Whether to create a bare repository.",
  842. action="store_true",
  843. )
  844. parser.add_argument("--depth", type=int, help="Depth at which to fetch")
  845. parser.add_argument(
  846. "-b",
  847. "--branch",
  848. type=str,
  849. help="Check out branch instead of branch pointed to by remote HEAD",
  850. )
  851. parser.add_argument(
  852. "--refspec",
  853. type=str,
  854. help="References to fetch",
  855. action="append",
  856. )
  857. parser.add_argument(
  858. "--filter",
  859. dest="filter_spec",
  860. type=str,
  861. help="git-rev-list-style object filter",
  862. )
  863. parser.add_argument(
  864. "--protocol",
  865. type=int,
  866. help="Git protocol version to use",
  867. )
  868. parser.add_argument(
  869. "--recurse-submodules",
  870. action="store_true",
  871. help="Initialize and clone submodules",
  872. )
  873. parser.add_argument("source", help="Repository to clone from")
  874. parser.add_argument("target", nargs="?", help="Directory to clone into")
  875. args = parser.parse_args(args)
  876. try:
  877. porcelain.clone(
  878. args.source,
  879. args.target,
  880. bare=args.bare,
  881. depth=args.depth,
  882. branch=args.branch,
  883. refspec=args.refspec,
  884. filter_spec=args.filter_spec,
  885. protocol_version=args.protocol,
  886. recurse_submodules=args.recurse_submodules,
  887. )
  888. except GitProtocolError as e:
  889. logging.exception(e)
  890. def _get_commit_message_with_template(initial_message, repo=None, commit=None):
  891. """Get commit message with an initial message template."""
  892. # Start with the initial message
  893. template = initial_message
  894. if template and not template.endswith(b"\n"):
  895. template += b"\n"
  896. template += b"\n"
  897. template += b"# Please enter the commit message for your changes. Lines starting\n"
  898. template += b"# with '#' will be ignored, and an empty message aborts the commit.\n"
  899. template += b"#\n"
  900. # Add branch info if repo is provided
  901. if repo:
  902. try:
  903. ref_names, _ref_sha = repo.refs.follow(b"HEAD")
  904. ref_path = ref_names[-1] # Get the final reference
  905. if ref_path.startswith(b"refs/heads/"):
  906. branch = ref_path[11:] # Remove 'refs/heads/' prefix
  907. else:
  908. branch = ref_path
  909. template += b"# On branch %s\n" % branch
  910. except (KeyError, IndexError):
  911. template += b"# On branch (unknown)\n"
  912. template += b"#\n"
  913. template += b"# Changes to be committed:\n"
  914. # Launch editor
  915. content = launch_editor(template)
  916. # Remove comment lines and strip
  917. lines = content.split(b"\n")
  918. message_lines = [line for line in lines if not line.strip().startswith(b"#")]
  919. message = b"\n".join(message_lines).strip()
  920. if not message:
  921. raise CommitMessageError("Aborting commit due to empty commit message")
  922. return message
  923. class cmd_commit(Command):
  924. """Record changes to the repository."""
  925. def run(self, args) -> Optional[int]:
  926. """Execute the commit command.
  927. Args:
  928. args: Command line arguments
  929. """
  930. parser = argparse.ArgumentParser()
  931. parser.add_argument("--message", "-m", help="Commit message")
  932. parser.add_argument(
  933. "-a",
  934. "--all",
  935. action="store_true",
  936. help="Automatically stage all tracked files that have been modified",
  937. )
  938. parser.add_argument(
  939. "--amend",
  940. action="store_true",
  941. help="Replace the tip of the current branch by creating a new commit",
  942. )
  943. args = parser.parse_args(args)
  944. message: Union[bytes, str, Callable]
  945. if args.message:
  946. message = args.message
  947. elif args.amend:
  948. # For amend, create a callable that opens editor with original message pre-populated
  949. def get_amend_message(repo, commit):
  950. # Get the original commit message from current HEAD
  951. try:
  952. head_commit = repo[repo.head()]
  953. original_message = head_commit.message
  954. except KeyError:
  955. original_message = b""
  956. # Open editor with original message
  957. return _get_commit_message_with_template(original_message, repo, commit)
  958. message = get_amend_message
  959. else:
  960. # For regular commits, use empty template
  961. def get_regular_message(repo, commit):
  962. return _get_commit_message_with_template(b"", repo, commit)
  963. message = get_regular_message
  964. try:
  965. porcelain.commit(".", message=message, all=args.all, amend=args.amend)
  966. except CommitMessageError as e:
  967. logging.exception(e)
  968. return 1
  969. return None
  970. class cmd_commit_tree(Command):
  971. """Create a new commit object from a tree."""
  972. def run(self, args) -> None:
  973. """Execute the commit-tree command.
  974. Args:
  975. args: Command line arguments
  976. """
  977. parser = argparse.ArgumentParser()
  978. parser.add_argument("--message", "-m", required=True, help="Commit message")
  979. parser.add_argument("tree", help="Tree SHA to commit")
  980. args = parser.parse_args(args)
  981. porcelain.commit_tree(".", tree=args.tree, message=args.message)
  982. class cmd_update_server_info(Command):
  983. """Update auxiliary info file to help dumb servers."""
  984. def run(self, args) -> None:
  985. """Execute the update-server-info command.
  986. Args:
  987. args: Command line arguments
  988. """
  989. porcelain.update_server_info(".")
  990. class cmd_symbolic_ref(Command):
  991. """Read, modify and delete symbolic refs."""
  992. def run(self, args) -> Optional[int]:
  993. """Execute the symbolic-ref command.
  994. Args:
  995. args: Command line arguments
  996. """
  997. parser = argparse.ArgumentParser()
  998. parser.add_argument("name", help="Symbolic reference name")
  999. parser.add_argument("ref", nargs="?", help="Target reference")
  1000. parser.add_argument("--force", action="store_true", help="Force update")
  1001. args = parser.parse_args(args)
  1002. # If ref is provided, we're setting; otherwise we're reading
  1003. if args.ref:
  1004. # Set symbolic reference
  1005. from .repo import Repo
  1006. with Repo(".") as repo:
  1007. repo.refs.set_symbolic_ref(args.name.encode(), args.ref.encode())
  1008. return 0
  1009. else:
  1010. # Read symbolic reference
  1011. from .repo import Repo
  1012. with Repo(".") as repo:
  1013. try:
  1014. target = repo.refs.read_ref(args.name.encode())
  1015. if target.startswith(b"ref: "):
  1016. logger.info(target[5:].decode())
  1017. else:
  1018. logger.info(target.decode())
  1019. return 0
  1020. except KeyError:
  1021. logging.error("fatal: ref '%s' is not a symbolic ref", args.name)
  1022. return 1
  1023. class cmd_pack_refs(Command):
  1024. """Pack heads and tags for efficient repository access."""
  1025. def run(self, argv) -> None:
  1026. """Execute the pack-refs command.
  1027. Args:
  1028. argv: Command line arguments
  1029. """
  1030. parser = argparse.ArgumentParser()
  1031. parser.add_argument("--all", action="store_true")
  1032. # ignored, we never prune
  1033. parser.add_argument("--no-prune", action="store_true")
  1034. args = parser.parse_args(argv)
  1035. porcelain.pack_refs(".", all=args.all)
  1036. class cmd_show(Command):
  1037. """Show various types of objects."""
  1038. def run(self, argv) -> None:
  1039. """Execute the show command.
  1040. Args:
  1041. argv: Command line arguments
  1042. """
  1043. parser = argparse.ArgumentParser()
  1044. parser.add_argument("objectish", type=str, nargs="*")
  1045. parser.add_argument(
  1046. "--color",
  1047. choices=["always", "never", "auto"],
  1048. default="auto",
  1049. help="Use colored output (requires rich)",
  1050. )
  1051. args = parser.parse_args(argv)
  1052. # Determine if we should use color
  1053. def _should_use_color():
  1054. if args.color == "always":
  1055. return True
  1056. elif args.color == "never":
  1057. return False
  1058. else: # auto
  1059. return sys.stdout.isatty()
  1060. def _create_output_stream(outstream):
  1061. """Create output stream, optionally with colorization."""
  1062. if not _should_use_color():
  1063. return outstream
  1064. from .diff import ColorizedDiffStream
  1065. if not ColorizedDiffStream.is_available():
  1066. if args.color == "always":
  1067. raise ImportError(
  1068. "Rich is required for colored output. Install with: pip install 'dulwich[colordiff]'"
  1069. )
  1070. else:
  1071. logging.warning(
  1072. "Rich not available, disabling colored output. Install with: pip install 'dulwich[colordiff]'"
  1073. )
  1074. return outstream
  1075. return ColorizedDiffStream(outstream.buffer)
  1076. with Repo(".") as repo:
  1077. config = repo.get_config_stack()
  1078. with get_pager(config=config, cmd_name="show") as outstream:
  1079. output_stream = _create_output_stream(outstream)
  1080. porcelain.show(repo, args.objectish or None, outstream=output_stream)
  1081. class cmd_diff_tree(Command):
  1082. """Compare the content and mode of trees."""
  1083. def run(self, args) -> None:
  1084. """Execute the diff-tree command.
  1085. Args:
  1086. args: Command line arguments
  1087. """
  1088. parser = argparse.ArgumentParser()
  1089. parser.add_argument("old_tree", help="Old tree SHA")
  1090. parser.add_argument("new_tree", help="New tree SHA")
  1091. args = parser.parse_args(args)
  1092. porcelain.diff_tree(".", args.old_tree, args.new_tree)
  1093. class cmd_rev_list(Command):
  1094. """List commit objects in reverse chronological order."""
  1095. def run(self, args) -> None:
  1096. """Execute the rev-list command.
  1097. Args:
  1098. args: Command line arguments
  1099. """
  1100. parser = argparse.ArgumentParser()
  1101. parser.add_argument("commits", nargs="+", help="Commit IDs to list")
  1102. args = parser.parse_args(args)
  1103. porcelain.rev_list(".", args.commits)
  1104. class cmd_tag(Command):
  1105. """Create, list, delete or verify a tag object."""
  1106. def run(self, args) -> None:
  1107. """Execute the tag command.
  1108. Args:
  1109. args: Command line arguments
  1110. """
  1111. parser = argparse.ArgumentParser()
  1112. parser.add_argument(
  1113. "-a",
  1114. "--annotated",
  1115. help="Create an annotated tag.",
  1116. action="store_true",
  1117. )
  1118. parser.add_argument(
  1119. "-s", "--sign", help="Sign the annotated tag.", action="store_true"
  1120. )
  1121. parser.add_argument("tag_name", help="Name of the tag to create")
  1122. args = parser.parse_args(args)
  1123. porcelain.tag_create(
  1124. ".", args.tag_name, annotated=args.annotated, sign=args.sign
  1125. )
  1126. class cmd_repack(Command):
  1127. """Pack unpacked objects in a repository."""
  1128. def run(self, args) -> None:
  1129. """Execute the repack command.
  1130. Args:
  1131. args: Command line arguments
  1132. """
  1133. parser = argparse.ArgumentParser()
  1134. parser.parse_args(args)
  1135. porcelain.repack(".")
  1136. class cmd_reflog(Command):
  1137. """Manage reflog information."""
  1138. def run(self, args) -> None:
  1139. """Execute the reflog command.
  1140. Args:
  1141. args: Command line arguments
  1142. """
  1143. parser = argparse.ArgumentParser()
  1144. parser.add_argument(
  1145. "ref", nargs="?", default="HEAD", help="Reference to show reflog for"
  1146. )
  1147. parser.add_argument(
  1148. "--all", action="store_true", help="Show reflogs for all refs"
  1149. )
  1150. args = parser.parse_args(args)
  1151. with Repo(".") as repo:
  1152. config = repo.get_config_stack()
  1153. with get_pager(config=config, cmd_name="reflog") as outstream:
  1154. if args.all:
  1155. # Show reflogs for all refs
  1156. for ref_bytes, entry in porcelain.reflog(repo, all=True):
  1157. ref_str = ref_bytes.decode("utf-8", "replace")
  1158. short_new = entry.new_sha[:8].decode("ascii")
  1159. outstream.write(
  1160. f"{short_new} {ref_str}: {entry.message.decode('utf-8', 'replace')}\n"
  1161. )
  1162. else:
  1163. ref = (
  1164. args.ref.encode("utf-8")
  1165. if isinstance(args.ref, str)
  1166. else args.ref
  1167. )
  1168. for i, entry in enumerate(porcelain.reflog(repo, ref)):
  1169. # Format similar to git reflog
  1170. from dulwich.reflog import Entry
  1171. assert isinstance(entry, Entry)
  1172. short_new = entry.new_sha[:8].decode("ascii")
  1173. message = (
  1174. entry.message.decode("utf-8", "replace")
  1175. if entry.message
  1176. else ""
  1177. )
  1178. outstream.write(
  1179. f"{short_new} {ref.decode('utf-8', 'replace')}@{{{i}}}: {message}\n"
  1180. )
  1181. class cmd_reset(Command):
  1182. """Reset current HEAD to the specified state."""
  1183. def run(self, args) -> None:
  1184. """Execute the reset command.
  1185. Args:
  1186. args: Command line arguments
  1187. """
  1188. parser = argparse.ArgumentParser()
  1189. mode_group = parser.add_mutually_exclusive_group()
  1190. mode_group.add_argument(
  1191. "--hard", action="store_true", help="Reset working tree and index"
  1192. )
  1193. mode_group.add_argument("--soft", action="store_true", help="Reset only HEAD")
  1194. mode_group.add_argument(
  1195. "--mixed", action="store_true", help="Reset HEAD and index"
  1196. )
  1197. parser.add_argument("treeish", nargs="?", help="Commit/tree to reset to")
  1198. args = parser.parse_args(args)
  1199. if args.hard:
  1200. mode = "hard"
  1201. elif args.soft:
  1202. mode = "soft"
  1203. elif args.mixed:
  1204. mode = "mixed"
  1205. else:
  1206. # Default to mixed behavior
  1207. mode = "mixed"
  1208. # Use the porcelain.reset function for all modes
  1209. porcelain.reset(".", mode=mode, treeish=args.treeish)
  1210. class cmd_revert(Command):
  1211. """Revert some existing commits."""
  1212. def run(self, args) -> None:
  1213. """Execute the revert command.
  1214. Args:
  1215. args: Command line arguments
  1216. """
  1217. parser = argparse.ArgumentParser()
  1218. parser.add_argument(
  1219. "--no-commit",
  1220. "-n",
  1221. action="store_true",
  1222. help="Apply changes but don't create a commit",
  1223. )
  1224. parser.add_argument("-m", "--message", help="Custom commit message")
  1225. parser.add_argument("commits", nargs="+", help="Commits to revert")
  1226. args = parser.parse_args(args)
  1227. result = porcelain.revert(
  1228. ".", commits=args.commits, no_commit=args.no_commit, message=args.message
  1229. )
  1230. if result and not args.no_commit:
  1231. logger.info("[%s] Revert completed", result.decode("ascii")[:7])
  1232. class cmd_daemon(Command):
  1233. """Run a simple Git protocol server."""
  1234. def run(self, args) -> None:
  1235. """Execute the daemon command.
  1236. Args:
  1237. args: Command line arguments
  1238. """
  1239. from dulwich import log_utils
  1240. from .protocol import TCP_GIT_PORT
  1241. parser = argparse.ArgumentParser()
  1242. parser.add_argument(
  1243. "-l",
  1244. "--listen_address",
  1245. default="localhost",
  1246. help="Binding IP address.",
  1247. )
  1248. parser.add_argument(
  1249. "-p",
  1250. "--port",
  1251. type=int,
  1252. default=TCP_GIT_PORT,
  1253. help="Binding TCP port.",
  1254. )
  1255. parser.add_argument(
  1256. "gitdir", nargs="?", default=".", help="Git directory to serve"
  1257. )
  1258. args = parser.parse_args(args)
  1259. log_utils.default_logging_config()
  1260. porcelain.daemon(args.gitdir, address=args.listen_address, port=args.port)
  1261. class cmd_web_daemon(Command):
  1262. """Run a simple HTTP server for Git repositories."""
  1263. def run(self, args) -> None:
  1264. """Execute the web-daemon command.
  1265. Args:
  1266. args: Command line arguments
  1267. """
  1268. from dulwich import log_utils
  1269. parser = argparse.ArgumentParser()
  1270. parser.add_argument(
  1271. "-l",
  1272. "--listen_address",
  1273. default="",
  1274. help="Binding IP address.",
  1275. )
  1276. parser.add_argument(
  1277. "-p",
  1278. "--port",
  1279. type=int,
  1280. default=8000,
  1281. help="Binding TCP port.",
  1282. )
  1283. parser.add_argument(
  1284. "gitdir", nargs="?", default=".", help="Git directory to serve"
  1285. )
  1286. args = parser.parse_args(args)
  1287. log_utils.default_logging_config()
  1288. porcelain.web_daemon(args.gitdir, address=args.listen_address, port=args.port)
  1289. class cmd_write_tree(Command):
  1290. """Create a tree object from the current index."""
  1291. def run(self, args) -> None:
  1292. """Execute the write-tree command.
  1293. Args:
  1294. args: Command line arguments
  1295. """
  1296. parser = argparse.ArgumentParser()
  1297. parser.parse_args(args)
  1298. sys.stdout.write("{}\n".format(porcelain.write_tree(".").decode()))
  1299. class cmd_receive_pack(Command):
  1300. """Receive what is pushed into the repository."""
  1301. def run(self, args) -> None:
  1302. """Execute the receive-pack command.
  1303. Args:
  1304. args: Command line arguments
  1305. """
  1306. parser = argparse.ArgumentParser()
  1307. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  1308. args = parser.parse_args(args)
  1309. porcelain.receive_pack(args.gitdir)
  1310. class cmd_upload_pack(Command):
  1311. """Send objects packed back to git-fetch-pack."""
  1312. def run(self, args) -> None:
  1313. """Execute the upload-pack command.
  1314. Args:
  1315. args: Command line arguments
  1316. """
  1317. parser = argparse.ArgumentParser()
  1318. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  1319. args = parser.parse_args(args)
  1320. porcelain.upload_pack(args.gitdir)
  1321. class cmd_shortlog(Command):
  1322. """Show a shortlog of commits by author."""
  1323. def run(self, args) -> None:
  1324. """Execute the shortlog command with the given CLI arguments.
  1325. Args:
  1326. args: List of command line arguments.
  1327. """
  1328. parser = argparse.ArgumentParser()
  1329. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  1330. parser.add_argument("--summary", action="store_true", help="Show summary only")
  1331. parser.add_argument(
  1332. "--sort", action="store_true", help="Sort authors by commit count"
  1333. )
  1334. args = parser.parse_args(args)
  1335. shortlog_items: list[dict[str, str]] = porcelain.shortlog(
  1336. repo=args.gitdir,
  1337. summary_only=args.summary,
  1338. sort_by_commits=args.sort,
  1339. )
  1340. for item in shortlog_items:
  1341. author: str = item["author"]
  1342. messages: str = item["messages"]
  1343. if args.summary:
  1344. count = len(messages.splitlines())
  1345. sys.stdout.write(f"{count}\t{author}\n")
  1346. else:
  1347. sys.stdout.write(f"{author} ({len(messages.splitlines())}):\n")
  1348. for msg in messages.splitlines():
  1349. sys.stdout.write(f" {msg}\n")
  1350. sys.stdout.write("\n")
  1351. class cmd_status(Command):
  1352. """Show the working tree status."""
  1353. def run(self, args) -> None:
  1354. """Execute the status command.
  1355. Args:
  1356. args: Command line arguments
  1357. """
  1358. parser = argparse.ArgumentParser()
  1359. parser.add_argument("gitdir", nargs="?", default=".", help="Git directory")
  1360. args = parser.parse_args(args)
  1361. status = porcelain.status(args.gitdir)
  1362. if any(names for (kind, names) in status.staged.items()):
  1363. sys.stdout.write("Changes to be committed:\n\n")
  1364. for kind, names in status.staged.items():
  1365. for name in names:
  1366. sys.stdout.write(
  1367. f"\t{kind}: {name.decode(sys.getfilesystemencoding())}\n"
  1368. )
  1369. sys.stdout.write("\n")
  1370. if status.unstaged:
  1371. sys.stdout.write("Changes not staged for commit:\n\n")
  1372. for name in status.unstaged:
  1373. sys.stdout.write(f"\t{name.decode(sys.getfilesystemencoding())}\n")
  1374. sys.stdout.write("\n")
  1375. if status.untracked:
  1376. sys.stdout.write("Untracked files:\n\n")
  1377. for name in status.untracked:
  1378. sys.stdout.write(f"\t{name}\n")
  1379. sys.stdout.write("\n")
  1380. class cmd_ls_remote(Command):
  1381. """List references in a remote repository."""
  1382. def run(self, args) -> None:
  1383. """Execute the ls-remote command.
  1384. Args:
  1385. args: Command line arguments
  1386. """
  1387. parser = argparse.ArgumentParser()
  1388. parser.add_argument(
  1389. "--symref", action="store_true", help="Show symbolic references"
  1390. )
  1391. parser.add_argument("url", help="Remote URL to list references from")
  1392. args = parser.parse_args(args)
  1393. result = porcelain.ls_remote(args.url)
  1394. if args.symref:
  1395. # Show symrefs first, like git does
  1396. for ref, target in sorted(result.symrefs.items()):
  1397. if target:
  1398. sys.stdout.write(f"ref: {target.decode()}\t{ref.decode()}\n")
  1399. # Show regular refs
  1400. for ref in sorted(result.refs):
  1401. sha = result.refs[ref]
  1402. if sha is not None:
  1403. sys.stdout.write(f"{sha.decode()}\t{ref.decode()}\n")
  1404. class cmd_ls_tree(Command):
  1405. """List the contents of a tree object."""
  1406. def run(self, args) -> None:
  1407. """Execute the ls-tree command.
  1408. Args:
  1409. args: Command line arguments
  1410. """
  1411. parser = argparse.ArgumentParser()
  1412. parser.add_argument(
  1413. "-r",
  1414. "--recursive",
  1415. action="store_true",
  1416. help="Recursively list tree contents.",
  1417. )
  1418. parser.add_argument(
  1419. "--name-only", action="store_true", help="Only display name."
  1420. )
  1421. parser.add_argument("treeish", nargs="?", help="Tree-ish to list")
  1422. args = parser.parse_args(args)
  1423. with Repo(".") as repo:
  1424. config = repo.get_config_stack()
  1425. with get_pager(config=config, cmd_name="ls-tree") as outstream:
  1426. porcelain.ls_tree(
  1427. repo,
  1428. args.treeish,
  1429. outstream=outstream,
  1430. recursive=args.recursive,
  1431. name_only=args.name_only,
  1432. )
  1433. class cmd_pack_objects(Command):
  1434. """Create a packed archive of objects."""
  1435. def run(self, args) -> None:
  1436. """Execute the pack-objects command.
  1437. Args:
  1438. args: Command line arguments
  1439. """
  1440. parser = argparse.ArgumentParser()
  1441. parser.add_argument(
  1442. "--stdout", action="store_true", help="Write pack to stdout"
  1443. )
  1444. parser.add_argument("--deltify", action="store_true", help="Create deltas")
  1445. parser.add_argument(
  1446. "--no-reuse-deltas", action="store_true", help="Don't reuse existing deltas"
  1447. )
  1448. parser.add_argument("basename", nargs="?", help="Base name for pack files")
  1449. args = parser.parse_args(args)
  1450. if not args.stdout and not args.basename:
  1451. parser.error("basename required when not using --stdout")
  1452. object_ids = [line.strip().encode() for line in sys.stdin.readlines()]
  1453. deltify = args.deltify
  1454. reuse_deltas = not args.no_reuse_deltas
  1455. if args.stdout:
  1456. packf = getattr(sys.stdout, "buffer", sys.stdout)
  1457. assert isinstance(packf, BinaryIO)
  1458. idxf = None
  1459. close = []
  1460. else:
  1461. packf = open(args.basename + ".pack", "wb")
  1462. idxf = open(args.basename + ".idx", "wb")
  1463. close = [packf, idxf]
  1464. porcelain.pack_objects(
  1465. ".", object_ids, packf, idxf, deltify=deltify, reuse_deltas=reuse_deltas
  1466. )
  1467. for f in close:
  1468. f.close()
  1469. class cmd_unpack_objects(Command):
  1470. """Unpack objects from a packed archive."""
  1471. def run(self, args) -> None:
  1472. """Execute the unpack-objects command.
  1473. Args:
  1474. args: Command line arguments
  1475. """
  1476. parser = argparse.ArgumentParser()
  1477. parser.add_argument("pack_file", help="Pack file to unpack")
  1478. args = parser.parse_args(args)
  1479. count = porcelain.unpack_objects(args.pack_file)
  1480. logger.info("Unpacked %d objects", count)
  1481. class cmd_prune(Command):
  1482. """Prune all unreachable objects from the object database."""
  1483. def run(self, args) -> Optional[int]:
  1484. """Execute the prune command.
  1485. Args:
  1486. args: Command line arguments
  1487. """
  1488. import datetime
  1489. import time
  1490. from dulwich.object_store import DEFAULT_TEMPFILE_GRACE_PERIOD
  1491. parser = argparse.ArgumentParser(
  1492. description="Remove temporary pack files left behind by interrupted operations"
  1493. )
  1494. parser.add_argument(
  1495. "--expire",
  1496. nargs="?",
  1497. const="2.weeks.ago",
  1498. help="Only prune files older than the specified date (default: 2.weeks.ago)",
  1499. )
  1500. parser.add_argument(
  1501. "--dry-run",
  1502. "-n",
  1503. action="store_true",
  1504. help="Only report what would be removed",
  1505. )
  1506. parser.add_argument(
  1507. "--verbose",
  1508. "-v",
  1509. action="store_true",
  1510. help="Report all actions",
  1511. )
  1512. args = parser.parse_args(args)
  1513. # Parse expire grace period
  1514. grace_period = DEFAULT_TEMPFILE_GRACE_PERIOD
  1515. if args.expire:
  1516. try:
  1517. grace_period = parse_relative_time(args.expire)
  1518. except ValueError:
  1519. # Try to parse as absolute date
  1520. try:
  1521. date = datetime.datetime.strptime(args.expire, "%Y-%m-%d")
  1522. grace_period = int(time.time() - date.timestamp())
  1523. except ValueError:
  1524. logger.error("Invalid expire date: %s", args.expire)
  1525. return 1
  1526. # Progress callback
  1527. def progress(msg):
  1528. if args.verbose:
  1529. logger.info(msg)
  1530. try:
  1531. porcelain.prune(
  1532. ".",
  1533. grace_period=grace_period,
  1534. dry_run=args.dry_run,
  1535. progress=progress if args.verbose else None,
  1536. )
  1537. return None
  1538. except porcelain.Error as e:
  1539. logger.error("%s", e)
  1540. return 1
  1541. class cmd_pull(Command):
  1542. """Fetch from and integrate with another repository or a local branch."""
  1543. def run(self, args) -> None:
  1544. """Execute the pull command.
  1545. Args:
  1546. args: Command line arguments
  1547. """
  1548. parser = argparse.ArgumentParser()
  1549. parser.add_argument("from_location", type=str)
  1550. parser.add_argument("refspec", type=str, nargs="*")
  1551. parser.add_argument("--filter", type=str, nargs=1)
  1552. parser.add_argument("--protocol", type=int)
  1553. args = parser.parse_args(args)
  1554. porcelain.pull(
  1555. ".",
  1556. args.from_location or None,
  1557. args.refspec or None,
  1558. filter_spec=args.filter,
  1559. protocol_version=args.protocol or None,
  1560. )
  1561. class cmd_push(Command):
  1562. """Update remote refs along with associated objects."""
  1563. def run(self, argv) -> Optional[int]:
  1564. """Execute the push command.
  1565. Args:
  1566. argv: Command line arguments
  1567. """
  1568. parser = argparse.ArgumentParser()
  1569. parser.add_argument("-f", "--force", action="store_true", help="Force")
  1570. parser.add_argument("to_location", type=str)
  1571. parser.add_argument("refspec", type=str, nargs="*")
  1572. args = parser.parse_args(argv)
  1573. try:
  1574. porcelain.push(
  1575. ".", args.to_location, args.refspec or None, force=args.force
  1576. )
  1577. except porcelain.DivergedBranches:
  1578. sys.stderr.write("Diverged branches; specify --force to override")
  1579. return 1
  1580. return None
  1581. class cmd_remote_add(Command):
  1582. """Add a remote repository."""
  1583. def run(self, args) -> None:
  1584. """Execute the remote-add command.
  1585. Args:
  1586. args: Command line arguments
  1587. """
  1588. parser = argparse.ArgumentParser()
  1589. parser.add_argument("name", help="Name of the remote")
  1590. parser.add_argument("url", help="URL of the remote")
  1591. args = parser.parse_args(args)
  1592. porcelain.remote_add(".", args.name, args.url)
  1593. class SuperCommand(Command):
  1594. """Base class for commands that have subcommands."""
  1595. subcommands: ClassVar[dict[str, type[Command]]] = {}
  1596. default_command: ClassVar[Optional[type[Command]]] = None
  1597. def run(self, args):
  1598. """Execute the subcommand command.
  1599. Args:
  1600. args: Command line arguments
  1601. """
  1602. if not args:
  1603. if self.default_command:
  1604. return self.default_command().run(args)
  1605. else:
  1606. logger.info(
  1607. "Supported subcommands: %s", ", ".join(self.subcommands.keys())
  1608. )
  1609. return False
  1610. cmd = args[0]
  1611. try:
  1612. cmd_kls = self.subcommands[cmd]
  1613. except KeyError:
  1614. logger.error("No such subcommand: %s", args[0])
  1615. sys.exit(1)
  1616. return cmd_kls().run(args[1:])
  1617. class cmd_remote(SuperCommand):
  1618. """Manage set of tracked repositories."""
  1619. subcommands: ClassVar[dict[str, type[Command]]] = {
  1620. "add": cmd_remote_add,
  1621. }
  1622. class cmd_submodule_list(Command):
  1623. """List submodules."""
  1624. def run(self, argv) -> None:
  1625. """Execute the submodule-list command.
  1626. Args:
  1627. argv: Command line arguments
  1628. """
  1629. parser = argparse.ArgumentParser()
  1630. parser.parse_args(argv)
  1631. for path, sha in porcelain.submodule_list("."):
  1632. sys.stdout.write(f" {sha} {path}\n")
  1633. class cmd_submodule_init(Command):
  1634. """Initialize submodules."""
  1635. def run(self, argv) -> None:
  1636. """Execute the submodule-init command.
  1637. Args:
  1638. argv: Command line arguments
  1639. """
  1640. parser = argparse.ArgumentParser()
  1641. parser.parse_args(argv)
  1642. porcelain.submodule_init(".")
  1643. class cmd_submodule_add(Command):
  1644. """Add a submodule."""
  1645. def run(self, argv) -> None:
  1646. """Execute the submodule-add command.
  1647. Args:
  1648. argv: Command line arguments
  1649. """
  1650. parser = argparse.ArgumentParser()
  1651. parser.add_argument("url", help="URL of repository to add as submodule")
  1652. parser.add_argument("path", nargs="?", help="Path where submodule should live")
  1653. parser.add_argument("--name", help="Name for the submodule")
  1654. args = parser.parse_args(argv)
  1655. porcelain.submodule_add(".", args.url, args.path, args.name)
  1656. class cmd_submodule_update(Command):
  1657. """Update submodules."""
  1658. def run(self, argv) -> None:
  1659. """Execute the submodule-update command.
  1660. Args:
  1661. argv: Command line arguments
  1662. """
  1663. parser = argparse.ArgumentParser()
  1664. parser.add_argument(
  1665. "--init", action="store_true", help="Initialize submodules first"
  1666. )
  1667. parser.add_argument(
  1668. "--force",
  1669. action="store_true",
  1670. help="Force update even if local changes exist",
  1671. )
  1672. parser.add_argument(
  1673. "paths", nargs="*", help="Specific submodule paths to update"
  1674. )
  1675. args = parser.parse_args(argv)
  1676. paths = args.paths if args.paths else None
  1677. porcelain.submodule_update(".", paths=paths, init=args.init, force=args.force)
  1678. class cmd_submodule(SuperCommand):
  1679. """Initialize, update or inspect submodules."""
  1680. subcommands: ClassVar[dict[str, type[Command]]] = {
  1681. "add": cmd_submodule_add,
  1682. "init": cmd_submodule_init,
  1683. "list": cmd_submodule_list,
  1684. "update": cmd_submodule_update,
  1685. }
  1686. default_command = cmd_submodule_list
  1687. class cmd_check_ignore(Command):
  1688. """Check whether files are excluded by gitignore."""
  1689. def run(self, args):
  1690. """Execute the check-ignore command.
  1691. Args:
  1692. args: Command line arguments
  1693. """
  1694. parser = argparse.ArgumentParser()
  1695. parser.add_argument("paths", nargs="+", help="Paths to check")
  1696. args = parser.parse_args(args)
  1697. ret = 1
  1698. for path in porcelain.check_ignore(".", args.paths):
  1699. logger.info(path)
  1700. ret = 0
  1701. return ret
  1702. class cmd_check_mailmap(Command):
  1703. """Show canonical names and email addresses of contacts."""
  1704. def run(self, args) -> None:
  1705. """Execute the check-mailmap command.
  1706. Args:
  1707. args: Command line arguments
  1708. """
  1709. parser = argparse.ArgumentParser()
  1710. parser.add_argument("identities", nargs="+", help="Identities to check")
  1711. args = parser.parse_args(args)
  1712. for identity in args.identities:
  1713. canonical_identity = porcelain.check_mailmap(".", identity)
  1714. logger.info(canonical_identity)
  1715. class cmd_branch(Command):
  1716. """List, create, or delete branches."""
  1717. def run(self, args) -> Optional[int]:
  1718. """Execute the branch command.
  1719. Args:
  1720. args: Command line arguments
  1721. """
  1722. parser = argparse.ArgumentParser()
  1723. parser.add_argument(
  1724. "branch",
  1725. type=str,
  1726. nargs="?",
  1727. help="Name of the branch",
  1728. )
  1729. parser.add_argument(
  1730. "-d",
  1731. "--delete",
  1732. action="store_true",
  1733. help="Delete branch",
  1734. )
  1735. parser.add_argument("--all", action="store_true", help="List all branches")
  1736. parser.add_argument(
  1737. "--merged", action="store_true", help="List merged into current branch"
  1738. )
  1739. parser.add_argument(
  1740. "--no-merged",
  1741. action="store_true",
  1742. help="List branches not merged into current branch",
  1743. )
  1744. parser.add_argument(
  1745. "--remotes", action="store_true", help="List remotes branches"
  1746. )
  1747. parser.add_argument(
  1748. "--contains",
  1749. nargs="?",
  1750. const="HEAD",
  1751. help="List branches that contain a specific commit",
  1752. )
  1753. parser.add_argument(
  1754. "--column", action="store_true", help="Display branch list in columns"
  1755. )
  1756. parser.add_argument(
  1757. "--list",
  1758. nargs="?",
  1759. const=None,
  1760. help="List branches matching a pattern",
  1761. )
  1762. args = parser.parse_args(args)
  1763. def print_branches(
  1764. branches: Union[Iterator[bytes], list[bytes]], use_columns=False
  1765. ) -> None:
  1766. if use_columns:
  1767. write_columns(branches, sys.stdout)
  1768. else:
  1769. for branch in branches:
  1770. sys.stdout.write(f"{branch.decode()}\n")
  1771. branches: Union[Iterator[bytes], list[bytes], None] = None
  1772. try:
  1773. if args.all:
  1774. branches = porcelain.branch_list(".") + porcelain.branch_remotes_list(
  1775. "."
  1776. )
  1777. elif args.remotes:
  1778. branches = porcelain.branch_remotes_list(".")
  1779. elif args.merged:
  1780. branches = porcelain.merged_branches(".")
  1781. elif args.no_merged:
  1782. branches = porcelain.no_merged_branches(".")
  1783. elif args.contains:
  1784. try:
  1785. branches = list(
  1786. porcelain.branches_containing(".", commit=args.contains)
  1787. )
  1788. except KeyError as e:
  1789. sys.stderr.write(
  1790. f"error: object name {e.args[0].decode()} not found\n"
  1791. )
  1792. return 1
  1793. except porcelain.Error as e:
  1794. sys.stderr.write(f"{e}")
  1795. return 1
  1796. pattern = args.list
  1797. if pattern is not None and branches:
  1798. branches = porcelain.filter_branches_by_pattern(branches, pattern)
  1799. if branches is not None:
  1800. print_branches(branches, args.column)
  1801. return 0
  1802. if not args.branch:
  1803. logger.error("Usage: dulwich branch [-d] BRANCH_NAME")
  1804. return 1
  1805. if args.delete:
  1806. porcelain.branch_delete(".", name=args.branch)
  1807. else:
  1808. try:
  1809. porcelain.branch_create(".", name=args.branch)
  1810. except porcelain.Error as e:
  1811. sys.stderr.write(f"{e}")
  1812. return 1
  1813. return 0
  1814. class cmd_checkout(Command):
  1815. """Switch branches or restore working tree files."""
  1816. def run(self, args) -> Optional[int]:
  1817. """Execute the checkout command.
  1818. Args:
  1819. args: Command line arguments
  1820. """
  1821. parser = argparse.ArgumentParser()
  1822. parser.add_argument(
  1823. "target",
  1824. type=str,
  1825. help="Name of the branch, tag, or commit to checkout",
  1826. )
  1827. parser.add_argument(
  1828. "-f",
  1829. "--force",
  1830. action="store_true",
  1831. help="Force checkout",
  1832. )
  1833. parser.add_argument(
  1834. "-b",
  1835. "--new-branch",
  1836. type=str,
  1837. help="Create a new branch at the target and switch to it",
  1838. )
  1839. args = parser.parse_args(args)
  1840. if not args.target:
  1841. logger.error("Usage: dulwich checkout TARGET [--force] [-b NEW_BRANCH]")
  1842. return 1
  1843. try:
  1844. porcelain.checkout(
  1845. ".", target=args.target, force=args.force, new_branch=args.new_branch
  1846. )
  1847. except porcelain.CheckoutError as e:
  1848. sys.stderr.write(f"{e}\n")
  1849. return 1
  1850. return 0
  1851. class cmd_stash_list(Command):
  1852. """List stash entries."""
  1853. def run(self, args) -> None:
  1854. """Execute the stash-list command.
  1855. Args:
  1856. args: Command line arguments
  1857. """
  1858. parser = argparse.ArgumentParser()
  1859. parser.parse_args(args)
  1860. from .repo import Repo
  1861. from .stash import Stash
  1862. with Repo(".") as r:
  1863. stash = Stash.from_repo(r)
  1864. for i, entry in enumerate(stash.stashes()):
  1865. logger.info(
  1866. "stash@{%d}: %s",
  1867. i,
  1868. entry.message.decode("utf-8", "replace").rstrip("\n"),
  1869. )
  1870. class cmd_stash_push(Command):
  1871. """Save your local modifications to a new stash."""
  1872. def run(self, args) -> None:
  1873. """Execute the stash-push command.
  1874. Args:
  1875. args: Command line arguments
  1876. """
  1877. parser = argparse.ArgumentParser()
  1878. parser.parse_args(args)
  1879. porcelain.stash_push(".")
  1880. logger.info("Saved working directory and index state")
  1881. class cmd_stash_pop(Command):
  1882. """Apply a stash and remove it from the stash list."""
  1883. def run(self, args) -> None:
  1884. """Execute the stash-pop command.
  1885. Args:
  1886. args: Command line arguments
  1887. """
  1888. parser = argparse.ArgumentParser()
  1889. parser.parse_args(args)
  1890. porcelain.stash_pop(".")
  1891. logger.info("Restored working directory and index state")
  1892. class cmd_bisect(SuperCommand):
  1893. """Use binary search to find the commit that introduced a bug."""
  1894. subcommands: ClassVar[dict[str, type[Command]]] = {}
  1895. def run(self, args):
  1896. """Execute the bisect command.
  1897. Args:
  1898. args: Command line arguments
  1899. """
  1900. parser = argparse.ArgumentParser(prog="dulwich bisect")
  1901. subparsers = parser.add_subparsers(dest="subcommand", help="bisect subcommands")
  1902. # bisect start
  1903. start_parser = subparsers.add_parser("start", help="Start a new bisect session")
  1904. start_parser.add_argument("bad", nargs="?", help="Bad commit")
  1905. start_parser.add_argument("good", nargs="*", help="Good commit(s)")
  1906. start_parser.add_argument(
  1907. "--no-checkout",
  1908. action="store_true",
  1909. help="Don't checkout commits during bisect",
  1910. )
  1911. start_parser.add_argument(
  1912. "--term-bad", default="bad", help="Term to use for bad commits"
  1913. )
  1914. start_parser.add_argument(
  1915. "--term-good", default="good", help="Term to use for good commits"
  1916. )
  1917. start_parser.add_argument(
  1918. "--", dest="paths", nargs="*", help="Paths to limit bisect to"
  1919. )
  1920. # bisect bad
  1921. bad_parser = subparsers.add_parser("bad", help="Mark a commit as bad")
  1922. bad_parser.add_argument("rev", nargs="?", help="Commit to mark as bad")
  1923. # bisect good
  1924. good_parser = subparsers.add_parser("good", help="Mark a commit as good")
  1925. good_parser.add_argument("rev", nargs="?", help="Commit to mark as good")
  1926. # bisect skip
  1927. skip_parser = subparsers.add_parser("skip", help="Skip commits")
  1928. skip_parser.add_argument("revs", nargs="*", help="Commits to skip")
  1929. # bisect reset
  1930. reset_parser = subparsers.add_parser("reset", help="Reset bisect state")
  1931. reset_parser.add_argument("commit", nargs="?", help="Commit to reset to")
  1932. # bisect log
  1933. subparsers.add_parser("log", help="Show bisect log")
  1934. # bisect replay
  1935. replay_parser = subparsers.add_parser("replay", help="Replay bisect log")
  1936. replay_parser.add_argument("logfile", help="Log file to replay")
  1937. # bisect help
  1938. subparsers.add_parser("help", help="Show help")
  1939. parsed_args = parser.parse_args(args)
  1940. if not parsed_args.subcommand:
  1941. parser.print_help()
  1942. return 1
  1943. try:
  1944. if parsed_args.subcommand == "start":
  1945. next_sha = porcelain.bisect_start(
  1946. bad=parsed_args.bad,
  1947. good=parsed_args.good if parsed_args.good else None,
  1948. paths=parsed_args.paths,
  1949. no_checkout=parsed_args.no_checkout,
  1950. term_bad=parsed_args.term_bad,
  1951. term_good=parsed_args.term_good,
  1952. )
  1953. if next_sha:
  1954. logger.info(
  1955. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  1956. )
  1957. elif parsed_args.subcommand == "bad":
  1958. next_sha = porcelain.bisect_bad(rev=parsed_args.rev)
  1959. if next_sha:
  1960. logger.info(
  1961. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  1962. )
  1963. else:
  1964. # Bisect complete - find the first bad commit
  1965. with porcelain.open_repo_closing(".") as r:
  1966. bad_ref = os.path.join(r.controldir(), "refs", "bisect", "bad")
  1967. with open(bad_ref, "rb") as f:
  1968. bad_sha = f.read().strip()
  1969. commit = r.object_store[bad_sha]
  1970. assert isinstance(commit, Commit)
  1971. message = commit.message.decode(
  1972. "utf-8", errors="replace"
  1973. ).split("\n")[0]
  1974. logger.info(
  1975. "%s is the first bad commit", bad_sha.decode("ascii")
  1976. )
  1977. logger.info("commit %s", bad_sha.decode("ascii"))
  1978. logger.info(" %s", message)
  1979. elif parsed_args.subcommand == "good":
  1980. next_sha = porcelain.bisect_good(rev=parsed_args.rev)
  1981. if next_sha:
  1982. logger.info(
  1983. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  1984. )
  1985. elif parsed_args.subcommand == "skip":
  1986. next_sha = porcelain.bisect_skip(
  1987. revs=parsed_args.revs if parsed_args.revs else None
  1988. )
  1989. if next_sha:
  1990. logger.info(
  1991. "Bisecting: checking out '%s'", next_sha.decode("ascii")
  1992. )
  1993. elif parsed_args.subcommand == "reset":
  1994. porcelain.bisect_reset(commit=parsed_args.commit)
  1995. logger.info("Bisect reset")
  1996. elif parsed_args.subcommand == "log":
  1997. log = porcelain.bisect_log()
  1998. logger.info(log.rstrip())
  1999. elif parsed_args.subcommand == "replay":
  2000. porcelain.bisect_replay(".", log_file=parsed_args.logfile)
  2001. logger.info("Replayed bisect log from %s", parsed_args.logfile)
  2002. elif parsed_args.subcommand == "help":
  2003. parser.print_help()
  2004. except porcelain.Error as e:
  2005. logger.error("%s", e)
  2006. return 1
  2007. except ValueError as e:
  2008. logger.error("%s", e)
  2009. return 1
  2010. return 0
  2011. class cmd_stash(SuperCommand):
  2012. """Stash the changes in a dirty working directory away."""
  2013. subcommands: ClassVar[dict[str, type[Command]]] = {
  2014. "list": cmd_stash_list,
  2015. "pop": cmd_stash_pop,
  2016. "push": cmd_stash_push,
  2017. }
  2018. class cmd_ls_files(Command):
  2019. """Show information about files in the index and working tree."""
  2020. def run(self, args) -> None:
  2021. """Execute the ls-files command.
  2022. Args:
  2023. args: Command line arguments
  2024. """
  2025. parser = argparse.ArgumentParser()
  2026. parser.parse_args(args)
  2027. for name in porcelain.ls_files("."):
  2028. logger.info(name)
  2029. class cmd_describe(Command):
  2030. """Give an object a human readable name based on an available ref."""
  2031. def run(self, args) -> None:
  2032. """Execute the describe command.
  2033. Args:
  2034. args: Command line arguments
  2035. """
  2036. parser = argparse.ArgumentParser()
  2037. parser.parse_args(args)
  2038. logger.info(porcelain.describe("."))
  2039. class cmd_merge(Command):
  2040. """Join two or more development histories together."""
  2041. def run(self, args) -> Optional[int]:
  2042. """Execute the merge command.
  2043. Args:
  2044. args: Command line arguments
  2045. """
  2046. parser = argparse.ArgumentParser()
  2047. parser.add_argument("commit", type=str, help="Commit to merge")
  2048. parser.add_argument(
  2049. "--no-commit", action="store_true", help="Do not create a merge commit"
  2050. )
  2051. parser.add_argument(
  2052. "--no-ff", action="store_true", help="Force create a merge commit"
  2053. )
  2054. parser.add_argument("-m", "--message", type=str, help="Merge commit message")
  2055. args = parser.parse_args(args)
  2056. try:
  2057. merge_commit_id, conflicts = porcelain.merge(
  2058. ".",
  2059. args.commit,
  2060. no_commit=args.no_commit,
  2061. no_ff=args.no_ff,
  2062. message=args.message,
  2063. )
  2064. if conflicts:
  2065. logger.warning("Merge conflicts in %d file(s):", len(conflicts))
  2066. for conflict_path in conflicts:
  2067. logger.warning(" %s", conflict_path.decode())
  2068. logger.error(
  2069. "Automatic merge failed; fix conflicts and then commit the result."
  2070. )
  2071. return 1
  2072. elif merge_commit_id is None and not args.no_commit:
  2073. logger.info("Already up to date.")
  2074. elif args.no_commit:
  2075. logger.info("Automatic merge successful; not committing as requested.")
  2076. else:
  2077. assert merge_commit_id is not None
  2078. logger.info(
  2079. "Merge successful. Created merge commit %s",
  2080. merge_commit_id.decode(),
  2081. )
  2082. return 0
  2083. except porcelain.Error as e:
  2084. logger.error("%s", e)
  2085. return 1
  2086. class cmd_notes_add(Command):
  2087. """Add notes to a commit."""
  2088. def run(self, args) -> None:
  2089. """Execute the notes-add command.
  2090. Args:
  2091. args: Command line arguments
  2092. """
  2093. parser = argparse.ArgumentParser()
  2094. parser.add_argument("object", help="Object to annotate")
  2095. parser.add_argument("-m", "--message", help="Note message", required=True)
  2096. parser.add_argument(
  2097. "--ref", default="commits", help="Notes ref (default: commits)"
  2098. )
  2099. args = parser.parse_args(args)
  2100. porcelain.notes_add(".", args.object, args.message, ref=args.ref)
  2101. class cmd_notes_show(Command):
  2102. """Show notes for a commit."""
  2103. def run(self, args) -> None:
  2104. """Execute the notes-show command.
  2105. Args:
  2106. args: Command line arguments
  2107. """
  2108. parser = argparse.ArgumentParser()
  2109. parser.add_argument("object", help="Object to show notes for")
  2110. parser.add_argument(
  2111. "--ref", default="commits", help="Notes ref (default: commits)"
  2112. )
  2113. args = parser.parse_args(args)
  2114. note = porcelain.notes_show(".", args.object, ref=args.ref)
  2115. if note:
  2116. sys.stdout.buffer.write(note)
  2117. else:
  2118. logger.info("No notes found for object %s", args.object)
  2119. class cmd_notes_remove(Command):
  2120. """Remove notes for a commit."""
  2121. def run(self, args) -> None:
  2122. """Execute the notes-remove command.
  2123. Args:
  2124. args: Command line arguments
  2125. """
  2126. parser = argparse.ArgumentParser()
  2127. parser.add_argument("object", help="Object to remove notes from")
  2128. parser.add_argument(
  2129. "--ref", default="commits", help="Notes ref (default: commits)"
  2130. )
  2131. args = parser.parse_args(args)
  2132. result = porcelain.notes_remove(".", args.object, ref=args.ref)
  2133. if result:
  2134. logger.info("Removed notes for object %s", args.object)
  2135. else:
  2136. logger.info("No notes found for object %s", args.object)
  2137. class cmd_notes_list(Command):
  2138. """List all note objects."""
  2139. def run(self, args) -> None:
  2140. """Execute the notes-list command.
  2141. Args:
  2142. args: Command line arguments
  2143. """
  2144. parser = argparse.ArgumentParser()
  2145. parser.add_argument(
  2146. "--ref", default="commits", help="Notes ref (default: commits)"
  2147. )
  2148. args = parser.parse_args(args)
  2149. notes = porcelain.notes_list(".", ref=args.ref)
  2150. for object_sha, note_content in notes:
  2151. logger.info(object_sha.hex())
  2152. class cmd_notes(SuperCommand):
  2153. """Add or inspect object notes."""
  2154. subcommands: ClassVar[dict[str, type[Command]]] = {
  2155. "add": cmd_notes_add,
  2156. "show": cmd_notes_show,
  2157. "remove": cmd_notes_remove,
  2158. "list": cmd_notes_list,
  2159. }
  2160. default_command = cmd_notes_list
  2161. class cmd_cherry_pick(Command):
  2162. """Apply the changes introduced by some existing commits."""
  2163. def run(self, args) -> Optional[int]:
  2164. """Execute the cherry-pick command.
  2165. Args:
  2166. args: Command line arguments
  2167. """
  2168. parser = argparse.ArgumentParser(
  2169. description="Apply the changes introduced by some existing commits"
  2170. )
  2171. parser.add_argument("commit", nargs="?", help="Commit to cherry-pick")
  2172. parser.add_argument(
  2173. "-n",
  2174. "--no-commit",
  2175. action="store_true",
  2176. help="Apply changes without making a commit",
  2177. )
  2178. parser.add_argument(
  2179. "--continue",
  2180. dest="continue_",
  2181. action="store_true",
  2182. help="Continue after resolving conflicts",
  2183. )
  2184. parser.add_argument(
  2185. "--abort",
  2186. action="store_true",
  2187. help="Abort the current cherry-pick operation",
  2188. )
  2189. args = parser.parse_args(args)
  2190. # Check argument validity
  2191. if args.continue_ or args.abort:
  2192. if args.commit is not None:
  2193. parser.error("Cannot specify commit with --continue or --abort")
  2194. return 1
  2195. else:
  2196. if args.commit is None:
  2197. parser.error("Commit argument is required")
  2198. return 1
  2199. try:
  2200. commit_arg = args.commit
  2201. result = porcelain.cherry_pick(
  2202. ".",
  2203. commit_arg,
  2204. no_commit=args.no_commit,
  2205. continue_=args.continue_,
  2206. abort=args.abort,
  2207. )
  2208. if args.abort:
  2209. logger.info("Cherry-pick aborted.")
  2210. elif args.continue_:
  2211. if result:
  2212. logger.info("Cherry-pick completed: %s", result.decode())
  2213. else:
  2214. logger.info("Cherry-pick completed.")
  2215. elif result is None:
  2216. if args.no_commit:
  2217. logger.info("Cherry-pick applied successfully (no commit created).")
  2218. else:
  2219. # This shouldn't happen unless there were conflicts
  2220. logger.warning("Cherry-pick resulted in conflicts.")
  2221. else:
  2222. logger.info("Cherry-pick successful: %s", result.decode())
  2223. return None
  2224. except porcelain.Error as e:
  2225. logger.error("%s", e)
  2226. return 1
  2227. class cmd_merge_tree(Command):
  2228. """Show three-way merge without touching index."""
  2229. def run(self, args) -> Optional[int]:
  2230. """Execute the merge-tree command.
  2231. Args:
  2232. args: Command line arguments
  2233. """
  2234. parser = argparse.ArgumentParser(
  2235. description="Perform a tree-level merge without touching the working directory"
  2236. )
  2237. parser.add_argument(
  2238. "base_tree",
  2239. nargs="?",
  2240. help="The common ancestor tree (optional, defaults to empty tree)",
  2241. )
  2242. parser.add_argument("our_tree", help="Our side of the merge")
  2243. parser.add_argument("their_tree", help="Their side of the merge")
  2244. parser.add_argument(
  2245. "-z",
  2246. "--name-only",
  2247. action="store_true",
  2248. help="Output only conflict paths, null-terminated",
  2249. )
  2250. args = parser.parse_args(args)
  2251. try:
  2252. # Determine base tree - if only two args provided, base is None
  2253. if args.base_tree is None:
  2254. # Only two arguments provided
  2255. base_tree = None
  2256. our_tree = args.our_tree
  2257. their_tree = args.their_tree
  2258. else:
  2259. # Three arguments provided
  2260. base_tree = args.base_tree
  2261. our_tree = args.our_tree
  2262. their_tree = args.their_tree
  2263. merged_tree_id, conflicts = porcelain.merge_tree(
  2264. ".", base_tree, our_tree, their_tree
  2265. )
  2266. if args.name_only:
  2267. # Output only conflict paths, null-terminated
  2268. for conflict_path in conflicts:
  2269. sys.stdout.buffer.write(conflict_path)
  2270. sys.stdout.buffer.write(b"\0")
  2271. else:
  2272. # Output the merged tree SHA
  2273. logger.info(merged_tree_id.decode("ascii"))
  2274. # Output conflict information
  2275. if conflicts:
  2276. logger.warning("\nConflicts in %d file(s):", len(conflicts))
  2277. for conflict_path in conflicts:
  2278. logger.warning(" %s", conflict_path.decode())
  2279. return None
  2280. except porcelain.Error as e:
  2281. logger.error("%s", e)
  2282. return 1
  2283. except KeyError as e:
  2284. logger.error("Object not found: %s", e)
  2285. return 1
  2286. class cmd_gc(Command):
  2287. """Cleanup unnecessary files and optimize the local repository."""
  2288. def run(self, args) -> Optional[int]:
  2289. """Execute the gc command.
  2290. Args:
  2291. args: Command line arguments
  2292. """
  2293. import datetime
  2294. import time
  2295. parser = argparse.ArgumentParser()
  2296. parser.add_argument(
  2297. "--auto",
  2298. action="store_true",
  2299. help="Only run gc if needed",
  2300. )
  2301. parser.add_argument(
  2302. "--aggressive",
  2303. action="store_true",
  2304. help="Use more aggressive settings",
  2305. )
  2306. parser.add_argument(
  2307. "--no-prune",
  2308. action="store_true",
  2309. help="Do not prune unreachable objects",
  2310. )
  2311. parser.add_argument(
  2312. "--prune",
  2313. nargs="?",
  2314. const="now",
  2315. help="Prune unreachable objects older than date (default: 2 weeks ago)",
  2316. )
  2317. parser.add_argument(
  2318. "--dry-run",
  2319. "-n",
  2320. action="store_true",
  2321. help="Only report what would be done",
  2322. )
  2323. parser.add_argument(
  2324. "--quiet",
  2325. "-q",
  2326. action="store_true",
  2327. help="Only report errors",
  2328. )
  2329. args = parser.parse_args(args)
  2330. # Parse prune grace period
  2331. grace_period = None
  2332. if args.prune:
  2333. try:
  2334. grace_period = parse_relative_time(args.prune)
  2335. except ValueError:
  2336. # Try to parse as absolute date
  2337. try:
  2338. date = datetime.datetime.strptime(args.prune, "%Y-%m-%d")
  2339. grace_period = int(time.time() - date.timestamp())
  2340. except ValueError:
  2341. logger.error("Invalid prune date: %s", args.prune)
  2342. return 1
  2343. elif not args.no_prune:
  2344. # Default to 2 weeks
  2345. grace_period = 1209600
  2346. # Progress callback
  2347. def progress(msg):
  2348. if not args.quiet:
  2349. logger.info(msg)
  2350. try:
  2351. stats = porcelain.gc(
  2352. ".",
  2353. auto=args.auto,
  2354. aggressive=args.aggressive,
  2355. prune=not args.no_prune,
  2356. grace_period=grace_period,
  2357. dry_run=args.dry_run,
  2358. progress=progress if not args.quiet else None,
  2359. )
  2360. # Report results
  2361. if not args.quiet:
  2362. if args.dry_run:
  2363. logger.info("\nDry run results:")
  2364. else:
  2365. logger.info("\nGarbage collection complete:")
  2366. if stats.pruned_objects:
  2367. logger.info(
  2368. " Pruned %d unreachable objects", len(stats.pruned_objects)
  2369. )
  2370. logger.info(" Freed %s", format_bytes(stats.bytes_freed))
  2371. if stats.packs_before != stats.packs_after:
  2372. logger.info(
  2373. " Reduced pack files from %d to %d",
  2374. stats.packs_before,
  2375. stats.packs_after,
  2376. )
  2377. except porcelain.Error as e:
  2378. logger.error("%s", e)
  2379. return 1
  2380. return None
  2381. class cmd_count_objects(Command):
  2382. """Count unpacked number of objects and their disk consumption."""
  2383. def run(self, args) -> None:
  2384. """Execute the count-objects command.
  2385. Args:
  2386. args: Command line arguments
  2387. """
  2388. parser = argparse.ArgumentParser()
  2389. parser.add_argument(
  2390. "-v",
  2391. "--verbose",
  2392. action="store_true",
  2393. help="Display verbose information.",
  2394. )
  2395. args = parser.parse_args(args)
  2396. if args.verbose:
  2397. stats = porcelain.count_objects(".", verbose=True)
  2398. # Display verbose output
  2399. logger.info("count: %d", stats.count)
  2400. logger.info("size: %d", stats.size // 1024) # Size in KiB
  2401. assert stats.in_pack is not None
  2402. logger.info("in-pack: %d", stats.in_pack)
  2403. assert stats.packs is not None
  2404. logger.info("packs: %d", stats.packs)
  2405. assert stats.size_pack is not None
  2406. logger.info("size-pack: %d", stats.size_pack // 1024) # Size in KiB
  2407. else:
  2408. # Simple output
  2409. stats = porcelain.count_objects(".", verbose=False)
  2410. logger.info("%d objects, %d kilobytes", stats.count, stats.size // 1024)
  2411. class cmd_rebase(Command):
  2412. """Reapply commits on top of another base tip."""
  2413. def run(self, args) -> int:
  2414. """Execute the rebase command.
  2415. Args:
  2416. args: Command line arguments
  2417. """
  2418. parser = argparse.ArgumentParser()
  2419. parser.add_argument(
  2420. "upstream", nargs="?", help="Upstream branch to rebase onto"
  2421. )
  2422. parser.add_argument("--onto", type=str, help="Rebase onto specific commit")
  2423. parser.add_argument(
  2424. "--branch", type=str, help="Branch to rebase (default: current)"
  2425. )
  2426. parser.add_argument(
  2427. "-i", "--interactive", action="store_true", help="Interactive rebase"
  2428. )
  2429. parser.add_argument(
  2430. "--edit-todo",
  2431. action="store_true",
  2432. help="Edit the todo list during an interactive rebase",
  2433. )
  2434. parser.add_argument(
  2435. "--abort", action="store_true", help="Abort an in-progress rebase"
  2436. )
  2437. parser.add_argument(
  2438. "--continue",
  2439. dest="continue_rebase",
  2440. action="store_true",
  2441. help="Continue an in-progress rebase",
  2442. )
  2443. parser.add_argument(
  2444. "--skip", action="store_true", help="Skip current commit and continue"
  2445. )
  2446. args = parser.parse_args(args)
  2447. # Handle abort/continue/skip first
  2448. if args.abort:
  2449. try:
  2450. porcelain.rebase(".", args.upstream or "HEAD", abort=True)
  2451. logger.info("Rebase aborted.")
  2452. except porcelain.Error as e:
  2453. logger.error("%s", e)
  2454. return 1
  2455. return 0
  2456. if args.continue_rebase:
  2457. try:
  2458. # Check if interactive rebase is in progress
  2459. if porcelain.is_interactive_rebase("."):
  2460. result = porcelain.rebase(
  2461. ".",
  2462. args.upstream or "HEAD",
  2463. continue_rebase=True,
  2464. interactive=True,
  2465. )
  2466. if result:
  2467. logger.info("Rebase complete.")
  2468. else:
  2469. logger.info("Rebase paused. Use --continue to resume.")
  2470. else:
  2471. new_shas = porcelain.rebase(
  2472. ".", args.upstream or "HEAD", continue_rebase=True
  2473. )
  2474. logger.info("Rebase complete.")
  2475. except porcelain.Error as e:
  2476. logger.error("%s", e)
  2477. return 1
  2478. return 0
  2479. if args.edit_todo:
  2480. # Edit todo list for interactive rebase
  2481. try:
  2482. porcelain.rebase(".", args.upstream or "HEAD", edit_todo=True)
  2483. logger.info("Todo list updated.")
  2484. except porcelain.Error as e:
  2485. logger.error("%s", e)
  2486. return 1
  2487. return 0
  2488. # Normal rebase requires upstream
  2489. if not args.upstream:
  2490. logger.error("Missing required argument 'upstream'")
  2491. return 1
  2492. try:
  2493. if args.interactive:
  2494. # Interactive rebase
  2495. result = porcelain.rebase(
  2496. ".",
  2497. args.upstream,
  2498. onto=args.onto,
  2499. branch=args.branch,
  2500. interactive=True,
  2501. )
  2502. if result:
  2503. logger.info(
  2504. "Interactive rebase started. Edit the todo list and save."
  2505. )
  2506. else:
  2507. logger.info("No commits to rebase.")
  2508. else:
  2509. # Regular rebase
  2510. new_shas = porcelain.rebase(
  2511. ".",
  2512. args.upstream,
  2513. onto=args.onto,
  2514. branch=args.branch,
  2515. )
  2516. if new_shas:
  2517. logger.info("Successfully rebased %d commits.", len(new_shas))
  2518. else:
  2519. logger.info("Already up to date.")
  2520. return 0
  2521. except porcelain.Error as e:
  2522. logger.error("%s", e)
  2523. return 1
  2524. class cmd_filter_branch(Command):
  2525. """Rewrite branches."""
  2526. def run(self, args) -> Optional[int]:
  2527. """Execute the filter-branch command.
  2528. Args:
  2529. args: Command line arguments
  2530. """
  2531. import subprocess
  2532. parser = argparse.ArgumentParser(description="Rewrite branches")
  2533. # Supported Git-compatible options
  2534. parser.add_argument(
  2535. "--subdirectory-filter",
  2536. type=str,
  2537. help="Only include history for subdirectory",
  2538. )
  2539. parser.add_argument("--env-filter", type=str, help="Environment filter command")
  2540. parser.add_argument("--tree-filter", type=str, help="Tree filter command")
  2541. parser.add_argument("--index-filter", type=str, help="Index filter command")
  2542. parser.add_argument("--parent-filter", type=str, help="Parent filter command")
  2543. parser.add_argument("--msg-filter", type=str, help="Message filter command")
  2544. parser.add_argument("--commit-filter", type=str, help="Commit filter command")
  2545. parser.add_argument(
  2546. "--tag-name-filter", type=str, help="Tag name filter command"
  2547. )
  2548. parser.add_argument(
  2549. "--prune-empty", action="store_true", help="Remove empty commits"
  2550. )
  2551. parser.add_argument(
  2552. "--original",
  2553. type=str,
  2554. default="refs/original",
  2555. help="Namespace for original refs",
  2556. )
  2557. parser.add_argument(
  2558. "-f",
  2559. "--force",
  2560. action="store_true",
  2561. help="Force operation even if refs/original/* exists",
  2562. )
  2563. # Branch/ref to rewrite (defaults to HEAD)
  2564. parser.add_argument(
  2565. "branch", nargs="?", default="HEAD", help="Branch or ref to rewrite"
  2566. )
  2567. args = parser.parse_args(args)
  2568. # Track if any filter fails
  2569. filter_error = False
  2570. # Setup environment for filters
  2571. env = os.environ.copy()
  2572. # Helper function to run shell commands
  2573. def run_filter(cmd, input_data=None, cwd=None, extra_env=None):
  2574. nonlocal filter_error
  2575. filter_env = env.copy()
  2576. if extra_env:
  2577. filter_env.update(extra_env)
  2578. result = subprocess.run(
  2579. cmd,
  2580. shell=True,
  2581. input=input_data,
  2582. cwd=cwd,
  2583. env=filter_env,
  2584. capture_output=True,
  2585. )
  2586. if result.returncode != 0:
  2587. filter_error = True
  2588. return None
  2589. return result.stdout
  2590. # Create filter functions based on arguments
  2591. filter_message = None
  2592. if args.msg_filter:
  2593. def filter_message(message):
  2594. result = run_filter(args.msg_filter, input_data=message)
  2595. return result if result is not None else message
  2596. tree_filter = None
  2597. if args.tree_filter:
  2598. def tree_filter(tree_sha, tmpdir):
  2599. from dulwich.objects import Blob, Tree
  2600. # Export tree to tmpdir
  2601. with Repo(".") as r:
  2602. tree = r.object_store[tree_sha]
  2603. for entry in tree.items():
  2604. path = Path(tmpdir) / entry.path.decode()
  2605. if entry.mode & 0o040000: # Directory
  2606. path.mkdir(exist_ok=True)
  2607. else:
  2608. obj = r.object_store[entry.sha]
  2609. path.write_bytes(obj.data)
  2610. # Run the filter command in the temp directory
  2611. run_filter(args.tree_filter, cwd=tmpdir)
  2612. # Rebuild tree from modified temp directory
  2613. def build_tree_from_dir(dir_path):
  2614. tree = Tree()
  2615. for name in sorted(os.listdir(dir_path)):
  2616. if name.startswith("."):
  2617. continue
  2618. path = os.path.join(dir_path, name)
  2619. if os.path.isdir(path):
  2620. subtree_sha = build_tree_from_dir(path)
  2621. tree.add(name.encode(), 0o040000, subtree_sha)
  2622. else:
  2623. with open(path, "rb") as f:
  2624. data = f.read()
  2625. blob = Blob.from_string(data)
  2626. r.object_store.add_object(blob)
  2627. # Use appropriate file mode
  2628. mode = os.stat(path).st_mode
  2629. if mode & 0o100:
  2630. file_mode = 0o100755
  2631. else:
  2632. file_mode = 0o100644
  2633. tree.add(name.encode(), file_mode, blob.id)
  2634. r.object_store.add_object(tree)
  2635. return tree.id
  2636. return build_tree_from_dir(tmpdir)
  2637. index_filter = None
  2638. if args.index_filter:
  2639. def index_filter(tree_sha, index_path):
  2640. run_filter(args.index_filter, extra_env={"GIT_INDEX_FILE": index_path})
  2641. return None # Read back from index
  2642. parent_filter = None
  2643. if args.parent_filter:
  2644. def parent_filter(parents):
  2645. parent_str = " ".join(p.hex() for p in parents)
  2646. result = run_filter(args.parent_filter, input_data=parent_str.encode())
  2647. if result is None:
  2648. return parents
  2649. output = result.decode().strip()
  2650. if not output:
  2651. return []
  2652. new_parents = []
  2653. for sha in output.split():
  2654. if valid_hexsha(sha):
  2655. new_parents.append(sha)
  2656. return new_parents
  2657. commit_filter = None
  2658. if args.commit_filter:
  2659. def commit_filter(commit_obj, tree_sha):
  2660. # The filter receives: tree parent1 parent2...
  2661. cmd_input = tree_sha.hex()
  2662. for parent in commit_obj.parents:
  2663. cmd_input += " " + parent.hex()
  2664. result = run_filter(
  2665. args.commit_filter,
  2666. input_data=cmd_input.encode(),
  2667. extra_env={"GIT_COMMIT": commit_obj.id.hex()},
  2668. )
  2669. if result is None:
  2670. return None
  2671. output = result.decode().strip()
  2672. if not output:
  2673. return None # Skip commit
  2674. if valid_hexsha(output):
  2675. return output
  2676. return None
  2677. tag_name_filter = None
  2678. if args.tag_name_filter:
  2679. def tag_name_filter(tag_name):
  2680. result = run_filter(args.tag_name_filter, input_data=tag_name)
  2681. return result.strip() if result is not None else tag_name
  2682. # Open repo once
  2683. with Repo(".") as r:
  2684. # Check for refs/original if not forcing
  2685. if not args.force:
  2686. original_prefix = args.original.encode() + b"/"
  2687. for ref in r.refs.allkeys():
  2688. if ref.startswith(original_prefix):
  2689. logger.error("Cannot create a new backup.")
  2690. logger.error(
  2691. "A previous backup already exists in %s/", args.original
  2692. )
  2693. logger.error("Force overwriting the backup with -f")
  2694. return 1
  2695. try:
  2696. # Call porcelain.filter_branch with the repo object
  2697. result = porcelain.filter_branch(
  2698. r,
  2699. args.branch,
  2700. filter_message=filter_message,
  2701. tree_filter=tree_filter if args.tree_filter else None,
  2702. index_filter=index_filter if args.index_filter else None,
  2703. parent_filter=parent_filter if args.parent_filter else None,
  2704. commit_filter=commit_filter if args.commit_filter else None,
  2705. subdirectory_filter=args.subdirectory_filter,
  2706. prune_empty=args.prune_empty,
  2707. tag_name_filter=tag_name_filter if args.tag_name_filter else None,
  2708. force=args.force,
  2709. keep_original=True, # Always keep original with git
  2710. )
  2711. # Check if any filter failed
  2712. if filter_error:
  2713. logger.error("Filter command failed")
  2714. return 1
  2715. # Git filter-branch shows progress
  2716. if result:
  2717. logger.info("Rewrite %s (%d commits)", args.branch, len(result))
  2718. # Git shows: Ref 'refs/heads/branch' was rewritten
  2719. if args.branch != "HEAD":
  2720. ref_name = (
  2721. args.branch
  2722. if args.branch.startswith("refs/")
  2723. else f"refs/heads/{args.branch}"
  2724. )
  2725. logger.info("Ref '%s' was rewritten", ref_name)
  2726. return 0
  2727. except porcelain.Error as e:
  2728. logger.error("%s", e)
  2729. return 1
  2730. class cmd_lfs(Command):
  2731. """Git Large File Storage management."""
  2732. """Git LFS management commands."""
  2733. def run(self, argv) -> None:
  2734. """Execute the lfs command.
  2735. Args:
  2736. argv: Command line arguments
  2737. """
  2738. parser = argparse.ArgumentParser(prog="dulwich lfs")
  2739. subparsers = parser.add_subparsers(dest="subcommand", help="LFS subcommands")
  2740. # lfs init
  2741. subparsers.add_parser("init", help="Initialize Git LFS")
  2742. # lfs track
  2743. parser_track = subparsers.add_parser(
  2744. "track", help="Track file patterns with LFS"
  2745. )
  2746. parser_track.add_argument("patterns", nargs="*", help="File patterns to track")
  2747. # lfs untrack
  2748. parser_untrack = subparsers.add_parser(
  2749. "untrack", help="Untrack file patterns from LFS"
  2750. )
  2751. parser_untrack.add_argument(
  2752. "patterns", nargs="+", help="File patterns to untrack"
  2753. )
  2754. # lfs ls-files
  2755. parser_ls = subparsers.add_parser("ls-files", help="List LFS files")
  2756. parser_ls.add_argument("--ref", help="Git ref to check (defaults to HEAD)")
  2757. # lfs migrate
  2758. parser_migrate = subparsers.add_parser("migrate", help="Migrate files to LFS")
  2759. parser_migrate.add_argument("--include", nargs="+", help="Patterns to include")
  2760. parser_migrate.add_argument("--exclude", nargs="+", help="Patterns to exclude")
  2761. parser_migrate.add_argument(
  2762. "--everything", action="store_true", help="Migrate all files above 100MB"
  2763. )
  2764. # lfs pointer
  2765. parser_pointer = subparsers.add_parser("pointer", help="Check LFS pointers")
  2766. parser_pointer.add_argument(
  2767. "--check", nargs="*", dest="paths", help="Check if files are LFS pointers"
  2768. )
  2769. # lfs clean
  2770. parser_clean = subparsers.add_parser("clean", help="Clean file to LFS pointer")
  2771. parser_clean.add_argument("path", help="File path to clean")
  2772. # lfs smudge
  2773. parser_smudge = subparsers.add_parser(
  2774. "smudge", help="Smudge LFS pointer to content"
  2775. )
  2776. parser_smudge.add_argument(
  2777. "--stdin", action="store_true", help="Read pointer from stdin"
  2778. )
  2779. # lfs fetch
  2780. parser_fetch = subparsers.add_parser(
  2781. "fetch", help="Fetch LFS objects from remote"
  2782. )
  2783. parser_fetch.add_argument(
  2784. "--remote", default="origin", help="Remote to fetch from"
  2785. )
  2786. parser_fetch.add_argument("refs", nargs="*", help="Specific refs to fetch")
  2787. # lfs pull
  2788. parser_pull = subparsers.add_parser(
  2789. "pull", help="Pull LFS objects for current checkout"
  2790. )
  2791. parser_pull.add_argument(
  2792. "--remote", default="origin", help="Remote to pull from"
  2793. )
  2794. # lfs push
  2795. parser_push = subparsers.add_parser("push", help="Push LFS objects to remote")
  2796. parser_push.add_argument("--remote", default="origin", help="Remote to push to")
  2797. parser_push.add_argument("refs", nargs="*", help="Specific refs to push")
  2798. # lfs status
  2799. subparsers.add_parser("status", help="Show status of LFS files")
  2800. args = parser.parse_args(argv)
  2801. if args.subcommand == "init":
  2802. porcelain.lfs_init()
  2803. logger.info("Git LFS initialized.")
  2804. elif args.subcommand == "track":
  2805. if args.patterns:
  2806. tracked = porcelain.lfs_track(patterns=args.patterns)
  2807. logger.info("Tracking patterns:")
  2808. else:
  2809. tracked = porcelain.lfs_track()
  2810. logger.info("Currently tracked patterns:")
  2811. for pattern in tracked:
  2812. logger.info(" %s", pattern)
  2813. elif args.subcommand == "untrack":
  2814. tracked = porcelain.lfs_untrack(patterns=args.patterns)
  2815. logger.info("Remaining tracked patterns:")
  2816. for pattern in tracked:
  2817. logger.info(" %s", to_display_str(pattern))
  2818. elif args.subcommand == "ls-files":
  2819. files = porcelain.lfs_ls_files(ref=args.ref)
  2820. for path, oid, size in files:
  2821. logger.info(
  2822. "%s * %s (%s)",
  2823. to_display_str(oid[:12]),
  2824. to_display_str(path),
  2825. format_bytes(size),
  2826. )
  2827. elif args.subcommand == "migrate":
  2828. count = porcelain.lfs_migrate(
  2829. include=args.include, exclude=args.exclude, everything=args.everything
  2830. )
  2831. logger.info("Migrated %d file(s) to Git LFS.", count)
  2832. elif args.subcommand == "pointer":
  2833. if args.paths is not None:
  2834. results = porcelain.lfs_pointer_check(paths=args.paths or None)
  2835. for file_path, pointer in results.items():
  2836. if pointer:
  2837. logger.info(
  2838. "%s: LFS pointer (oid: %s, size: %s)",
  2839. to_display_str(file_path),
  2840. to_display_str(pointer.oid[:12]),
  2841. format_bytes(pointer.size),
  2842. )
  2843. else:
  2844. logger.warning(
  2845. "%s: Not an LFS pointer", to_display_str(file_path)
  2846. )
  2847. elif args.subcommand == "clean":
  2848. pointer = porcelain.lfs_clean(path=args.path)
  2849. sys.stdout.buffer.write(pointer)
  2850. elif args.subcommand == "smudge":
  2851. if args.stdin:
  2852. pointer_content = sys.stdin.buffer.read()
  2853. content = porcelain.lfs_smudge(pointer_content=pointer_content)
  2854. sys.stdout.buffer.write(content)
  2855. else:
  2856. logger.error("--stdin required for smudge command")
  2857. sys.exit(1)
  2858. elif args.subcommand == "fetch":
  2859. refs = args.refs or None
  2860. count = porcelain.lfs_fetch(remote=args.remote, refs=refs)
  2861. logger.info("Fetched %d LFS object(s).", count)
  2862. elif args.subcommand == "pull":
  2863. count = porcelain.lfs_pull(remote=args.remote)
  2864. logger.info("Pulled %d LFS object(s).", count)
  2865. elif args.subcommand == "push":
  2866. refs = args.refs or None
  2867. count = porcelain.lfs_push(remote=args.remote, refs=refs)
  2868. logger.info("Pushed %d LFS object(s).", count)
  2869. elif args.subcommand == "status":
  2870. status = porcelain.lfs_status()
  2871. if status["tracked"]:
  2872. logger.info("LFS tracked files: %d", len(status["tracked"]))
  2873. if status["missing"]:
  2874. logger.warning("\nMissing LFS objects:")
  2875. for file_path in status["missing"]:
  2876. logger.warning(" %s", to_display_str(file_path))
  2877. if status["not_staged"]:
  2878. logger.info("\nModified LFS files not staged:")
  2879. for file_path in status["not_staged"]:
  2880. logger.warning(" %s", to_display_str(file_path))
  2881. if not any(status.values()):
  2882. logger.info("No LFS files found.")
  2883. else:
  2884. parser.print_help()
  2885. sys.exit(1)
  2886. class cmd_help(Command):
  2887. """Display help information about git."""
  2888. def run(self, args) -> None:
  2889. """Execute the help command.
  2890. Args:
  2891. args: Command line arguments
  2892. """
  2893. parser = argparse.ArgumentParser()
  2894. parser.add_argument(
  2895. "-a",
  2896. "--all",
  2897. action="store_true",
  2898. help="List all commands.",
  2899. )
  2900. args = parser.parse_args(args)
  2901. if args.all:
  2902. logger.info("Available commands:")
  2903. for cmd in sorted(commands):
  2904. logger.info(" %s", cmd)
  2905. else:
  2906. logger.info(
  2907. "The dulwich command line tool is currently a very basic frontend for the\n"
  2908. "Dulwich python module. For full functionality, please see the API reference.\n"
  2909. "\n"
  2910. "For a list of supported commands, see 'dulwich help -a'."
  2911. )
  2912. class cmd_format_patch(Command):
  2913. """Prepare patches for e-mail submission."""
  2914. def run(self, args) -> None:
  2915. """Execute the format-patch command.
  2916. Args:
  2917. args: Command line arguments
  2918. """
  2919. parser = argparse.ArgumentParser()
  2920. parser.add_argument(
  2921. "committish",
  2922. nargs="?",
  2923. help="Commit or commit range (e.g., HEAD~3..HEAD or origin/master..HEAD)",
  2924. )
  2925. parser.add_argument(
  2926. "-n",
  2927. "--numbered",
  2928. type=int,
  2929. default=1,
  2930. help="Number of commits to format (default: 1)",
  2931. )
  2932. parser.add_argument(
  2933. "-o",
  2934. "--output-directory",
  2935. dest="outdir",
  2936. help="Output directory for patches",
  2937. )
  2938. parser.add_argument(
  2939. "--stdout",
  2940. action="store_true",
  2941. help="Output patches to stdout",
  2942. )
  2943. args = parser.parse_args(args)
  2944. # Parse committish using the new function
  2945. committish: Optional[Union[bytes, tuple[bytes, bytes]]] = None
  2946. if args.committish:
  2947. with Repo(".") as r:
  2948. range_result = parse_commit_range(r, args.committish)
  2949. if range_result:
  2950. # Convert Commit objects to their SHAs
  2951. committish = (range_result[0].id, range_result[1].id)
  2952. else:
  2953. committish = (
  2954. args.committish.encode()
  2955. if isinstance(args.committish, str)
  2956. else args.committish
  2957. )
  2958. filenames = porcelain.format_patch(
  2959. ".",
  2960. committish=committish,
  2961. outstream=sys.stdout,
  2962. outdir=args.outdir,
  2963. n=args.numbered,
  2964. stdout=args.stdout,
  2965. )
  2966. if not args.stdout:
  2967. for filename in filenames:
  2968. logger.info(filename)
  2969. class cmd_bundle(Command):
  2970. """Create, unpack, and manipulate bundle files."""
  2971. def run(self, args) -> int:
  2972. """Execute the bundle command.
  2973. Args:
  2974. args: Command line arguments
  2975. """
  2976. if not args:
  2977. logger.error("Usage: bundle <create|verify|list-heads|unbundle> <options>")
  2978. return 1
  2979. subcommand = args[0]
  2980. subargs = args[1:]
  2981. if subcommand == "create":
  2982. return self._create(subargs)
  2983. elif subcommand == "verify":
  2984. return self._verify(subargs)
  2985. elif subcommand == "list-heads":
  2986. return self._list_heads(subargs)
  2987. elif subcommand == "unbundle":
  2988. return self._unbundle(subargs)
  2989. else:
  2990. logger.error("Unknown bundle subcommand: %s", subcommand)
  2991. return 1
  2992. def _create(self, args) -> int:
  2993. parser = argparse.ArgumentParser(prog="bundle create")
  2994. parser.add_argument(
  2995. "-q", "--quiet", action="store_true", help="Suppress progress"
  2996. )
  2997. parser.add_argument("--progress", action="store_true", help="Show progress")
  2998. parser.add_argument(
  2999. "--version", type=int, choices=[2, 3], help="Bundle version"
  3000. )
  3001. parser.add_argument("--all", action="store_true", help="Include all refs")
  3002. parser.add_argument("--stdin", action="store_true", help="Read refs from stdin")
  3003. parser.add_argument("file", help="Output bundle file (use - for stdout)")
  3004. parser.add_argument("refs", nargs="*", help="References or rev-list args")
  3005. parsed_args = parser.parse_args(args)
  3006. repo = Repo(".")
  3007. progress = None
  3008. if parsed_args.progress and not parsed_args.quiet:
  3009. def progress(msg: str) -> None:
  3010. logger.error(msg)
  3011. refs_to_include = []
  3012. prerequisites = []
  3013. if parsed_args.all:
  3014. refs_to_include = list(repo.refs.keys())
  3015. elif parsed_args.stdin:
  3016. for line in sys.stdin:
  3017. ref = line.strip().encode("utf-8")
  3018. if ref:
  3019. refs_to_include.append(ref)
  3020. elif parsed_args.refs:
  3021. for ref_arg in parsed_args.refs:
  3022. if ".." in ref_arg:
  3023. range_result = parse_commit_range(repo, ref_arg)
  3024. if range_result:
  3025. start_commit, _end_commit = range_result
  3026. prerequisites.append(start_commit.id)
  3027. # For ranges like A..B, we need to include B if it's a ref
  3028. # Split the range to get the end part
  3029. end_part = ref_arg.split("..")[1]
  3030. if end_part: # Not empty (not "A..")
  3031. end_ref = end_part.encode("utf-8")
  3032. if end_ref in repo.refs:
  3033. refs_to_include.append(end_ref)
  3034. else:
  3035. sha = repo.refs[ref_arg.encode("utf-8")]
  3036. refs_to_include.append(ref_arg.encode("utf-8"))
  3037. else:
  3038. if ref_arg.startswith("^"):
  3039. sha = repo.refs[ref_arg[1:].encode("utf-8")]
  3040. prerequisites.append(sha)
  3041. else:
  3042. sha = repo.refs[ref_arg.encode("utf-8")]
  3043. refs_to_include.append(ref_arg.encode("utf-8"))
  3044. else:
  3045. logger.error("No refs specified. Use --all, --stdin, or specify refs")
  3046. return 1
  3047. if not refs_to_include:
  3048. logger.error("fatal: Refusing to create empty bundle.")
  3049. return 1
  3050. bundle = create_bundle_from_repo(
  3051. repo,
  3052. refs=refs_to_include,
  3053. prerequisites=prerequisites,
  3054. version=parsed_args.version,
  3055. progress=progress,
  3056. )
  3057. if parsed_args.file == "-":
  3058. write_bundle(sys.stdout.buffer, bundle)
  3059. else:
  3060. with open(parsed_args.file, "wb") as f:
  3061. write_bundle(f, bundle)
  3062. return 0
  3063. def _verify(self, args) -> int:
  3064. parser = argparse.ArgumentParser(prog="bundle verify")
  3065. parser.add_argument(
  3066. "-q", "--quiet", action="store_true", help="Suppress output"
  3067. )
  3068. parser.add_argument("file", help="Bundle file to verify (use - for stdin)")
  3069. parsed_args = parser.parse_args(args)
  3070. repo = Repo(".")
  3071. def verify_bundle(bundle):
  3072. missing_prereqs = []
  3073. for prereq_sha, comment in bundle.prerequisites:
  3074. try:
  3075. repo.object_store[prereq_sha]
  3076. except KeyError:
  3077. missing_prereqs.append(prereq_sha)
  3078. if missing_prereqs:
  3079. if not parsed_args.quiet:
  3080. logger.info("The bundle requires these prerequisite commits:")
  3081. for sha in missing_prereqs:
  3082. logger.info(" %s", sha.decode())
  3083. return 1
  3084. else:
  3085. if not parsed_args.quiet:
  3086. logger.info(
  3087. "The bundle is valid and can be applied to the current repository"
  3088. )
  3089. return 0
  3090. if parsed_args.file == "-":
  3091. bundle = read_bundle(sys.stdin.buffer)
  3092. return verify_bundle(bundle)
  3093. else:
  3094. with open(parsed_args.file, "rb") as f:
  3095. bundle = read_bundle(f)
  3096. return verify_bundle(bundle)
  3097. def _list_heads(self, args) -> int:
  3098. parser = argparse.ArgumentParser(prog="bundle list-heads")
  3099. parser.add_argument("file", help="Bundle file (use - for stdin)")
  3100. parser.add_argument("refnames", nargs="*", help="Only show these refs")
  3101. parsed_args = parser.parse_args(args)
  3102. def list_heads(bundle):
  3103. for ref, sha in bundle.references.items():
  3104. if not parsed_args.refnames or ref.decode() in parsed_args.refnames:
  3105. logger.info("%s %s", sha.decode(), ref.decode())
  3106. if parsed_args.file == "-":
  3107. bundle = read_bundle(sys.stdin.buffer)
  3108. list_heads(bundle)
  3109. else:
  3110. with open(parsed_args.file, "rb") as f:
  3111. bundle = read_bundle(f)
  3112. list_heads(bundle)
  3113. return 0
  3114. def _unbundle(self, args) -> int:
  3115. parser = argparse.ArgumentParser(prog="bundle unbundle")
  3116. parser.add_argument("--progress", action="store_true", help="Show progress")
  3117. parser.add_argument("file", help="Bundle file (use - for stdin)")
  3118. parser.add_argument("refnames", nargs="*", help="Only unbundle these refs")
  3119. parsed_args = parser.parse_args(args)
  3120. repo = Repo(".")
  3121. progress = None
  3122. if parsed_args.progress:
  3123. def progress(msg: str) -> None:
  3124. logger.error(msg)
  3125. if parsed_args.file == "-":
  3126. bundle = read_bundle(sys.stdin.buffer)
  3127. # Process the bundle while file is still available via stdin
  3128. bundle.store_objects(repo.object_store, progress=progress)
  3129. else:
  3130. # Keep the file open during bundle processing
  3131. with open(parsed_args.file, "rb") as f:
  3132. bundle = read_bundle(f)
  3133. # Process pack data while file is still open
  3134. bundle.store_objects(repo.object_store, progress=progress)
  3135. for ref, sha in bundle.references.items():
  3136. if not parsed_args.refnames or ref.decode() in parsed_args.refnames:
  3137. logger.info(ref.decode())
  3138. return 0
  3139. class cmd_worktree_add(Command):
  3140. """Create a new worktree."""
  3141. """Add a new worktree to the repository."""
  3142. def run(self, args) -> Optional[int]:
  3143. """Execute the worktree-add command.
  3144. Args:
  3145. args: Command line arguments
  3146. """
  3147. parser = argparse.ArgumentParser(
  3148. description="Add a new worktree", prog="dulwich worktree add"
  3149. )
  3150. parser.add_argument("path", help="Path for the new worktree")
  3151. parser.add_argument("committish", nargs="?", help="Commit-ish to checkout")
  3152. parser.add_argument("-b", "--create-branch", help="Create a new branch")
  3153. parser.add_argument(
  3154. "-B", "--force-create-branch", help="Create or reset a branch"
  3155. )
  3156. parser.add_argument(
  3157. "--detach", action="store_true", help="Detach HEAD in new worktree"
  3158. )
  3159. parser.add_argument("--force", action="store_true", help="Force creation")
  3160. parsed_args = parser.parse_args(args)
  3161. from dulwich import porcelain
  3162. branch = None
  3163. commit = None
  3164. if parsed_args.create_branch or parsed_args.force_create_branch:
  3165. branch = (
  3166. parsed_args.create_branch or parsed_args.force_create_branch
  3167. ).encode()
  3168. elif parsed_args.committish and not parsed_args.detach:
  3169. # If committish is provided and not detaching, treat as branch
  3170. branch = parsed_args.committish.encode()
  3171. elif parsed_args.committish:
  3172. # If committish is provided and detaching, treat as commit
  3173. commit = parsed_args.committish.encode()
  3174. worktree_path = porcelain.worktree_add(
  3175. repo=".",
  3176. path=parsed_args.path,
  3177. branch=branch,
  3178. commit=commit,
  3179. detach=parsed_args.detach,
  3180. force=parsed_args.force or bool(parsed_args.force_create_branch),
  3181. )
  3182. logger.info("Worktree added: %s", worktree_path)
  3183. return 0
  3184. class cmd_worktree_list(Command):
  3185. """List worktrees."""
  3186. """List details of each worktree."""
  3187. def run(self, args) -> Optional[int]:
  3188. """Execute the worktree-list command.
  3189. Args:
  3190. args: Command line arguments
  3191. """
  3192. parser = argparse.ArgumentParser(
  3193. description="List worktrees", prog="dulwich worktree list"
  3194. )
  3195. parser.add_argument(
  3196. "-v", "--verbose", action="store_true", help="Show additional information"
  3197. )
  3198. parser.add_argument(
  3199. "--porcelain", action="store_true", help="Machine-readable output"
  3200. )
  3201. parsed_args = parser.parse_args(args)
  3202. from dulwich import porcelain
  3203. worktrees = porcelain.worktree_list(repo=".")
  3204. for wt in worktrees:
  3205. path = wt.path
  3206. if wt.bare:
  3207. status = "(bare)"
  3208. elif wt.detached:
  3209. status = (
  3210. f"(detached HEAD {wt.head[:7].decode() if wt.head else 'unknown'})"
  3211. )
  3212. elif wt.branch:
  3213. branch_name = wt.branch.decode().replace("refs/heads/", "")
  3214. status = f"[{branch_name}]"
  3215. else:
  3216. status = "(unknown)"
  3217. if parsed_args.porcelain:
  3218. locked = "locked" if wt.locked else "unlocked"
  3219. prunable = "prunable" if wt.prunable else "unprunable"
  3220. logger.info(
  3221. "%s %s %s %s %s",
  3222. path,
  3223. wt.head.decode() if wt.head else "unknown",
  3224. status,
  3225. locked,
  3226. prunable,
  3227. )
  3228. else:
  3229. line = f"{path} {status}"
  3230. if wt.locked:
  3231. line += " locked"
  3232. if wt.prunable:
  3233. line += " prunable"
  3234. logger.info(line)
  3235. return 0
  3236. class cmd_worktree_remove(Command):
  3237. """Remove a worktree."""
  3238. """Remove a worktree."""
  3239. def run(self, args) -> Optional[int]:
  3240. """Execute the worktree-remove command.
  3241. Args:
  3242. args: Command line arguments
  3243. """
  3244. parser = argparse.ArgumentParser(
  3245. description="Remove a worktree", prog="dulwich worktree remove"
  3246. )
  3247. parser.add_argument("worktree", help="Path to worktree to remove")
  3248. parser.add_argument("--force", action="store_true", help="Force removal")
  3249. parsed_args = parser.parse_args(args)
  3250. from dulwich import porcelain
  3251. porcelain.worktree_remove(
  3252. repo=".", path=parsed_args.worktree, force=parsed_args.force
  3253. )
  3254. logger.info("Worktree removed: %s", parsed_args.worktree)
  3255. return 0
  3256. class cmd_worktree_prune(Command):
  3257. """Prune worktree information."""
  3258. """Prune worktree information."""
  3259. def run(self, args) -> Optional[int]:
  3260. """Execute the worktree-prune command.
  3261. Args:
  3262. args: Command line arguments
  3263. """
  3264. parser = argparse.ArgumentParser(
  3265. description="Prune worktree information", prog="dulwich worktree prune"
  3266. )
  3267. parser.add_argument(
  3268. "--dry-run", action="store_true", help="Do not remove anything"
  3269. )
  3270. parser.add_argument(
  3271. "-v", "--verbose", action="store_true", help="Report all removals"
  3272. )
  3273. parser.add_argument(
  3274. "--expire", type=int, help="Expire worktrees older than time (seconds)"
  3275. )
  3276. parsed_args = parser.parse_args(args)
  3277. from dulwich import porcelain
  3278. pruned = porcelain.worktree_prune(
  3279. repo=".", dry_run=parsed_args.dry_run, expire=parsed_args.expire
  3280. )
  3281. if pruned:
  3282. if parsed_args.dry_run:
  3283. logger.info("Would prune worktrees:")
  3284. elif parsed_args.verbose:
  3285. logger.info("Pruned worktrees:")
  3286. for wt_id in pruned:
  3287. logger.info(" %s", wt_id)
  3288. elif parsed_args.verbose:
  3289. logger.info("No worktrees to prune")
  3290. return 0
  3291. class cmd_worktree_lock(Command):
  3292. """Lock a worktree to prevent it from being pruned."""
  3293. """Lock a worktree."""
  3294. def run(self, args) -> Optional[int]:
  3295. """Execute the worktree-lock command.
  3296. Args:
  3297. args: Command line arguments
  3298. """
  3299. parser = argparse.ArgumentParser(
  3300. description="Lock a worktree", prog="dulwich worktree lock"
  3301. )
  3302. parser.add_argument("worktree", help="Path to worktree to lock")
  3303. parser.add_argument("--reason", help="Reason for locking")
  3304. parsed_args = parser.parse_args(args)
  3305. from dulwich import porcelain
  3306. porcelain.worktree_lock(
  3307. repo=".", path=parsed_args.worktree, reason=parsed_args.reason
  3308. )
  3309. logger.info("Worktree locked: %s", parsed_args.worktree)
  3310. return 0
  3311. class cmd_worktree_unlock(Command):
  3312. """Unlock a locked worktree."""
  3313. """Unlock a worktree."""
  3314. def run(self, args) -> Optional[int]:
  3315. """Execute the worktree-unlock command.
  3316. Args:
  3317. args: Command line arguments
  3318. """
  3319. parser = argparse.ArgumentParser(
  3320. description="Unlock a worktree", prog="dulwich worktree unlock"
  3321. )
  3322. parser.add_argument("worktree", help="Path to worktree to unlock")
  3323. parsed_args = parser.parse_args(args)
  3324. from dulwich import porcelain
  3325. porcelain.worktree_unlock(repo=".", path=parsed_args.worktree)
  3326. logger.info("Worktree unlocked: %s", parsed_args.worktree)
  3327. return 0
  3328. class cmd_worktree_move(Command):
  3329. """Move a worktree to a new location."""
  3330. """Move a worktree."""
  3331. def run(self, args) -> Optional[int]:
  3332. """Execute the worktree-move command.
  3333. Args:
  3334. args: Command line arguments
  3335. """
  3336. parser = argparse.ArgumentParser(
  3337. description="Move a worktree", prog="dulwich worktree move"
  3338. )
  3339. parser.add_argument("worktree", help="Path to worktree to move")
  3340. parser.add_argument("new_path", help="New path for the worktree")
  3341. parsed_args = parser.parse_args(args)
  3342. from dulwich import porcelain
  3343. porcelain.worktree_move(
  3344. repo=".", old_path=parsed_args.worktree, new_path=parsed_args.new_path
  3345. )
  3346. logger.info(
  3347. "Worktree moved: %s -> %s", parsed_args.worktree, parsed_args.new_path
  3348. )
  3349. return 0
  3350. class cmd_worktree(SuperCommand):
  3351. """Manage multiple working trees."""
  3352. """Manage multiple working trees."""
  3353. subcommands: ClassVar[dict[str, type[Command]]] = {
  3354. "add": cmd_worktree_add,
  3355. "list": cmd_worktree_list,
  3356. "remove": cmd_worktree_remove,
  3357. "prune": cmd_worktree_prune,
  3358. "lock": cmd_worktree_lock,
  3359. "unlock": cmd_worktree_unlock,
  3360. "move": cmd_worktree_move,
  3361. }
  3362. default_command = cmd_worktree_list
  3363. commands = {
  3364. "add": cmd_add,
  3365. "annotate": cmd_annotate,
  3366. "archive": cmd_archive,
  3367. "bisect": cmd_bisect,
  3368. "blame": cmd_blame,
  3369. "branch": cmd_branch,
  3370. "bundle": cmd_bundle,
  3371. "check-ignore": cmd_check_ignore,
  3372. "check-mailmap": cmd_check_mailmap,
  3373. "checkout": cmd_checkout,
  3374. "cherry-pick": cmd_cherry_pick,
  3375. "clone": cmd_clone,
  3376. "commit": cmd_commit,
  3377. "commit-tree": cmd_commit_tree,
  3378. "count-objects": cmd_count_objects,
  3379. "describe": cmd_describe,
  3380. "daemon": cmd_daemon,
  3381. "diff": cmd_diff,
  3382. "diff-tree": cmd_diff_tree,
  3383. "dump-pack": cmd_dump_pack,
  3384. "dump-index": cmd_dump_index,
  3385. "fetch-pack": cmd_fetch_pack,
  3386. "fetch": cmd_fetch,
  3387. "filter-branch": cmd_filter_branch,
  3388. "for-each-ref": cmd_for_each_ref,
  3389. "format-patch": cmd_format_patch,
  3390. "fsck": cmd_fsck,
  3391. "gc": cmd_gc,
  3392. "help": cmd_help,
  3393. "init": cmd_init,
  3394. "lfs": cmd_lfs,
  3395. "log": cmd_log,
  3396. "ls-files": cmd_ls_files,
  3397. "ls-remote": cmd_ls_remote,
  3398. "ls-tree": cmd_ls_tree,
  3399. "merge": cmd_merge,
  3400. "merge-tree": cmd_merge_tree,
  3401. "notes": cmd_notes,
  3402. "pack-objects": cmd_pack_objects,
  3403. "pack-refs": cmd_pack_refs,
  3404. "prune": cmd_prune,
  3405. "pull": cmd_pull,
  3406. "push": cmd_push,
  3407. "rebase": cmd_rebase,
  3408. "receive-pack": cmd_receive_pack,
  3409. "reflog": cmd_reflog,
  3410. "remote": cmd_remote,
  3411. "repack": cmd_repack,
  3412. "reset": cmd_reset,
  3413. "revert": cmd_revert,
  3414. "rev-list": cmd_rev_list,
  3415. "rm": cmd_rm,
  3416. "mv": cmd_mv,
  3417. "show": cmd_show,
  3418. "stash": cmd_stash,
  3419. "status": cmd_status,
  3420. "shortlog": cmd_shortlog,
  3421. "symbolic-ref": cmd_symbolic_ref,
  3422. "submodule": cmd_submodule,
  3423. "tag": cmd_tag,
  3424. "unpack-objects": cmd_unpack_objects,
  3425. "update-server-info": cmd_update_server_info,
  3426. "upload-pack": cmd_upload_pack,
  3427. "web-daemon": cmd_web_daemon,
  3428. "worktree": cmd_worktree,
  3429. "write-tree": cmd_write_tree,
  3430. }
  3431. def main(argv=None) -> Optional[int]:
  3432. """Main entry point for the Dulwich CLI.
  3433. Args:
  3434. argv: Command line arguments (defaults to sys.argv[1:])
  3435. Returns:
  3436. Exit code or None
  3437. """
  3438. if argv is None:
  3439. argv = sys.argv[1:]
  3440. # Parse only the global options and command, stop at first positional
  3441. parser = argparse.ArgumentParser(
  3442. prog="dulwich",
  3443. description="Simple command-line interface to Dulwich",
  3444. add_help=False, # We'll handle help ourselves
  3445. )
  3446. parser.add_argument("--no-pager", action="store_true", help="Disable pager")
  3447. parser.add_argument("--pager", action="store_true", help="Force enable pager")
  3448. parser.add_argument("--help", "-h", action="store_true", help="Show help")
  3449. # Parse known args to separate global options from command args
  3450. global_args, remaining = parser.parse_known_args(argv)
  3451. # Apply global pager settings
  3452. if global_args.no_pager:
  3453. disable_pager()
  3454. elif global_args.pager:
  3455. enable_pager()
  3456. # Handle help
  3457. if global_args.help or not remaining:
  3458. parser = argparse.ArgumentParser(
  3459. prog="dulwich", description="Simple command-line interface to Dulwich"
  3460. )
  3461. parser.add_argument("--no-pager", action="store_true", help="Disable pager")
  3462. parser.add_argument("--pager", action="store_true", help="Force enable pager")
  3463. parser.add_argument(
  3464. "command",
  3465. nargs="?",
  3466. help=f"Command to run. Available: {', '.join(sorted(commands.keys()))}",
  3467. )
  3468. parser.print_help()
  3469. return 1
  3470. logging.basicConfig(
  3471. level=logging.INFO,
  3472. format="%(message)s",
  3473. )
  3474. # First remaining arg is the command
  3475. cmd = remaining[0]
  3476. cmd_args = remaining[1:]
  3477. try:
  3478. cmd_kls = commands[cmd]
  3479. except KeyError:
  3480. logging.fatal("No such subcommand: %s", cmd)
  3481. return 1
  3482. # TODO(jelmer): Return non-0 on errors
  3483. return cmd_kls().run(cmd_args)
  3484. def _main() -> None:
  3485. if "DULWICH_PDB" in os.environ and getattr(signal, "SIGQUIT", None):
  3486. signal.signal(signal.SIGQUIT, signal_quit) # type: ignore
  3487. signal.signal(signal.SIGINT, signal_int)
  3488. sys.exit(main())
  3489. if __name__ == "__main__":
  3490. _main()