porcelain.py 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  1. # porcelain.py -- Porcelain-like layer on top of Dulwich
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as public by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Simple wrapper that provides porcelain-like functions on top of Dulwich.
  22. Currently implemented:
  23. * archive
  24. * add
  25. * branch{_create,_delete,_list}
  26. * check_ignore
  27. * checkout
  28. * checkout_branch
  29. * clone
  30. * cone mode{_init, _set, _add}
  31. * commit
  32. * commit_tree
  33. * daemon
  34. * describe
  35. * diff_tree
  36. * fetch
  37. * for_each_ref
  38. * init
  39. * ls_files
  40. * ls_remote
  41. * ls_tree
  42. * merge
  43. * pull
  44. * push
  45. * rm
  46. * remote{_add}
  47. * receive_pack
  48. * reset
  49. * sparse_checkout
  50. * submodule_add
  51. * submodule_init
  52. * submodule_list
  53. * rev_list
  54. * tag{_create,_delete,_list}
  55. * upload_pack
  56. * update_server_info
  57. * status
  58. * symbolic_ref
  59. These functions are meant to behave similarly to the git subcommands.
  60. Differences in behaviour are considered bugs.
  61. Note: one of the consequences of this is that paths tend to be
  62. interpreted relative to the current working directory rather than relative
  63. to the repository root.
  64. Functions should generally accept both unicode strings and bytestrings
  65. """
  66. import datetime
  67. import fnmatch
  68. import os
  69. import posixpath
  70. import stat
  71. import sys
  72. import time
  73. from collections import namedtuple
  74. from contextlib import closing, contextmanager
  75. from io import BytesIO, RawIOBase
  76. from pathlib import Path
  77. from typing import Optional, Union
  78. from .archive import tar_stream
  79. from .client import get_transport_and_path
  80. from .config import Config, ConfigFile, StackedConfig, read_submodules
  81. from .diff_tree import (
  82. CHANGE_ADD,
  83. CHANGE_COPY,
  84. CHANGE_DELETE,
  85. CHANGE_MODIFY,
  86. CHANGE_RENAME,
  87. RENAME_CHANGE_TYPES,
  88. )
  89. from .errors import SendPackError
  90. from .graph import can_fast_forward
  91. from .ignore import IgnoreFilterManager
  92. from .index import (
  93. _fs_to_tree_path,
  94. blob_from_path_and_stat,
  95. build_file_from_blob,
  96. get_unstaged_changes,
  97. update_working_tree,
  98. )
  99. from .object_store import tree_lookup_path
  100. from .objects import (
  101. Commit,
  102. Tag,
  103. format_timezone,
  104. parse_timezone,
  105. pretty_format_tree_entry,
  106. )
  107. from .objectspec import (
  108. parse_commit,
  109. parse_object,
  110. parse_ref,
  111. parse_reftuples,
  112. parse_tree,
  113. )
  114. from .pack import write_pack_from_container, write_pack_index
  115. from .patch import write_tree_diff
  116. from .protocol import ZERO_SHA, Protocol
  117. from .refs import (
  118. LOCAL_BRANCH_PREFIX,
  119. LOCAL_TAG_PREFIX,
  120. Ref,
  121. _import_remote_refs,
  122. )
  123. from .repo import BaseRepo, Repo, get_user_identity
  124. from .server import (
  125. FileSystemBackend,
  126. ReceivePackHandler,
  127. TCPGitServer,
  128. UploadPackHandler,
  129. )
  130. from .server import update_server_info as server_update_server_info
  131. from .sparse_patterns import (
  132. SparseCheckoutConflictError,
  133. apply_included_paths,
  134. determine_included_paths,
  135. )
  136. # Module level tuple definition for status output
  137. GitStatus = namedtuple("GitStatus", "staged unstaged untracked")
  138. class NoneStream(RawIOBase):
  139. """Fallback if stdout or stderr are unavailable, does nothing."""
  140. def read(self, size=-1) -> None:
  141. return None
  142. def readall(self) -> bytes:
  143. return b""
  144. def readinto(self, b) -> None:
  145. return None
  146. def write(self, b) -> None:
  147. return None
  148. default_bytes_out_stream = getattr(sys.stdout, "buffer", None) or NoneStream()
  149. default_bytes_err_stream = getattr(sys.stderr, "buffer", None) or NoneStream()
  150. DEFAULT_ENCODING = "utf-8"
  151. class Error(Exception):
  152. """Porcelain-based error."""
  153. def __init__(self, msg) -> None:
  154. super().__init__(msg)
  155. class RemoteExists(Error):
  156. """Raised when the remote already exists."""
  157. class TimezoneFormatError(Error):
  158. """Raised when the timezone cannot be determined from a given string."""
  159. class CheckoutError(Error):
  160. """Indicates that a checkout cannot be performed."""
  161. def parse_timezone_format(tz_str):
  162. """Parse given string and attempt to return a timezone offset.
  163. Different formats are considered in the following order:
  164. - Git internal format: <unix timestamp> <timezone offset>
  165. - RFC 2822: e.g. Mon, 20 Nov 1995 19:12:08 -0500
  166. - ISO 8601: e.g. 1995-11-20T19:12:08-0500
  167. Args:
  168. tz_str: datetime string
  169. Returns: Timezone offset as integer
  170. Raises:
  171. TimezoneFormatError: if timezone information cannot be extracted
  172. """
  173. import re
  174. # Git internal format
  175. internal_format_pattern = re.compile("^[0-9]+ [+-][0-9]{,4}$")
  176. if re.match(internal_format_pattern, tz_str):
  177. try:
  178. tz_internal = parse_timezone(tz_str.split(" ")[1].encode(DEFAULT_ENCODING))
  179. return tz_internal[0]
  180. except ValueError:
  181. pass
  182. # RFC 2822
  183. import email.utils
  184. rfc_2822 = email.utils.parsedate_tz(tz_str)
  185. if rfc_2822:
  186. return rfc_2822[9]
  187. # ISO 8601
  188. # Supported offsets:
  189. # sHHMM, sHH:MM, sHH
  190. iso_8601_pattern = re.compile(
  191. "[0-9] ?([+-])([0-9]{2})(?::(?=[0-9]{2}))?([0-9]{2})?$"
  192. )
  193. match = re.search(iso_8601_pattern, tz_str)
  194. total_secs = 0
  195. if match:
  196. sign, hours, minutes = match.groups()
  197. total_secs += int(hours) * 3600
  198. if minutes:
  199. total_secs += int(minutes) * 60
  200. total_secs = -total_secs if sign == "-" else total_secs
  201. return total_secs
  202. # YYYY.MM.DD, MM/DD/YYYY, DD.MM.YYYY contain no timezone information
  203. raise TimezoneFormatError(tz_str)
  204. def get_user_timezones():
  205. """Retrieve local timezone as described in
  206. https://raw.githubusercontent.com/git/git/v2.3.0/Documentation/date-formats.txt
  207. Returns: A tuple containing author timezone, committer timezone.
  208. """
  209. local_timezone = time.localtime().tm_gmtoff
  210. if os.environ.get("GIT_AUTHOR_DATE"):
  211. author_timezone = parse_timezone_format(os.environ["GIT_AUTHOR_DATE"])
  212. else:
  213. author_timezone = local_timezone
  214. if os.environ.get("GIT_COMMITTER_DATE"):
  215. commit_timezone = parse_timezone_format(os.environ["GIT_COMMITTER_DATE"])
  216. else:
  217. commit_timezone = local_timezone
  218. return author_timezone, commit_timezone
  219. def open_repo(path_or_repo):
  220. """Open an argument that can be a repository or a path for a repository."""
  221. if isinstance(path_or_repo, BaseRepo):
  222. return path_or_repo
  223. return Repo(path_or_repo)
  224. @contextmanager
  225. def _noop_context_manager(obj):
  226. """Context manager that has the same api as closing but does nothing."""
  227. yield obj
  228. def open_repo_closing(path_or_repo):
  229. """Open an argument that can be a repository or a path for a repository.
  230. returns a context manager that will close the repo on exit if the argument
  231. is a path, else does nothing if the argument is a repo.
  232. """
  233. if isinstance(path_or_repo, BaseRepo):
  234. return _noop_context_manager(path_or_repo)
  235. return closing(Repo(path_or_repo))
  236. def path_to_tree_path(repopath, path, tree_encoding=DEFAULT_ENCODING):
  237. """Convert a path to a path usable in an index, e.g. bytes and relative to
  238. the repository root.
  239. Args:
  240. repopath: Repository path, absolute or relative to the cwd
  241. path: A path, absolute or relative to the cwd
  242. Returns: A path formatted for use in e.g. an index
  243. """
  244. # Resolve might returns a relative path on Windows
  245. # https://bugs.python.org/issue38671
  246. if sys.platform == "win32":
  247. path = os.path.abspath(path)
  248. path = Path(path)
  249. resolved_path = path.resolve()
  250. # Resolve and abspath seems to behave differently regarding symlinks,
  251. # as we are doing abspath on the file path, we need to do the same on
  252. # the repo path or they might not match
  253. if sys.platform == "win32":
  254. repopath = os.path.abspath(repopath)
  255. repopath = Path(repopath).resolve()
  256. try:
  257. relpath = resolved_path.relative_to(repopath)
  258. except ValueError:
  259. # If path is a symlink that points to a file outside the repo, we
  260. # want the relpath for the link itself, not the resolved target
  261. if path.is_symlink():
  262. parent = path.parent.resolve()
  263. relpath = (parent / path.name).relative_to(repopath)
  264. else:
  265. raise
  266. if sys.platform == "win32":
  267. return str(relpath).replace(os.path.sep, "/").encode(tree_encoding)
  268. else:
  269. return bytes(relpath)
  270. class DivergedBranches(Error):
  271. """Branches have diverged and fast-forward is not possible."""
  272. def __init__(self, current_sha, new_sha) -> None:
  273. self.current_sha = current_sha
  274. self.new_sha = new_sha
  275. def check_diverged(repo, current_sha, new_sha) -> None:
  276. """Check if updating to a sha can be done with fast forwarding.
  277. Args:
  278. repo: Repository object
  279. current_sha: Current head sha
  280. new_sha: New head sha
  281. """
  282. try:
  283. can = can_fast_forward(repo, current_sha, new_sha)
  284. except KeyError:
  285. can = False
  286. if not can:
  287. raise DivergedBranches(current_sha, new_sha)
  288. def archive(
  289. repo,
  290. committish=None,
  291. outstream=default_bytes_out_stream,
  292. errstream=default_bytes_err_stream,
  293. ) -> None:
  294. """Create an archive.
  295. Args:
  296. repo: Path of repository for which to generate an archive.
  297. committish: Commit SHA1 or ref to use
  298. outstream: Output stream (defaults to stdout)
  299. errstream: Error stream (defaults to stderr)
  300. """
  301. if committish is None:
  302. committish = "HEAD"
  303. with open_repo_closing(repo) as repo_obj:
  304. c = parse_commit(repo_obj, committish)
  305. for chunk in tar_stream(
  306. repo_obj.object_store, repo_obj.object_store[c.tree], c.commit_time
  307. ):
  308. outstream.write(chunk)
  309. def update_server_info(repo=".") -> None:
  310. """Update server info files for a repository.
  311. Args:
  312. repo: path to the repository
  313. """
  314. with open_repo_closing(repo) as r:
  315. server_update_server_info(r)
  316. def symbolic_ref(repo, ref_name, force=False) -> None:
  317. """Set git symbolic ref into HEAD.
  318. Args:
  319. repo: path to the repository
  320. ref_name: short name of the new ref
  321. force: force settings without checking if it exists in refs/heads
  322. """
  323. with open_repo_closing(repo) as repo_obj:
  324. ref_path = _make_branch_ref(ref_name)
  325. if not force and ref_path not in repo_obj.refs.keys():
  326. raise Error(f"fatal: ref `{ref_name}` is not a ref")
  327. repo_obj.refs.set_symbolic_ref(b"HEAD", ref_path)
  328. def pack_refs(repo, all=False) -> None:
  329. with open_repo_closing(repo) as repo_obj:
  330. refs = repo_obj.refs
  331. packed_refs = {
  332. ref: refs[ref]
  333. for ref in refs
  334. if (all or ref.startswith(LOCAL_TAG_PREFIX)) and ref != b"HEAD"
  335. }
  336. refs.add_packed_refs(packed_refs)
  337. def commit(
  338. repo=".",
  339. message=None,
  340. author=None,
  341. author_timezone=None,
  342. committer=None,
  343. commit_timezone=None,
  344. encoding=None,
  345. no_verify=False,
  346. signoff=False,
  347. ):
  348. """Create a new commit.
  349. Args:
  350. repo: Path to repository
  351. message: Optional commit message
  352. author: Optional author name and email
  353. author_timezone: Author timestamp timezone
  354. committer: Optional committer name and email
  355. commit_timezone: Commit timestamp timezone
  356. no_verify: Skip pre-commit and commit-msg hooks
  357. signoff: GPG Sign the commit (bool, defaults to False,
  358. pass True to use default GPG key,
  359. pass a str containing Key ID to use a specific GPG key)
  360. Returns: SHA1 of the new commit
  361. """
  362. # FIXME: Support --all argument
  363. if getattr(message, "encode", None):
  364. message = message.encode(encoding or DEFAULT_ENCODING)
  365. if getattr(author, "encode", None):
  366. author = author.encode(encoding or DEFAULT_ENCODING)
  367. if getattr(committer, "encode", None):
  368. committer = committer.encode(encoding or DEFAULT_ENCODING)
  369. local_timezone = get_user_timezones()
  370. if author_timezone is None:
  371. author_timezone = local_timezone[0]
  372. if commit_timezone is None:
  373. commit_timezone = local_timezone[1]
  374. with open_repo_closing(repo) as r:
  375. return r.do_commit(
  376. message=message,
  377. author=author,
  378. author_timezone=author_timezone,
  379. committer=committer,
  380. commit_timezone=commit_timezone,
  381. encoding=encoding,
  382. no_verify=no_verify,
  383. sign=signoff if isinstance(signoff, (str, bool)) else None,
  384. )
  385. def commit_tree(repo, tree, message=None, author=None, committer=None):
  386. """Create a new commit object.
  387. Args:
  388. repo: Path to repository
  389. tree: An existing tree object
  390. author: Optional author name and email
  391. committer: Optional committer name and email
  392. """
  393. with open_repo_closing(repo) as r:
  394. return r.do_commit(
  395. message=message, tree=tree, committer=committer, author=author
  396. )
  397. def init(path=".", *, bare=False, symlinks: Optional[bool] = None):
  398. """Create a new git repository.
  399. Args:
  400. path: Path to repository.
  401. bare: Whether to create a bare repository.
  402. symlinks: Whether to create actual symlinks (defaults to autodetect)
  403. Returns: A Repo instance
  404. """
  405. if not os.path.exists(path):
  406. os.mkdir(path)
  407. if bare:
  408. return Repo.init_bare(path)
  409. else:
  410. return Repo.init(path, symlinks=symlinks)
  411. def clone(
  412. source,
  413. target=None,
  414. bare=False,
  415. checkout=None,
  416. errstream=default_bytes_err_stream,
  417. outstream=None,
  418. origin: Optional[str] = "origin",
  419. depth: Optional[int] = None,
  420. branch: Optional[Union[str, bytes]] = None,
  421. config: Optional[Config] = None,
  422. filter_spec=None,
  423. protocol_version: Optional[int] = None,
  424. **kwargs,
  425. ):
  426. """Clone a local or remote git repository.
  427. Args:
  428. source: Path or URL for source repository
  429. target: Path to target repository (optional)
  430. bare: Whether or not to create a bare repository
  431. checkout: Whether or not to check-out HEAD after cloning
  432. errstream: Optional stream to write progress to
  433. outstream: Optional stream to write progress to (deprecated)
  434. origin: Name of remote from the repository used to clone
  435. depth: Depth to fetch at
  436. branch: Optional branch or tag to be used as HEAD in the new repository
  437. instead of the cloned repository's HEAD.
  438. config: Configuration to use
  439. refspecs: refspecs to fetch. Can be a bytestring, a string, or a list of
  440. bytestring/string.
  441. filter_spec: A git-rev-list-style object filter spec, as an ASCII string.
  442. Only used if the server supports the Git protocol-v2 'filter'
  443. feature, and ignored otherwise.
  444. protocol_version: desired Git protocol version. By default the highest
  445. mutually supported protocol version will be used.
  446. Returns: The new repository
  447. """
  448. if outstream is not None:
  449. import warnings
  450. warnings.warn(
  451. "outstream= has been deprecated in favour of errstream=.",
  452. DeprecationWarning,
  453. stacklevel=3,
  454. )
  455. # TODO(jelmer): Capture logging output and stream to errstream
  456. if config is None:
  457. config = StackedConfig.default()
  458. if checkout is None:
  459. checkout = not bare
  460. if checkout and bare:
  461. raise Error("checkout and bare are incompatible")
  462. if target is None:
  463. target = source.split("/")[-1]
  464. if isinstance(branch, str):
  465. branch = branch.encode(DEFAULT_ENCODING)
  466. mkdir = not os.path.exists(target)
  467. (client, path) = get_transport_and_path(source, config=config, **kwargs)
  468. if filter_spec:
  469. filter_spec = filter_spec.encode("ascii")
  470. return client.clone(
  471. path,
  472. target,
  473. mkdir=mkdir,
  474. bare=bare,
  475. origin=origin,
  476. checkout=checkout,
  477. branch=branch,
  478. progress=errstream.write,
  479. depth=depth,
  480. filter_spec=filter_spec,
  481. protocol_version=protocol_version,
  482. )
  483. def add(repo=".", paths=None):
  484. """Add files to the staging area.
  485. Args:
  486. repo: Repository for the files
  487. paths: Paths to add. No value passed stages all modified files.
  488. Returns: Tuple with set of added files and ignored files
  489. If the repository contains ignored directories, the returned set will
  490. contain the path to an ignored directory (with trailing slash). Individual
  491. files within ignored directories will not be returned.
  492. """
  493. ignored = set()
  494. with open_repo_closing(repo) as r:
  495. repo_path = Path(r.path).resolve()
  496. ignore_manager = IgnoreFilterManager.from_repo(r)
  497. if not paths:
  498. paths = list(
  499. get_untracked_paths(
  500. str(Path(os.getcwd()).resolve()),
  501. str(repo_path),
  502. r.open_index(),
  503. )
  504. )
  505. relpaths = []
  506. if not isinstance(paths, list):
  507. paths = [paths]
  508. for p in paths:
  509. path = Path(p)
  510. relpath = str(path.resolve().relative_to(repo_path))
  511. # FIXME: Support patterns
  512. if path.is_dir():
  513. relpath = os.path.join(relpath, "")
  514. if ignore_manager.is_ignored(relpath):
  515. ignored.add(relpath)
  516. continue
  517. relpaths.append(relpath)
  518. r.stage(relpaths)
  519. return (relpaths, ignored)
  520. def _is_subdir(subdir, parentdir):
  521. """Check whether subdir is parentdir or a subdir of parentdir.
  522. If parentdir or subdir is a relative path, it will be disamgibuated
  523. relative to the pwd.
  524. """
  525. parentdir_abs = os.path.realpath(parentdir) + os.path.sep
  526. subdir_abs = os.path.realpath(subdir) + os.path.sep
  527. return subdir_abs.startswith(parentdir_abs)
  528. # TODO: option to remove ignored files also, in line with `git clean -fdx`
  529. def clean(repo=".", target_dir=None) -> None:
  530. """Remove any untracked files from the target directory recursively.
  531. Equivalent to running ``git clean -fd`` in target_dir.
  532. Args:
  533. repo: Repository where the files may be tracked
  534. target_dir: Directory to clean - current directory if None
  535. """
  536. if target_dir is None:
  537. target_dir = os.getcwd()
  538. with open_repo_closing(repo) as r:
  539. if not _is_subdir(target_dir, r.path):
  540. raise Error("target_dir must be in the repo's working dir")
  541. config = r.get_config_stack()
  542. config.get_boolean((b"clean",), b"requireForce", True)
  543. # TODO(jelmer): if require_force is set, then make sure that -f, -i or
  544. # -n is specified.
  545. index = r.open_index()
  546. ignore_manager = IgnoreFilterManager.from_repo(r)
  547. paths_in_wd = _walk_working_dir_paths(target_dir, r.path)
  548. # Reverse file visit order, so that files and subdirectories are
  549. # removed before containing directory
  550. for ap, is_dir in reversed(list(paths_in_wd)):
  551. if is_dir:
  552. # All subdirectories and files have been removed if untracked,
  553. # so dir contains no tracked files iff it is empty.
  554. is_empty = len(os.listdir(ap)) == 0
  555. if is_empty:
  556. os.rmdir(ap)
  557. else:
  558. ip = path_to_tree_path(r.path, ap)
  559. is_tracked = ip in index
  560. rp = os.path.relpath(ap, r.path)
  561. is_ignored = ignore_manager.is_ignored(rp)
  562. if not is_tracked and not is_ignored:
  563. os.remove(ap)
  564. def remove(repo=".", paths=None, cached=False) -> None:
  565. """Remove files from the staging area.
  566. Args:
  567. repo: Repository for the files
  568. paths: Paths to remove
  569. """
  570. with open_repo_closing(repo) as r:
  571. index = r.open_index()
  572. for p in paths:
  573. full_path = os.fsencode(os.path.abspath(p))
  574. tree_path = path_to_tree_path(r.path, p)
  575. try:
  576. index_sha = index[tree_path].sha
  577. except KeyError as exc:
  578. raise Error(f"{p} did not match any files") from exc
  579. if not cached:
  580. try:
  581. st = os.lstat(full_path)
  582. except OSError:
  583. pass
  584. else:
  585. try:
  586. blob = blob_from_path_and_stat(full_path, st)
  587. except OSError:
  588. pass
  589. else:
  590. try:
  591. committed_sha = tree_lookup_path(
  592. r.__getitem__, r[r.head()].tree, tree_path
  593. )[1]
  594. except KeyError:
  595. committed_sha = None
  596. if blob.id != index_sha and index_sha != committed_sha:
  597. raise Error(
  598. "file has staged content differing "
  599. f"from both the file and head: {p}"
  600. )
  601. if index_sha != committed_sha:
  602. raise Error(f"file has staged changes: {p}")
  603. os.remove(full_path)
  604. del index[tree_path]
  605. index.write()
  606. rm = remove
  607. def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
  608. if commit.encoding:
  609. encoding = commit.encoding.decode("ascii")
  610. else:
  611. encoding = default_encoding
  612. return contents.decode(encoding, "replace")
  613. def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
  614. if commit.encoding:
  615. encoding = commit.encoding.decode("ascii")
  616. else:
  617. encoding = default_encoding
  618. return contents.encode(encoding)
  619. def print_commit(commit, decode, outstream=sys.stdout) -> None:
  620. """Write a human-readable commit log entry.
  621. Args:
  622. commit: A `Commit` object
  623. outstream: A stream file to write to
  624. """
  625. outstream.write("-" * 50 + "\n")
  626. outstream.write("commit: " + commit.id.decode("ascii") + "\n")
  627. if len(commit.parents) > 1:
  628. outstream.write(
  629. "merge: "
  630. + "...".join([c.decode("ascii") for c in commit.parents[1:]])
  631. + "\n"
  632. )
  633. outstream.write("Author: " + decode(commit.author) + "\n")
  634. if commit.author != commit.committer:
  635. outstream.write("Committer: " + decode(commit.committer) + "\n")
  636. time_tuple = time.gmtime(commit.author_time + commit.author_timezone)
  637. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  638. timezone_str = format_timezone(commit.author_timezone).decode("ascii")
  639. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  640. if commit.message:
  641. outstream.write("\n")
  642. outstream.write(decode(commit.message) + "\n")
  643. outstream.write("\n")
  644. def print_tag(tag, decode, outstream=sys.stdout) -> None:
  645. """Write a human-readable tag.
  646. Args:
  647. tag: A `Tag` object
  648. decode: Function for decoding bytes to unicode string
  649. outstream: A stream to write to
  650. """
  651. outstream.write("Tagger: " + decode(tag.tagger) + "\n")
  652. time_tuple = time.gmtime(tag.tag_time + tag.tag_timezone)
  653. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  654. timezone_str = format_timezone(tag.tag_timezone).decode("ascii")
  655. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  656. outstream.write("\n")
  657. outstream.write(decode(tag.message))
  658. outstream.write("\n")
  659. def show_blob(repo, blob, decode, outstream=sys.stdout) -> None:
  660. """Write a blob to a stream.
  661. Args:
  662. repo: A `Repo` object
  663. blob: A `Blob` object
  664. decode: Function for decoding bytes to unicode string
  665. outstream: A stream file to write to
  666. """
  667. outstream.write(decode(blob.data))
  668. def show_commit(repo, commit, decode, outstream=sys.stdout) -> None:
  669. """Show a commit to a stream.
  670. Args:
  671. repo: A `Repo` object
  672. commit: A `Commit` object
  673. decode: Function for decoding bytes to unicode string
  674. outstream: Stream to write to
  675. """
  676. print_commit(commit, decode=decode, outstream=outstream)
  677. if commit.parents:
  678. parent_commit = repo[commit.parents[0]]
  679. base_tree = parent_commit.tree
  680. else:
  681. base_tree = None
  682. diffstream = BytesIO()
  683. write_tree_diff(diffstream, repo.object_store, base_tree, commit.tree)
  684. diffstream.seek(0)
  685. outstream.write(commit_decode(commit, diffstream.getvalue()))
  686. def show_tree(repo, tree, decode, outstream=sys.stdout) -> None:
  687. """Print a tree to a stream.
  688. Args:
  689. repo: A `Repo` object
  690. tree: A `Tree` object
  691. decode: Function for decoding bytes to unicode string
  692. outstream: Stream to write to
  693. """
  694. for n in tree:
  695. outstream.write(decode(n) + "\n")
  696. def show_tag(repo, tag, decode, outstream=sys.stdout) -> None:
  697. """Print a tag to a stream.
  698. Args:
  699. repo: A `Repo` object
  700. tag: A `Tag` object
  701. decode: Function for decoding bytes to unicode string
  702. outstream: Stream to write to
  703. """
  704. print_tag(tag, decode, outstream)
  705. show_object(repo, repo[tag.object[1]], decode, outstream)
  706. def show_object(repo, obj, decode, outstream):
  707. return {
  708. b"tree": show_tree,
  709. b"blob": show_blob,
  710. b"commit": show_commit,
  711. b"tag": show_tag,
  712. }[obj.type_name](repo, obj, decode, outstream)
  713. def print_name_status(changes):
  714. """Print a simple status summary, listing changed files."""
  715. for change in changes:
  716. if not change:
  717. continue
  718. if isinstance(change, list):
  719. change = change[0]
  720. if change.type == CHANGE_ADD:
  721. path1 = change.new.path
  722. path2 = ""
  723. kind = "A"
  724. elif change.type == CHANGE_DELETE:
  725. path1 = change.old.path
  726. path2 = ""
  727. kind = "D"
  728. elif change.type == CHANGE_MODIFY:
  729. path1 = change.new.path
  730. path2 = ""
  731. kind = "M"
  732. elif change.type in RENAME_CHANGE_TYPES:
  733. path1 = change.old.path
  734. path2 = change.new.path
  735. if change.type == CHANGE_RENAME:
  736. kind = "R"
  737. elif change.type == CHANGE_COPY:
  738. kind = "C"
  739. yield "%-8s%-20s%-20s" % (kind, path1, path2) # noqa: UP031
  740. def log(
  741. repo=".",
  742. paths=None,
  743. outstream=sys.stdout,
  744. max_entries=None,
  745. reverse=False,
  746. name_status=False,
  747. ) -> None:
  748. """Write commit logs.
  749. Args:
  750. repo: Path to repository
  751. paths: Optional set of specific paths to print entries for
  752. outstream: Stream to write log output to
  753. reverse: Reverse order in which entries are printed
  754. name_status: Print name status
  755. max_entries: Optional maximum number of entries to display
  756. """
  757. with open_repo_closing(repo) as r:
  758. try:
  759. include = [r.head()]
  760. except KeyError:
  761. include = []
  762. walker = r.get_walker(
  763. include=include, max_entries=max_entries, paths=paths, reverse=reverse
  764. )
  765. for entry in walker:
  766. def decode(x):
  767. return commit_decode(entry.commit, x)
  768. print_commit(entry.commit, decode, outstream)
  769. if name_status:
  770. outstream.writelines(
  771. [line + "\n" for line in print_name_status(entry.changes())]
  772. )
  773. # TODO(jelmer): better default for encoding?
  774. def show(
  775. repo=".",
  776. objects=None,
  777. outstream=sys.stdout,
  778. default_encoding=DEFAULT_ENCODING,
  779. ) -> None:
  780. """Print the changes in a commit.
  781. Args:
  782. repo: Path to repository
  783. objects: Objects to show (defaults to [HEAD])
  784. outstream: Stream to write to
  785. default_encoding: Default encoding to use if none is set in the
  786. commit
  787. """
  788. if objects is None:
  789. objects = ["HEAD"]
  790. if not isinstance(objects, list):
  791. objects = [objects]
  792. with open_repo_closing(repo) as r:
  793. for objectish in objects:
  794. o = parse_object(r, objectish)
  795. if isinstance(o, Commit):
  796. def decode(x):
  797. return commit_decode(o, x, default_encoding)
  798. else:
  799. def decode(x):
  800. return x.decode(default_encoding)
  801. show_object(r, o, decode, outstream)
  802. def diff_tree(repo, old_tree, new_tree, outstream=default_bytes_out_stream) -> None:
  803. """Compares the content and mode of blobs found via two tree objects.
  804. Args:
  805. repo: Path to repository
  806. old_tree: Id of old tree
  807. new_tree: Id of new tree
  808. outstream: Stream to write to
  809. """
  810. with open_repo_closing(repo) as r:
  811. write_tree_diff(outstream, r.object_store, old_tree, new_tree)
  812. def rev_list(repo, commits, outstream=sys.stdout) -> None:
  813. """Lists commit objects in reverse chronological order.
  814. Args:
  815. repo: Path to repository
  816. commits: Commits over which to iterate
  817. outstream: Stream to write to
  818. """
  819. with open_repo_closing(repo) as r:
  820. for entry in r.get_walker(include=[r[c].id for c in commits]):
  821. outstream.write(entry.commit.id + b"\n")
  822. def _canonical_part(url: str) -> str:
  823. name = url.rsplit("/", 1)[-1]
  824. if name.endswith(".git"):
  825. name = name[:-4]
  826. return name
  827. def submodule_add(repo, url, path=None, name=None) -> None:
  828. """Add a new submodule.
  829. Args:
  830. repo: Path to repository
  831. url: URL of repository to add as submodule
  832. path: Path where submodule should live
  833. """
  834. with open_repo_closing(repo) as r:
  835. if path is None:
  836. path = os.path.relpath(_canonical_part(url), r.path)
  837. if name is None:
  838. name = path
  839. # TODO(jelmer): Move this logic to dulwich.submodule
  840. gitmodules_path = os.path.join(r.path, ".gitmodules")
  841. try:
  842. config = ConfigFile.from_path(gitmodules_path)
  843. except FileNotFoundError:
  844. config = ConfigFile()
  845. config.path = gitmodules_path
  846. config.set(("submodule", name), "url", url)
  847. config.set(("submodule", name), "path", path)
  848. config.write_to_path()
  849. def submodule_init(repo) -> None:
  850. """Initialize submodules.
  851. Args:
  852. repo: Path to repository
  853. """
  854. with open_repo_closing(repo) as r:
  855. config = r.get_config()
  856. gitmodules_path = os.path.join(r.path, ".gitmodules")
  857. for path, url, name in read_submodules(gitmodules_path):
  858. config.set((b"submodule", name), b"active", True)
  859. config.set((b"submodule", name), b"url", url)
  860. config.write_to_path()
  861. def submodule_list(repo):
  862. """List submodules.
  863. Args:
  864. repo: Path to repository
  865. """
  866. from .submodule import iter_cached_submodules
  867. with open_repo_closing(repo) as r:
  868. for path, sha in iter_cached_submodules(r.object_store, r[r.head()].tree):
  869. yield path, sha.decode(DEFAULT_ENCODING)
  870. def tag_create(
  871. repo,
  872. tag: Union[str, bytes],
  873. author: Optional[Union[str, bytes]] = None,
  874. message: Optional[Union[str, bytes]] = None,
  875. annotated=False,
  876. objectish: Union[str, bytes] = "HEAD",
  877. tag_time=None,
  878. tag_timezone=None,
  879. sign: bool = False,
  880. encoding: str = DEFAULT_ENCODING,
  881. ) -> None:
  882. """Creates a tag in git via dulwich calls.
  883. Args:
  884. repo: Path to repository
  885. tag: tag string
  886. author: tag author (optional, if annotated is set)
  887. message: tag message (optional)
  888. annotated: whether to create an annotated tag
  889. objectish: object the tag should point at, defaults to HEAD
  890. tag_time: Optional time for annotated tag
  891. tag_timezone: Optional timezone for annotated tag
  892. sign: GPG Sign the tag (bool, defaults to False,
  893. pass True to use default GPG key,
  894. pass a str containing Key ID to use a specific GPG key)
  895. """
  896. with open_repo_closing(repo) as r:
  897. object = parse_object(r, objectish)
  898. if isinstance(tag, str):
  899. tag = tag.encode(encoding)
  900. if annotated:
  901. # Create the tag object
  902. tag_obj = Tag()
  903. if author is None:
  904. author = get_user_identity(r.get_config_stack())
  905. elif isinstance(author, str):
  906. author = author.encode(encoding)
  907. else:
  908. assert isinstance(author, bytes)
  909. tag_obj.tagger = author
  910. if isinstance(message, str):
  911. message = message.encode(encoding)
  912. elif isinstance(message, bytes):
  913. pass
  914. else:
  915. message = b""
  916. tag_obj.message = message + "\n".encode(encoding)
  917. tag_obj.name = tag
  918. tag_obj.object = (type(object), object.id)
  919. if tag_time is None:
  920. tag_time = int(time.time())
  921. tag_obj.tag_time = tag_time
  922. if tag_timezone is None:
  923. tag_timezone = get_user_timezones()[1]
  924. elif isinstance(tag_timezone, str):
  925. tag_timezone = parse_timezone(tag_timezone)
  926. tag_obj.tag_timezone = tag_timezone
  927. if sign:
  928. tag_obj.sign(sign if isinstance(sign, str) else None)
  929. r.object_store.add_object(tag_obj)
  930. tag_id = tag_obj.id
  931. else:
  932. tag_id = object.id
  933. r.refs[_make_tag_ref(tag)] = tag_id
  934. def tag_list(repo, outstream=sys.stdout):
  935. """List all tags.
  936. Args:
  937. repo: Path to repository
  938. outstream: Stream to write tags to
  939. """
  940. with open_repo_closing(repo) as r:
  941. tags = sorted(r.refs.as_dict(b"refs/tags"))
  942. return tags
  943. def tag_delete(repo, name) -> None:
  944. """Remove a tag.
  945. Args:
  946. repo: Path to repository
  947. name: Name of tag to remove
  948. """
  949. with open_repo_closing(repo) as r:
  950. if isinstance(name, bytes):
  951. names = [name]
  952. elif isinstance(name, list):
  953. names = name
  954. else:
  955. raise Error(f"Unexpected tag name type {name!r}")
  956. for name in names:
  957. del r.refs[_make_tag_ref(name)]
  958. def reset(repo, mode, treeish="HEAD") -> None:
  959. """Reset current HEAD to the specified state.
  960. Args:
  961. repo: Path to repository
  962. mode: Mode ("hard", "soft", "mixed")
  963. treeish: Treeish to reset to
  964. """
  965. if mode != "hard":
  966. raise Error("hard is the only mode currently supported")
  967. with open_repo_closing(repo) as r:
  968. tree = parse_tree(r, treeish)
  969. # Get current HEAD tree for comparison
  970. try:
  971. current_head = r.refs[b"HEAD"]
  972. current_tree = r[current_head].tree
  973. except KeyError:
  974. current_tree = None
  975. # Get configuration for working directory update
  976. config = r.get_config()
  977. honor_filemode = config.get_boolean(b"core", b"filemode", os.name != "nt")
  978. # Import validation functions
  979. from .index import validate_path_element_default, validate_path_element_ntfs
  980. if config.get_boolean(b"core", b"core.protectNTFS", os.name == "nt"):
  981. validate_path_element = validate_path_element_ntfs
  982. else:
  983. validate_path_element = validate_path_element_default
  984. if config.get_boolean(b"core", b"symlinks", True):
  985. # Import symlink function
  986. from .index import symlink
  987. symlink_fn = symlink
  988. else:
  989. def symlink_fn( # type: ignore
  990. source, target, target_is_directory=False, *, dir_fd=None
  991. ) -> None:
  992. mode = "w" + ("b" if isinstance(source, bytes) else "")
  993. with open(target, mode) as f:
  994. f.write(source)
  995. # Update working tree and index
  996. update_working_tree(
  997. r,
  998. current_tree,
  999. tree.id,
  1000. honor_filemode=honor_filemode,
  1001. validate_path_element=validate_path_element,
  1002. symlink_fn=symlink_fn,
  1003. force_remove_untracked=True,
  1004. )
  1005. def get_remote_repo(
  1006. repo: Repo, remote_location: Optional[Union[str, bytes]] = None
  1007. ) -> tuple[Optional[str], str]:
  1008. config = repo.get_config()
  1009. if remote_location is None:
  1010. remote_location = get_branch_remote(repo)
  1011. if isinstance(remote_location, str):
  1012. encoded_location = remote_location.encode()
  1013. else:
  1014. encoded_location = remote_location
  1015. section = (b"remote", encoded_location)
  1016. remote_name: Optional[str] = None
  1017. if config.has_section(section):
  1018. remote_name = encoded_location.decode()
  1019. encoded_location = config.get(section, "url")
  1020. else:
  1021. remote_name = None
  1022. return (remote_name, encoded_location.decode())
  1023. def push(
  1024. repo,
  1025. remote_location=None,
  1026. refspecs=None,
  1027. outstream=default_bytes_out_stream,
  1028. errstream=default_bytes_err_stream,
  1029. force=False,
  1030. **kwargs,
  1031. ) -> None:
  1032. """Remote push with dulwich via dulwich.client.
  1033. Args:
  1034. repo: Path to repository
  1035. remote_location: Location of the remote
  1036. refspecs: Refs to push to remote
  1037. outstream: A stream file to write output
  1038. errstream: A stream file to write errors
  1039. force: Force overwriting refs
  1040. """
  1041. # Open the repo
  1042. with open_repo_closing(repo) as r:
  1043. if refspecs is None:
  1044. refspecs = [active_branch(r)]
  1045. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1046. # Get the client and path
  1047. client, path = get_transport_and_path(
  1048. remote_location, config=r.get_config_stack(), **kwargs
  1049. )
  1050. selected_refs = []
  1051. remote_changed_refs = {}
  1052. def update_refs(refs):
  1053. selected_refs.extend(parse_reftuples(r.refs, refs, refspecs, force=force))
  1054. new_refs = {}
  1055. # TODO: Handle selected_refs == {None: None}
  1056. for lh, rh, force_ref in selected_refs:
  1057. if lh is None:
  1058. new_refs[rh] = ZERO_SHA
  1059. remote_changed_refs[rh] = None
  1060. else:
  1061. try:
  1062. localsha = r.refs[lh]
  1063. except KeyError as exc:
  1064. raise Error(f"No valid ref {lh} in local repository") from exc
  1065. if not force_ref and rh in refs:
  1066. check_diverged(r, refs[rh], localsha)
  1067. new_refs[rh] = localsha
  1068. remote_changed_refs[rh] = localsha
  1069. return new_refs
  1070. err_encoding = getattr(errstream, "encoding", None) or DEFAULT_ENCODING
  1071. remote_location = client.get_url(path)
  1072. try:
  1073. result = client.send_pack(
  1074. path,
  1075. update_refs,
  1076. generate_pack_data=r.generate_pack_data,
  1077. progress=errstream.write,
  1078. )
  1079. except SendPackError as exc:
  1080. raise Error(
  1081. "Push to " + remote_location + " failed -> " + exc.args[0].decode(),
  1082. ) from exc
  1083. else:
  1084. errstream.write(
  1085. b"Push to " + remote_location.encode(err_encoding) + b" successful.\n"
  1086. )
  1087. for ref, error in (result.ref_status or {}).items():
  1088. if error is not None:
  1089. errstream.write(
  1090. b"Push of ref %s failed: %s\n" % (ref, error.encode(err_encoding))
  1091. )
  1092. else:
  1093. errstream.write(b"Ref %s updated\n" % ref)
  1094. if remote_name is not None:
  1095. _import_remote_refs(r.refs, remote_name, remote_changed_refs)
  1096. def pull(
  1097. repo,
  1098. remote_location=None,
  1099. refspecs=None,
  1100. outstream=default_bytes_out_stream,
  1101. errstream=default_bytes_err_stream,
  1102. fast_forward=True,
  1103. ff_only=False,
  1104. force=False,
  1105. filter_spec=None,
  1106. protocol_version=None,
  1107. **kwargs,
  1108. ) -> None:
  1109. """Pull from remote via dulwich.client.
  1110. Args:
  1111. repo: Path to repository
  1112. remote_location: Location of the remote
  1113. refspecs: refspecs to fetch. Can be a bytestring, a string, or a list of
  1114. bytestring/string.
  1115. outstream: A stream file to write to output
  1116. errstream: A stream file to write to errors
  1117. fast_forward: If True, raise an exception when fast-forward is not possible
  1118. ff_only: If True, only allow fast-forward merges. Raises DivergedBranches
  1119. when branches have diverged rather than performing a merge.
  1120. filter_spec: A git-rev-list-style object filter spec, as an ASCII string.
  1121. Only used if the server supports the Git protocol-v2 'filter'
  1122. feature, and ignored otherwise.
  1123. protocol_version: desired Git protocol version. By default the highest
  1124. mutually supported protocol version will be used
  1125. """
  1126. # Open the repo
  1127. with open_repo_closing(repo) as r:
  1128. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1129. selected_refs = []
  1130. if refspecs is None:
  1131. refspecs = [b"HEAD"]
  1132. def determine_wants(remote_refs, *args, **kwargs):
  1133. selected_refs.extend(
  1134. parse_reftuples(remote_refs, r.refs, refspecs, force=force)
  1135. )
  1136. return [
  1137. remote_refs[lh]
  1138. for (lh, rh, force_ref) in selected_refs
  1139. if remote_refs[lh] not in r.object_store
  1140. ]
  1141. client, path = get_transport_and_path(
  1142. remote_location, config=r.get_config_stack(), **kwargs
  1143. )
  1144. if filter_spec:
  1145. filter_spec = filter_spec.encode("ascii")
  1146. fetch_result = client.fetch(
  1147. path,
  1148. r,
  1149. progress=errstream.write,
  1150. determine_wants=determine_wants,
  1151. filter_spec=filter_spec,
  1152. protocol_version=protocol_version,
  1153. )
  1154. # Store the old HEAD tree before making changes
  1155. try:
  1156. old_head = r.refs[b"HEAD"]
  1157. old_tree_id = r[old_head].tree
  1158. except KeyError:
  1159. old_tree_id = None
  1160. merged = False
  1161. for lh, rh, force_ref in selected_refs:
  1162. if not force_ref and rh in r.refs:
  1163. try:
  1164. check_diverged(r, r.refs.follow(rh)[1], fetch_result.refs[lh])
  1165. except DivergedBranches as exc:
  1166. if ff_only or fast_forward:
  1167. raise
  1168. else:
  1169. # Perform merge
  1170. merge_result, conflicts = _do_merge(r, fetch_result.refs[lh])
  1171. if conflicts:
  1172. raise Error(
  1173. f"Merge conflicts occurred: {conflicts}"
  1174. ) from exc
  1175. merged = True
  1176. # Skip updating ref since merge already updated HEAD
  1177. continue
  1178. r.refs[rh] = fetch_result.refs[lh]
  1179. # Only update HEAD if we didn't perform a merge
  1180. if selected_refs and not merged:
  1181. r[b"HEAD"] = fetch_result.refs[selected_refs[0][1]]
  1182. # Update working tree to match the new HEAD
  1183. # Skip if merge was performed as merge already updates the working tree
  1184. if not merged and old_tree_id is not None:
  1185. new_tree_id = r[b"HEAD"].tree
  1186. update_working_tree(r, old_tree_id, new_tree_id)
  1187. if remote_name is not None:
  1188. _import_remote_refs(r.refs, remote_name, fetch_result.refs)
  1189. def status(repo=".", ignored=False, untracked_files="all"):
  1190. """Returns staged, unstaged, and untracked changes relative to the HEAD.
  1191. Args:
  1192. repo: Path to repository or repository object
  1193. ignored: Whether to include ignored files in untracked
  1194. untracked_files: How to handle untracked files, defaults to "all":
  1195. "no": do not return untracked files
  1196. "all": include all files in untracked directories
  1197. Using untracked_files="no" can be faster than "all" when the worktreee
  1198. contains many untracked files/directories.
  1199. Note: untracked_files="normal" (git's default) is not implemented.
  1200. Returns: GitStatus tuple,
  1201. staged - dict with lists of staged paths (diff index/HEAD)
  1202. unstaged - list of unstaged paths (diff index/working-tree)
  1203. untracked - list of untracked, un-ignored & non-.git paths
  1204. """
  1205. with open_repo_closing(repo) as r:
  1206. # 1. Get status of staged
  1207. tracked_changes = get_tree_changes(r)
  1208. # 2. Get status of unstaged
  1209. index = r.open_index()
  1210. normalizer = r.get_blob_normalizer()
  1211. filter_callback = normalizer.checkin_normalize
  1212. unstaged_changes = list(get_unstaged_changes(index, r.path, filter_callback))
  1213. untracked_paths = get_untracked_paths(
  1214. r.path,
  1215. r.path,
  1216. index,
  1217. exclude_ignored=not ignored,
  1218. untracked_files=untracked_files,
  1219. )
  1220. if sys.platform == "win32":
  1221. untracked_changes = [
  1222. path.replace(os.path.sep, "/") for path in untracked_paths
  1223. ]
  1224. else:
  1225. untracked_changes = list(untracked_paths)
  1226. return GitStatus(tracked_changes, unstaged_changes, untracked_changes)
  1227. def _walk_working_dir_paths(frompath, basepath, prune_dirnames=None):
  1228. """Get path, is_dir for files in working dir from frompath.
  1229. Args:
  1230. frompath: Path to begin walk
  1231. basepath: Path to compare to
  1232. prune_dirnames: Optional callback to prune dirnames during os.walk
  1233. dirnames will be set to result of prune_dirnames(dirpath, dirnames)
  1234. """
  1235. for dirpath, dirnames, filenames in os.walk(frompath):
  1236. # Skip .git and below.
  1237. if ".git" in dirnames:
  1238. dirnames.remove(".git")
  1239. if dirpath != basepath:
  1240. continue
  1241. if ".git" in filenames:
  1242. filenames.remove(".git")
  1243. if dirpath != basepath:
  1244. continue
  1245. if dirpath != frompath:
  1246. yield dirpath, True
  1247. for filename in filenames:
  1248. filepath = os.path.join(dirpath, filename)
  1249. yield filepath, False
  1250. if prune_dirnames:
  1251. dirnames[:] = prune_dirnames(dirpath, dirnames)
  1252. def get_untracked_paths(
  1253. frompath, basepath, index, exclude_ignored=False, untracked_files="all"
  1254. ):
  1255. """Get untracked paths.
  1256. Args:
  1257. frompath: Path to walk
  1258. basepath: Path to compare to
  1259. index: Index to check against
  1260. exclude_ignored: Whether to exclude ignored paths
  1261. untracked_files: How to handle untracked files:
  1262. - "no": return an empty list
  1263. - "all": return all files in untracked directories
  1264. - "normal": Not implemented
  1265. Note: ignored directories will never be walked for performance reasons.
  1266. If exclude_ignored is False, only the path to an ignored directory will
  1267. be yielded, no files inside the directory will be returned
  1268. """
  1269. if untracked_files == "normal":
  1270. raise NotImplementedError("normal is not yet supported")
  1271. if untracked_files not in ("no", "all"):
  1272. raise ValueError("untracked_files must be one of (no, all)")
  1273. if untracked_files == "no":
  1274. return
  1275. with open_repo_closing(basepath) as r:
  1276. ignore_manager = IgnoreFilterManager.from_repo(r)
  1277. ignored_dirs = []
  1278. def prune_dirnames(dirpath, dirnames):
  1279. for i in range(len(dirnames) - 1, -1, -1):
  1280. path = os.path.join(dirpath, dirnames[i])
  1281. ip = os.path.join(os.path.relpath(path, basepath), "")
  1282. if ignore_manager.is_ignored(ip):
  1283. if not exclude_ignored:
  1284. ignored_dirs.append(
  1285. os.path.join(os.path.relpath(path, frompath), "")
  1286. )
  1287. del dirnames[i]
  1288. return dirnames
  1289. for ap, is_dir in _walk_working_dir_paths(
  1290. frompath, basepath, prune_dirnames=prune_dirnames
  1291. ):
  1292. if not is_dir:
  1293. ip = path_to_tree_path(basepath, ap)
  1294. if ip not in index:
  1295. if not exclude_ignored or not ignore_manager.is_ignored(
  1296. os.path.relpath(ap, basepath)
  1297. ):
  1298. yield os.path.relpath(ap, frompath)
  1299. yield from ignored_dirs
  1300. def get_tree_changes(repo):
  1301. """Return add/delete/modify changes to tree by comparing index to HEAD.
  1302. Args:
  1303. repo: repo path or object
  1304. Returns: dict with lists for each type of change
  1305. """
  1306. with open_repo_closing(repo) as r:
  1307. index = r.open_index()
  1308. # Compares the Index to the HEAD & determines changes
  1309. # Iterate through the changes and report add/delete/modify
  1310. # TODO: call out to dulwich.diff_tree somehow.
  1311. tracked_changes = {
  1312. "add": [],
  1313. "delete": [],
  1314. "modify": [],
  1315. }
  1316. try:
  1317. tree_id = r[b"HEAD"].tree
  1318. except KeyError:
  1319. tree_id = None
  1320. for change in index.changes_from_tree(r.object_store, tree_id):
  1321. if not change[0][0]:
  1322. tracked_changes["add"].append(change[0][1])
  1323. elif not change[0][1]:
  1324. tracked_changes["delete"].append(change[0][0])
  1325. elif change[0][0] == change[0][1]:
  1326. tracked_changes["modify"].append(change[0][0])
  1327. else:
  1328. raise NotImplementedError("git mv ops not yet supported")
  1329. return tracked_changes
  1330. def daemon(path=".", address=None, port=None) -> None:
  1331. """Run a daemon serving Git requests over TCP/IP.
  1332. Args:
  1333. path: Path to the directory to serve.
  1334. address: Optional address to listen on (defaults to ::)
  1335. port: Optional port to listen on (defaults to TCP_GIT_PORT)
  1336. """
  1337. # TODO(jelmer): Support git-daemon-export-ok and --export-all.
  1338. backend = FileSystemBackend(path)
  1339. server = TCPGitServer(backend, address, port)
  1340. server.serve_forever()
  1341. def web_daemon(path=".", address=None, port=None) -> None:
  1342. """Run a daemon serving Git requests over HTTP.
  1343. Args:
  1344. path: Path to the directory to serve
  1345. address: Optional address to listen on (defaults to ::)
  1346. port: Optional port to listen on (defaults to 80)
  1347. """
  1348. from .web import (
  1349. WSGIRequestHandlerLogger,
  1350. WSGIServerLogger,
  1351. make_server,
  1352. make_wsgi_chain,
  1353. )
  1354. backend = FileSystemBackend(path)
  1355. app = make_wsgi_chain(backend)
  1356. server = make_server(
  1357. address,
  1358. port,
  1359. app,
  1360. handler_class=WSGIRequestHandlerLogger,
  1361. server_class=WSGIServerLogger,
  1362. )
  1363. server.serve_forever()
  1364. def upload_pack(path=".", inf=None, outf=None) -> int:
  1365. """Upload a pack file after negotiating its contents using smart protocol.
  1366. Args:
  1367. path: Path to the repository
  1368. inf: Input stream to communicate with client
  1369. outf: Output stream to communicate with client
  1370. """
  1371. if outf is None:
  1372. outf = getattr(sys.stdout, "buffer", sys.stdout)
  1373. if inf is None:
  1374. inf = getattr(sys.stdin, "buffer", sys.stdin)
  1375. path = os.path.expanduser(path)
  1376. backend = FileSystemBackend(path)
  1377. def send_fn(data) -> None:
  1378. outf.write(data)
  1379. outf.flush()
  1380. proto = Protocol(inf.read, send_fn)
  1381. handler = UploadPackHandler(backend, [path], proto)
  1382. # FIXME: Catch exceptions and write a single-line summary to outf.
  1383. handler.handle()
  1384. return 0
  1385. def receive_pack(path=".", inf=None, outf=None) -> int:
  1386. """Receive a pack file after negotiating its contents using smart protocol.
  1387. Args:
  1388. path: Path to the repository
  1389. inf: Input stream to communicate with client
  1390. outf: Output stream to communicate with client
  1391. """
  1392. if outf is None:
  1393. outf = getattr(sys.stdout, "buffer", sys.stdout)
  1394. if inf is None:
  1395. inf = getattr(sys.stdin, "buffer", sys.stdin)
  1396. path = os.path.expanduser(path)
  1397. backend = FileSystemBackend(path)
  1398. def send_fn(data) -> None:
  1399. outf.write(data)
  1400. outf.flush()
  1401. proto = Protocol(inf.read, send_fn)
  1402. handler = ReceivePackHandler(backend, [path], proto)
  1403. # FIXME: Catch exceptions and write a single-line summary to outf.
  1404. handler.handle()
  1405. return 0
  1406. def _make_branch_ref(name: Union[str, bytes]) -> Ref:
  1407. if isinstance(name, str):
  1408. name = name.encode(DEFAULT_ENCODING)
  1409. return LOCAL_BRANCH_PREFIX + name
  1410. def _make_tag_ref(name: Union[str, bytes]) -> Ref:
  1411. if isinstance(name, str):
  1412. name = name.encode(DEFAULT_ENCODING)
  1413. return LOCAL_TAG_PREFIX + name
  1414. def branch_delete(repo, name) -> None:
  1415. """Delete a branch.
  1416. Args:
  1417. repo: Path to the repository
  1418. name: Name of the branch
  1419. """
  1420. with open_repo_closing(repo) as r:
  1421. if isinstance(name, list):
  1422. names = name
  1423. else:
  1424. names = [name]
  1425. for name in names:
  1426. del r.refs[_make_branch_ref(name)]
  1427. def branch_create(repo, name, objectish=None, force=False) -> None:
  1428. """Create a branch.
  1429. Args:
  1430. repo: Path to the repository
  1431. name: Name of the new branch
  1432. objectish: Target object to point new branch at (defaults to HEAD)
  1433. force: Force creation of branch, even if it already exists
  1434. """
  1435. with open_repo_closing(repo) as r:
  1436. if objectish is None:
  1437. objectish = "HEAD"
  1438. object = parse_object(r, objectish)
  1439. refname = _make_branch_ref(name)
  1440. ref_message = b"branch: Created from " + objectish.encode(DEFAULT_ENCODING)
  1441. if force:
  1442. r.refs.set_if_equals(refname, None, object.id, message=ref_message)
  1443. else:
  1444. if not r.refs.add_if_new(refname, object.id, message=ref_message):
  1445. raise Error(f"Branch with name {name} already exists.")
  1446. def branch_list(repo):
  1447. """List all branches.
  1448. Args:
  1449. repo: Path to the repository
  1450. """
  1451. with open_repo_closing(repo) as r:
  1452. return r.refs.keys(base=LOCAL_BRANCH_PREFIX)
  1453. def active_branch(repo):
  1454. """Return the active branch in the repository, if any.
  1455. Args:
  1456. repo: Repository to open
  1457. Returns:
  1458. branch name
  1459. Raises:
  1460. KeyError: if the repository does not have a working tree
  1461. IndexError: if HEAD is floating
  1462. """
  1463. with open_repo_closing(repo) as r:
  1464. active_ref = r.refs.follow(b"HEAD")[0][1]
  1465. if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
  1466. raise ValueError(active_ref)
  1467. return active_ref[len(LOCAL_BRANCH_PREFIX) :]
  1468. def get_branch_remote(repo):
  1469. """Return the active branch's remote name, if any.
  1470. Args:
  1471. repo: Repository to open
  1472. Returns:
  1473. remote name
  1474. Raises:
  1475. KeyError: if the repository does not have a working tree
  1476. """
  1477. with open_repo_closing(repo) as r:
  1478. branch_name = active_branch(r.path)
  1479. config = r.get_config()
  1480. try:
  1481. remote_name = config.get((b"branch", branch_name), b"remote")
  1482. except KeyError:
  1483. remote_name = b"origin"
  1484. return remote_name
  1485. def fetch(
  1486. repo,
  1487. remote_location=None,
  1488. outstream=sys.stdout,
  1489. errstream=default_bytes_err_stream,
  1490. message=None,
  1491. depth=None,
  1492. prune=False,
  1493. prune_tags=False,
  1494. force=False,
  1495. **kwargs,
  1496. ):
  1497. """Fetch objects from a remote server.
  1498. Args:
  1499. repo: Path to the repository
  1500. remote_location: String identifying a remote server
  1501. outstream: Output stream (defaults to stdout)
  1502. errstream: Error stream (defaults to stderr)
  1503. message: Reflog message (defaults to b"fetch: from <remote_name>")
  1504. depth: Depth to fetch at
  1505. prune: Prune remote removed refs
  1506. prune_tags: Prune reomte removed tags
  1507. Returns:
  1508. Dictionary with refs on the remote
  1509. """
  1510. with open_repo_closing(repo) as r:
  1511. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1512. if message is None:
  1513. message = b"fetch: from " + remote_location.encode(DEFAULT_ENCODING)
  1514. client, path = get_transport_and_path(
  1515. remote_location, config=r.get_config_stack(), **kwargs
  1516. )
  1517. fetch_result = client.fetch(path, r, progress=errstream.write, depth=depth)
  1518. if remote_name is not None:
  1519. _import_remote_refs(
  1520. r.refs,
  1521. remote_name,
  1522. fetch_result.refs,
  1523. message,
  1524. prune=prune,
  1525. prune_tags=prune_tags,
  1526. )
  1527. return fetch_result
  1528. def for_each_ref(
  1529. repo: Union[Repo, str] = ".",
  1530. pattern: Optional[Union[str, bytes]] = None,
  1531. ) -> list[tuple[bytes, bytes, bytes]]:
  1532. """Iterate over all refs that match the (optional) pattern.
  1533. Args:
  1534. repo: Path to the repository
  1535. pattern: Optional glob (7) patterns to filter the refs with
  1536. Returns:
  1537. List of bytes tuples with: (sha, object_type, ref_name)
  1538. """
  1539. if isinstance(pattern, str):
  1540. pattern = os.fsencode(pattern)
  1541. with open_repo_closing(repo) as r:
  1542. refs = r.get_refs()
  1543. if pattern:
  1544. matching_refs: dict[bytes, bytes] = {}
  1545. pattern_parts = pattern.split(b"/")
  1546. for ref, sha in refs.items():
  1547. matches = False
  1548. # git for-each-ref uses glob (7) style patterns, but fnmatch
  1549. # is greedy and also matches slashes, unlike glob.glob.
  1550. # We have to check parts of the pattern individually.
  1551. # See https://github.com/python/cpython/issues/72904
  1552. ref_parts = ref.split(b"/")
  1553. if len(ref_parts) > len(pattern_parts):
  1554. continue
  1555. for pat, ref_part in zip(pattern_parts, ref_parts):
  1556. matches = fnmatch.fnmatchcase(ref_part, pat)
  1557. if not matches:
  1558. break
  1559. if matches:
  1560. matching_refs[ref] = sha
  1561. refs = matching_refs
  1562. ret: list[tuple[bytes, bytes, bytes]] = [
  1563. (sha, r.get_object(sha).type_name, ref)
  1564. for ref, sha in sorted(
  1565. refs.items(),
  1566. key=lambda ref_sha: ref_sha[0],
  1567. )
  1568. if ref != b"HEAD"
  1569. ]
  1570. return ret
  1571. def ls_remote(remote, config: Optional[Config] = None, **kwargs):
  1572. """List the refs in a remote.
  1573. Args:
  1574. remote: Remote repository location
  1575. config: Configuration to use
  1576. Returns:
  1577. Dictionary with remote refs
  1578. """
  1579. if config is None:
  1580. config = StackedConfig.default()
  1581. client, host_path = get_transport_and_path(remote, config=config, **kwargs)
  1582. return client.get_refs(host_path)
  1583. def repack(repo) -> None:
  1584. """Repack loose files in a repository.
  1585. Currently this only packs loose objects.
  1586. Args:
  1587. repo: Path to the repository
  1588. """
  1589. with open_repo_closing(repo) as r:
  1590. r.object_store.pack_loose_objects()
  1591. def pack_objects(
  1592. repo,
  1593. object_ids,
  1594. packf,
  1595. idxf,
  1596. delta_window_size=None,
  1597. deltify=None,
  1598. reuse_deltas=True,
  1599. ) -> None:
  1600. """Pack objects into a file.
  1601. Args:
  1602. repo: Path to the repository
  1603. object_ids: List of object ids to write
  1604. packf: File-like object to write to
  1605. idxf: File-like object to write to (can be None)
  1606. delta_window_size: Sliding window size for searching for deltas;
  1607. Set to None for default window size.
  1608. deltify: Whether to deltify objects
  1609. reuse_deltas: Allow reuse of existing deltas while deltifying
  1610. """
  1611. with open_repo_closing(repo) as r:
  1612. entries, data_sum = write_pack_from_container(
  1613. packf.write,
  1614. r.object_store,
  1615. [(oid, None) for oid in object_ids],
  1616. deltify=deltify,
  1617. delta_window_size=delta_window_size,
  1618. reuse_deltas=reuse_deltas,
  1619. )
  1620. if idxf is not None:
  1621. entries = sorted([(k, v[0], v[1]) for (k, v) in entries.items()])
  1622. write_pack_index(idxf, entries, data_sum)
  1623. def ls_tree(
  1624. repo,
  1625. treeish=b"HEAD",
  1626. outstream=sys.stdout,
  1627. recursive=False,
  1628. name_only=False,
  1629. ) -> None:
  1630. """List contents of a tree.
  1631. Args:
  1632. repo: Path to the repository
  1633. treeish: Tree id to list
  1634. outstream: Output stream (defaults to stdout)
  1635. recursive: Whether to recursively list files
  1636. name_only: Only print item name
  1637. """
  1638. def list_tree(store, treeid, base) -> None:
  1639. for name, mode, sha in store[treeid].iteritems():
  1640. if base:
  1641. name = posixpath.join(base, name)
  1642. if name_only:
  1643. outstream.write(name + b"\n")
  1644. else:
  1645. outstream.write(pretty_format_tree_entry(name, mode, sha))
  1646. if stat.S_ISDIR(mode) and recursive:
  1647. list_tree(store, sha, name)
  1648. with open_repo_closing(repo) as r:
  1649. tree = parse_tree(r, treeish)
  1650. list_tree(r.object_store, tree.id, "")
  1651. def remote_add(repo, name: Union[bytes, str], url: Union[bytes, str]) -> None:
  1652. """Add a remote.
  1653. Args:
  1654. repo: Path to the repository
  1655. name: Remote name
  1656. url: Remote URL
  1657. """
  1658. if not isinstance(name, bytes):
  1659. name = name.encode(DEFAULT_ENCODING)
  1660. if not isinstance(url, bytes):
  1661. url = url.encode(DEFAULT_ENCODING)
  1662. with open_repo_closing(repo) as r:
  1663. c = r.get_config()
  1664. section = (b"remote", name)
  1665. if c.has_section(section):
  1666. raise RemoteExists(section)
  1667. c.set(section, b"url", url)
  1668. c.write_to_path()
  1669. def remote_remove(repo: Repo, name: Union[bytes, str]) -> None:
  1670. """Remove a remote.
  1671. Args:
  1672. repo: Path to the repository
  1673. name: Remote name
  1674. """
  1675. if not isinstance(name, bytes):
  1676. name = name.encode(DEFAULT_ENCODING)
  1677. with open_repo_closing(repo) as r:
  1678. c = r.get_config()
  1679. section = (b"remote", name)
  1680. del c[section]
  1681. c.write_to_path()
  1682. def check_ignore(repo, paths, no_index=False):
  1683. """Debug gitignore files.
  1684. Args:
  1685. repo: Path to the repository
  1686. paths: List of paths to check for
  1687. no_index: Don't check index
  1688. Returns: List of ignored files
  1689. """
  1690. with open_repo_closing(repo) as r:
  1691. index = r.open_index()
  1692. ignore_manager = IgnoreFilterManager.from_repo(r)
  1693. for path in paths:
  1694. if not no_index and path_to_tree_path(r.path, path) in index:
  1695. continue
  1696. if os.path.isabs(path):
  1697. path = os.path.relpath(path, r.path)
  1698. if ignore_manager.is_ignored(path):
  1699. yield path
  1700. def update_head(repo, target, detached=False, new_branch=None) -> None:
  1701. """Update HEAD to point at a new branch/commit.
  1702. Note that this does not actually update the working tree.
  1703. Args:
  1704. repo: Path to the repository
  1705. detached: Create a detached head
  1706. target: Branch or committish to switch to
  1707. new_branch: New branch to create
  1708. """
  1709. with open_repo_closing(repo) as r:
  1710. if new_branch is not None:
  1711. to_set = _make_branch_ref(new_branch)
  1712. else:
  1713. to_set = b"HEAD"
  1714. if detached:
  1715. # TODO(jelmer): Provide some way so that the actual ref gets
  1716. # updated rather than what it points to, so the delete isn't
  1717. # necessary.
  1718. del r.refs[to_set]
  1719. r.refs[to_set] = parse_commit(r, target).id
  1720. else:
  1721. r.refs.set_symbolic_ref(to_set, parse_ref(r, target))
  1722. if new_branch is not None:
  1723. r.refs.set_symbolic_ref(b"HEAD", to_set)
  1724. def checkout(
  1725. repo,
  1726. target: Union[bytes, str],
  1727. force: bool = False,
  1728. new_branch: Optional[Union[bytes, str]] = None,
  1729. ) -> None:
  1730. """Switch to a branch or commit, updating both HEAD and the working tree.
  1731. This is similar to 'git checkout', allowing you to switch to a branch,
  1732. tag, or specific commit. Unlike update_head, this function also updates
  1733. the working tree to match the target.
  1734. Args:
  1735. repo: Path to repository or repository object
  1736. target: Branch name, tag, or commit SHA to checkout
  1737. force: Force checkout even if there are local changes
  1738. new_branch: Create a new branch at target (like git checkout -b)
  1739. Raises:
  1740. CheckoutError: If checkout cannot be performed due to conflicts
  1741. KeyError: If the target reference cannot be found
  1742. """
  1743. with open_repo_closing(repo) as r:
  1744. if isinstance(target, str):
  1745. target = target.encode(DEFAULT_ENCODING)
  1746. if isinstance(new_branch, str):
  1747. new_branch = new_branch.encode(DEFAULT_ENCODING)
  1748. # Parse the target to get the commit
  1749. target_commit = parse_commit(r, target)
  1750. target_tree_id = target_commit.tree
  1751. # Get current HEAD tree for comparison
  1752. try:
  1753. current_head = r.refs[b"HEAD"]
  1754. current_tree_id = r[current_head].tree
  1755. except KeyError:
  1756. # No HEAD yet (empty repo)
  1757. current_tree_id = None
  1758. # Check for uncommitted changes if not forcing
  1759. if not force and current_tree_id is not None:
  1760. status_report = status(r)
  1761. changes = []
  1762. # staged is a dict with 'add', 'delete', 'modify' keys
  1763. if isinstance(status_report.staged, dict):
  1764. changes.extend(status_report.staged.get("add", []))
  1765. changes.extend(status_report.staged.get("delete", []))
  1766. changes.extend(status_report.staged.get("modify", []))
  1767. # unstaged is a list
  1768. changes.extend(status_report.unstaged)
  1769. if changes:
  1770. # Check if any changes would conflict with checkout
  1771. target_tree = r[target_tree_id]
  1772. for change in changes:
  1773. if isinstance(change, str):
  1774. change = change.encode(DEFAULT_ENCODING)
  1775. try:
  1776. target_tree.lookup_path(r.object_store.__getitem__, change)
  1777. # File exists in target tree - would overwrite local changes
  1778. raise CheckoutError(
  1779. f"Your local changes to '{change.decode()}' would be "
  1780. "overwritten by checkout. Please commit or stash before switching."
  1781. )
  1782. except KeyError:
  1783. # File doesn't exist in target tree - change can be preserved
  1784. pass
  1785. # Get configuration for working directory update
  1786. config = r.get_config()
  1787. honor_filemode = config.get_boolean(b"core", b"filemode", os.name != "nt")
  1788. # Import validation functions
  1789. from .index import validate_path_element_default, validate_path_element_ntfs
  1790. if config.get_boolean(b"core", b"core.protectNTFS", os.name == "nt"):
  1791. validate_path_element = validate_path_element_ntfs
  1792. else:
  1793. validate_path_element = validate_path_element_default
  1794. if config.get_boolean(b"core", b"symlinks", True):
  1795. # Import symlink function
  1796. from .index import symlink
  1797. symlink_fn = symlink
  1798. else:
  1799. def symlink_fn(source, target) -> None: # type: ignore
  1800. mode = "w" + ("b" if isinstance(source, bytes) else "")
  1801. with open(target, mode) as f:
  1802. f.write(source)
  1803. # Update working tree
  1804. update_working_tree(
  1805. r,
  1806. current_tree_id,
  1807. target_tree_id,
  1808. honor_filemode=honor_filemode,
  1809. validate_path_element=validate_path_element,
  1810. symlink_fn=symlink_fn,
  1811. force_remove_untracked=force,
  1812. )
  1813. # Update HEAD
  1814. if new_branch:
  1815. # Create new branch and switch to it
  1816. branch_create(r, new_branch, objectish=target_commit.id.decode("ascii"))
  1817. update_head(r, new_branch)
  1818. else:
  1819. # Check if target is a branch name (with or without refs/heads/ prefix)
  1820. branch_ref = None
  1821. if target in r.refs.keys():
  1822. if target.startswith(LOCAL_BRANCH_PREFIX):
  1823. branch_ref = target
  1824. else:
  1825. # Try adding refs/heads/ prefix
  1826. potential_branch = _make_branch_ref(target)
  1827. if potential_branch in r.refs.keys():
  1828. branch_ref = potential_branch
  1829. if branch_ref:
  1830. # It's a branch - update HEAD symbolically
  1831. update_head(r, branch_ref)
  1832. else:
  1833. # It's a tag, other ref, or commit SHA - detached HEAD
  1834. update_head(r, target_commit.id.decode("ascii"), detached=True)
  1835. def reset_file(repo, file_path: str, target: bytes = b"HEAD", symlink_fn=None) -> None:
  1836. """Reset the file to specific commit or branch.
  1837. Args:
  1838. repo: dulwich Repo object
  1839. file_path: file to reset, relative to the repository path
  1840. target: branch or commit or b'HEAD' to reset
  1841. """
  1842. tree = parse_tree(repo, treeish=target)
  1843. tree_path = _fs_to_tree_path(file_path)
  1844. file_entry = tree.lookup_path(repo.object_store.__getitem__, tree_path)
  1845. full_path = os.path.join(os.fsencode(repo.path), tree_path)
  1846. blob = repo.object_store[file_entry[1]]
  1847. mode = file_entry[0]
  1848. build_file_from_blob(blob, mode, full_path, symlink_fn=symlink_fn)
  1849. def checkout_branch(repo, target: Union[bytes, str], force: bool = False) -> None:
  1850. """Switch branches or restore working tree files.
  1851. This is now a wrapper around the general checkout() function.
  1852. Preserved for backward compatibility.
  1853. Args:
  1854. repo: dulwich Repo object
  1855. target: branch name or commit sha to checkout
  1856. force: true or not to force checkout
  1857. """
  1858. import warnings
  1859. warnings.warn(
  1860. "checkout_branch is deprecated, use checkout instead.",
  1861. DeprecationWarning,
  1862. stacklevel=2,
  1863. )
  1864. # Simply delegate to the new checkout function
  1865. checkout(repo, target, force=force)
  1866. def sparse_checkout(
  1867. repo, patterns=None, force: bool = False, cone: Union[bool, None] = None
  1868. ):
  1869. """Perform a sparse checkout in the repository (either 'full' or 'cone mode').
  1870. Perform sparse checkout in either 'cone' (directory-based) mode or
  1871. 'full pattern' (.gitignore) mode, depending on the ``cone`` parameter.
  1872. If ``cone`` is ``None``, the mode is inferred from the repository's
  1873. ``core.sparseCheckoutCone`` config setting.
  1874. Steps:
  1875. 1) If ``patterns`` is provided, write them to ``.git/info/sparse-checkout``.
  1876. 2) Determine which paths in the index are included vs. excluded.
  1877. - If ``cone=True``, use "cone-compatible" directory-based logic.
  1878. - If ``cone=False``, use standard .gitignore-style matching.
  1879. 3) Update the index's skip-worktree bits and add/remove files in
  1880. the working tree accordingly.
  1881. 4) If ``force=False``, refuse to remove files that have local modifications.
  1882. Args:
  1883. repo: Path to the repository or a Repo object.
  1884. patterns: Optional list of sparse-checkout patterns to write.
  1885. force: Whether to force removal of locally modified files (default False).
  1886. cone: Boolean indicating cone mode (True/False). If None, read from config.
  1887. Returns:
  1888. None
  1889. """
  1890. with open_repo_closing(repo) as repo_obj:
  1891. # --- 0) Possibly infer 'cone' from config ---
  1892. if cone is None:
  1893. cone = repo_obj.infer_cone_mode()
  1894. # --- 1) Read or write patterns ---
  1895. if patterns is None:
  1896. lines = repo_obj.get_sparse_checkout_patterns()
  1897. if lines is None:
  1898. raise Error("No sparse checkout patterns found.")
  1899. else:
  1900. lines = patterns
  1901. repo_obj.set_sparse_checkout_patterns(patterns)
  1902. # --- 2) Determine the set of included paths ---
  1903. included_paths = determine_included_paths(repo_obj, lines, cone)
  1904. # --- 3) Apply those results to the index & working tree ---
  1905. try:
  1906. apply_included_paths(repo_obj, included_paths, force=force)
  1907. except SparseCheckoutConflictError as exc:
  1908. raise CheckoutError(*exc.args) from exc
  1909. def cone_mode_init(repo):
  1910. """Initialize a repository to use sparse checkout in 'cone' mode.
  1911. Sets ``core.sparseCheckout`` and ``core.sparseCheckoutCone`` in the config.
  1912. Writes an initial ``.git/info/sparse-checkout`` file that includes only
  1913. top-level files (and excludes all subdirectories), e.g. ``["/*", "!/*/"]``.
  1914. Then performs a sparse checkout to update the working tree accordingly.
  1915. If no directories are specified, then only top-level files are included:
  1916. https://git-scm.com/docs/git-sparse-checkout#_internalscone_mode_handling
  1917. Args:
  1918. repo: Path to the repository or a Repo object.
  1919. Returns:
  1920. None
  1921. """
  1922. with open_repo_closing(repo) as repo_obj:
  1923. repo_obj.configure_for_cone_mode()
  1924. patterns = ["/*", "!/*/"] # root-level files only
  1925. sparse_checkout(repo_obj, patterns, force=True, cone=True)
  1926. def cone_mode_set(repo, dirs, force=False):
  1927. """Overwrite the existing 'cone-mode' sparse patterns with a new set of directories.
  1928. Ensures ``core.sparseCheckout`` and ``core.sparseCheckoutCone`` are enabled.
  1929. Writes new patterns so that only the specified directories (and top-level files)
  1930. remain in the working tree, and applies the sparse checkout update.
  1931. Args:
  1932. repo: Path to the repository or a Repo object.
  1933. dirs: List of directory names to include.
  1934. force: Whether to forcibly discard local modifications (default False).
  1935. Returns:
  1936. None
  1937. """
  1938. with open_repo_closing(repo) as repo_obj:
  1939. repo_obj.configure_for_cone_mode()
  1940. repo_obj.set_cone_mode_patterns(dirs=dirs)
  1941. new_patterns = repo_obj.get_sparse_checkout_patterns()
  1942. # Finally, apply the patterns and update the working tree
  1943. sparse_checkout(repo_obj, new_patterns, force=force, cone=True)
  1944. def cone_mode_add(repo, dirs, force=False):
  1945. """Add new directories to the existing 'cone-mode' sparse-checkout patterns.
  1946. Reads the current patterns from ``.git/info/sparse-checkout``, adds pattern
  1947. lines to include the specified directories, and then performs a sparse
  1948. checkout to update the working tree accordingly.
  1949. Args:
  1950. repo: Path to the repository or a Repo object.
  1951. dirs: List of directory names to add to the sparse-checkout.
  1952. force: Whether to forcibly discard local modifications (default False).
  1953. Returns:
  1954. None
  1955. """
  1956. with open_repo_closing(repo) as repo_obj:
  1957. repo_obj.configure_for_cone_mode()
  1958. # Do not pass base patterns as dirs
  1959. base_patterns = ["/*", "!/*/"]
  1960. existing_dirs = [
  1961. pat.strip("/")
  1962. for pat in repo_obj.get_sparse_checkout_patterns()
  1963. if pat not in base_patterns
  1964. ]
  1965. added_dirs = existing_dirs + (dirs or [])
  1966. repo_obj.set_cone_mode_patterns(dirs=added_dirs)
  1967. new_patterns = repo_obj.get_sparse_checkout_patterns()
  1968. sparse_checkout(repo_obj, patterns=new_patterns, force=force, cone=True)
  1969. def check_mailmap(repo, contact):
  1970. """Check canonical name and email of contact.
  1971. Args:
  1972. repo: Path to the repository
  1973. contact: Contact name and/or email
  1974. Returns: Canonical contact data
  1975. """
  1976. with open_repo_closing(repo) as r:
  1977. from .mailmap import Mailmap
  1978. try:
  1979. mailmap = Mailmap.from_path(os.path.join(r.path, ".mailmap"))
  1980. except FileNotFoundError:
  1981. mailmap = Mailmap()
  1982. return mailmap.lookup(contact)
  1983. def fsck(repo):
  1984. """Check a repository.
  1985. Args:
  1986. repo: A path to the repository
  1987. Returns: Iterator over errors/warnings
  1988. """
  1989. with open_repo_closing(repo) as r:
  1990. # TODO(jelmer): check pack files
  1991. # TODO(jelmer): check graph
  1992. # TODO(jelmer): check refs
  1993. for sha in r.object_store:
  1994. o = r.object_store[sha]
  1995. try:
  1996. o.check()
  1997. except Exception as e:
  1998. yield (sha, e)
  1999. def stash_list(repo):
  2000. """List all stashes in a repository."""
  2001. with open_repo_closing(repo) as r:
  2002. from .stash import Stash
  2003. stash = Stash.from_repo(r)
  2004. return enumerate(list(stash.stashes()))
  2005. def stash_push(repo) -> None:
  2006. """Push a new stash onto the stack."""
  2007. with open_repo_closing(repo) as r:
  2008. from .stash import Stash
  2009. stash = Stash.from_repo(r)
  2010. stash.push()
  2011. def stash_pop(repo) -> None:
  2012. """Pop a stash from the stack."""
  2013. with open_repo_closing(repo) as r:
  2014. from .stash import Stash
  2015. stash = Stash.from_repo(r)
  2016. stash.pop()
  2017. def stash_drop(repo, index) -> None:
  2018. """Drop a stash from the stack."""
  2019. with open_repo_closing(repo) as r:
  2020. from .stash import Stash
  2021. stash = Stash.from_repo(r)
  2022. stash.drop(index)
  2023. def ls_files(repo):
  2024. """List all files in an index."""
  2025. with open_repo_closing(repo) as r:
  2026. return sorted(r.open_index())
  2027. def find_unique_abbrev(object_store, object_id):
  2028. """For now, just return 7 characters."""
  2029. # TODO(jelmer): Add some logic here to return a number of characters that
  2030. # scales relative with the size of the repository
  2031. return object_id.decode("ascii")[:7]
  2032. def describe(repo, abbrev=None):
  2033. """Describe the repository version.
  2034. Args:
  2035. repo: git repository
  2036. abbrev: number of characters of commit to take, default is 7
  2037. Returns: a string description of the current git revision
  2038. Examples: "gabcdefh", "v0.1" or "v0.1-5-gabcdefh".
  2039. """
  2040. abbrev_slice = slice(0, abbrev if abbrev is not None else 7)
  2041. # Get the repository
  2042. with open_repo_closing(repo) as r:
  2043. # Get a list of all tags
  2044. refs = r.get_refs()
  2045. tags = {}
  2046. for key, value in refs.items():
  2047. key = key.decode()
  2048. obj = r.get_object(value)
  2049. if "tags" not in key:
  2050. continue
  2051. _, tag = key.rsplit("/", 1)
  2052. try:
  2053. commit = obj.object
  2054. except AttributeError:
  2055. continue
  2056. else:
  2057. commit = r.get_object(commit[1])
  2058. tags[tag] = [
  2059. datetime.datetime(*time.gmtime(commit.commit_time)[:6]),
  2060. commit.id.decode("ascii"),
  2061. ]
  2062. sorted_tags = sorted(tags.items(), key=lambda tag: tag[1][0], reverse=True)
  2063. # Get the latest commit
  2064. latest_commit = r[r.head()]
  2065. # If there are no tags, return the latest commit
  2066. if len(sorted_tags) == 0:
  2067. if abbrev is not None:
  2068. return "g{}".format(latest_commit.id.decode("ascii")[abbrev_slice])
  2069. return f"g{find_unique_abbrev(r.object_store, latest_commit.id)}"
  2070. # We're now 0 commits from the top
  2071. commit_count = 0
  2072. # Walk through all commits
  2073. walker = r.get_walker()
  2074. for entry in walker:
  2075. # Check if tag
  2076. commit_id = entry.commit.id.decode("ascii")
  2077. for tag in sorted_tags:
  2078. tag_name = tag[0]
  2079. tag_commit = tag[1][1]
  2080. if commit_id == tag_commit:
  2081. if commit_count == 0:
  2082. return tag_name
  2083. else:
  2084. return "{}-{}-g{}".format(
  2085. tag_name,
  2086. commit_count,
  2087. latest_commit.id.decode("ascii")[abbrev_slice],
  2088. )
  2089. commit_count += 1
  2090. # Return plain commit if no parent tag can be found
  2091. return "g{}".format(latest_commit.id.decode("ascii")[abbrev_slice])
  2092. def get_object_by_path(repo, path, committish=None):
  2093. """Get an object by path.
  2094. Args:
  2095. repo: A path to the repository
  2096. path: Path to look up
  2097. committish: Commit to look up path in
  2098. Returns: A `ShaFile` object
  2099. """
  2100. if committish is None:
  2101. committish = "HEAD"
  2102. # Get the repository
  2103. with open_repo_closing(repo) as r:
  2104. commit = parse_commit(r, committish)
  2105. base_tree = commit.tree
  2106. if not isinstance(path, bytes):
  2107. path = commit_encode(commit, path)
  2108. (mode, sha) = tree_lookup_path(r.object_store.__getitem__, base_tree, path)
  2109. return r[sha]
  2110. def write_tree(repo):
  2111. """Write a tree object from the index.
  2112. Args:
  2113. repo: Repository for which to write tree
  2114. Returns: tree id for the tree that was written
  2115. """
  2116. with open_repo_closing(repo) as r:
  2117. return r.open_index().commit(r.object_store)
  2118. def _do_merge(
  2119. r,
  2120. merge_commit_id,
  2121. no_commit=False,
  2122. no_ff=False,
  2123. message=None,
  2124. author=None,
  2125. committer=None,
  2126. ):
  2127. """Internal merge implementation that operates on an open repository.
  2128. Args:
  2129. r: Open repository object
  2130. merge_commit_id: SHA of commit to merge
  2131. no_commit: If True, do not create a merge commit
  2132. no_ff: If True, force creation of a merge commit
  2133. message: Optional merge commit message
  2134. author: Optional author for merge commit
  2135. committer: Optional committer for merge commit
  2136. Returns:
  2137. Tuple of (merge_commit_sha, conflicts) where merge_commit_sha is None
  2138. if no_commit=True or there were conflicts
  2139. """
  2140. from .graph import find_merge_base
  2141. from .merge import three_way_merge
  2142. # Get HEAD commit
  2143. try:
  2144. head_commit_id = r.refs[b"HEAD"]
  2145. except KeyError:
  2146. raise Error("No HEAD reference found")
  2147. head_commit = r[head_commit_id]
  2148. merge_commit = r[merge_commit_id]
  2149. # Check if fast-forward is possible
  2150. merge_bases = find_merge_base(r, [head_commit_id, merge_commit_id])
  2151. if not merge_bases:
  2152. raise Error("No common ancestor found")
  2153. # Use the first merge base
  2154. base_commit_id = merge_bases[0]
  2155. # Check for fast-forward
  2156. if base_commit_id == head_commit_id and not no_ff:
  2157. # Fast-forward merge
  2158. r.refs[b"HEAD"] = merge_commit_id
  2159. # Update the working directory
  2160. update_working_tree(r, head_commit.tree, merge_commit.tree)
  2161. return (merge_commit_id, [])
  2162. if base_commit_id == merge_commit_id:
  2163. # Already up to date
  2164. return (None, [])
  2165. # Perform three-way merge
  2166. base_commit = r[base_commit_id]
  2167. merged_tree, conflicts = three_way_merge(
  2168. r.object_store, base_commit, head_commit, merge_commit
  2169. )
  2170. # Add merged tree to object store
  2171. r.object_store.add_object(merged_tree)
  2172. # Update index and working directory
  2173. update_working_tree(r, head_commit.tree, merged_tree.id)
  2174. if conflicts or no_commit:
  2175. # Don't create a commit if there are conflicts or no_commit is True
  2176. return (None, conflicts)
  2177. # Create merge commit
  2178. merge_commit_obj = Commit()
  2179. merge_commit_obj.tree = merged_tree.id
  2180. merge_commit_obj.parents = [head_commit_id, merge_commit_id]
  2181. # Set author/committer
  2182. if author is None:
  2183. author = get_user_identity(r.get_config_stack())
  2184. if committer is None:
  2185. committer = author
  2186. merge_commit_obj.author = author
  2187. merge_commit_obj.committer = committer
  2188. # Set timestamps
  2189. timestamp = int(time.time())
  2190. timezone = 0 # UTC
  2191. merge_commit_obj.author_time = timestamp
  2192. merge_commit_obj.author_timezone = timezone
  2193. merge_commit_obj.commit_time = timestamp
  2194. merge_commit_obj.commit_timezone = timezone
  2195. # Set commit message
  2196. if message is None:
  2197. message = f"Merge commit '{merge_commit_id.decode()[:7]}'\n"
  2198. merge_commit_obj.message = message.encode() if isinstance(message, str) else message
  2199. # Add commit to object store
  2200. r.object_store.add_object(merge_commit_obj)
  2201. # Update HEAD
  2202. r.refs[b"HEAD"] = merge_commit_obj.id
  2203. return (merge_commit_obj.id, [])
  2204. def merge(
  2205. repo,
  2206. committish,
  2207. no_commit=False,
  2208. no_ff=False,
  2209. message=None,
  2210. author=None,
  2211. committer=None,
  2212. ):
  2213. """Merge a commit into the current branch.
  2214. Args:
  2215. repo: Repository to merge into
  2216. committish: Commit to merge
  2217. no_commit: If True, do not create a merge commit
  2218. no_ff: If True, force creation of a merge commit
  2219. message: Optional merge commit message
  2220. author: Optional author for merge commit
  2221. committer: Optional committer for merge commit
  2222. Returns:
  2223. Tuple of (merge_commit_sha, conflicts) where merge_commit_sha is None
  2224. if no_commit=True or there were conflicts
  2225. Raises:
  2226. Error: If there is no HEAD reference or commit cannot be found
  2227. """
  2228. with open_repo_closing(repo) as r:
  2229. # Parse the commit to merge
  2230. try:
  2231. merge_commit_id = parse_commit(r, committish)
  2232. except KeyError:
  2233. raise Error(f"Cannot find commit '{committish}'")
  2234. return _do_merge(
  2235. r, merge_commit_id, no_commit, no_ff, message, author, committer
  2236. )