2
0

porcelain.py 60 KB

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