porcelain.py 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. # porcelain.py -- Porcelain-like layer on top of Dulwich
  2. # Copyright (C) 2013 Jelmer Vernooij <jelmer@samba.org>
  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. * clone
  26. * commit
  27. * commit-tree
  28. * daemon
  29. * diff-tree
  30. * fetch
  31. * init
  32. * ls-remote
  33. * ls-tree
  34. * pull
  35. * push
  36. * rm
  37. * remote{_add}
  38. * receive-pack
  39. * reset
  40. * rev-list
  41. * tag{_create,_delete,_list}
  42. * upload-pack
  43. * update-server-info
  44. * status
  45. * symbolic-ref
  46. These functions are meant to behave similarly to the git subcommands.
  47. Differences in behaviour are considered bugs.
  48. """
  49. from collections import namedtuple
  50. from contextlib import (
  51. closing,
  52. contextmanager,
  53. )
  54. import os
  55. import posixpath
  56. import stat
  57. import sys
  58. import time
  59. from dulwich.archive import (
  60. tar_stream,
  61. )
  62. from dulwich.client import (
  63. get_transport_and_path,
  64. )
  65. from dulwich.diff_tree import (
  66. CHANGE_ADD,
  67. CHANGE_DELETE,
  68. CHANGE_MODIFY,
  69. CHANGE_RENAME,
  70. CHANGE_COPY,
  71. RENAME_CHANGE_TYPES,
  72. )
  73. from dulwich.errors import (
  74. SendPackError,
  75. UpdateRefsError,
  76. )
  77. from dulwich.index import get_unstaged_changes
  78. from dulwich.objects import (
  79. Commit,
  80. Tag,
  81. format_timezone,
  82. parse_timezone,
  83. pretty_format_tree_entry,
  84. )
  85. from dulwich.objectspec import (
  86. parse_object,
  87. parse_reftuples,
  88. )
  89. from dulwich.pack import (
  90. write_pack_index,
  91. write_pack_objects,
  92. )
  93. from dulwich.patch import write_tree_diff
  94. from dulwich.protocol import (
  95. Protocol,
  96. ZERO_SHA,
  97. )
  98. from dulwich.refs import ANNOTATED_TAG_SUFFIX
  99. from dulwich.repo import (BaseRepo, Repo)
  100. from dulwich.server import (
  101. FileSystemBackend,
  102. TCPGitServer,
  103. ReceivePackHandler,
  104. UploadPackHandler,
  105. update_server_info as server_update_server_info,
  106. )
  107. # Module level tuple definition for status output
  108. GitStatus = namedtuple('GitStatus', 'staged unstaged untracked')
  109. default_bytes_out_stream = getattr(sys.stdout, 'buffer', sys.stdout)
  110. default_bytes_err_stream = getattr(sys.stderr, 'buffer', sys.stderr)
  111. DEFAULT_ENCODING = 'utf-8'
  112. class RemoteExists(Exception):
  113. """Raised when the remote already exists."""
  114. def open_repo(path_or_repo):
  115. """Open an argument that can be a repository or a path for a repository."""
  116. if isinstance(path_or_repo, BaseRepo):
  117. return path_or_repo
  118. return Repo(path_or_repo)
  119. @contextmanager
  120. def _noop_context_manager(obj):
  121. """Context manager that has the same api as closing but does nothing."""
  122. yield obj
  123. def open_repo_closing(path_or_repo):
  124. """Open an argument that can be a repository or a path for a repository.
  125. returns a context manager that will close the repo on exit if the argument
  126. is a path, else does nothing if the argument is a repo.
  127. """
  128. if isinstance(path_or_repo, BaseRepo):
  129. return _noop_context_manager(path_or_repo)
  130. return closing(Repo(path_or_repo))
  131. def archive(repo, committish=None, outstream=default_bytes_out_stream,
  132. errstream=default_bytes_err_stream):
  133. """Create an archive.
  134. :param repo: Path of repository for which to generate an archive.
  135. :param committish: Commit SHA1 or ref to use
  136. :param outstream: Output stream (defaults to stdout)
  137. :param errstream: Error stream (defaults to stderr)
  138. """
  139. if committish is None:
  140. committish = "HEAD"
  141. with open_repo_closing(repo) as repo_obj:
  142. c = repo_obj[committish]
  143. for chunk in tar_stream(
  144. repo_obj.object_store, repo_obj.object_store[c.tree],
  145. c.commit_time):
  146. outstream.write(chunk)
  147. def update_server_info(repo="."):
  148. """Update server info files for a repository.
  149. :param repo: path to the repository
  150. """
  151. with open_repo_closing(repo) as r:
  152. server_update_server_info(r)
  153. def symbolic_ref(repo, ref_name, force=False):
  154. """Set git symbolic ref into HEAD.
  155. :param repo: path to the repository
  156. :param ref_name: short name of the new ref
  157. :param force: force settings without checking if it exists in refs/heads
  158. """
  159. with open_repo_closing(repo) as repo_obj:
  160. ref_path = b'refs/heads/' + ref_name
  161. if not force and ref_path not in repo_obj.refs.keys():
  162. raise ValueError('fatal: ref `%s` is not a ref' % ref_name)
  163. repo_obj.refs.set_symbolic_ref(b'HEAD', ref_path)
  164. def commit(repo=".", message=None, author=None, committer=None):
  165. """Create a new commit.
  166. :param repo: Path to repository
  167. :param message: Optional commit message
  168. :param author: Optional author name and email
  169. :param committer: Optional committer name and email
  170. :return: SHA1 of the new commit
  171. """
  172. # FIXME: Support --all argument
  173. # FIXME: Support --signoff argument
  174. with open_repo_closing(repo) as r:
  175. return r.do_commit(message=message, author=author, committer=committer)
  176. def commit_tree(repo, tree, message=None, author=None, committer=None):
  177. """Create a new commit object.
  178. :param repo: Path to repository
  179. :param tree: An existing tree object
  180. :param author: Optional author name and email
  181. :param committer: Optional committer name and email
  182. """
  183. with open_repo_closing(repo) as r:
  184. return r.do_commit(
  185. message=message, tree=tree, committer=committer, author=author)
  186. def init(path=".", bare=False):
  187. """Create a new git repository.
  188. :param path: Path to repository.
  189. :param bare: Whether to create a bare repository.
  190. :return: A Repo instance
  191. """
  192. if not os.path.exists(path):
  193. os.mkdir(path)
  194. if bare:
  195. return Repo.init_bare(path)
  196. else:
  197. return Repo.init(path)
  198. def clone(source, target=None, bare=False, checkout=None,
  199. errstream=default_bytes_err_stream, outstream=None,
  200. origin=b"origin"):
  201. """Clone a local or remote git repository.
  202. :param source: Path or URL for source repository
  203. :param target: Path to target repository (optional)
  204. :param bare: Whether or not to create a bare repository
  205. :param checkout: Whether or not to check-out HEAD after cloning
  206. :param errstream: Optional stream to write progress to
  207. :param outstream: Optional stream to write progress to (deprecated)
  208. :return: The new repository
  209. """
  210. if outstream is not None:
  211. import warnings
  212. warnings.warn(
  213. "outstream= has been deprecated in favour of errstream=.",
  214. DeprecationWarning, stacklevel=3)
  215. errstream = outstream
  216. if checkout is None:
  217. checkout = (not bare)
  218. if checkout and bare:
  219. raise ValueError("checkout and bare are incompatible")
  220. client, host_path = get_transport_and_path(source)
  221. if target is None:
  222. target = host_path.split("/")[-1]
  223. if not os.path.exists(target):
  224. os.mkdir(target)
  225. if bare:
  226. r = Repo.init_bare(target)
  227. else:
  228. r = Repo.init(target)
  229. try:
  230. remote_refs = client.fetch(
  231. host_path, r, determine_wants=r.object_store.determine_wants_all,
  232. progress=errstream.write)
  233. r.refs.import_refs(
  234. b'refs/remotes/' + origin,
  235. {n[len(b'refs/heads/'):]: v for (n, v) in remote_refs.items()
  236. if n.startswith(b'refs/heads/')})
  237. r.refs.import_refs(
  238. b'refs/tags',
  239. {n[len(b'refs/tags/'):]: v for (n, v) in remote_refs.items()
  240. if n.startswith(b'refs/tags/') and
  241. not n.endswith(ANNOTATED_TAG_SUFFIX)})
  242. if b"HEAD" in remote_refs and not bare:
  243. # TODO(jelmer): Support symref capability,
  244. # https://github.com/jelmer/dulwich/issues/485
  245. r[b"HEAD"] = remote_refs[b"HEAD"]
  246. target_config = r.get_config()
  247. if not isinstance(source, bytes):
  248. source = source.encode(DEFAULT_ENCODING)
  249. target_config.set((b'remote', b'origin'), b'url', source)
  250. target_config.set(
  251. (b'remote', b'origin'), b'fetch',
  252. b'+refs/heads/*:refs/remotes/origin/*')
  253. target_config.write_to_path()
  254. if checkout and b"HEAD" in r.refs:
  255. errstream.write(b'Checking out HEAD\n')
  256. r.reset_index()
  257. except:
  258. r.close()
  259. raise
  260. return r
  261. def add(repo=".", paths=None):
  262. """Add files to the staging area.
  263. :param repo: Repository for the files
  264. :param paths: Paths to add. No value passed stages all modified files.
  265. """
  266. with open_repo_closing(repo) as r:
  267. if not paths:
  268. paths = list(
  269. get_untracked_paths(os.getcwd(), r.path, r.open_index()))
  270. # TODO(jelmer): Possibly allow passing in absolute paths?
  271. relpaths = []
  272. if not isinstance(paths, list):
  273. paths = [paths]
  274. for p in paths:
  275. # FIXME: Support patterns, directories.
  276. if os.path.isabs(p) and p.startswith(repo.path):
  277. relpath = os.path.relpath(p, repo.path)
  278. else:
  279. relpath = p
  280. relpaths.append(relpath)
  281. r.stage(relpaths)
  282. def rm(repo=".", paths=None):
  283. """Remove files from the staging area.
  284. :param repo: Repository for the files
  285. :param paths: Paths to remove
  286. """
  287. with open_repo_closing(repo) as r:
  288. index = r.open_index()
  289. for p in paths:
  290. del index[p.encode(sys.getfilesystemencoding())]
  291. index.write()
  292. def commit_decode(commit, contents, default_encoding=DEFAULT_ENCODING):
  293. if commit.encoding is not None:
  294. return contents.decode(commit.encoding, "replace")
  295. return contents.decode(default_encoding, "replace")
  296. def print_commit(commit, decode, outstream=sys.stdout):
  297. """Write a human-readable commit log entry.
  298. :param commit: A `Commit` object
  299. :param outstream: A stream file to write to
  300. """
  301. outstream.write("-" * 50 + "\n")
  302. outstream.write("commit: " + commit.id.decode('ascii') + "\n")
  303. if len(commit.parents) > 1:
  304. outstream.write(
  305. "merge: " +
  306. "...".join([c.decode('ascii') for c in commit.parents[1:]]) + "\n")
  307. outstream.write("Author: " + decode(commit.author) + "\n")
  308. if commit.author != commit.committer:
  309. outstream.write("Committer: " + decode(commit.committer) + "\n")
  310. time_tuple = time.gmtime(commit.author_time + commit.author_timezone)
  311. time_str = time.strftime("%a %b %d %Y %H:%M:%S", time_tuple)
  312. timezone_str = format_timezone(commit.author_timezone).decode('ascii')
  313. outstream.write("Date: " + time_str + " " + timezone_str + "\n")
  314. outstream.write("\n")
  315. outstream.write(decode(commit.message) + "\n")
  316. outstream.write("\n")
  317. def print_tag(tag, decode, outstream=sys.stdout):
  318. """Write a human-readable tag.
  319. :param tag: A `Tag` object
  320. :param decode: Function for decoding bytes to unicode string
  321. :param outstream: A stream to write to
  322. """
  323. outstream.write("Tagger: " + decode(tag.tagger) + "\n")
  324. outstream.write("Date: " + decode(tag.tag_time) + "\n")
  325. outstream.write("\n")
  326. outstream.write(decode(tag.message) + "\n")
  327. outstream.write("\n")
  328. def show_blob(repo, blob, decode, outstream=sys.stdout):
  329. """Write a blob to a stream.
  330. :param repo: A `Repo` object
  331. :param blob: A `Blob` object
  332. :param decode: Function for decoding bytes to unicode string
  333. :param outstream: A stream file to write to
  334. """
  335. outstream.write(decode(blob.data))
  336. def show_commit(repo, commit, decode, outstream=sys.stdout):
  337. """Show a commit to a stream.
  338. :param repo: A `Repo` object
  339. :param commit: A `Commit` object
  340. :param decode: Function for decoding bytes to unicode string
  341. :param outstream: Stream to write to
  342. """
  343. print_commit(commit, decode=decode, outstream=outstream)
  344. parent_commit = repo[commit.parents[0]]
  345. write_tree_diff(
  346. outstream, repo.object_store, parent_commit.tree, commit.tree)
  347. def show_tree(repo, tree, decode, outstream=sys.stdout):
  348. """Print a tree to a stream.
  349. :param repo: A `Repo` object
  350. :param tree: A `Tree` object
  351. :param decode: Function for decoding bytes to unicode string
  352. :param outstream: Stream to write to
  353. """
  354. for n in tree:
  355. outstream.write(decode(n) + "\n")
  356. def show_tag(repo, tag, decode, outstream=sys.stdout):
  357. """Print a tag to a stream.
  358. :param repo: A `Repo` object
  359. :param tag: A `Tag` object
  360. :param decode: Function for decoding bytes to unicode string
  361. :param outstream: Stream to write to
  362. """
  363. print_tag(tag, decode, outstream)
  364. show_object(repo, repo[tag.object[1]], outstream)
  365. def show_object(repo, obj, decode, outstream):
  366. return {
  367. b"tree": show_tree,
  368. b"blob": show_blob,
  369. b"commit": show_commit,
  370. b"tag": show_tag,
  371. }[obj.type_name](repo, obj, decode, outstream)
  372. def print_name_status(changes):
  373. """Print a simple status summary, listing changed files.
  374. """
  375. for change in changes:
  376. if not change:
  377. continue
  378. if isinstance(change, list):
  379. change = change[0]
  380. if change.type == CHANGE_ADD:
  381. path1 = change.new.path
  382. path2 = ''
  383. kind = 'A'
  384. elif change.type == CHANGE_DELETE:
  385. path1 = change.old.path
  386. path2 = ''
  387. kind = 'D'
  388. elif change.type == CHANGE_MODIFY:
  389. path1 = change.new.path
  390. path2 = ''
  391. kind = 'M'
  392. elif change.type in RENAME_CHANGE_TYPES:
  393. path1 = change.old.path
  394. path2 = change.new.path
  395. if change.type == CHANGE_RENAME:
  396. kind = 'R'
  397. elif change.type == CHANGE_COPY:
  398. kind = 'C'
  399. yield '%-8s%-20s%-20s' % (kind, path1, path2)
  400. def log(repo=".", paths=None, outstream=sys.stdout, max_entries=None,
  401. reverse=False, name_status=False):
  402. """Write commit logs.
  403. :param repo: Path to repository
  404. :param paths: Optional set of specific paths to print entries for
  405. :param outstream: Stream to write log output to
  406. :param reverse: Reverse order in which entries are printed
  407. :param name_status: Print name status
  408. :param max_entries: Optional maximum number of entries to display
  409. """
  410. with open_repo_closing(repo) as r:
  411. walker = r.get_walker(
  412. max_entries=max_entries, paths=paths, reverse=reverse)
  413. for entry in walker:
  414. def decode(x):
  415. return commit_decode(entry.commit, x)
  416. print_commit(entry.commit, decode, outstream)
  417. if name_status:
  418. outstream.writelines(
  419. [l+'\n' for l in print_name_status(entry.changes())])
  420. # TODO(jelmer): better default for encoding?
  421. def show(repo=".", objects=None, outstream=sys.stdout,
  422. default_encoding=DEFAULT_ENCODING):
  423. """Print the changes in a commit.
  424. :param repo: Path to repository
  425. :param objects: Objects to show (defaults to [HEAD])
  426. :param outstream: Stream to write to
  427. :param default_encoding: Default encoding to use if none is set in the
  428. commit
  429. """
  430. if objects is None:
  431. objects = ["HEAD"]
  432. if not isinstance(objects, list):
  433. objects = [objects]
  434. with open_repo_closing(repo) as r:
  435. for objectish in objects:
  436. o = parse_object(r, objectish)
  437. if isinstance(o, Commit):
  438. def decode(x):
  439. return commit_decode(o, x, default_encoding)
  440. else:
  441. def decode(x):
  442. return x.decode(default_encoding)
  443. show_object(r, o, decode, outstream)
  444. def diff_tree(repo, old_tree, new_tree, outstream=sys.stdout):
  445. """Compares the content and mode of blobs found via two tree objects.
  446. :param repo: Path to repository
  447. :param old_tree: Id of old tree
  448. :param new_tree: Id of new tree
  449. :param outstream: Stream to write to
  450. """
  451. with open_repo_closing(repo) as r:
  452. write_tree_diff(outstream, r.object_store, old_tree, new_tree)
  453. def rev_list(repo, commits, outstream=sys.stdout):
  454. """Lists commit objects in reverse chronological order.
  455. :param repo: Path to repository
  456. :param commits: Commits over which to iterate
  457. :param outstream: Stream to write to
  458. """
  459. with open_repo_closing(repo) as r:
  460. for entry in r.get_walker(include=[r[c].id for c in commits]):
  461. outstream.write(entry.commit.id + b"\n")
  462. def tag(*args, **kwargs):
  463. import warnings
  464. warnings.warn("tag has been deprecated in favour of tag_create.",
  465. DeprecationWarning)
  466. return tag_create(*args, **kwargs)
  467. def tag_create(
  468. repo, tag, author=None, message=None, annotated=False,
  469. objectish="HEAD", tag_time=None, tag_timezone=None):
  470. """Creates a tag in git via dulwich calls:
  471. :param repo: Path to repository
  472. :param tag: tag string
  473. :param author: tag author (optional, if annotated is set)
  474. :param message: tag message (optional)
  475. :param annotated: whether to create an annotated tag
  476. :param objectish: object the tag should point at, defaults to HEAD
  477. :param tag_time: Optional time for annotated tag
  478. :param tag_timezone: Optional timezone for annotated tag
  479. """
  480. with open_repo_closing(repo) as r:
  481. object = parse_object(r, objectish)
  482. if annotated:
  483. # Create the tag object
  484. tag_obj = Tag()
  485. if author is None:
  486. # TODO(jelmer): Don't use repo private method.
  487. author = r._get_user_identity()
  488. tag_obj.tagger = author
  489. tag_obj.message = message
  490. tag_obj.name = tag
  491. tag_obj.object = (type(object), object.id)
  492. if tag_time is None:
  493. tag_time = int(time.time())
  494. tag_obj.tag_time = tag_time
  495. if tag_timezone is None:
  496. # TODO(jelmer) Use current user timezone rather than UTC
  497. tag_timezone = 0
  498. elif isinstance(tag_timezone, str):
  499. tag_timezone = parse_timezone(tag_timezone)
  500. tag_obj.tag_timezone = tag_timezone
  501. r.object_store.add_object(tag_obj)
  502. tag_id = tag_obj.id
  503. else:
  504. tag_id = object.id
  505. r.refs[b'refs/tags/' + tag] = tag_id
  506. def list_tags(*args, **kwargs):
  507. import warnings
  508. warnings.warn("list_tags has been deprecated in favour of tag_list.",
  509. DeprecationWarning)
  510. return tag_list(*args, **kwargs)
  511. def tag_list(repo, outstream=sys.stdout):
  512. """List all tags.
  513. :param repo: Path to repository
  514. :param outstream: Stream to write tags to
  515. """
  516. with open_repo_closing(repo) as r:
  517. tags = sorted(r.refs.as_dict(b"refs/tags"))
  518. return tags
  519. def tag_delete(repo, name):
  520. """Remove a tag.
  521. :param repo: Path to repository
  522. :param name: Name of tag to remove
  523. """
  524. with open_repo_closing(repo) as r:
  525. if isinstance(name, bytes):
  526. names = [name]
  527. elif isinstance(name, list):
  528. names = name
  529. else:
  530. raise TypeError("Unexpected tag name type %r" % name)
  531. for name in names:
  532. del r.refs[b"refs/tags/" + name]
  533. def reset(repo, mode, committish="HEAD"):
  534. """Reset current HEAD to the specified state.
  535. :param repo: Path to repository
  536. :param mode: Mode ("hard", "soft", "mixed")
  537. """
  538. if mode != "hard":
  539. raise ValueError("hard is the only mode currently supported")
  540. with open_repo_closing(repo) as r:
  541. tree = r[committish].tree
  542. r.reset_index(tree)
  543. def push(repo, remote_location, refspecs,
  544. outstream=default_bytes_out_stream,
  545. errstream=default_bytes_err_stream):
  546. """Remote push with dulwich via dulwich.client
  547. :param repo: Path to repository
  548. :param remote_location: Location of the remote
  549. :param refspecs: Refs to push to remote
  550. :param outstream: A stream file to write output
  551. :param errstream: A stream file to write errors
  552. """
  553. # Open the repo
  554. with open_repo_closing(repo) as r:
  555. # Get the client and path
  556. client, path = get_transport_and_path(remote_location)
  557. selected_refs = []
  558. def update_refs(refs):
  559. selected_refs.extend(parse_reftuples(r.refs, refs, refspecs))
  560. new_refs = {}
  561. # TODO: Handle selected_refs == {None: None}
  562. for (lh, rh, force) in selected_refs:
  563. if lh is None:
  564. new_refs[rh] = ZERO_SHA
  565. else:
  566. new_refs[rh] = r.refs[lh]
  567. return new_refs
  568. err_encoding = getattr(errstream, 'encoding', None) or DEFAULT_ENCODING
  569. remote_location_bytes = client.get_url(path).encode(err_encoding)
  570. try:
  571. client.send_pack(
  572. path, update_refs, r.object_store.generate_pack_contents,
  573. progress=errstream.write)
  574. errstream.write(
  575. b"Push to " + remote_location_bytes + b" successful.\n")
  576. except (UpdateRefsError, SendPackError) as e:
  577. errstream.write(b"Push to " + remote_location_bytes +
  578. b" failed -> " + e.message.encode(err_encoding) +
  579. b"\n")
  580. def pull(repo, remote_location=None, refspecs=None,
  581. outstream=default_bytes_out_stream,
  582. errstream=default_bytes_err_stream):
  583. """Pull from remote via dulwich.client
  584. :param repo: Path to repository
  585. :param remote_location: Location of the remote
  586. :param refspec: refspecs to fetch
  587. :param outstream: A stream file to write to output
  588. :param errstream: A stream file to write to errors
  589. """
  590. # Open the repo
  591. with open_repo_closing(repo) as r:
  592. if remote_location is None:
  593. # TODO(jelmer): Lookup 'remote' for current branch in config
  594. raise NotImplementedError(
  595. "looking up remote from branch config not supported yet")
  596. if refspecs is None:
  597. refspecs = [b"HEAD"]
  598. selected_refs = []
  599. def determine_wants(remote_refs):
  600. selected_refs.extend(
  601. parse_reftuples(remote_refs, r.refs, refspecs))
  602. return [remote_refs[lh] for (lh, rh, force) in selected_refs]
  603. client, path = get_transport_and_path(remote_location)
  604. remote_refs = client.fetch(
  605. path, r, progress=errstream.write, determine_wants=determine_wants)
  606. for (lh, rh, force) in selected_refs:
  607. r.refs[rh] = remote_refs[lh]
  608. if selected_refs:
  609. r[b'HEAD'] = remote_refs[selected_refs[0][1]]
  610. # Perform 'git checkout .' - syncs staged changes
  611. tree = r[b"HEAD"].tree
  612. r.reset_index(tree=tree)
  613. def status(repo="."):
  614. """Returns staged, unstaged, and untracked changes relative to the HEAD.
  615. :param repo: Path to repository or repository object
  616. :return: GitStatus tuple,
  617. staged - list of staged paths (diff index/HEAD)
  618. unstaged - list of unstaged paths (diff index/working-tree)
  619. untracked - list of untracked, un-ignored & non-.git paths
  620. """
  621. with open_repo_closing(repo) as r:
  622. # 1. Get status of staged
  623. tracked_changes = get_tree_changes(r)
  624. # 2. Get status of unstaged
  625. index = r.open_index()
  626. unstaged_changes = list(get_unstaged_changes(index, r.path))
  627. untracked_changes = list(get_untracked_paths(r.path, r.path, index))
  628. return GitStatus(tracked_changes, unstaged_changes, untracked_changes)
  629. def get_untracked_paths(frompath, basepath, index):
  630. """Get untracked paths.
  631. ;param frompath: Path to walk
  632. :param basepath: Path to compare to
  633. :param index: Index to check against
  634. """
  635. # If nothing is specified, add all non-ignored files.
  636. for dirpath, dirnames, filenames in os.walk(frompath):
  637. # Skip .git and below.
  638. if '.git' in dirnames:
  639. dirnames.remove('.git')
  640. for filename in filenames:
  641. p = os.path.join(dirpath[len(basepath)+1:], filename)
  642. if p not in index:
  643. yield p
  644. def get_tree_changes(repo):
  645. """Return add/delete/modify changes to tree by comparing index to HEAD.
  646. :param repo: repo path or object
  647. :return: dict with lists for each type of change
  648. """
  649. with open_repo_closing(repo) as r:
  650. index = r.open_index()
  651. # Compares the Index to the HEAD & determines changes
  652. # Iterate through the changes and report add/delete/modify
  653. # TODO: call out to dulwich.diff_tree somehow.
  654. tracked_changes = {
  655. 'add': [],
  656. 'delete': [],
  657. 'modify': [],
  658. }
  659. try:
  660. tree_id = r[b'HEAD'].tree
  661. except KeyError:
  662. tree_id = None
  663. for change in index.changes_from_tree(r.object_store, tree_id):
  664. if not change[0][0]:
  665. tracked_changes['add'].append(change[0][1])
  666. elif not change[0][1]:
  667. tracked_changes['delete'].append(change[0][0])
  668. elif change[0][0] == change[0][1]:
  669. tracked_changes['modify'].append(change[0][0])
  670. else:
  671. raise AssertionError('git mv ops not yet supported')
  672. return tracked_changes
  673. def daemon(path=".", address=None, port=None):
  674. """Run a daemon serving Git requests over TCP/IP.
  675. :param path: Path to the directory to serve.
  676. :param address: Optional address to listen on (defaults to ::)
  677. :param port: Optional port to listen on (defaults to TCP_GIT_PORT)
  678. """
  679. # TODO(jelmer): Support git-daemon-export-ok and --export-all.
  680. backend = FileSystemBackend(path)
  681. server = TCPGitServer(backend, address, port)
  682. server.serve_forever()
  683. def web_daemon(path=".", address=None, port=None):
  684. """Run a daemon serving Git requests over HTTP.
  685. :param path: Path to the directory to serve
  686. :param address: Optional address to listen on (defaults to ::)
  687. :param port: Optional port to listen on (defaults to 80)
  688. """
  689. from dulwich.web import (
  690. make_wsgi_chain,
  691. make_server,
  692. WSGIRequestHandlerLogger,
  693. WSGIServerLogger)
  694. backend = FileSystemBackend(path)
  695. app = make_wsgi_chain(backend)
  696. server = make_server(address, port, app,
  697. handler_class=WSGIRequestHandlerLogger,
  698. server_class=WSGIServerLogger)
  699. server.serve_forever()
  700. def upload_pack(path=".", inf=None, outf=None):
  701. """Upload a pack file after negotiating its contents using smart protocol.
  702. :param path: Path to the repository
  703. :param inf: Input stream to communicate with client
  704. :param outf: Output stream to communicate with client
  705. """
  706. if outf is None:
  707. outf = getattr(sys.stdout, 'buffer', sys.stdout)
  708. if inf is None:
  709. inf = getattr(sys.stdin, 'buffer', sys.stdin)
  710. path = os.path.expanduser(path)
  711. backend = FileSystemBackend(path)
  712. def send_fn(data):
  713. outf.write(data)
  714. outf.flush()
  715. proto = Protocol(inf.read, send_fn)
  716. handler = UploadPackHandler(backend, [path], proto)
  717. # FIXME: Catch exceptions and write a single-line summary to outf.
  718. handler.handle()
  719. return 0
  720. def receive_pack(path=".", inf=None, outf=None):
  721. """Receive a pack file after negotiating its contents using smart protocol.
  722. :param path: Path to the repository
  723. :param inf: Input stream to communicate with client
  724. :param outf: Output stream to communicate with client
  725. """
  726. if outf is None:
  727. outf = getattr(sys.stdout, 'buffer', sys.stdout)
  728. if inf is None:
  729. inf = getattr(sys.stdin, 'buffer', sys.stdin)
  730. path = os.path.expanduser(path)
  731. backend = FileSystemBackend(path)
  732. def send_fn(data):
  733. outf.write(data)
  734. outf.flush()
  735. proto = Protocol(inf.read, send_fn)
  736. handler = ReceivePackHandler(backend, [path], proto)
  737. # FIXME: Catch exceptions and write a single-line summary to outf.
  738. handler.handle()
  739. return 0
  740. def branch_delete(repo, name):
  741. """Delete a branch.
  742. :param repo: Path to the repository
  743. :param name: Name of the branch
  744. """
  745. with open_repo_closing(repo) as r:
  746. if isinstance(name, bytes):
  747. names = [name]
  748. elif isinstance(name, list):
  749. names = name
  750. else:
  751. raise TypeError("Unexpected branch name type %r" % name)
  752. for name in names:
  753. del r.refs[b"refs/heads/" + name]
  754. def branch_create(repo, name, objectish=None, force=False):
  755. """Create a branch.
  756. :param repo: Path to the repository
  757. :param name: Name of the new branch
  758. :param objectish: Target object to point new branch at (defaults to HEAD)
  759. :param force: Force creation of branch, even if it already exists
  760. """
  761. with open_repo_closing(repo) as r:
  762. if objectish is None:
  763. objectish = "HEAD"
  764. object = parse_object(r, objectish)
  765. refname = b"refs/heads/" + name
  766. if refname in r.refs and not force:
  767. raise KeyError("Branch with name %s already exists." % name)
  768. r.refs[refname] = object.id
  769. def branch_list(repo):
  770. """List all branches.
  771. :param repo: Path to the repository
  772. """
  773. with open_repo_closing(repo) as r:
  774. return r.refs.keys(base=b"refs/heads/")
  775. def fetch(repo, remote_location, outstream=sys.stdout,
  776. errstream=default_bytes_err_stream):
  777. """Fetch objects from a remote server.
  778. :param repo: Path to the repository
  779. :param remote_location: String identifying a remote server
  780. :param outstream: Output stream (defaults to stdout)
  781. :param errstream: Error stream (defaults to stderr)
  782. :return: Dictionary with refs on the remote
  783. """
  784. with open_repo_closing(repo) as r:
  785. client, path = get_transport_and_path(remote_location)
  786. remote_refs = client.fetch(path, r, progress=errstream.write)
  787. return remote_refs
  788. def ls_remote(remote):
  789. """List the refs in a remote.
  790. :param remote: Remote repository location
  791. :return: Dictionary with remote refs
  792. """
  793. client, host_path = get_transport_and_path(remote)
  794. return client.get_refs(host_path)
  795. def repack(repo):
  796. """Repack loose files in a repository.
  797. Currently this only packs loose objects.
  798. :param repo: Path to the repository
  799. """
  800. with open_repo_closing(repo) as r:
  801. r.object_store.pack_loose_objects()
  802. def pack_objects(repo, object_ids, packf, idxf, delta_window_size=None):
  803. """Pack objects into a file.
  804. :param repo: Path to the repository
  805. :param object_ids: List of object ids to write
  806. :param packf: File-like object to write to
  807. :param idxf: File-like object to write to (can be None)
  808. """
  809. with open_repo_closing(repo) as r:
  810. entries, data_sum = write_pack_objects(
  811. packf,
  812. r.object_store.iter_shas((oid, None) for oid in object_ids),
  813. delta_window_size=delta_window_size)
  814. if idxf is not None:
  815. entries = sorted([(k, v[0], v[1]) for (k, v) in entries.items()])
  816. write_pack_index(idxf, entries, data_sum)
  817. def ls_tree(repo, tree_ish=None, outstream=sys.stdout, recursive=False,
  818. name_only=False):
  819. """List contents of a tree.
  820. :param repo: Path to the repository
  821. :param tree_ish: Tree id to list
  822. :param outstream: Output stream (defaults to stdout)
  823. :param recursive: Whether to recursively list files
  824. :param name_only: Only print item name
  825. """
  826. def list_tree(store, treeid, base):
  827. for (name, mode, sha) in store[treeid].iteritems():
  828. if base:
  829. name = posixpath.join(base, name)
  830. if name_only:
  831. outstream.write(name + b"\n")
  832. else:
  833. outstream.write(pretty_format_tree_entry(name, mode, sha))
  834. if stat.S_ISDIR(mode):
  835. list_tree(store, sha, name)
  836. if tree_ish is None:
  837. tree_ish = "HEAD"
  838. with open_repo_closing(repo) as r:
  839. c = r[tree_ish]
  840. treeid = c.tree
  841. list_tree(r.object_store, treeid, "")
  842. def remote_add(repo, name, url):
  843. """Add a remote.
  844. :param repo: Path to the repository
  845. :param name: Remote name
  846. :param url: Remote URL
  847. """
  848. if not isinstance(name, bytes):
  849. name = name.encode(DEFAULT_ENCODING)
  850. if not isinstance(url, bytes):
  851. url = url.encode(DEFAULT_ENCODING)
  852. with open_repo_closing(repo) as r:
  853. c = r.get_config()
  854. section = (b'remote', name)
  855. if c.has_section(section):
  856. raise RemoteExists(section)
  857. c.set(section, b"url", url)
  858. c.write_to_path()