porcelain.py 84 KB

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