porcelain.py 61 KB

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