porcelain.py 62 KB

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