cli.py 109 KB

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