porcelain.py 66 KB

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