porcelain.py 56 KB

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