porcelain.py 61 KB

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