porcelain.py 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  1. # porcelain.py -- Porcelain-like layer on top of Dulwich
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Simple wrapper that provides porcelain-like functions on top of Dulwich.
  21. Currently implemented:
  22. * archive
  23. * add
  24. * branch{_create,_delete,_list}
  25. * check-ignore
  26. * checkout
  27. * clone
  28. * commit
  29. * commit-tree
  30. * daemon
  31. * describe
  32. * diff-tree
  33. * fetch
  34. * init
  35. * ls-files
  36. * ls-remote
  37. * ls-tree
  38. * pull
  39. * push
  40. * rm
  41. * remote{_add}
  42. * receive-pack
  43. * reset
  44. * rev-list
  45. * tag{_create,_delete,_list}
  46. * upload-pack
  47. * update-server-info
  48. * status
  49. * symbolic-ref
  50. These functions are meant to behave similarly to the git subcommands.
  51. Differences in behaviour are considered bugs.
  52. Note: one of the consequences of this is that paths tend to be
  53. interpreted relative to the current working directory rather than relative
  54. to the repository root.
  55. Functions should generally accept both unicode strings and bytestrings
  56. """
  57. from collections import namedtuple
  58. from contextlib import (
  59. closing,
  60. contextmanager,
  61. )
  62. from io import BytesIO, RawIOBase
  63. import datetime
  64. import os
  65. from pathlib import Path
  66. import posixpath
  67. import stat
  68. import sys
  69. import time
  70. from typing import (
  71. Dict,
  72. Optional,
  73. Tuple,
  74. Union,
  75. )
  76. from dulwich.archive import (
  77. tar_stream,
  78. )
  79. from dulwich.client import (
  80. get_transport_and_path,
  81. )
  82. from dulwich.clone import (
  83. do_clone,
  84. )
  85. from dulwich.config import (
  86. StackedConfig,
  87. )
  88. from dulwich.diff_tree import (
  89. CHANGE_ADD,
  90. CHANGE_DELETE,
  91. CHANGE_MODIFY,
  92. CHANGE_RENAME,
  93. CHANGE_COPY,
  94. RENAME_CHANGE_TYPES,
  95. )
  96. from dulwich.errors import (
  97. SendPackError,
  98. )
  99. from dulwich.graph import (
  100. can_fast_forward,
  101. )
  102. from dulwich.ignore import IgnoreFilterManager
  103. from dulwich.index import (
  104. blob_from_path_and_stat,
  105. get_unstaged_changes,
  106. build_file_from_blob,
  107. _fs_to_tree_path,
  108. )
  109. from dulwich.object_store import (
  110. tree_lookup_path,
  111. )
  112. from dulwich.objects import (
  113. Commit,
  114. Tag,
  115. format_timezone,
  116. parse_timezone,
  117. pretty_format_tree_entry,
  118. )
  119. from dulwich.objectspec import (
  120. parse_commit,
  121. parse_object,
  122. parse_ref,
  123. parse_reftuples,
  124. parse_tree,
  125. )
  126. from dulwich.pack import (
  127. write_pack_index,
  128. write_pack_objects,
  129. )
  130. from dulwich.patch import write_tree_diff
  131. from dulwich.protocol import (
  132. Protocol,
  133. ZERO_SHA,
  134. )
  135. from dulwich.refs import (
  136. ANNOTATED_TAG_SUFFIX,
  137. LOCAL_BRANCH_PREFIX,
  138. LOCAL_TAG_PREFIX,
  139. strip_peeled_refs,
  140. RefsContainer,
  141. )
  142. from dulwich.repo import BaseRepo, Repo
  143. from dulwich.server import (
  144. FileSystemBackend,
  145. TCPGitServer,
  146. ReceivePackHandler,
  147. UploadPackHandler,
  148. update_server_info as server_update_server_info,
  149. )
  150. # Module level tuple definition for status output
  151. GitStatus = namedtuple("GitStatus", "staged unstaged untracked")
  152. class NoneStream(RawIOBase):
  153. """Fallback if stdout or stderr are unavailable, does nothing."""
  154. def read(self, size=-1):
  155. return None
  156. def readall(self):
  157. return None
  158. def readinto(self, b):
  159. return None
  160. def write(self, b):
  161. return None
  162. default_bytes_out_stream = getattr(sys.stdout, "buffer", None) or NoneStream()
  163. default_bytes_err_stream = getattr(sys.stderr, "buffer", None) or NoneStream()
  164. DEFAULT_ENCODING = "utf-8"
  165. class Error(Exception):
  166. """Porcelain-based error. """
  167. def __init__(self, msg, inner=None):
  168. super(Error, self).__init__(msg)
  169. self.inner = inner
  170. class RemoteExists(Error):
  171. """Raised when the remote already exists."""
  172. def open_repo(path_or_repo):
  173. """Open an argument that can be a repository or a path for a repository."""
  174. if isinstance(path_or_repo, BaseRepo):
  175. return path_or_repo
  176. return Repo(path_or_repo)
  177. @contextmanager
  178. def _noop_context_manager(obj):
  179. """Context manager that has the same api as closing but does nothing."""
  180. yield obj
  181. def open_repo_closing(path_or_repo):
  182. """Open an argument that can be a repository or a path for a repository.
  183. returns a context manager that will close the repo on exit if the argument
  184. is a path, else does nothing if the argument is a repo.
  185. """
  186. if isinstance(path_or_repo, BaseRepo):
  187. return _noop_context_manager(path_or_repo)
  188. return closing(Repo(path_or_repo))
  189. def path_to_tree_path(repopath, path, tree_encoding=DEFAULT_ENCODING):
  190. """Convert a path to a path usable in an index, e.g. bytes and relative to
  191. the repository root.
  192. Args:
  193. repopath: Repository path, absolute or relative to the cwd
  194. path: A path, absolute or relative to the cwd
  195. Returns: A path formatted for use in e.g. an index
  196. """
  197. # Resolve might returns a relative path on Windows
  198. # https://bugs.python.org/issue38671
  199. if sys.platform == "win32":
  200. path = os.path.abspath(path)
  201. path = Path(path)
  202. resolved_path = path.resolve()
  203. # Resolve and abspath seems to behave differently regarding symlinks,
  204. # as we are doing abspath on the file path, we need to do the same on
  205. # the repo path or they might not match
  206. if sys.platform == "win32":
  207. repopath = os.path.abspath(repopath)
  208. repopath = Path(repopath).resolve()
  209. try:
  210. relpath = resolved_path.relative_to(repopath)
  211. except ValueError:
  212. # If path is a symlink that points to a file outside the repo, we
  213. # want the relpath for the link itself, not the resolved target
  214. if path.is_symlink():
  215. parent = path.parent.resolve()
  216. relpath = (parent / path.name).relative_to(repopath)
  217. else:
  218. raise
  219. if sys.platform == "win32":
  220. return str(relpath).replace(os.path.sep, "/").encode(tree_encoding)
  221. else:
  222. return bytes(relpath)
  223. class DivergedBranches(Error):
  224. """Branches have diverged and fast-forward is not possible."""
  225. def check_diverged(repo, current_sha, new_sha):
  226. """Check if updating to a sha can be done with fast forwarding.
  227. Args:
  228. repo: Repository object
  229. current_sha: Current head sha
  230. new_sha: New head sha
  231. """
  232. try:
  233. can = can_fast_forward(repo, current_sha, new_sha)
  234. except KeyError:
  235. can = False
  236. if not can:
  237. raise DivergedBranches(current_sha, new_sha)
  238. def archive(
  239. repo,
  240. committish=None,
  241. outstream=default_bytes_out_stream,
  242. errstream=default_bytes_err_stream,
  243. ):
  244. """Create an archive.
  245. Args:
  246. repo: Path of repository for which to generate an archive.
  247. committish: Commit SHA1 or ref to use
  248. outstream: Output stream (defaults to stdout)
  249. errstream: Error stream (defaults to stderr)
  250. """
  251. if committish is None:
  252. committish = "HEAD"
  253. with open_repo_closing(repo) as repo_obj:
  254. c = parse_commit(repo_obj, committish)
  255. for chunk in tar_stream(
  256. repo_obj.object_store, repo_obj.object_store[c.tree], c.commit_time
  257. ):
  258. outstream.write(chunk)
  259. def update_server_info(repo="."):
  260. """Update server info files for a repository.
  261. Args:
  262. repo: path to the repository
  263. """
  264. with open_repo_closing(repo) as r:
  265. server_update_server_info(r)
  266. def symbolic_ref(repo, ref_name, force=False):
  267. """Set git symbolic ref into HEAD.
  268. Args:
  269. repo: path to the repository
  270. ref_name: short name of the new ref
  271. force: force settings without checking if it exists in refs/heads
  272. """
  273. with open_repo_closing(repo) as repo_obj:
  274. ref_path = _make_branch_ref(ref_name)
  275. if not force and ref_path not in repo_obj.refs.keys():
  276. raise Error("fatal: ref `%s` is not a ref" % ref_name)
  277. repo_obj.refs.set_symbolic_ref(b"HEAD", ref_path)
  278. def commit(
  279. repo=".",
  280. message=None,
  281. author=None,
  282. committer=None,
  283. encoding=None,
  284. no_verify=False,
  285. ):
  286. """Create a new commit.
  287. Args:
  288. repo: Path to repository
  289. message: Optional commit message
  290. author: Optional author name and email
  291. committer: Optional committer name and email
  292. no_verify: Skip pre-commit and commit-msg hooks
  293. Returns: SHA1 of the new commit
  294. """
  295. # FIXME: Support --all argument
  296. # FIXME: Support --signoff argument
  297. if getattr(message, "encode", None):
  298. message = message.encode(encoding or DEFAULT_ENCODING)
  299. if getattr(author, "encode", None):
  300. author = author.encode(encoding or DEFAULT_ENCODING)
  301. if getattr(committer, "encode", None):
  302. committer = committer.encode(encoding or DEFAULT_ENCODING)
  303. with open_repo_closing(repo) as r:
  304. return r.do_commit(
  305. message=message,
  306. author=author,
  307. committer=committer,
  308. encoding=encoding,
  309. no_verify=no_verify,
  310. )
  311. def commit_tree(repo, tree, message=None, author=None, committer=None):
  312. """Create a new commit object.
  313. Args:
  314. repo: Path to repository
  315. tree: An existing tree object
  316. author: Optional author name and email
  317. committer: Optional committer name and email
  318. """
  319. with open_repo_closing(repo) as r:
  320. return r.do_commit(
  321. message=message, tree=tree, committer=committer, author=author
  322. )
  323. def init(path=".", bare=False):
  324. """Create a new git repository.
  325. Args:
  326. path: Path to repository.
  327. bare: Whether to create a bare repository.
  328. Returns: A Repo instance
  329. """
  330. if not os.path.exists(path):
  331. os.mkdir(path)
  332. if bare:
  333. return Repo.init_bare(path)
  334. else:
  335. return Repo.init(path)
  336. def clone(
  337. source,
  338. target=None,
  339. bare=False,
  340. checkout=None,
  341. errstream=default_bytes_err_stream,
  342. outstream=None,
  343. origin=b"origin",
  344. depth=None,
  345. branch=None,
  346. **kwargs
  347. ):
  348. """Clone a local or remote git repository.
  349. Args:
  350. source: Path or URL for source repository
  351. target: Path to target repository (optional)
  352. bare: Whether or not to create a bare repository
  353. checkout: Whether or not to check-out HEAD after cloning
  354. errstream: Optional stream to write progress to
  355. outstream: Optional stream to write progress to (deprecated)
  356. origin: Name of remote from the repository used to clone
  357. depth: Depth to fetch at
  358. branch: Optional branch or tag to be used as HEAD in the new repository
  359. instead of the cloned repository's HEAD.
  360. Returns: The new repository
  361. """
  362. if outstream is not None:
  363. import warnings
  364. warnings.warn(
  365. "outstream= has been deprecated in favour of errstream=.",
  366. DeprecationWarning,
  367. stacklevel=3,
  368. )
  369. errstream = outstream
  370. if checkout is None:
  371. checkout = not bare
  372. if checkout and bare:
  373. raise Error("checkout and bare are incompatible")
  374. if target is None:
  375. target = source.split("/")[-1]
  376. mkdir = not os.path.exists(target)
  377. if not isinstance(source, bytes):
  378. source = source.encode(DEFAULT_ENCODING)
  379. def clone_refs(target_repo, ref_message):
  380. fetch_result = fetch(
  381. target_repo,
  382. origin,
  383. errstream=errstream,
  384. message=ref_message,
  385. depth=depth,
  386. **kwargs
  387. )
  388. head_ref = fetch_result.symrefs.get(b"HEAD", None)
  389. try:
  390. head_sha = target_repo[fetch_result.refs[b"HEAD"]].id
  391. except KeyError:
  392. head_sha = None
  393. return head_ref, head_sha
  394. return do_clone(
  395. source,
  396. target,
  397. clone_refs=clone_refs,
  398. mkdir=mkdir,
  399. bare=bare,
  400. origin=origin,
  401. checkout=checkout,
  402. errstream=errstream,
  403. branch=branch,
  404. )
  405. def add(repo=".", paths=None):
  406. """Add files to the staging area.
  407. Args:
  408. repo: Repository for the files
  409. paths: Paths to add. No value passed stages all modified files.
  410. Returns: Tuple with set of added files and ignored files
  411. If the repository contains ignored directories, the returned set will
  412. contain the path to an ignored directory (with trailing slash). Individual
  413. files within ignored directories will not be returned.
  414. """
  415. ignored = set()
  416. with open_repo_closing(repo) as r:
  417. repo_path = Path(r.path).resolve()
  418. ignore_manager = IgnoreFilterManager.from_repo(r)
  419. if not paths:
  420. paths = list(
  421. get_untracked_paths(
  422. str(Path(os.getcwd()).resolve()),
  423. str(repo_path),
  424. r.open_index(),
  425. )
  426. )
  427. relpaths = []
  428. if not isinstance(paths, list):
  429. paths = [paths]
  430. for p in paths:
  431. path = Path(p)
  432. relpath = str(path.resolve().relative_to(repo_path))
  433. # FIXME: Support patterns
  434. if path.is_dir():
  435. relpath = os.path.join(relpath, "")
  436. if ignore_manager.is_ignored(relpath):
  437. ignored.add(relpath)
  438. continue
  439. relpaths.append(relpath)
  440. r.stage(relpaths)
  441. return (relpaths, ignored)
  442. def _is_subdir(subdir, parentdir):
  443. """Check whether subdir is parentdir or a subdir of parentdir
  444. If parentdir or subdir is a relative path, it will be disamgibuated
  445. relative to the pwd.
  446. """
  447. parentdir_abs = os.path.realpath(parentdir) + os.path.sep
  448. subdir_abs = os.path.realpath(subdir) + os.path.sep
  449. return subdir_abs.startswith(parentdir_abs)
  450. # TODO: option to remove ignored files also, in line with `git clean -fdx`
  451. def clean(repo=".", target_dir=None):
  452. """Remove any untracked files from the target directory recursively
  453. Equivalent to running `git clean -fd` in target_dir.
  454. Args:
  455. repo: Repository where the files may be tracked
  456. target_dir: Directory to clean - current directory if None
  457. """
  458. if target_dir is None:
  459. target_dir = os.getcwd()
  460. with open_repo_closing(repo) as r:
  461. if not _is_subdir(target_dir, r.path):
  462. raise Error("target_dir must be in the repo's working dir")
  463. config = r.get_config_stack()
  464. require_force = config.get_boolean( # noqa: F841
  465. (b"clean",), b"requireForce", True
  466. )
  467. # TODO(jelmer): if require_force is set, then make sure that -f, -i or
  468. # -n is specified.
  469. index = r.open_index()
  470. ignore_manager = IgnoreFilterManager.from_repo(r)
  471. paths_in_wd = _walk_working_dir_paths(target_dir, r.path)
  472. # Reverse file visit order, so that files and subdirectories are
  473. # removed before containing directory
  474. for ap, is_dir in reversed(list(paths_in_wd)):
  475. if is_dir:
  476. # All subdirectories and files have been removed if untracked,
  477. # so dir contains no tracked files iff it is empty.
  478. is_empty = len(os.listdir(ap)) == 0
  479. if is_empty:
  480. os.rmdir(ap)
  481. else:
  482. ip = path_to_tree_path(r.path, ap)
  483. is_tracked = ip in index
  484. rp = os.path.relpath(ap, r.path)
  485. is_ignored = ignore_manager.is_ignored(rp)
  486. if not is_tracked and not is_ignored:
  487. os.remove(ap)
  488. def remove(repo=".", paths=None, cached=False):
  489. """Remove files from the staging area.
  490. Args:
  491. repo: Repository for the files
  492. paths: Paths to remove
  493. """
  494. with open_repo_closing(repo) as r:
  495. index = r.open_index()
  496. for p in paths:
  497. full_path = os.fsencode(os.path.abspath(p))
  498. tree_path = path_to_tree_path(r.path, p)
  499. try:
  500. index_sha = index[tree_path].sha
  501. except KeyError:
  502. raise Error("%s did not match any files" % p)
  503. if not cached:
  504. try:
  505. st = os.lstat(full_path)
  506. except OSError:
  507. pass
  508. else:
  509. try:
  510. blob = blob_from_path_and_stat(full_path, st)
  511. except IOError:
  512. pass
  513. else:
  514. try:
  515. committed_sha = tree_lookup_path(
  516. r.__getitem__, r[r.head()].tree, tree_path
  517. )[1]
  518. except KeyError:
  519. committed_sha = None
  520. if blob.id != index_sha and index_sha != committed_sha:
  521. raise Error(
  522. "file has staged content differing "
  523. "from both the file and head: %s" % p
  524. )
  525. if index_sha != committed_sha:
  526. raise Error("file has staged changes: %s" % p)
  527. os.remove(full_path)
  528. del index[tree_path]
  529. index.write()
  530. rm = remove
  531. def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
  532. if commit.encoding:
  533. encoding = commit.encoding.decode("ascii")
  534. else:
  535. encoding = default_encoding
  536. return contents.decode(encoding, "replace")
  537. def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
  538. if commit.encoding:
  539. encoding = commit.encoding.decode("ascii")
  540. else:
  541. encoding = default_encoding
  542. return contents.encode(encoding)
  543. def print_commit(commit, decode, outstream=sys.stdout):
  544. """Write a human-readable commit log entry.
  545. Args:
  546. commit: A `Commit` object
  547. outstream: A stream file to write to
  548. """
  549. outstream.write("-" * 50 + "\n")
  550. outstream.write("commit: " + commit.id.decode("ascii") + "\n")
  551. if len(commit.parents) > 1:
  552. outstream.write(
  553. "merge: "
  554. + "...".join([c.decode("ascii") for c in commit.parents[1:]])
  555. + "\n"
  556. )
  557. outstream.write("Author: " + decode(commit.author) + "\n")
  558. if commit.author != commit.committer:
  559. outstream.write("Committer: " + decode(commit.committer) + "\n")
  560. time_tuple = time.gmtime(commit.author_time + commit.author_timezone)
  561. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  562. timezone_str = format_timezone(commit.author_timezone).decode("ascii")
  563. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  564. outstream.write("\n")
  565. outstream.write(decode(commit.message) + "\n")
  566. outstream.write("\n")
  567. def print_tag(tag, decode, outstream=sys.stdout):
  568. """Write a human-readable tag.
  569. Args:
  570. tag: A `Tag` object
  571. decode: Function for decoding bytes to unicode string
  572. outstream: A stream to write to
  573. """
  574. outstream.write("Tagger: " + decode(tag.tagger) + "\n")
  575. time_tuple = time.gmtime(tag.tag_time + tag.tag_timezone)
  576. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  577. timezone_str = format_timezone(tag.tag_timezone).decode("ascii")
  578. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  579. outstream.write("\n")
  580. outstream.write(decode(tag.message))
  581. outstream.write("\n")
  582. def show_blob(repo, blob, decode, outstream=sys.stdout):
  583. """Write a blob to a stream.
  584. Args:
  585. repo: A `Repo` object
  586. blob: A `Blob` object
  587. decode: Function for decoding bytes to unicode string
  588. outstream: A stream file to write to
  589. """
  590. outstream.write(decode(blob.data))
  591. def show_commit(repo, commit, decode, outstream=sys.stdout):
  592. """Show a commit to a stream.
  593. Args:
  594. repo: A `Repo` object
  595. commit: A `Commit` object
  596. decode: Function for decoding bytes to unicode string
  597. outstream: Stream to write to
  598. """
  599. print_commit(commit, decode=decode, outstream=outstream)
  600. if commit.parents:
  601. parent_commit = repo[commit.parents[0]]
  602. base_tree = parent_commit.tree
  603. else:
  604. base_tree = None
  605. diffstream = BytesIO()
  606. write_tree_diff(diffstream, repo.object_store, base_tree, commit.tree)
  607. diffstream.seek(0)
  608. outstream.write(commit_decode(commit, diffstream.getvalue()))
  609. def show_tree(repo, tree, decode, outstream=sys.stdout):
  610. """Print a tree to a stream.
  611. Args:
  612. repo: A `Repo` object
  613. tree: A `Tree` object
  614. decode: Function for decoding bytes to unicode string
  615. outstream: Stream to write to
  616. """
  617. for n in tree:
  618. outstream.write(decode(n) + "\n")
  619. def show_tag(repo, tag, decode, outstream=sys.stdout):
  620. """Print a tag to a stream.
  621. Args:
  622. repo: A `Repo` object
  623. tag: A `Tag` object
  624. decode: Function for decoding bytes to unicode string
  625. outstream: Stream to write to
  626. """
  627. print_tag(tag, decode, outstream)
  628. show_object(repo, repo[tag.object[1]], decode, outstream)
  629. def show_object(repo, obj, decode, outstream):
  630. return {
  631. b"tree": show_tree,
  632. b"blob": show_blob,
  633. b"commit": show_commit,
  634. b"tag": show_tag,
  635. }[obj.type_name](repo, obj, decode, outstream)
  636. def print_name_status(changes):
  637. """Print a simple status summary, listing changed files."""
  638. for change in changes:
  639. if not change:
  640. continue
  641. if isinstance(change, list):
  642. change = change[0]
  643. if change.type == CHANGE_ADD:
  644. path1 = change.new.path
  645. path2 = ""
  646. kind = "A"
  647. elif change.type == CHANGE_DELETE:
  648. path1 = change.old.path
  649. path2 = ""
  650. kind = "D"
  651. elif change.type == CHANGE_MODIFY:
  652. path1 = change.new.path
  653. path2 = ""
  654. kind = "M"
  655. elif change.type in RENAME_CHANGE_TYPES:
  656. path1 = change.old.path
  657. path2 = change.new.path
  658. if change.type == CHANGE_RENAME:
  659. kind = "R"
  660. elif change.type == CHANGE_COPY:
  661. kind = "C"
  662. yield "%-8s%-20s%-20s" % (kind, path1, path2)
  663. def log(
  664. repo=".",
  665. paths=None,
  666. outstream=sys.stdout,
  667. max_entries=None,
  668. reverse=False,
  669. name_status=False,
  670. ):
  671. """Write commit logs.
  672. Args:
  673. repo: Path to repository
  674. paths: Optional set of specific paths to print entries for
  675. outstream: Stream to write log output to
  676. reverse: Reverse order in which entries are printed
  677. name_status: Print name status
  678. max_entries: Optional maximum number of entries to display
  679. """
  680. with open_repo_closing(repo) as r:
  681. walker = r.get_walker(max_entries=max_entries, paths=paths, reverse=reverse)
  682. for entry in walker:
  683. def decode(x):
  684. return commit_decode(entry.commit, x)
  685. print_commit(entry.commit, decode, outstream)
  686. if name_status:
  687. outstream.writelines(
  688. [line + "\n" for line in print_name_status(entry.changes())]
  689. )
  690. # TODO(jelmer): better default for encoding?
  691. def show(
  692. repo=".",
  693. objects=None,
  694. outstream=sys.stdout,
  695. default_encoding=DEFAULT_ENCODING,
  696. ):
  697. """Print the changes in a commit.
  698. Args:
  699. repo: Path to repository
  700. objects: Objects to show (defaults to [HEAD])
  701. outstream: Stream to write to
  702. default_encoding: Default encoding to use if none is set in the
  703. commit
  704. """
  705. if objects is None:
  706. objects = ["HEAD"]
  707. if not isinstance(objects, list):
  708. objects = [objects]
  709. with open_repo_closing(repo) as r:
  710. for objectish in objects:
  711. o = parse_object(r, objectish)
  712. if isinstance(o, Commit):
  713. def decode(x):
  714. return commit_decode(o, x, default_encoding)
  715. else:
  716. def decode(x):
  717. return x.decode(default_encoding)
  718. show_object(r, o, decode, outstream)
  719. def diff_tree(repo, old_tree, new_tree, outstream=sys.stdout):
  720. """Compares the content and mode of blobs found via two tree objects.
  721. Args:
  722. repo: Path to repository
  723. old_tree: Id of old tree
  724. new_tree: Id of new tree
  725. outstream: Stream to write to
  726. """
  727. with open_repo_closing(repo) as r:
  728. write_tree_diff(outstream, r.object_store, old_tree, new_tree)
  729. def rev_list(repo, commits, outstream=sys.stdout):
  730. """Lists commit objects in reverse chronological order.
  731. Args:
  732. repo: Path to repository
  733. commits: Commits over which to iterate
  734. outstream: Stream to write to
  735. """
  736. with open_repo_closing(repo) as r:
  737. for entry in r.get_walker(include=[r[c].id for c in commits]):
  738. outstream.write(entry.commit.id + b"\n")
  739. def tag(*args, **kwargs):
  740. import warnings
  741. warnings.warn(
  742. "tag has been deprecated in favour of tag_create.", DeprecationWarning
  743. )
  744. return tag_create(*args, **kwargs)
  745. def tag_create(
  746. repo,
  747. tag,
  748. author=None,
  749. message=None,
  750. annotated=False,
  751. objectish="HEAD",
  752. tag_time=None,
  753. tag_timezone=None,
  754. sign=False,
  755. ):
  756. """Creates a tag in git via dulwich calls:
  757. Args:
  758. repo: Path to repository
  759. tag: tag string
  760. author: tag author (optional, if annotated is set)
  761. message: tag message (optional)
  762. annotated: whether to create an annotated tag
  763. objectish: object the tag should point at, defaults to HEAD
  764. tag_time: Optional time for annotated tag
  765. tag_timezone: Optional timezone for annotated tag
  766. sign: GPG Sign the tag (bool, defaults to False,
  767. pass True to use default GPG key,
  768. pass a str containing Key ID to use a specific GPG key)
  769. """
  770. with open_repo_closing(repo) as r:
  771. object = parse_object(r, objectish)
  772. if annotated:
  773. # Create the tag object
  774. tag_obj = Tag()
  775. if author is None:
  776. # TODO(jelmer): Don't use repo private method.
  777. author = r._get_user_identity(r.get_config_stack())
  778. tag_obj.tagger = author
  779. tag_obj.message = message + "\n".encode()
  780. tag_obj.name = tag
  781. tag_obj.object = (type(object), object.id)
  782. if tag_time is None:
  783. tag_time = int(time.time())
  784. tag_obj.tag_time = tag_time
  785. if tag_timezone is None:
  786. # TODO(jelmer) Use current user timezone rather than UTC
  787. tag_timezone = 0
  788. elif isinstance(tag_timezone, str):
  789. tag_timezone = parse_timezone(tag_timezone)
  790. tag_obj.tag_timezone = tag_timezone
  791. if sign:
  792. tag_obj.sign(sign if isinstance(sign, str) else None)
  793. r.object_store.add_object(tag_obj)
  794. tag_id = tag_obj.id
  795. else:
  796. tag_id = object.id
  797. r.refs[_make_tag_ref(tag)] = tag_id
  798. def list_tags(*args, **kwargs):
  799. import warnings
  800. warnings.warn(
  801. "list_tags has been deprecated in favour of tag_list.",
  802. DeprecationWarning,
  803. )
  804. return tag_list(*args, **kwargs)
  805. def tag_list(repo, outstream=sys.stdout):
  806. """List all tags.
  807. Args:
  808. repo: Path to repository
  809. outstream: Stream to write tags to
  810. """
  811. with open_repo_closing(repo) as r:
  812. tags = sorted(r.refs.as_dict(b"refs/tags"))
  813. return tags
  814. def tag_delete(repo, name):
  815. """Remove a tag.
  816. Args:
  817. repo: Path to repository
  818. name: Name of tag to remove
  819. """
  820. with open_repo_closing(repo) as r:
  821. if isinstance(name, bytes):
  822. names = [name]
  823. elif isinstance(name, list):
  824. names = name
  825. else:
  826. raise Error("Unexpected tag name type %r" % name)
  827. for name in names:
  828. del r.refs[_make_tag_ref(name)]
  829. def reset(repo, mode, treeish="HEAD"):
  830. """Reset current HEAD to the specified state.
  831. Args:
  832. repo: Path to repository
  833. mode: Mode ("hard", "soft", "mixed")
  834. treeish: Treeish to reset to
  835. """
  836. if mode != "hard":
  837. raise Error("hard is the only mode currently supported")
  838. with open_repo_closing(repo) as r:
  839. tree = parse_tree(r, treeish)
  840. r.reset_index(tree.id)
  841. def get_remote_repo(
  842. repo: Repo, remote_location: Optional[Union[str, bytes]] = None
  843. ) -> Tuple[Optional[str], str]:
  844. config = repo.get_config()
  845. if remote_location is None:
  846. remote_location = get_branch_remote(repo)
  847. if isinstance(remote_location, str):
  848. encoded_location = remote_location.encode()
  849. else:
  850. encoded_location = remote_location
  851. section = (b"remote", encoded_location)
  852. remote_name = None # type: Optional[str]
  853. if config.has_section(section):
  854. remote_name = encoded_location.decode()
  855. url = config.get(section, "url")
  856. encoded_location = url
  857. else:
  858. remote_name = None
  859. return (remote_name, encoded_location.decode())
  860. def push(
  861. repo,
  862. remote_location=None,
  863. refspecs=None,
  864. outstream=default_bytes_out_stream,
  865. errstream=default_bytes_err_stream,
  866. force=False,
  867. **kwargs
  868. ):
  869. """Remote push with dulwich via dulwich.client
  870. Args:
  871. repo: Path to repository
  872. remote_location: Location of the remote
  873. refspecs: Refs to push to remote
  874. outstream: A stream file to write output
  875. errstream: A stream file to write errors
  876. force: Force overwriting refs
  877. """
  878. # Open the repo
  879. with open_repo_closing(repo) as r:
  880. if refspecs is None:
  881. refspecs = [active_branch(r)]
  882. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  883. # Get the client and path
  884. client, path = get_transport_and_path(
  885. remote_location, config=r.get_config_stack(), **kwargs
  886. )
  887. selected_refs = []
  888. remote_changed_refs = {}
  889. def update_refs(refs):
  890. selected_refs.extend(parse_reftuples(r.refs, refs, refspecs, force=force))
  891. new_refs = {}
  892. # TODO: Handle selected_refs == {None: None}
  893. for (lh, rh, force_ref) in selected_refs:
  894. if lh is None:
  895. new_refs[rh] = ZERO_SHA
  896. remote_changed_refs[rh] = None
  897. else:
  898. try:
  899. localsha = r.refs[lh]
  900. except KeyError:
  901. raise Error("No valid ref %s in local repository" % lh)
  902. if not force_ref and rh in refs:
  903. check_diverged(r, refs[rh], localsha)
  904. new_refs[rh] = localsha
  905. remote_changed_refs[rh] = localsha
  906. return new_refs
  907. err_encoding = getattr(errstream, "encoding", None) or DEFAULT_ENCODING
  908. remote_location = client.get_url(path)
  909. try:
  910. result = client.send_pack(
  911. path,
  912. update_refs,
  913. generate_pack_data=r.generate_pack_data,
  914. progress=errstream.write,
  915. )
  916. except SendPackError as e:
  917. raise Error(
  918. "Push to " + remote_location + " failed -> " + e.args[0].decode(),
  919. inner=e,
  920. )
  921. else:
  922. errstream.write(
  923. b"Push to " + remote_location.encode(err_encoding) + b" successful.\n"
  924. )
  925. for ref, error in (result.ref_status or {}).items():
  926. if error is not None:
  927. errstream.write(
  928. b"Push of ref %s failed: %s\n" % (ref, error.encode(err_encoding))
  929. )
  930. else:
  931. errstream.write(b"Ref %s updated\n" % ref)
  932. if remote_name is not None:
  933. _import_remote_refs(r.refs, remote_name, remote_changed_refs)
  934. def pull(
  935. repo,
  936. remote_location=None,
  937. refspecs=None,
  938. outstream=default_bytes_out_stream,
  939. errstream=default_bytes_err_stream,
  940. fast_forward=True,
  941. force=False,
  942. **kwargs
  943. ):
  944. """Pull from remote via dulwich.client
  945. Args:
  946. repo: Path to repository
  947. remote_location: Location of the remote
  948. refspec: refspecs to fetch
  949. outstream: A stream file to write to output
  950. errstream: A stream file to write to errors
  951. """
  952. # Open the repo
  953. with open_repo_closing(repo) as r:
  954. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  955. if refspecs is None:
  956. refspecs = [b"HEAD"]
  957. selected_refs = []
  958. def determine_wants(remote_refs, **kwargs):
  959. selected_refs.extend(
  960. parse_reftuples(remote_refs, r.refs, refspecs, force=force)
  961. )
  962. return [
  963. remote_refs[lh]
  964. for (lh, rh, force_ref) in selected_refs
  965. if remote_refs[lh] not in r.object_store
  966. ]
  967. client, path = get_transport_and_path(
  968. remote_location, config=r.get_config_stack(), **kwargs
  969. )
  970. fetch_result = client.fetch(
  971. path, r, progress=errstream.write, determine_wants=determine_wants
  972. )
  973. for (lh, rh, force_ref) in selected_refs:
  974. try:
  975. check_diverged(r, r.refs[rh], fetch_result.refs[lh])
  976. except DivergedBranches:
  977. if fast_forward:
  978. raise
  979. else:
  980. raise NotImplementedError("merge is not yet supported")
  981. r.refs[rh] = fetch_result.refs[lh]
  982. if selected_refs:
  983. r[b"HEAD"] = fetch_result.refs[selected_refs[0][1]]
  984. # Perform 'git checkout .' - syncs staged changes
  985. tree = r[b"HEAD"].tree
  986. r.reset_index(tree=tree)
  987. if remote_name is not None:
  988. _import_remote_refs(r.refs, remote_name, fetch_result.refs)
  989. def status(repo=".", ignored=False):
  990. """Returns staged, unstaged, and untracked changes relative to the HEAD.
  991. Args:
  992. repo: Path to repository or repository object
  993. ignored: Whether to include ignored files in `untracked`
  994. Returns: GitStatus tuple,
  995. staged - dict with lists of staged paths (diff index/HEAD)
  996. unstaged - list of unstaged paths (diff index/working-tree)
  997. untracked - list of untracked, un-ignored & non-.git paths
  998. """
  999. with open_repo_closing(repo) as r:
  1000. # 1. Get status of staged
  1001. tracked_changes = get_tree_changes(r)
  1002. # 2. Get status of unstaged
  1003. index = r.open_index()
  1004. normalizer = r.get_blob_normalizer()
  1005. filter_callback = normalizer.checkin_normalize
  1006. unstaged_changes = list(get_unstaged_changes(index, r.path, filter_callback))
  1007. untracked_paths = get_untracked_paths(
  1008. r.path, r.path, index, exclude_ignored=not ignored
  1009. )
  1010. untracked_changes = list(untracked_paths)
  1011. return GitStatus(tracked_changes, unstaged_changes, untracked_changes)
  1012. def _walk_working_dir_paths(frompath, basepath, prune_dirnames=None):
  1013. """Get path, is_dir for files in working dir from frompath
  1014. Args:
  1015. frompath: Path to begin walk
  1016. basepath: Path to compare to
  1017. prune_dirnames: Optional callback to prune dirnames during os.walk
  1018. dirnames will be set to result of prune_dirnames(dirpath, dirnames)
  1019. """
  1020. for dirpath, dirnames, filenames in os.walk(frompath):
  1021. # Skip .git and below.
  1022. if ".git" in dirnames:
  1023. dirnames.remove(".git")
  1024. if dirpath != basepath:
  1025. continue
  1026. if ".git" in filenames:
  1027. filenames.remove(".git")
  1028. if dirpath != basepath:
  1029. continue
  1030. if dirpath != frompath:
  1031. yield dirpath, True
  1032. for filename in filenames:
  1033. filepath = os.path.join(dirpath, filename)
  1034. yield filepath, False
  1035. if prune_dirnames:
  1036. dirnames[:] = prune_dirnames(dirpath, dirnames)
  1037. def get_untracked_paths(frompath, basepath, index, exclude_ignored=False):
  1038. """Get untracked paths.
  1039. Args:
  1040. frompath: Path to walk
  1041. basepath: Path to compare to
  1042. index: Index to check against
  1043. exclude_ignored: Whether to exclude ignored paths
  1044. Note: ignored directories will never be walked for performance reasons.
  1045. If exclude_ignored is False, only the path to an ignored directory will
  1046. be yielded, no files inside the directory will be returned
  1047. """
  1048. with open_repo_closing(basepath) as r:
  1049. ignore_manager = IgnoreFilterManager.from_repo(r)
  1050. ignored_dirs = []
  1051. def prune_dirnames(dirpath, dirnames):
  1052. for i in range(len(dirnames) - 1, -1, -1):
  1053. path = os.path.join(dirpath, dirnames[i])
  1054. ip = os.path.join(os.path.relpath(path, basepath), "")
  1055. if ignore_manager.is_ignored(ip):
  1056. if not exclude_ignored:
  1057. ignored_dirs.append(
  1058. os.path.join(os.path.relpath(path, frompath), "")
  1059. )
  1060. del dirnames[i]
  1061. return dirnames
  1062. for ap, is_dir in _walk_working_dir_paths(
  1063. frompath, basepath, prune_dirnames=prune_dirnames
  1064. ):
  1065. if not is_dir:
  1066. ip = path_to_tree_path(basepath, ap)
  1067. if ip not in index:
  1068. if (
  1069. not exclude_ignored
  1070. or not ignore_manager.is_ignored(
  1071. os.path.relpath(ap, basepath)
  1072. )
  1073. ):
  1074. yield os.path.relpath(ap, frompath)
  1075. yield from ignored_dirs
  1076. def get_tree_changes(repo):
  1077. """Return add/delete/modify changes to tree by comparing index to HEAD.
  1078. Args:
  1079. repo: repo path or object
  1080. Returns: dict with lists for each type of change
  1081. """
  1082. with open_repo_closing(repo) as r:
  1083. index = r.open_index()
  1084. # Compares the Index to the HEAD & determines changes
  1085. # Iterate through the changes and report add/delete/modify
  1086. # TODO: call out to dulwich.diff_tree somehow.
  1087. tracked_changes = {
  1088. "add": [],
  1089. "delete": [],
  1090. "modify": [],
  1091. }
  1092. try:
  1093. tree_id = r[b"HEAD"].tree
  1094. except KeyError:
  1095. tree_id = None
  1096. for change in index.changes_from_tree(r.object_store, tree_id):
  1097. if not change[0][0]:
  1098. tracked_changes["add"].append(change[0][1])
  1099. elif not change[0][1]:
  1100. tracked_changes["delete"].append(change[0][0])
  1101. elif change[0][0] == change[0][1]:
  1102. tracked_changes["modify"].append(change[0][0])
  1103. else:
  1104. raise NotImplementedError("git mv ops not yet supported")
  1105. return tracked_changes
  1106. def daemon(path=".", address=None, port=None):
  1107. """Run a daemon serving Git requests over TCP/IP.
  1108. Args:
  1109. path: Path to the directory to serve.
  1110. address: Optional address to listen on (defaults to ::)
  1111. port: Optional port to listen on (defaults to TCP_GIT_PORT)
  1112. """
  1113. # TODO(jelmer): Support git-daemon-export-ok and --export-all.
  1114. backend = FileSystemBackend(path)
  1115. server = TCPGitServer(backend, address, port)
  1116. server.serve_forever()
  1117. def web_daemon(path=".", address=None, port=None):
  1118. """Run a daemon serving Git requests over HTTP.
  1119. Args:
  1120. path: Path to the directory to serve
  1121. address: Optional address to listen on (defaults to ::)
  1122. port: Optional port to listen on (defaults to 80)
  1123. """
  1124. from dulwich.web import (
  1125. make_wsgi_chain,
  1126. make_server,
  1127. WSGIRequestHandlerLogger,
  1128. WSGIServerLogger,
  1129. )
  1130. backend = FileSystemBackend(path)
  1131. app = make_wsgi_chain(backend)
  1132. server = make_server(
  1133. address,
  1134. port,
  1135. app,
  1136. handler_class=WSGIRequestHandlerLogger,
  1137. server_class=WSGIServerLogger,
  1138. )
  1139. server.serve_forever()
  1140. def upload_pack(path=".", inf=None, outf=None):
  1141. """Upload a pack file after negotiating its contents using smart protocol.
  1142. Args:
  1143. path: Path to the repository
  1144. inf: Input stream to communicate with client
  1145. outf: Output stream to communicate with client
  1146. """
  1147. if outf is None:
  1148. outf = getattr(sys.stdout, "buffer", sys.stdout)
  1149. if inf is None:
  1150. inf = getattr(sys.stdin, "buffer", sys.stdin)
  1151. path = os.path.expanduser(path)
  1152. backend = FileSystemBackend(path)
  1153. def send_fn(data):
  1154. outf.write(data)
  1155. outf.flush()
  1156. proto = Protocol(inf.read, send_fn)
  1157. handler = UploadPackHandler(backend, [path], proto)
  1158. # FIXME: Catch exceptions and write a single-line summary to outf.
  1159. handler.handle()
  1160. return 0
  1161. def receive_pack(path=".", inf=None, outf=None):
  1162. """Receive a pack file after negotiating its contents using smart protocol.
  1163. Args:
  1164. path: Path to the repository
  1165. inf: Input stream to communicate with client
  1166. outf: Output stream to communicate with client
  1167. """
  1168. if outf is None:
  1169. outf = getattr(sys.stdout, "buffer", sys.stdout)
  1170. if inf is None:
  1171. inf = getattr(sys.stdin, "buffer", sys.stdin)
  1172. path = os.path.expanduser(path)
  1173. backend = FileSystemBackend(path)
  1174. def send_fn(data):
  1175. outf.write(data)
  1176. outf.flush()
  1177. proto = Protocol(inf.read, send_fn)
  1178. handler = ReceivePackHandler(backend, [path], proto)
  1179. # FIXME: Catch exceptions and write a single-line summary to outf.
  1180. handler.handle()
  1181. return 0
  1182. def _make_branch_ref(name):
  1183. if getattr(name, "encode", None):
  1184. name = name.encode(DEFAULT_ENCODING)
  1185. return LOCAL_BRANCH_PREFIX + name
  1186. def _make_tag_ref(name):
  1187. if getattr(name, "encode", None):
  1188. name = name.encode(DEFAULT_ENCODING)
  1189. return LOCAL_TAG_PREFIX + name
  1190. def branch_delete(repo, name):
  1191. """Delete a branch.
  1192. Args:
  1193. repo: Path to the repository
  1194. name: Name of the branch
  1195. """
  1196. with open_repo_closing(repo) as r:
  1197. if isinstance(name, list):
  1198. names = name
  1199. else:
  1200. names = [name]
  1201. for name in names:
  1202. del r.refs[_make_branch_ref(name)]
  1203. def branch_create(repo, name, objectish=None, force=False):
  1204. """Create a branch.
  1205. Args:
  1206. repo: Path to the repository
  1207. name: Name of the new branch
  1208. objectish: Target object to point new branch at (defaults to HEAD)
  1209. force: Force creation of branch, even if it already exists
  1210. """
  1211. with open_repo_closing(repo) as r:
  1212. if objectish is None:
  1213. objectish = "HEAD"
  1214. object = parse_object(r, objectish)
  1215. refname = _make_branch_ref(name)
  1216. ref_message = b"branch: Created from " + objectish.encode("utf-8")
  1217. if force:
  1218. r.refs.set_if_equals(refname, None, object.id, message=ref_message)
  1219. else:
  1220. if not r.refs.add_if_new(refname, object.id, message=ref_message):
  1221. raise Error("Branch with name %s already exists." % name)
  1222. def branch_list(repo):
  1223. """List all branches.
  1224. Args:
  1225. repo: Path to the repository
  1226. """
  1227. with open_repo_closing(repo) as r:
  1228. return r.refs.keys(base=LOCAL_BRANCH_PREFIX)
  1229. def active_branch(repo):
  1230. """Return the active branch in the repository, if any.
  1231. Args:
  1232. repo: Repository to open
  1233. Returns:
  1234. branch name
  1235. Raises:
  1236. KeyError: if the repository does not have a working tree
  1237. IndexError: if HEAD is floating
  1238. """
  1239. with open_repo_closing(repo) as r:
  1240. active_ref = r.refs.follow(b"HEAD")[0][1]
  1241. if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
  1242. raise ValueError(active_ref)
  1243. return active_ref[len(LOCAL_BRANCH_PREFIX) :]
  1244. def get_branch_remote(repo):
  1245. """Return the active branch's remote name, if any.
  1246. Args:
  1247. repo: Repository to open
  1248. Returns:
  1249. remote name
  1250. Raises:
  1251. KeyError: if the repository does not have a working tree
  1252. """
  1253. with open_repo_closing(repo) as r:
  1254. branch_name = active_branch(r.path)
  1255. config = r.get_config()
  1256. try:
  1257. remote_name = config.get((b"branch", branch_name), b"remote")
  1258. except KeyError:
  1259. remote_name = b"origin"
  1260. return remote_name
  1261. def _import_remote_refs(
  1262. refs_container: RefsContainer,
  1263. remote_name: str,
  1264. refs: Dict[str, str],
  1265. message: Optional[bytes] = None,
  1266. prune: bool = False,
  1267. prune_tags: bool = False,
  1268. ):
  1269. stripped_refs = strip_peeled_refs(refs)
  1270. branches = {
  1271. n[len(LOCAL_BRANCH_PREFIX) :]: v
  1272. for (n, v) in stripped_refs.items()
  1273. if n.startswith(LOCAL_BRANCH_PREFIX)
  1274. }
  1275. refs_container.import_refs(
  1276. b"refs/remotes/" + remote_name.encode(),
  1277. branches,
  1278. message=message,
  1279. prune=prune,
  1280. )
  1281. tags = {
  1282. n[len(b"refs/tags/") :]: v
  1283. for (n, v) in stripped_refs.items()
  1284. if n.startswith(b"refs/tags/") and not n.endswith(ANNOTATED_TAG_SUFFIX)
  1285. }
  1286. refs_container.import_refs(b"refs/tags", tags, message=message, prune=prune_tags)
  1287. def fetch(
  1288. repo,
  1289. remote_location=None,
  1290. outstream=sys.stdout,
  1291. errstream=default_bytes_err_stream,
  1292. message=None,
  1293. depth=None,
  1294. prune=False,
  1295. prune_tags=False,
  1296. force=False,
  1297. **kwargs
  1298. ):
  1299. """Fetch objects from a remote server.
  1300. Args:
  1301. repo: Path to the repository
  1302. remote_location: String identifying a remote server
  1303. outstream: Output stream (defaults to stdout)
  1304. errstream: Error stream (defaults to stderr)
  1305. message: Reflog message (defaults to b"fetch: from <remote_name>")
  1306. depth: Depth to fetch at
  1307. prune: Prune remote removed refs
  1308. prune_tags: Prune reomte removed tags
  1309. Returns:
  1310. Dictionary with refs on the remote
  1311. """
  1312. with open_repo_closing(repo) as r:
  1313. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1314. if message is None:
  1315. message = b"fetch: from " + remote_location.encode("utf-8")
  1316. client, path = get_transport_and_path(
  1317. remote_location, config=r.get_config_stack(), **kwargs
  1318. )
  1319. fetch_result = client.fetch(path, r, progress=errstream.write, depth=depth)
  1320. if remote_name is not None:
  1321. _import_remote_refs(
  1322. r.refs,
  1323. remote_name,
  1324. fetch_result.refs,
  1325. message,
  1326. prune=prune,
  1327. prune_tags=prune_tags,
  1328. )
  1329. return fetch_result
  1330. def ls_remote(remote, config=None, **kwargs):
  1331. """List the refs in a remote.
  1332. Args:
  1333. remote: Remote repository location
  1334. config: Configuration to use
  1335. Returns:
  1336. Dictionary with remote refs
  1337. """
  1338. if config is None:
  1339. config = StackedConfig.default()
  1340. client, host_path = get_transport_and_path(remote, config=config, **kwargs)
  1341. return client.get_refs(host_path)
  1342. def repack(repo):
  1343. """Repack loose files in a repository.
  1344. Currently this only packs loose objects.
  1345. Args:
  1346. repo: Path to the repository
  1347. """
  1348. with open_repo_closing(repo) as r:
  1349. r.object_store.pack_loose_objects()
  1350. def pack_objects(repo, object_ids, packf, idxf, delta_window_size=None):
  1351. """Pack objects into a file.
  1352. Args:
  1353. repo: Path to the repository
  1354. object_ids: List of object ids to write
  1355. packf: File-like object to write to
  1356. idxf: File-like object to write to (can be None)
  1357. """
  1358. with open_repo_closing(repo) as r:
  1359. entries, data_sum = write_pack_objects(
  1360. packf,
  1361. r.object_store.iter_shas((oid, None) for oid in object_ids),
  1362. delta_window_size=delta_window_size,
  1363. )
  1364. if idxf is not None:
  1365. entries = sorted([(k, v[0], v[1]) for (k, v) in entries.items()])
  1366. write_pack_index(idxf, entries, data_sum)
  1367. def ls_tree(
  1368. repo,
  1369. treeish=b"HEAD",
  1370. outstream=sys.stdout,
  1371. recursive=False,
  1372. name_only=False,
  1373. ):
  1374. """List contents of a tree.
  1375. Args:
  1376. repo: Path to the repository
  1377. tree_ish: Tree id to list
  1378. outstream: Output stream (defaults to stdout)
  1379. recursive: Whether to recursively list files
  1380. name_only: Only print item name
  1381. """
  1382. def list_tree(store, treeid, base):
  1383. for (name, mode, sha) in store[treeid].iteritems():
  1384. if base:
  1385. name = posixpath.join(base, name)
  1386. if name_only:
  1387. outstream.write(name + b"\n")
  1388. else:
  1389. outstream.write(pretty_format_tree_entry(name, mode, sha))
  1390. if stat.S_ISDIR(mode) and recursive:
  1391. list_tree(store, sha, name)
  1392. with open_repo_closing(repo) as r:
  1393. tree = parse_tree(r, treeish)
  1394. list_tree(r.object_store, tree.id, "")
  1395. def remote_add(repo, name, url):
  1396. """Add a remote.
  1397. Args:
  1398. repo: Path to the repository
  1399. name: Remote name
  1400. url: Remote URL
  1401. """
  1402. if not isinstance(name, bytes):
  1403. name = name.encode(DEFAULT_ENCODING)
  1404. if not isinstance(url, bytes):
  1405. url = url.encode(DEFAULT_ENCODING)
  1406. with open_repo_closing(repo) as r:
  1407. c = r.get_config()
  1408. section = (b"remote", name)
  1409. if c.has_section(section):
  1410. raise RemoteExists(section)
  1411. c.set(section, b"url", url)
  1412. c.write_to_path()
  1413. def check_ignore(repo, paths, no_index=False):
  1414. """Debug gitignore files.
  1415. Args:
  1416. repo: Path to the repository
  1417. paths: List of paths to check for
  1418. no_index: Don't check index
  1419. Returns: List of ignored files
  1420. """
  1421. with open_repo_closing(repo) as r:
  1422. index = r.open_index()
  1423. ignore_manager = IgnoreFilterManager.from_repo(r)
  1424. for path in paths:
  1425. if not no_index and path_to_tree_path(r.path, path) in index:
  1426. continue
  1427. if os.path.isabs(path):
  1428. path = os.path.relpath(path, r.path)
  1429. if ignore_manager.is_ignored(path):
  1430. yield path
  1431. def update_head(repo, target, detached=False, new_branch=None):
  1432. """Update HEAD to point at a new branch/commit.
  1433. Note that this does not actually update the working tree.
  1434. Args:
  1435. repo: Path to the repository
  1436. detach: Create a detached head
  1437. target: Branch or committish to switch to
  1438. new_branch: New branch to create
  1439. """
  1440. with open_repo_closing(repo) as r:
  1441. if new_branch is not None:
  1442. to_set = _make_branch_ref(new_branch)
  1443. else:
  1444. to_set = b"HEAD"
  1445. if detached:
  1446. # TODO(jelmer): Provide some way so that the actual ref gets
  1447. # updated rather than what it points to, so the delete isn't
  1448. # necessary.
  1449. del r.refs[to_set]
  1450. r.refs[to_set] = parse_commit(r, target).id
  1451. else:
  1452. r.refs.set_symbolic_ref(to_set, parse_ref(r, target))
  1453. if new_branch is not None:
  1454. r.refs.set_symbolic_ref(b"HEAD", to_set)
  1455. def reset_file(repo, file_path: str, target: bytes = b'HEAD'):
  1456. """Reset the file to specific commit or branch.
  1457. Args:
  1458. repo: dulwich Repo object
  1459. file_path: file to reset, relative to the repository path
  1460. target: branch or commit or b'HEAD' to reset
  1461. """
  1462. tree = parse_tree(repo, treeish=target)
  1463. file_path = _fs_to_tree_path(file_path)
  1464. file_entry = tree.lookup_path(repo.object_store.__getitem__, file_path)
  1465. full_path = os.path.join(repo.path.encode(), file_path)
  1466. blob = repo.object_store[file_entry[1]]
  1467. mode = file_entry[0]
  1468. build_file_from_blob(blob, mode, full_path)
  1469. def check_mailmap(repo, contact):
  1470. """Check canonical name and email of contact.
  1471. Args:
  1472. repo: Path to the repository
  1473. contact: Contact name and/or email
  1474. Returns: Canonical contact data
  1475. """
  1476. with open_repo_closing(repo) as r:
  1477. from dulwich.mailmap import Mailmap
  1478. try:
  1479. mailmap = Mailmap.from_path(os.path.join(r.path, ".mailmap"))
  1480. except FileNotFoundError:
  1481. mailmap = Mailmap()
  1482. return mailmap.lookup(contact)
  1483. def fsck(repo):
  1484. """Check a repository.
  1485. Args:
  1486. repo: A path to the repository
  1487. Returns: Iterator over errors/warnings
  1488. """
  1489. with open_repo_closing(repo) as r:
  1490. # TODO(jelmer): check pack files
  1491. # TODO(jelmer): check graph
  1492. # TODO(jelmer): check refs
  1493. for sha in r.object_store:
  1494. o = r.object_store[sha]
  1495. try:
  1496. o.check()
  1497. except Exception as e:
  1498. yield (sha, e)
  1499. def stash_list(repo):
  1500. """List all stashes in a repository."""
  1501. with open_repo_closing(repo) as r:
  1502. from dulwich.stash import Stash
  1503. stash = Stash.from_repo(r)
  1504. return enumerate(list(stash.stashes()))
  1505. def stash_push(repo):
  1506. """Push a new stash onto the stack."""
  1507. with open_repo_closing(repo) as r:
  1508. from dulwich.stash import Stash
  1509. stash = Stash.from_repo(r)
  1510. stash.push()
  1511. def stash_pop(repo, index):
  1512. """Pop a stash from the stack."""
  1513. with open_repo_closing(repo) as r:
  1514. from dulwich.stash import Stash
  1515. stash = Stash.from_repo(r)
  1516. stash.pop(index)
  1517. def stash_drop(repo, index):
  1518. """Drop a stash from the stack."""
  1519. with open_repo_closing(repo) as r:
  1520. from dulwich.stash import Stash
  1521. stash = Stash.from_repo(r)
  1522. stash.drop(index)
  1523. def ls_files(repo):
  1524. """List all files in an index."""
  1525. with open_repo_closing(repo) as r:
  1526. return sorted(r.open_index())
  1527. def describe(repo):
  1528. """Describe the repository version.
  1529. Args:
  1530. projdir: git repository root
  1531. Returns: a string description of the current git revision
  1532. Examples: "gabcdefh", "v0.1" or "v0.1-5-gabcdefh".
  1533. """
  1534. # Get the repository
  1535. with open_repo_closing(repo) as r:
  1536. # Get a list of all tags
  1537. refs = r.get_refs()
  1538. tags = {}
  1539. for key, value in refs.items():
  1540. key = key.decode()
  1541. obj = r.get_object(value)
  1542. if u"tags" not in key:
  1543. continue
  1544. _, tag = key.rsplit(u"/", 1)
  1545. try:
  1546. commit = obj.object
  1547. except AttributeError:
  1548. continue
  1549. else:
  1550. commit = r.get_object(commit[1])
  1551. tags[tag] = [
  1552. datetime.datetime(*time.gmtime(commit.commit_time)[:6]),
  1553. commit.id.decode("ascii"),
  1554. ]
  1555. sorted_tags = sorted(tags.items(), key=lambda tag: tag[1][0], reverse=True)
  1556. # If there are no tags, return the current commit
  1557. if len(sorted_tags) == 0:
  1558. return "g{}".format(r[r.head()].id.decode("ascii")[:7])
  1559. # We're now 0 commits from the top
  1560. commit_count = 0
  1561. # Get the latest commit
  1562. latest_commit = r[r.head()]
  1563. # Walk through all commits
  1564. walker = r.get_walker()
  1565. for entry in walker:
  1566. # Check if tag
  1567. commit_id = entry.commit.id.decode("ascii")
  1568. for tag in sorted_tags:
  1569. tag_name = tag[0]
  1570. tag_commit = tag[1][1]
  1571. if commit_id == tag_commit:
  1572. if commit_count == 0:
  1573. return tag_name
  1574. else:
  1575. return "{}-{}-g{}".format(
  1576. tag_name,
  1577. commit_count,
  1578. latest_commit.id.decode("ascii")[:7],
  1579. )
  1580. commit_count += 1
  1581. # Return plain commit if no parent tag can be found
  1582. return "g{}".format(latest_commit.id.decode("ascii")[:7])
  1583. def get_object_by_path(repo, path, committish=None):
  1584. """Get an object by path.
  1585. Args:
  1586. repo: A path to the repository
  1587. path: Path to look up
  1588. committish: Commit to look up path in
  1589. Returns: A `ShaFile` object
  1590. """
  1591. if committish is None:
  1592. committish = "HEAD"
  1593. # Get the repository
  1594. with open_repo_closing(repo) as r:
  1595. commit = parse_commit(r, committish)
  1596. base_tree = commit.tree
  1597. if not isinstance(path, bytes):
  1598. path = commit_encode(commit, path)
  1599. (mode, sha) = tree_lookup_path(r.object_store.__getitem__, base_tree, path)
  1600. return r[sha]
  1601. def write_tree(repo):
  1602. """Write a tree object from the index.
  1603. Args:
  1604. repo: Repository for which to write tree
  1605. Returns: tree id for the tree that was written
  1606. """
  1607. with open_repo_closing(repo) as r:
  1608. return r.open_index().commit(r.object_store)