porcelain.py 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881
  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)
  372. return client.clone(
  373. path,
  374. target,
  375. mkdir=mkdir,
  376. bare=bare,
  377. origin=origin,
  378. checkout=checkout,
  379. branch=branch,
  380. depth=depth,
  381. )
  382. def add(repo=".", paths=None):
  383. """Add files to the staging area.
  384. Args:
  385. repo: Repository for the files
  386. paths: Paths to add. No value passed stages all modified files.
  387. Returns: Tuple with set of added files and ignored files
  388. If the repository contains ignored directories, the returned set will
  389. contain the path to an ignored directory (with trailing slash). Individual
  390. files within ignored directories will not be returned.
  391. """
  392. ignored = set()
  393. with open_repo_closing(repo) as r:
  394. repo_path = Path(r.path).resolve()
  395. ignore_manager = IgnoreFilterManager.from_repo(r)
  396. if not paths:
  397. paths = list(
  398. get_untracked_paths(
  399. str(Path(os.getcwd()).resolve()),
  400. str(repo_path),
  401. r.open_index(),
  402. )
  403. )
  404. relpaths = []
  405. if not isinstance(paths, list):
  406. paths = [paths]
  407. for p in paths:
  408. path = Path(p)
  409. relpath = str(path.resolve().relative_to(repo_path))
  410. # FIXME: Support patterns
  411. if path.is_dir():
  412. relpath = os.path.join(relpath, "")
  413. if ignore_manager.is_ignored(relpath):
  414. ignored.add(relpath)
  415. continue
  416. relpaths.append(relpath)
  417. r.stage(relpaths)
  418. return (relpaths, ignored)
  419. def _is_subdir(subdir, parentdir):
  420. """Check whether subdir is parentdir or a subdir of parentdir
  421. If parentdir or subdir is a relative path, it will be disamgibuated
  422. relative to the pwd.
  423. """
  424. parentdir_abs = os.path.realpath(parentdir) + os.path.sep
  425. subdir_abs = os.path.realpath(subdir) + os.path.sep
  426. return subdir_abs.startswith(parentdir_abs)
  427. # TODO: option to remove ignored files also, in line with `git clean -fdx`
  428. def clean(repo=".", target_dir=None):
  429. """Remove any untracked files from the target directory recursively
  430. Equivalent to running `git clean -fd` in target_dir.
  431. Args:
  432. repo: Repository where the files may be tracked
  433. target_dir: Directory to clean - current directory if None
  434. """
  435. if target_dir is None:
  436. target_dir = os.getcwd()
  437. with open_repo_closing(repo) as r:
  438. if not _is_subdir(target_dir, r.path):
  439. raise Error("target_dir must be in the repo's working dir")
  440. config = r.get_config_stack()
  441. require_force = config.get_boolean( # noqa: F841
  442. (b"clean",), b"requireForce", True
  443. )
  444. # TODO(jelmer): if require_force is set, then make sure that -f, -i or
  445. # -n is specified.
  446. index = r.open_index()
  447. ignore_manager = IgnoreFilterManager.from_repo(r)
  448. paths_in_wd = _walk_working_dir_paths(target_dir, r.path)
  449. # Reverse file visit order, so that files and subdirectories are
  450. # removed before containing directory
  451. for ap, is_dir in reversed(list(paths_in_wd)):
  452. if is_dir:
  453. # All subdirectories and files have been removed if untracked,
  454. # so dir contains no tracked files iff it is empty.
  455. is_empty = len(os.listdir(ap)) == 0
  456. if is_empty:
  457. os.rmdir(ap)
  458. else:
  459. ip = path_to_tree_path(r.path, ap)
  460. is_tracked = ip in index
  461. rp = os.path.relpath(ap, r.path)
  462. is_ignored = ignore_manager.is_ignored(rp)
  463. if not is_tracked and not is_ignored:
  464. os.remove(ap)
  465. def remove(repo=".", paths=None, cached=False):
  466. """Remove files from the staging area.
  467. Args:
  468. repo: Repository for the files
  469. paths: Paths to remove
  470. """
  471. with open_repo_closing(repo) as r:
  472. index = r.open_index()
  473. for p in paths:
  474. full_path = os.fsencode(os.path.abspath(p))
  475. tree_path = path_to_tree_path(r.path, p)
  476. try:
  477. index_sha = index[tree_path].sha
  478. except KeyError:
  479. raise Error("%s did not match any files" % p)
  480. if not cached:
  481. try:
  482. st = os.lstat(full_path)
  483. except OSError:
  484. pass
  485. else:
  486. try:
  487. blob = blob_from_path_and_stat(full_path, st)
  488. except IOError:
  489. pass
  490. else:
  491. try:
  492. committed_sha = tree_lookup_path(
  493. r.__getitem__, r[r.head()].tree, tree_path
  494. )[1]
  495. except KeyError:
  496. committed_sha = None
  497. if blob.id != index_sha and index_sha != committed_sha:
  498. raise Error(
  499. "file has staged content differing "
  500. "from both the file and head: %s" % p
  501. )
  502. if index_sha != committed_sha:
  503. raise Error("file has staged changes: %s" % p)
  504. os.remove(full_path)
  505. del index[tree_path]
  506. index.write()
  507. rm = remove
  508. def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
  509. if commit.encoding:
  510. encoding = commit.encoding.decode("ascii")
  511. else:
  512. encoding = default_encoding
  513. return contents.decode(encoding, "replace")
  514. def commit_encode(commit, contents, default_encoding=DEFAULT_ENCODING):
  515. if commit.encoding:
  516. encoding = commit.encoding.decode("ascii")
  517. else:
  518. encoding = default_encoding
  519. return contents.encode(encoding)
  520. def print_commit(commit, decode, outstream=sys.stdout):
  521. """Write a human-readable commit log entry.
  522. Args:
  523. commit: A `Commit` object
  524. outstream: A stream file to write to
  525. """
  526. outstream.write("-" * 50 + "\n")
  527. outstream.write("commit: " + commit.id.decode("ascii") + "\n")
  528. if len(commit.parents) > 1:
  529. outstream.write(
  530. "merge: "
  531. + "...".join([c.decode("ascii") for c in commit.parents[1:]])
  532. + "\n"
  533. )
  534. outstream.write("Author: " + decode(commit.author) + "\n")
  535. if commit.author != commit.committer:
  536. outstream.write("Committer: " + decode(commit.committer) + "\n")
  537. time_tuple = time.gmtime(commit.author_time + commit.author_timezone)
  538. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  539. timezone_str = format_timezone(commit.author_timezone).decode("ascii")
  540. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  541. outstream.write("\n")
  542. outstream.write(decode(commit.message) + "\n")
  543. outstream.write("\n")
  544. def print_tag(tag, decode, outstream=sys.stdout):
  545. """Write a human-readable tag.
  546. Args:
  547. tag: A `Tag` object
  548. decode: Function for decoding bytes to unicode string
  549. outstream: A stream to write to
  550. """
  551. outstream.write("Tagger: " + decode(tag.tagger) + "\n")
  552. time_tuple = time.gmtime(tag.tag_time + tag.tag_timezone)
  553. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  554. timezone_str = format_timezone(tag.tag_timezone).decode("ascii")
  555. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  556. outstream.write("\n")
  557. outstream.write(decode(tag.message))
  558. outstream.write("\n")
  559. def show_blob(repo, blob, decode, outstream=sys.stdout):
  560. """Write a blob to a stream.
  561. Args:
  562. repo: A `Repo` object
  563. blob: A `Blob` object
  564. decode: Function for decoding bytes to unicode string
  565. outstream: A stream file to write to
  566. """
  567. outstream.write(decode(blob.data))
  568. def show_commit(repo, commit, decode, outstream=sys.stdout):
  569. """Show a commit to a stream.
  570. Args:
  571. repo: A `Repo` object
  572. commit: A `Commit` object
  573. decode: Function for decoding bytes to unicode string
  574. outstream: Stream to write to
  575. """
  576. print_commit(commit, decode=decode, outstream=outstream)
  577. if commit.parents:
  578. parent_commit = repo[commit.parents[0]]
  579. base_tree = parent_commit.tree
  580. else:
  581. base_tree = None
  582. diffstream = BytesIO()
  583. write_tree_diff(diffstream, repo.object_store, base_tree, commit.tree)
  584. diffstream.seek(0)
  585. outstream.write(commit_decode(commit, diffstream.getvalue()))
  586. def show_tree(repo, tree, decode, outstream=sys.stdout):
  587. """Print a tree to a stream.
  588. Args:
  589. repo: A `Repo` object
  590. tree: A `Tree` object
  591. decode: Function for decoding bytes to unicode string
  592. outstream: Stream to write to
  593. """
  594. for n in tree:
  595. outstream.write(decode(n) + "\n")
  596. def show_tag(repo, tag, decode, outstream=sys.stdout):
  597. """Print a tag to a stream.
  598. Args:
  599. repo: A `Repo` object
  600. tag: A `Tag` object
  601. decode: Function for decoding bytes to unicode string
  602. outstream: Stream to write to
  603. """
  604. print_tag(tag, decode, outstream)
  605. show_object(repo, repo[tag.object[1]], decode, outstream)
  606. def show_object(repo, obj, decode, outstream):
  607. return {
  608. b"tree": show_tree,
  609. b"blob": show_blob,
  610. b"commit": show_commit,
  611. b"tag": show_tag,
  612. }[obj.type_name](repo, obj, decode, outstream)
  613. def print_name_status(changes):
  614. """Print a simple status summary, listing changed files."""
  615. for change in changes:
  616. if not change:
  617. continue
  618. if isinstance(change, list):
  619. change = change[0]
  620. if change.type == CHANGE_ADD:
  621. path1 = change.new.path
  622. path2 = ""
  623. kind = "A"
  624. elif change.type == CHANGE_DELETE:
  625. path1 = change.old.path
  626. path2 = ""
  627. kind = "D"
  628. elif change.type == CHANGE_MODIFY:
  629. path1 = change.new.path
  630. path2 = ""
  631. kind = "M"
  632. elif change.type in RENAME_CHANGE_TYPES:
  633. path1 = change.old.path
  634. path2 = change.new.path
  635. if change.type == CHANGE_RENAME:
  636. kind = "R"
  637. elif change.type == CHANGE_COPY:
  638. kind = "C"
  639. yield "%-8s%-20s%-20s" % (kind, path1, path2)
  640. def log(
  641. repo=".",
  642. paths=None,
  643. outstream=sys.stdout,
  644. max_entries=None,
  645. reverse=False,
  646. name_status=False,
  647. ):
  648. """Write commit logs.
  649. Args:
  650. repo: Path to repository
  651. paths: Optional set of specific paths to print entries for
  652. outstream: Stream to write log output to
  653. reverse: Reverse order in which entries are printed
  654. name_status: Print name status
  655. max_entries: Optional maximum number of entries to display
  656. """
  657. with open_repo_closing(repo) as r:
  658. walker = r.get_walker(max_entries=max_entries, paths=paths, reverse=reverse)
  659. for entry in walker:
  660. def decode(x):
  661. return commit_decode(entry.commit, x)
  662. print_commit(entry.commit, decode, outstream)
  663. if name_status:
  664. outstream.writelines(
  665. [line + "\n" for line in print_name_status(entry.changes())]
  666. )
  667. # TODO(jelmer): better default for encoding?
  668. def show(
  669. repo=".",
  670. objects=None,
  671. outstream=sys.stdout,
  672. default_encoding=DEFAULT_ENCODING,
  673. ):
  674. """Print the changes in a commit.
  675. Args:
  676. repo: Path to repository
  677. objects: Objects to show (defaults to [HEAD])
  678. outstream: Stream to write to
  679. default_encoding: Default encoding to use if none is set in the
  680. commit
  681. """
  682. if objects is None:
  683. objects = ["HEAD"]
  684. if not isinstance(objects, list):
  685. objects = [objects]
  686. with open_repo_closing(repo) as r:
  687. for objectish in objects:
  688. o = parse_object(r, objectish)
  689. if isinstance(o, Commit):
  690. def decode(x):
  691. return commit_decode(o, x, default_encoding)
  692. else:
  693. def decode(x):
  694. return x.decode(default_encoding)
  695. show_object(r, o, decode, outstream)
  696. def diff_tree(repo, old_tree, new_tree, outstream=sys.stdout.buffer):
  697. """Compares the content and mode of blobs found via two tree objects.
  698. Args:
  699. repo: Path to repository
  700. old_tree: Id of old tree
  701. new_tree: Id of new tree
  702. outstream: Stream to write to
  703. """
  704. with open_repo_closing(repo) as r:
  705. write_tree_diff(outstream, r.object_store, old_tree, new_tree)
  706. def rev_list(repo, commits, outstream=sys.stdout):
  707. """Lists commit objects in reverse chronological order.
  708. Args:
  709. repo: Path to repository
  710. commits: Commits over which to iterate
  711. outstream: Stream to write to
  712. """
  713. with open_repo_closing(repo) as r:
  714. for entry in r.get_walker(include=[r[c].id for c in commits]):
  715. outstream.write(entry.commit.id + b"\n")
  716. def tag(*args, **kwargs):
  717. import warnings
  718. warnings.warn(
  719. "tag has been deprecated in favour of tag_create.", DeprecationWarning
  720. )
  721. return tag_create(*args, **kwargs)
  722. def tag_create(
  723. repo,
  724. tag,
  725. author=None,
  726. message=None,
  727. annotated=False,
  728. objectish="HEAD",
  729. tag_time=None,
  730. tag_timezone=None,
  731. sign=False,
  732. ):
  733. """Creates a tag in git via dulwich calls:
  734. Args:
  735. repo: Path to repository
  736. tag: tag string
  737. author: tag author (optional, if annotated is set)
  738. message: tag message (optional)
  739. annotated: whether to create an annotated tag
  740. objectish: object the tag should point at, defaults to HEAD
  741. tag_time: Optional time for annotated tag
  742. tag_timezone: Optional timezone for annotated tag
  743. sign: GPG Sign the tag (bool, defaults to False,
  744. pass True to use default GPG key,
  745. pass a str containing Key ID to use a specific GPG key)
  746. """
  747. with open_repo_closing(repo) as r:
  748. object = parse_object(r, objectish)
  749. if annotated:
  750. # Create the tag object
  751. tag_obj = Tag()
  752. if author is None:
  753. # TODO(jelmer): Don't use repo private method.
  754. author = r._get_user_identity(r.get_config_stack())
  755. tag_obj.tagger = author
  756. tag_obj.message = message + "\n".encode()
  757. tag_obj.name = tag
  758. tag_obj.object = (type(object), object.id)
  759. if tag_time is None:
  760. tag_time = int(time.time())
  761. tag_obj.tag_time = tag_time
  762. if tag_timezone is None:
  763. # TODO(jelmer) Use current user timezone rather than UTC
  764. tag_timezone = 0
  765. elif isinstance(tag_timezone, str):
  766. tag_timezone = parse_timezone(tag_timezone)
  767. tag_obj.tag_timezone = tag_timezone
  768. if sign:
  769. tag_obj.sign(sign if isinstance(sign, str) else None)
  770. r.object_store.add_object(tag_obj)
  771. tag_id = tag_obj.id
  772. else:
  773. tag_id = object.id
  774. r.refs[_make_tag_ref(tag)] = tag_id
  775. def list_tags(*args, **kwargs):
  776. import warnings
  777. warnings.warn(
  778. "list_tags has been deprecated in favour of tag_list.",
  779. DeprecationWarning,
  780. )
  781. return tag_list(*args, **kwargs)
  782. def tag_list(repo, outstream=sys.stdout):
  783. """List all tags.
  784. Args:
  785. repo: Path to repository
  786. outstream: Stream to write tags to
  787. """
  788. with open_repo_closing(repo) as r:
  789. tags = sorted(r.refs.as_dict(b"refs/tags"))
  790. return tags
  791. def tag_delete(repo, name):
  792. """Remove a tag.
  793. Args:
  794. repo: Path to repository
  795. name: Name of tag to remove
  796. """
  797. with open_repo_closing(repo) as r:
  798. if isinstance(name, bytes):
  799. names = [name]
  800. elif isinstance(name, list):
  801. names = name
  802. else:
  803. raise Error("Unexpected tag name type %r" % name)
  804. for name in names:
  805. del r.refs[_make_tag_ref(name)]
  806. def reset(repo, mode, treeish="HEAD"):
  807. """Reset current HEAD to the specified state.
  808. Args:
  809. repo: Path to repository
  810. mode: Mode ("hard", "soft", "mixed")
  811. treeish: Treeish to reset to
  812. """
  813. if mode != "hard":
  814. raise Error("hard is the only mode currently supported")
  815. with open_repo_closing(repo) as r:
  816. tree = parse_tree(r, treeish)
  817. r.reset_index(tree.id)
  818. def get_remote_repo(
  819. repo: Repo, remote_location: Optional[Union[str, bytes]] = None
  820. ) -> Tuple[Optional[str], str]:
  821. config = repo.get_config()
  822. if remote_location is None:
  823. remote_location = get_branch_remote(repo)
  824. if isinstance(remote_location, str):
  825. encoded_location = remote_location.encode()
  826. else:
  827. encoded_location = remote_location
  828. section = (b"remote", encoded_location)
  829. remote_name = None # type: Optional[str]
  830. if config.has_section(section):
  831. remote_name = encoded_location.decode()
  832. url = config.get(section, "url")
  833. encoded_location = url
  834. else:
  835. remote_name = None
  836. return (remote_name, encoded_location.decode())
  837. def push(
  838. repo,
  839. remote_location=None,
  840. refspecs=None,
  841. outstream=default_bytes_out_stream,
  842. errstream=default_bytes_err_stream,
  843. force=False,
  844. **kwargs
  845. ):
  846. """Remote push with dulwich via dulwich.client
  847. Args:
  848. repo: Path to repository
  849. remote_location: Location of the remote
  850. refspecs: Refs to push to remote
  851. outstream: A stream file to write output
  852. errstream: A stream file to write errors
  853. force: Force overwriting refs
  854. """
  855. # Open the repo
  856. with open_repo_closing(repo) as r:
  857. if refspecs is None:
  858. refspecs = [active_branch(r)]
  859. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  860. # Get the client and path
  861. client, path = get_transport_and_path(
  862. remote_location, config=r.get_config_stack(), **kwargs
  863. )
  864. selected_refs = []
  865. remote_changed_refs = {}
  866. def update_refs(refs):
  867. selected_refs.extend(parse_reftuples(r.refs, refs, refspecs, force=force))
  868. new_refs = {}
  869. # TODO: Handle selected_refs == {None: None}
  870. for (lh, rh, force_ref) in selected_refs:
  871. if lh is None:
  872. new_refs[rh] = ZERO_SHA
  873. remote_changed_refs[rh] = None
  874. else:
  875. try:
  876. localsha = r.refs[lh]
  877. except KeyError:
  878. raise Error("No valid ref %s in local repository" % lh)
  879. if not force_ref and rh in refs:
  880. check_diverged(r, refs[rh], localsha)
  881. new_refs[rh] = localsha
  882. remote_changed_refs[rh] = localsha
  883. return new_refs
  884. err_encoding = getattr(errstream, "encoding", None) or DEFAULT_ENCODING
  885. remote_location = client.get_url(path)
  886. try:
  887. result = client.send_pack(
  888. path,
  889. update_refs,
  890. generate_pack_data=r.generate_pack_data,
  891. progress=errstream.write,
  892. )
  893. except SendPackError as e:
  894. raise Error(
  895. "Push to " + remote_location + " failed -> " + e.args[0].decode(),
  896. inner=e,
  897. )
  898. else:
  899. errstream.write(
  900. b"Push to " + remote_location.encode(err_encoding) + b" successful.\n"
  901. )
  902. for ref, error in (result.ref_status or {}).items():
  903. if error is not None:
  904. errstream.write(
  905. b"Push of ref %s failed: %s\n" % (ref, error.encode(err_encoding))
  906. )
  907. else:
  908. errstream.write(b"Ref %s updated\n" % ref)
  909. if remote_name is not None:
  910. _import_remote_refs(r.refs, remote_name, remote_changed_refs)
  911. def pull(
  912. repo,
  913. remote_location=None,
  914. refspecs=None,
  915. outstream=default_bytes_out_stream,
  916. errstream=default_bytes_err_stream,
  917. fast_forward=True,
  918. force=False,
  919. **kwargs
  920. ):
  921. """Pull from remote via dulwich.client
  922. Args:
  923. repo: Path to repository
  924. remote_location: Location of the remote
  925. refspec: refspecs to fetch
  926. outstream: A stream file to write to output
  927. errstream: A stream file to write to errors
  928. """
  929. # Open the repo
  930. with open_repo_closing(repo) as r:
  931. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  932. if refspecs is None:
  933. refspecs = [b"HEAD"]
  934. selected_refs = []
  935. def determine_wants(remote_refs, **kwargs):
  936. selected_refs.extend(
  937. parse_reftuples(remote_refs, r.refs, refspecs, force=force)
  938. )
  939. return [
  940. remote_refs[lh]
  941. for (lh, rh, force_ref) in selected_refs
  942. if remote_refs[lh] not in r.object_store
  943. ]
  944. client, path = get_transport_and_path(
  945. remote_location, config=r.get_config_stack(), **kwargs
  946. )
  947. fetch_result = client.fetch(
  948. path, r, progress=errstream.write, determine_wants=determine_wants
  949. )
  950. for (lh, rh, force_ref) in selected_refs:
  951. try:
  952. check_diverged(r, r.refs[rh], fetch_result.refs[lh])
  953. except DivergedBranches:
  954. if fast_forward:
  955. raise
  956. else:
  957. raise NotImplementedError("merge is not yet supported")
  958. r.refs[rh] = fetch_result.refs[lh]
  959. if selected_refs:
  960. r[b"HEAD"] = fetch_result.refs[selected_refs[0][1]]
  961. # Perform 'git checkout .' - syncs staged changes
  962. tree = r[b"HEAD"].tree
  963. r.reset_index(tree=tree)
  964. if remote_name is not None:
  965. _import_remote_refs(r.refs, remote_name, fetch_result.refs)
  966. def status(repo=".", ignored=False):
  967. """Returns staged, unstaged, and untracked changes relative to the HEAD.
  968. Args:
  969. repo: Path to repository or repository object
  970. ignored: Whether to include ignored files in `untracked`
  971. Returns: GitStatus tuple,
  972. staged - dict with lists of staged paths (diff index/HEAD)
  973. unstaged - list of unstaged paths (diff index/working-tree)
  974. untracked - list of untracked, un-ignored & non-.git paths
  975. """
  976. with open_repo_closing(repo) as r:
  977. # 1. Get status of staged
  978. tracked_changes = get_tree_changes(r)
  979. # 2. Get status of unstaged
  980. index = r.open_index()
  981. normalizer = r.get_blob_normalizer()
  982. filter_callback = normalizer.checkin_normalize
  983. unstaged_changes = list(get_unstaged_changes(index, r.path, filter_callback))
  984. untracked_paths = get_untracked_paths(
  985. r.path, r.path, index, exclude_ignored=not ignored
  986. )
  987. untracked_changes = list(untracked_paths)
  988. return GitStatus(tracked_changes, unstaged_changes, untracked_changes)
  989. def _walk_working_dir_paths(frompath, basepath, prune_dirnames=None):
  990. """Get path, is_dir for files in working dir from frompath
  991. Args:
  992. frompath: Path to begin walk
  993. basepath: Path to compare to
  994. prune_dirnames: Optional callback to prune dirnames during os.walk
  995. dirnames will be set to result of prune_dirnames(dirpath, dirnames)
  996. """
  997. for dirpath, dirnames, filenames in os.walk(frompath):
  998. # Skip .git and below.
  999. if ".git" in dirnames:
  1000. dirnames.remove(".git")
  1001. if dirpath != basepath:
  1002. continue
  1003. if ".git" in filenames:
  1004. filenames.remove(".git")
  1005. if dirpath != basepath:
  1006. continue
  1007. if dirpath != frompath:
  1008. yield dirpath, True
  1009. for filename in filenames:
  1010. filepath = os.path.join(dirpath, filename)
  1011. yield filepath, False
  1012. if prune_dirnames:
  1013. dirnames[:] = prune_dirnames(dirpath, dirnames)
  1014. def get_untracked_paths(frompath, basepath, index, exclude_ignored=False):
  1015. """Get untracked paths.
  1016. Args:
  1017. frompath: Path to walk
  1018. basepath: Path to compare to
  1019. index: Index to check against
  1020. exclude_ignored: Whether to exclude ignored paths
  1021. Note: ignored directories will never be walked for performance reasons.
  1022. If exclude_ignored is False, only the path to an ignored directory will
  1023. be yielded, no files inside the directory will be returned
  1024. """
  1025. with open_repo_closing(basepath) as r:
  1026. ignore_manager = IgnoreFilterManager.from_repo(r)
  1027. ignored_dirs = []
  1028. def prune_dirnames(dirpath, dirnames):
  1029. for i in range(len(dirnames) - 1, -1, -1):
  1030. path = os.path.join(dirpath, dirnames[i])
  1031. ip = os.path.join(os.path.relpath(path, basepath), "")
  1032. if ignore_manager.is_ignored(ip):
  1033. if not exclude_ignored:
  1034. ignored_dirs.append(
  1035. os.path.join(os.path.relpath(path, frompath), "")
  1036. )
  1037. del dirnames[i]
  1038. return dirnames
  1039. for ap, is_dir in _walk_working_dir_paths(
  1040. frompath, basepath, prune_dirnames=prune_dirnames
  1041. ):
  1042. if not is_dir:
  1043. ip = path_to_tree_path(basepath, ap)
  1044. if ip not in index:
  1045. if (
  1046. not exclude_ignored
  1047. or not ignore_manager.is_ignored(
  1048. os.path.relpath(ap, basepath)
  1049. )
  1050. ):
  1051. yield os.path.relpath(ap, frompath)
  1052. yield from ignored_dirs
  1053. def get_tree_changes(repo):
  1054. """Return add/delete/modify changes to tree by comparing index to HEAD.
  1055. Args:
  1056. repo: repo path or object
  1057. Returns: dict with lists for each type of change
  1058. """
  1059. with open_repo_closing(repo) as r:
  1060. index = r.open_index()
  1061. # Compares the Index to the HEAD & determines changes
  1062. # Iterate through the changes and report add/delete/modify
  1063. # TODO: call out to dulwich.diff_tree somehow.
  1064. tracked_changes = {
  1065. "add": [],
  1066. "delete": [],
  1067. "modify": [],
  1068. }
  1069. try:
  1070. tree_id = r[b"HEAD"].tree
  1071. except KeyError:
  1072. tree_id = None
  1073. for change in index.changes_from_tree(r.object_store, tree_id):
  1074. if not change[0][0]:
  1075. tracked_changes["add"].append(change[0][1])
  1076. elif not change[0][1]:
  1077. tracked_changes["delete"].append(change[0][0])
  1078. elif change[0][0] == change[0][1]:
  1079. tracked_changes["modify"].append(change[0][0])
  1080. else:
  1081. raise NotImplementedError("git mv ops not yet supported")
  1082. return tracked_changes
  1083. def daemon(path=".", address=None, port=None):
  1084. """Run a daemon serving Git requests over TCP/IP.
  1085. Args:
  1086. path: Path to the directory to serve.
  1087. address: Optional address to listen on (defaults to ::)
  1088. port: Optional port to listen on (defaults to TCP_GIT_PORT)
  1089. """
  1090. # TODO(jelmer): Support git-daemon-export-ok and --export-all.
  1091. backend = FileSystemBackend(path)
  1092. server = TCPGitServer(backend, address, port)
  1093. server.serve_forever()
  1094. def web_daemon(path=".", address=None, port=None):
  1095. """Run a daemon serving Git requests over HTTP.
  1096. Args:
  1097. path: Path to the directory to serve
  1098. address: Optional address to listen on (defaults to ::)
  1099. port: Optional port to listen on (defaults to 80)
  1100. """
  1101. from dulwich.web import (
  1102. make_wsgi_chain,
  1103. make_server,
  1104. WSGIRequestHandlerLogger,
  1105. WSGIServerLogger,
  1106. )
  1107. backend = FileSystemBackend(path)
  1108. app = make_wsgi_chain(backend)
  1109. server = make_server(
  1110. address,
  1111. port,
  1112. app,
  1113. handler_class=WSGIRequestHandlerLogger,
  1114. server_class=WSGIServerLogger,
  1115. )
  1116. server.serve_forever()
  1117. def upload_pack(path=".", inf=None, outf=None):
  1118. """Upload a pack file after negotiating its contents using smart protocol.
  1119. Args:
  1120. path: Path to the repository
  1121. inf: Input stream to communicate with client
  1122. outf: Output stream to communicate with client
  1123. """
  1124. if outf is None:
  1125. outf = getattr(sys.stdout, "buffer", sys.stdout)
  1126. if inf is None:
  1127. inf = getattr(sys.stdin, "buffer", sys.stdin)
  1128. path = os.path.expanduser(path)
  1129. backend = FileSystemBackend(path)
  1130. def send_fn(data):
  1131. outf.write(data)
  1132. outf.flush()
  1133. proto = Protocol(inf.read, send_fn)
  1134. handler = UploadPackHandler(backend, [path], proto)
  1135. # FIXME: Catch exceptions and write a single-line summary to outf.
  1136. handler.handle()
  1137. return 0
  1138. def receive_pack(path=".", inf=None, outf=None):
  1139. """Receive 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 = ReceivePackHandler(backend, [path], proto)
  1156. # FIXME: Catch exceptions and write a single-line summary to outf.
  1157. handler.handle()
  1158. return 0
  1159. def _make_branch_ref(name):
  1160. if getattr(name, "encode", None):
  1161. name = name.encode(DEFAULT_ENCODING)
  1162. return LOCAL_BRANCH_PREFIX + name
  1163. def _make_tag_ref(name):
  1164. if getattr(name, "encode", None):
  1165. name = name.encode(DEFAULT_ENCODING)
  1166. return LOCAL_TAG_PREFIX + name
  1167. def branch_delete(repo, name):
  1168. """Delete a branch.
  1169. Args:
  1170. repo: Path to the repository
  1171. name: Name of the branch
  1172. """
  1173. with open_repo_closing(repo) as r:
  1174. if isinstance(name, list):
  1175. names = name
  1176. else:
  1177. names = [name]
  1178. for name in names:
  1179. del r.refs[_make_branch_ref(name)]
  1180. def branch_create(repo, name, objectish=None, force=False):
  1181. """Create a branch.
  1182. Args:
  1183. repo: Path to the repository
  1184. name: Name of the new branch
  1185. objectish: Target object to point new branch at (defaults to HEAD)
  1186. force: Force creation of branch, even if it already exists
  1187. """
  1188. with open_repo_closing(repo) as r:
  1189. if objectish is None:
  1190. objectish = "HEAD"
  1191. object = parse_object(r, objectish)
  1192. refname = _make_branch_ref(name)
  1193. ref_message = b"branch: Created from " + objectish.encode("utf-8")
  1194. if force:
  1195. r.refs.set_if_equals(refname, None, object.id, message=ref_message)
  1196. else:
  1197. if not r.refs.add_if_new(refname, object.id, message=ref_message):
  1198. raise Error("Branch with name %s already exists." % name)
  1199. def branch_list(repo):
  1200. """List all branches.
  1201. Args:
  1202. repo: Path to the repository
  1203. """
  1204. with open_repo_closing(repo) as r:
  1205. return r.refs.keys(base=LOCAL_BRANCH_PREFIX)
  1206. def active_branch(repo):
  1207. """Return the active branch in the repository, if any.
  1208. Args:
  1209. repo: Repository to open
  1210. Returns:
  1211. branch name
  1212. Raises:
  1213. KeyError: if the repository does not have a working tree
  1214. IndexError: if HEAD is floating
  1215. """
  1216. with open_repo_closing(repo) as r:
  1217. active_ref = r.refs.follow(b"HEAD")[0][1]
  1218. if not active_ref.startswith(LOCAL_BRANCH_PREFIX):
  1219. raise ValueError(active_ref)
  1220. return active_ref[len(LOCAL_BRANCH_PREFIX) :]
  1221. def get_branch_remote(repo):
  1222. """Return the active branch's remote name, if any.
  1223. Args:
  1224. repo: Repository to open
  1225. Returns:
  1226. remote name
  1227. Raises:
  1228. KeyError: if the repository does not have a working tree
  1229. """
  1230. with open_repo_closing(repo) as r:
  1231. branch_name = active_branch(r.path)
  1232. config = r.get_config()
  1233. try:
  1234. remote_name = config.get((b"branch", branch_name), b"remote")
  1235. except KeyError:
  1236. remote_name = b"origin"
  1237. return remote_name
  1238. def fetch(
  1239. repo,
  1240. remote_location=None,
  1241. outstream=sys.stdout,
  1242. errstream=default_bytes_err_stream,
  1243. message=None,
  1244. depth=None,
  1245. prune=False,
  1246. prune_tags=False,
  1247. force=False,
  1248. **kwargs
  1249. ):
  1250. """Fetch objects from a remote server.
  1251. Args:
  1252. repo: Path to the repository
  1253. remote_location: String identifying a remote server
  1254. outstream: Output stream (defaults to stdout)
  1255. errstream: Error stream (defaults to stderr)
  1256. message: Reflog message (defaults to b"fetch: from <remote_name>")
  1257. depth: Depth to fetch at
  1258. prune: Prune remote removed refs
  1259. prune_tags: Prune reomte removed tags
  1260. Returns:
  1261. Dictionary with refs on the remote
  1262. """
  1263. with open_repo_closing(repo) as r:
  1264. (remote_name, remote_location) = get_remote_repo(r, remote_location)
  1265. if message is None:
  1266. message = b"fetch: from " + remote_location.encode("utf-8")
  1267. client, path = get_transport_and_path(
  1268. remote_location, config=r.get_config_stack(), **kwargs
  1269. )
  1270. fetch_result = client.fetch(path, r, progress=errstream.write, depth=depth)
  1271. if remote_name is not None:
  1272. _import_remote_refs(
  1273. r.refs,
  1274. remote_name,
  1275. fetch_result.refs,
  1276. message,
  1277. prune=prune,
  1278. prune_tags=prune_tags,
  1279. )
  1280. return fetch_result
  1281. def ls_remote(remote, config=None, **kwargs):
  1282. """List the refs in a remote.
  1283. Args:
  1284. remote: Remote repository location
  1285. config: Configuration to use
  1286. Returns:
  1287. Dictionary with remote refs
  1288. """
  1289. if config is None:
  1290. config = StackedConfig.default()
  1291. client, host_path = get_transport_and_path(remote, config=config, **kwargs)
  1292. return client.get_refs(host_path)
  1293. def repack(repo):
  1294. """Repack loose files in a repository.
  1295. Currently this only packs loose objects.
  1296. Args:
  1297. repo: Path to the repository
  1298. """
  1299. with open_repo_closing(repo) as r:
  1300. r.object_store.pack_loose_objects()
  1301. def pack_objects(repo, object_ids, packf, idxf, delta_window_size=None):
  1302. """Pack objects into a file.
  1303. Args:
  1304. repo: Path to the repository
  1305. object_ids: List of object ids to write
  1306. packf: File-like object to write to
  1307. idxf: File-like object to write to (can be None)
  1308. """
  1309. with open_repo_closing(repo) as r:
  1310. entries, data_sum = write_pack_objects(
  1311. packf,
  1312. r.object_store.iter_shas((oid, None) for oid in object_ids),
  1313. delta_window_size=delta_window_size,
  1314. )
  1315. if idxf is not None:
  1316. entries = sorted([(k, v[0], v[1]) for (k, v) in entries.items()])
  1317. write_pack_index(idxf, entries, data_sum)
  1318. def ls_tree(
  1319. repo,
  1320. treeish=b"HEAD",
  1321. outstream=sys.stdout,
  1322. recursive=False,
  1323. name_only=False,
  1324. ):
  1325. """List contents of a tree.
  1326. Args:
  1327. repo: Path to the repository
  1328. tree_ish: Tree id to list
  1329. outstream: Output stream (defaults to stdout)
  1330. recursive: Whether to recursively list files
  1331. name_only: Only print item name
  1332. """
  1333. def list_tree(store, treeid, base):
  1334. for (name, mode, sha) in store[treeid].iteritems():
  1335. if base:
  1336. name = posixpath.join(base, name)
  1337. if name_only:
  1338. outstream.write(name + b"\n")
  1339. else:
  1340. outstream.write(pretty_format_tree_entry(name, mode, sha))
  1341. if stat.S_ISDIR(mode) and recursive:
  1342. list_tree(store, sha, name)
  1343. with open_repo_closing(repo) as r:
  1344. tree = parse_tree(r, treeish)
  1345. list_tree(r.object_store, tree.id, "")
  1346. def remote_add(repo, name, url):
  1347. """Add a remote.
  1348. Args:
  1349. repo: Path to the repository
  1350. name: Remote name
  1351. url: Remote URL
  1352. """
  1353. if not isinstance(name, bytes):
  1354. name = name.encode(DEFAULT_ENCODING)
  1355. if not isinstance(url, bytes):
  1356. url = url.encode(DEFAULT_ENCODING)
  1357. with open_repo_closing(repo) as r:
  1358. c = r.get_config()
  1359. section = (b"remote", name)
  1360. if c.has_section(section):
  1361. raise RemoteExists(section)
  1362. c.set(section, b"url", url)
  1363. c.write_to_path()
  1364. def check_ignore(repo, paths, no_index=False):
  1365. """Debug gitignore files.
  1366. Args:
  1367. repo: Path to the repository
  1368. paths: List of paths to check for
  1369. no_index: Don't check index
  1370. Returns: List of ignored files
  1371. """
  1372. with open_repo_closing(repo) as r:
  1373. index = r.open_index()
  1374. ignore_manager = IgnoreFilterManager.from_repo(r)
  1375. for path in paths:
  1376. if not no_index and path_to_tree_path(r.path, path) in index:
  1377. continue
  1378. if os.path.isabs(path):
  1379. path = os.path.relpath(path, r.path)
  1380. if ignore_manager.is_ignored(path):
  1381. yield path
  1382. def update_head(repo, target, detached=False, new_branch=None):
  1383. """Update HEAD to point at a new branch/commit.
  1384. Note that this does not actually update the working tree.
  1385. Args:
  1386. repo: Path to the repository
  1387. detach: Create a detached head
  1388. target: Branch or committish to switch to
  1389. new_branch: New branch to create
  1390. """
  1391. with open_repo_closing(repo) as r:
  1392. if new_branch is not None:
  1393. to_set = _make_branch_ref(new_branch)
  1394. else:
  1395. to_set = b"HEAD"
  1396. if detached:
  1397. # TODO(jelmer): Provide some way so that the actual ref gets
  1398. # updated rather than what it points to, so the delete isn't
  1399. # necessary.
  1400. del r.refs[to_set]
  1401. r.refs[to_set] = parse_commit(r, target).id
  1402. else:
  1403. r.refs.set_symbolic_ref(to_set, parse_ref(r, target))
  1404. if new_branch is not None:
  1405. r.refs.set_symbolic_ref(b"HEAD", to_set)
  1406. def reset_file(repo, file_path: str, target: bytes = b'HEAD'):
  1407. """Reset the file to specific commit or branch.
  1408. Args:
  1409. repo: dulwich Repo object
  1410. file_path: file to reset, relative to the repository path
  1411. target: branch or commit or b'HEAD' to reset
  1412. """
  1413. tree = parse_tree(repo, treeish=target)
  1414. file_path = _fs_to_tree_path(file_path)
  1415. file_entry = tree.lookup_path(repo.object_store.__getitem__, file_path)
  1416. full_path = os.path.join(repo.path.encode(), file_path)
  1417. blob = repo.object_store[file_entry[1]]
  1418. mode = file_entry[0]
  1419. build_file_from_blob(blob, mode, full_path)
  1420. def check_mailmap(repo, contact):
  1421. """Check canonical name and email of contact.
  1422. Args:
  1423. repo: Path to the repository
  1424. contact: Contact name and/or email
  1425. Returns: Canonical contact data
  1426. """
  1427. with open_repo_closing(repo) as r:
  1428. from dulwich.mailmap import Mailmap
  1429. try:
  1430. mailmap = Mailmap.from_path(os.path.join(r.path, ".mailmap"))
  1431. except FileNotFoundError:
  1432. mailmap = Mailmap()
  1433. return mailmap.lookup(contact)
  1434. def fsck(repo):
  1435. """Check a repository.
  1436. Args:
  1437. repo: A path to the repository
  1438. Returns: Iterator over errors/warnings
  1439. """
  1440. with open_repo_closing(repo) as r:
  1441. # TODO(jelmer): check pack files
  1442. # TODO(jelmer): check graph
  1443. # TODO(jelmer): check refs
  1444. for sha in r.object_store:
  1445. o = r.object_store[sha]
  1446. try:
  1447. o.check()
  1448. except Exception as e:
  1449. yield (sha, e)
  1450. def stash_list(repo):
  1451. """List all stashes in a repository."""
  1452. with open_repo_closing(repo) as r:
  1453. from dulwich.stash import Stash
  1454. stash = Stash.from_repo(r)
  1455. return enumerate(list(stash.stashes()))
  1456. def stash_push(repo):
  1457. """Push a new stash onto the stack."""
  1458. with open_repo_closing(repo) as r:
  1459. from dulwich.stash import Stash
  1460. stash = Stash.from_repo(r)
  1461. stash.push()
  1462. def stash_pop(repo, index):
  1463. """Pop a stash from the stack."""
  1464. with open_repo_closing(repo) as r:
  1465. from dulwich.stash import Stash
  1466. stash = Stash.from_repo(r)
  1467. stash.pop(index)
  1468. def stash_drop(repo, index):
  1469. """Drop a stash from the stack."""
  1470. with open_repo_closing(repo) as r:
  1471. from dulwich.stash import Stash
  1472. stash = Stash.from_repo(r)
  1473. stash.drop(index)
  1474. def ls_files(repo):
  1475. """List all files in an index."""
  1476. with open_repo_closing(repo) as r:
  1477. return sorted(r.open_index())
  1478. def describe(repo):
  1479. """Describe the repository version.
  1480. Args:
  1481. projdir: git repository root
  1482. Returns: a string description of the current git revision
  1483. Examples: "gabcdefh", "v0.1" or "v0.1-5-gabcdefh".
  1484. """
  1485. # Get the repository
  1486. with open_repo_closing(repo) as r:
  1487. # Get a list of all tags
  1488. refs = r.get_refs()
  1489. tags = {}
  1490. for key, value in refs.items():
  1491. key = key.decode()
  1492. obj = r.get_object(value)
  1493. if u"tags" not in key:
  1494. continue
  1495. _, tag = key.rsplit(u"/", 1)
  1496. try:
  1497. commit = obj.object
  1498. except AttributeError:
  1499. continue
  1500. else:
  1501. commit = r.get_object(commit[1])
  1502. tags[tag] = [
  1503. datetime.datetime(*time.gmtime(commit.commit_time)[:6]),
  1504. commit.id.decode("ascii"),
  1505. ]
  1506. sorted_tags = sorted(tags.items(), key=lambda tag: tag[1][0], reverse=True)
  1507. # If there are no tags, return the current commit
  1508. if len(sorted_tags) == 0:
  1509. return "g{}".format(r[r.head()].id.decode("ascii")[:7])
  1510. # We're now 0 commits from the top
  1511. commit_count = 0
  1512. # Get the latest commit
  1513. latest_commit = r[r.head()]
  1514. # Walk through all commits
  1515. walker = r.get_walker()
  1516. for entry in walker:
  1517. # Check if tag
  1518. commit_id = entry.commit.id.decode("ascii")
  1519. for tag in sorted_tags:
  1520. tag_name = tag[0]
  1521. tag_commit = tag[1][1]
  1522. if commit_id == tag_commit:
  1523. if commit_count == 0:
  1524. return tag_name
  1525. else:
  1526. return "{}-{}-g{}".format(
  1527. tag_name,
  1528. commit_count,
  1529. latest_commit.id.decode("ascii")[:7],
  1530. )
  1531. commit_count += 1
  1532. # Return plain commit if no parent tag can be found
  1533. return "g{}".format(latest_commit.id.decode("ascii")[:7])
  1534. def get_object_by_path(repo, path, committish=None):
  1535. """Get an object by path.
  1536. Args:
  1537. repo: A path to the repository
  1538. path: Path to look up
  1539. committish: Commit to look up path in
  1540. Returns: A `ShaFile` object
  1541. """
  1542. if committish is None:
  1543. committish = "HEAD"
  1544. # Get the repository
  1545. with open_repo_closing(repo) as r:
  1546. commit = parse_commit(r, committish)
  1547. base_tree = commit.tree
  1548. if not isinstance(path, bytes):
  1549. path = commit_encode(commit, path)
  1550. (mode, sha) = tree_lookup_path(r.object_store.__getitem__, base_tree, path)
  1551. return r[sha]
  1552. def write_tree(repo):
  1553. """Write a tree object from the index.
  1554. Args:
  1555. repo: Repository for which to write tree
  1556. Returns: tree id for the tree that was written
  1557. """
  1558. with open_repo_closing(repo) as r:
  1559. return r.open_index().commit(r.object_store)