porcelain.py 88 KB

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