refs.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. # refs.py -- For dealing with git refs
  2. # Copyright (C) 2008-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. """Ref handling.
  21. """
  22. import os
  23. import warnings
  24. from contextlib import suppress
  25. from typing import Dict, Optional
  26. from .errors import PackedRefsException, RefFormatError
  27. from .file import GitFile, ensure_dir_exists
  28. from .objects import ZERO_SHA, ObjectID, Tag, git_line, valid_hexsha
  29. from .pack import ObjectContainer
  30. Ref = bytes
  31. HEADREF = b"HEAD"
  32. SYMREF = b"ref: "
  33. LOCAL_BRANCH_PREFIX = b"refs/heads/"
  34. LOCAL_TAG_PREFIX = b"refs/tags/"
  35. LOCAL_REMOTE_PREFIX = b"refs/remotes/"
  36. BAD_REF_CHARS = set(b"\177 ~^:?*[")
  37. PEELED_TAG_SUFFIX = b"^{}"
  38. # For backwards compatibility
  39. ANNOTATED_TAG_SUFFIX = PEELED_TAG_SUFFIX
  40. class SymrefLoop(Exception):
  41. """There is a loop between one or more symrefs."""
  42. def __init__(self, ref, depth):
  43. self.ref = ref
  44. self.depth = depth
  45. def parse_symref_value(contents):
  46. """Parse a symref value.
  47. Args:
  48. contents: Contents to parse
  49. Returns: Destination
  50. """
  51. if contents.startswith(SYMREF):
  52. return contents[len(SYMREF) :].rstrip(b"\r\n")
  53. raise ValueError(contents)
  54. def check_ref_format(refname: Ref):
  55. """Check if a refname is correctly formatted.
  56. Implements all the same rules as git-check-ref-format[1].
  57. [1]
  58. http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  59. Args:
  60. refname: The refname to check
  61. Returns: True if refname is valid, False otherwise
  62. """
  63. # These could be combined into one big expression, but are listed
  64. # separately to parallel [1].
  65. if b"/." in refname or refname.startswith(b"."):
  66. return False
  67. if b"/" not in refname:
  68. return False
  69. if b".." in refname:
  70. return False
  71. for i, c in enumerate(refname):
  72. if ord(refname[i : i + 1]) < 0o40 or c in BAD_REF_CHARS:
  73. return False
  74. if refname[-1] in b"/.":
  75. return False
  76. if refname.endswith(b".lock"):
  77. return False
  78. if b"@{" in refname:
  79. return False
  80. if b"\\" in refname:
  81. return False
  82. return True
  83. class RefsContainer:
  84. """A container for refs."""
  85. def __init__(self, logger=None):
  86. self._logger = logger
  87. def _log(
  88. self,
  89. ref,
  90. old_sha,
  91. new_sha,
  92. committer=None,
  93. timestamp=None,
  94. timezone=None,
  95. message=None,
  96. ):
  97. if self._logger is None:
  98. return
  99. if message is None:
  100. return
  101. self._logger(ref, old_sha, new_sha, committer, timestamp, timezone, message)
  102. def set_symbolic_ref(
  103. self,
  104. name,
  105. other,
  106. committer=None,
  107. timestamp=None,
  108. timezone=None,
  109. message=None,
  110. ):
  111. """Make a ref point at another ref.
  112. Args:
  113. name: Name of the ref to set
  114. other: Name of the ref to point at
  115. message: Optional message
  116. """
  117. raise NotImplementedError(self.set_symbolic_ref)
  118. def get_packed_refs(self):
  119. """Get contents of the packed-refs file.
  120. Returns: Dictionary mapping ref names to SHA1s
  121. Note: Will return an empty dictionary when no packed-refs file is
  122. present.
  123. """
  124. raise NotImplementedError(self.get_packed_refs)
  125. def add_packed_refs(self, new_refs: Dict[Ref, Optional[ObjectID]]):
  126. """Add the given refs as packed refs.
  127. Args:
  128. new_refs: A mapping of ref names to targets; if a target is None that
  129. means remove the ref
  130. """
  131. raise NotImplementedError(self.add_packed_refs)
  132. def get_peeled(self, name):
  133. """Return the cached peeled value of a ref, if available.
  134. Args:
  135. name: Name of the ref to peel
  136. Returns: The peeled value of the ref. If the ref is known not point to
  137. a tag, this will be the SHA the ref refers to. If the ref may point
  138. to a tag, but no cached information is available, None is returned.
  139. """
  140. return None
  141. def import_refs(
  142. self,
  143. base: Ref,
  144. other: Dict[Ref, ObjectID],
  145. committer: Optional[bytes] = None,
  146. timestamp: Optional[bytes] = None,
  147. timezone: Optional[bytes] = None,
  148. message: Optional[bytes] = None,
  149. prune: bool = False,
  150. ):
  151. if prune:
  152. to_delete = set(self.subkeys(base))
  153. else:
  154. to_delete = set()
  155. for name, value in other.items():
  156. if value is None:
  157. to_delete.add(name)
  158. else:
  159. self.set_if_equals(
  160. b"/".join((base, name)), None, value, message=message
  161. )
  162. if to_delete:
  163. try:
  164. to_delete.remove(name)
  165. except KeyError:
  166. pass
  167. for ref in to_delete:
  168. self.remove_if_equals(b"/".join((base, ref)), None, message=message)
  169. def allkeys(self):
  170. """All refs present in this container."""
  171. raise NotImplementedError(self.allkeys)
  172. def __iter__(self):
  173. return iter(self.allkeys())
  174. def keys(self, base=None):
  175. """Refs present in this container.
  176. Args:
  177. base: An optional base to return refs under.
  178. Returns: An unsorted set of valid refs in this container, including
  179. packed refs.
  180. """
  181. if base is not None:
  182. return self.subkeys(base)
  183. else:
  184. return self.allkeys()
  185. def subkeys(self, base):
  186. """Refs present in this container under a base.
  187. Args:
  188. base: The base to return refs under.
  189. Returns: A set of valid refs in this container under the base; the base
  190. prefix is stripped from the ref names returned.
  191. """
  192. keys = set()
  193. base_len = len(base) + 1
  194. for refname in self.allkeys():
  195. if refname.startswith(base):
  196. keys.add(refname[base_len:])
  197. return keys
  198. def as_dict(self, base=None):
  199. """Return the contents of this container as a dictionary."""
  200. ret = {}
  201. keys = self.keys(base)
  202. if base is None:
  203. base = b""
  204. else:
  205. base = base.rstrip(b"/")
  206. for key in keys:
  207. try:
  208. ret[key] = self[(base + b"/" + key).strip(b"/")]
  209. except (SymrefLoop, KeyError):
  210. continue # Unable to resolve
  211. return ret
  212. def _check_refname(self, name):
  213. """Ensure a refname is valid and lives in refs or is HEAD.
  214. HEAD is not a valid refname according to git-check-ref-format, but this
  215. class needs to be able to touch HEAD. Also, check_ref_format expects
  216. refnames without the leading 'refs/', but this class requires that
  217. so it cannot touch anything outside the refs dir (or HEAD).
  218. Args:
  219. name: The name of the reference.
  220. Raises:
  221. KeyError: if a refname is not HEAD or is otherwise not valid.
  222. """
  223. if name in (HEADREF, b"refs/stash"):
  224. return
  225. if not name.startswith(b"refs/") or not check_ref_format(name[5:]):
  226. raise RefFormatError(name)
  227. def read_ref(self, refname):
  228. """Read a reference without following any references.
  229. Args:
  230. refname: The name of the reference
  231. Returns: The contents of the ref file, or None if it does
  232. not exist.
  233. """
  234. contents = self.read_loose_ref(refname)
  235. if not contents:
  236. contents = self.get_packed_refs().get(refname, None)
  237. return contents
  238. def read_loose_ref(self, name):
  239. """Read a loose reference and return its contents.
  240. Args:
  241. name: the refname to read
  242. Returns: The contents of the ref file, or None if it does
  243. not exist.
  244. """
  245. raise NotImplementedError(self.read_loose_ref)
  246. def follow(self, name):
  247. """Follow a reference name.
  248. Returns: a tuple of (refnames, sha), wheres refnames are the names of
  249. references in the chain
  250. """
  251. contents = SYMREF + name
  252. depth = 0
  253. refnames = []
  254. while contents.startswith(SYMREF):
  255. refname = contents[len(SYMREF) :]
  256. refnames.append(refname)
  257. contents = self.read_ref(refname)
  258. if not contents:
  259. break
  260. depth += 1
  261. if depth > 5:
  262. raise SymrefLoop(name, depth)
  263. return refnames, contents
  264. def __contains__(self, refname):
  265. if self.read_ref(refname):
  266. return True
  267. return False
  268. def __getitem__(self, name):
  269. """Get the SHA1 for a reference name.
  270. This method follows all symbolic references.
  271. """
  272. _, sha = self.follow(name)
  273. if sha is None:
  274. raise KeyError(name)
  275. return sha
  276. def set_if_equals(
  277. self,
  278. name,
  279. old_ref,
  280. new_ref,
  281. committer=None,
  282. timestamp=None,
  283. timezone=None,
  284. message=None,
  285. ):
  286. """Set a refname to new_ref only if it currently equals old_ref.
  287. This method follows all symbolic references if applicable for the
  288. subclass, and can be used to perform an atomic compare-and-swap
  289. operation.
  290. Args:
  291. name: The refname to set.
  292. old_ref: The old sha the refname must refer to, or None to set
  293. unconditionally.
  294. new_ref: The new sha the refname will refer to.
  295. message: Message for reflog
  296. Returns: True if the set was successful, False otherwise.
  297. """
  298. raise NotImplementedError(self.set_if_equals)
  299. def add_if_new(self, name, ref, committer=None, timestamp=None,
  300. timezone=None, message=None):
  301. """Add a new reference only if it does not already exist.
  302. Args:
  303. name: Ref name
  304. ref: Ref value
  305. """
  306. raise NotImplementedError(self.add_if_new)
  307. def __setitem__(self, name, ref):
  308. """Set a reference name to point to the given SHA1.
  309. This method follows all symbolic references if applicable for the
  310. subclass.
  311. Note: This method unconditionally overwrites the contents of a
  312. reference. To update atomically only if the reference has not
  313. changed, use set_if_equals().
  314. Args:
  315. name: The refname to set.
  316. ref: The new sha the refname will refer to.
  317. """
  318. self.set_if_equals(name, None, ref)
  319. def remove_if_equals(
  320. self,
  321. name,
  322. old_ref,
  323. committer=None,
  324. timestamp=None,
  325. timezone=None,
  326. message=None,
  327. ):
  328. """Remove a refname only if it currently equals old_ref.
  329. This method does not follow symbolic references, even if applicable for
  330. the subclass. It can be used to perform an atomic compare-and-delete
  331. operation.
  332. Args:
  333. name: The refname to delete.
  334. old_ref: The old sha the refname must refer to, or None to
  335. delete unconditionally.
  336. message: Message for reflog
  337. Returns: True if the delete was successful, False otherwise.
  338. """
  339. raise NotImplementedError(self.remove_if_equals)
  340. def __delitem__(self, name):
  341. """Remove a refname.
  342. This method does not follow symbolic references, even if applicable for
  343. the subclass.
  344. Note: This method unconditionally deletes the contents of a reference.
  345. To delete atomically only if the reference has not changed, use
  346. remove_if_equals().
  347. Args:
  348. name: The refname to delete.
  349. """
  350. self.remove_if_equals(name, None)
  351. def get_symrefs(self):
  352. """Get a dict with all symrefs in this container.
  353. Returns: Dictionary mapping source ref to target ref
  354. """
  355. ret = {}
  356. for src in self.allkeys():
  357. try:
  358. dst = parse_symref_value(self.read_ref(src))
  359. except ValueError:
  360. pass
  361. else:
  362. ret[src] = dst
  363. return ret
  364. class DictRefsContainer(RefsContainer):
  365. """RefsContainer backed by a simple dict.
  366. This container does not support symbolic or packed references and is not
  367. threadsafe.
  368. """
  369. def __init__(self, refs, logger=None):
  370. super().__init__(logger=logger)
  371. self._refs = refs
  372. self._peeled = {}
  373. self._watchers = set()
  374. def allkeys(self):
  375. return self._refs.keys()
  376. def read_loose_ref(self, name):
  377. return self._refs.get(name, None)
  378. def get_packed_refs(self):
  379. return {}
  380. def _notify(self, ref, newsha):
  381. for watcher in self._watchers:
  382. watcher._notify((ref, newsha))
  383. def set_symbolic_ref(
  384. self,
  385. name: Ref,
  386. other: Ref,
  387. committer=None,
  388. timestamp=None,
  389. timezone=None,
  390. message=None,
  391. ):
  392. old = self.follow(name)[-1]
  393. new = SYMREF + other
  394. self._refs[name] = new
  395. self._notify(name, new)
  396. self._log(
  397. name,
  398. old,
  399. new,
  400. committer=committer,
  401. timestamp=timestamp,
  402. timezone=timezone,
  403. message=message,
  404. )
  405. def set_if_equals(
  406. self,
  407. name,
  408. old_ref,
  409. new_ref,
  410. committer=None,
  411. timestamp=None,
  412. timezone=None,
  413. message=None,
  414. ):
  415. if old_ref is not None and self._refs.get(name, ZERO_SHA) != old_ref:
  416. return False
  417. realnames, _ = self.follow(name)
  418. for realname in realnames:
  419. self._check_refname(realname)
  420. old = self._refs.get(realname)
  421. self._refs[realname] = new_ref
  422. self._notify(realname, new_ref)
  423. self._log(
  424. realname,
  425. old,
  426. new_ref,
  427. committer=committer,
  428. timestamp=timestamp,
  429. timezone=timezone,
  430. message=message,
  431. )
  432. return True
  433. def add_if_new(
  434. self,
  435. name: Ref,
  436. ref: ObjectID,
  437. committer=None,
  438. timestamp=None,
  439. timezone=None,
  440. message: Optional[bytes] = None,
  441. ):
  442. if name in self._refs:
  443. return False
  444. self._refs[name] = ref
  445. self._notify(name, ref)
  446. self._log(
  447. name,
  448. None,
  449. ref,
  450. committer=committer,
  451. timestamp=timestamp,
  452. timezone=timezone,
  453. message=message,
  454. )
  455. return True
  456. def remove_if_equals(
  457. self,
  458. name,
  459. old_ref,
  460. committer=None,
  461. timestamp=None,
  462. timezone=None,
  463. message=None,
  464. ):
  465. if old_ref is not None and self._refs.get(name, ZERO_SHA) != old_ref:
  466. return False
  467. try:
  468. old = self._refs.pop(name)
  469. except KeyError:
  470. pass
  471. else:
  472. self._notify(name, None)
  473. self._log(
  474. name,
  475. old,
  476. None,
  477. committer=committer,
  478. timestamp=timestamp,
  479. timezone=timezone,
  480. message=message,
  481. )
  482. return True
  483. def get_peeled(self, name):
  484. return self._peeled.get(name)
  485. def _update(self, refs):
  486. """Update multiple refs; intended only for testing."""
  487. # TODO(dborowitz): replace this with a public function that uses
  488. # set_if_equal.
  489. for ref, sha in refs.items():
  490. self.set_if_equals(ref, None, sha)
  491. def _update_peeled(self, peeled):
  492. """Update cached peeled refs; intended only for testing."""
  493. self._peeled.update(peeled)
  494. class InfoRefsContainer(RefsContainer):
  495. """Refs container that reads refs from a info/refs file."""
  496. def __init__(self, f):
  497. self._refs = {}
  498. self._peeled = {}
  499. for line in f.readlines():
  500. sha, name = line.rstrip(b"\n").split(b"\t")
  501. if name.endswith(PEELED_TAG_SUFFIX):
  502. name = name[:-3]
  503. if not check_ref_format(name):
  504. raise ValueError("invalid ref name %r" % name)
  505. self._peeled[name] = sha
  506. else:
  507. if not check_ref_format(name):
  508. raise ValueError("invalid ref name %r" % name)
  509. self._refs[name] = sha
  510. def allkeys(self):
  511. return self._refs.keys()
  512. def read_loose_ref(self, name):
  513. return self._refs.get(name, None)
  514. def get_packed_refs(self):
  515. return {}
  516. def get_peeled(self, name):
  517. try:
  518. return self._peeled[name]
  519. except KeyError:
  520. return self._refs[name]
  521. class DiskRefsContainer(RefsContainer):
  522. """Refs container that reads refs from disk."""
  523. def __init__(self, path, worktree_path=None, logger=None):
  524. super().__init__(logger=logger)
  525. if getattr(path, "encode", None) is not None:
  526. path = os.fsencode(path)
  527. self.path = path
  528. if worktree_path is None:
  529. worktree_path = path
  530. if getattr(worktree_path, "encode", None) is not None:
  531. worktree_path = os.fsencode(worktree_path)
  532. self.worktree_path = worktree_path
  533. self._packed_refs = None
  534. self._peeled_refs = None
  535. def __repr__(self):
  536. return "{}({!r})".format(self.__class__.__name__, self.path)
  537. def subkeys(self, base):
  538. subkeys = set()
  539. path = self.refpath(base)
  540. for root, unused_dirs, files in os.walk(path):
  541. dir = root[len(path) :]
  542. if os.path.sep != "/":
  543. dir = dir.replace(os.fsencode(os.path.sep), b"/")
  544. dir = dir.strip(b"/")
  545. for filename in files:
  546. refname = b"/".join(([dir] if dir else []) + [filename])
  547. # check_ref_format requires at least one /, so we prepend the
  548. # base before calling it.
  549. if check_ref_format(base + b"/" + refname):
  550. subkeys.add(refname)
  551. for key in self.get_packed_refs():
  552. if key.startswith(base):
  553. subkeys.add(key[len(base) :].strip(b"/"))
  554. return subkeys
  555. def allkeys(self):
  556. allkeys = set()
  557. if os.path.exists(self.refpath(HEADREF)):
  558. allkeys.add(HEADREF)
  559. path = self.refpath(b"")
  560. refspath = self.refpath(b"refs")
  561. for root, unused_dirs, files in os.walk(refspath):
  562. dir = root[len(path) :]
  563. if os.path.sep != "/":
  564. dir = dir.replace(os.fsencode(os.path.sep), b"/")
  565. for filename in files:
  566. refname = b"/".join([dir, filename])
  567. if check_ref_format(refname):
  568. allkeys.add(refname)
  569. allkeys.update(self.get_packed_refs())
  570. return allkeys
  571. def refpath(self, name):
  572. """Return the disk path of a ref."""
  573. if os.path.sep != "/":
  574. name = name.replace(b"/", os.fsencode(os.path.sep))
  575. # TODO: as the 'HEAD' reference is working tree specific, it
  576. # should actually not be a part of RefsContainer
  577. if name == HEADREF:
  578. return os.path.join(self.worktree_path, name)
  579. else:
  580. return os.path.join(self.path, name)
  581. def get_packed_refs(self):
  582. """Get contents of the packed-refs file.
  583. Returns: Dictionary mapping ref names to SHA1s
  584. Note: Will return an empty dictionary when no packed-refs file is
  585. present.
  586. """
  587. # TODO: invalidate the cache on repacking
  588. if self._packed_refs is None:
  589. # set both to empty because we want _peeled_refs to be
  590. # None if and only if _packed_refs is also None.
  591. self._packed_refs = {}
  592. self._peeled_refs = {}
  593. path = os.path.join(self.path, b"packed-refs")
  594. try:
  595. f = GitFile(path, "rb")
  596. except FileNotFoundError:
  597. return {}
  598. with f:
  599. first_line = next(iter(f)).rstrip()
  600. if first_line.startswith(b"# pack-refs") and b" peeled" in first_line:
  601. for sha, name, peeled in read_packed_refs_with_peeled(f):
  602. self._packed_refs[name] = sha
  603. if peeled:
  604. self._peeled_refs[name] = peeled
  605. else:
  606. f.seek(0)
  607. for sha, name in read_packed_refs(f):
  608. self._packed_refs[name] = sha
  609. return self._packed_refs
  610. def add_packed_refs(self, new_refs: Dict[Ref, Optional[ObjectID]]):
  611. """Add the given refs as packed refs.
  612. Args:
  613. new_refs: A mapping of ref names to targets; if a target is None that
  614. means remove the ref
  615. """
  616. if not new_refs:
  617. return
  618. path = os.path.join(self.path, b"packed-refs")
  619. with GitFile(path, "wb") as f:
  620. # reread cached refs from disk, while holding the lock
  621. packed_refs = self.get_packed_refs().copy()
  622. for ref, target in new_refs.items():
  623. # sanity check
  624. if ref == HEADREF:
  625. raise ValueError("cannot pack HEAD")
  626. # remove any loose refs pointing to this one -- please
  627. # note that this bypasses remove_if_equals as we don't
  628. # want to affect packed refs in here
  629. with suppress(OSError):
  630. os.remove(self.refpath(ref))
  631. if target is not None:
  632. packed_refs[ref] = target
  633. else:
  634. packed_refs.pop(ref, None)
  635. write_packed_refs(f, packed_refs, self._peeled_refs)
  636. self._packed_refs = packed_refs
  637. def get_peeled(self, name):
  638. """Return the cached peeled value of a ref, if available.
  639. Args:
  640. name: Name of the ref to peel
  641. Returns: The peeled value of the ref. If the ref is known not point to
  642. a tag, this will be the SHA the ref refers to. If the ref may point
  643. to a tag, but no cached information is available, None is returned.
  644. """
  645. self.get_packed_refs()
  646. if self._peeled_refs is None or name not in self._packed_refs:
  647. # No cache: no peeled refs were read, or this ref is loose
  648. return None
  649. if name in self._peeled_refs:
  650. return self._peeled_refs[name]
  651. else:
  652. # Known not peelable
  653. return self[name]
  654. def read_loose_ref(self, name):
  655. """Read a reference file and return its contents.
  656. If the reference file a symbolic reference, only read the first line of
  657. the file. Otherwise, only read the first 40 bytes.
  658. Args:
  659. name: the refname to read, relative to refpath
  660. Returns: The contents of the ref file, or None if the file does not
  661. exist.
  662. Raises:
  663. IOError: if any other error occurs
  664. """
  665. filename = self.refpath(name)
  666. try:
  667. with GitFile(filename, "rb") as f:
  668. header = f.read(len(SYMREF))
  669. if header == SYMREF:
  670. # Read only the first line
  671. return header + next(iter(f)).rstrip(b"\r\n")
  672. else:
  673. # Read only the first 40 bytes
  674. return header + f.read(40 - len(SYMREF))
  675. except (OSError, UnicodeError):
  676. # don't assume anything specific about the error; in
  677. # particular, invalid or forbidden paths can raise weird
  678. # errors depending on the specific operating system
  679. return None
  680. def _remove_packed_ref(self, name):
  681. if self._packed_refs is None:
  682. return
  683. filename = os.path.join(self.path, b"packed-refs")
  684. # reread cached refs from disk, while holding the lock
  685. f = GitFile(filename, "wb")
  686. try:
  687. self._packed_refs = None
  688. self.get_packed_refs()
  689. if name not in self._packed_refs:
  690. return
  691. del self._packed_refs[name]
  692. with suppress(KeyError):
  693. del self._peeled_refs[name]
  694. write_packed_refs(f, self._packed_refs, self._peeled_refs)
  695. f.close()
  696. finally:
  697. f.abort()
  698. def set_symbolic_ref(
  699. self,
  700. name,
  701. other,
  702. committer=None,
  703. timestamp=None,
  704. timezone=None,
  705. message=None,
  706. ):
  707. """Make a ref point at another ref.
  708. Args:
  709. name: Name of the ref to set
  710. other: Name of the ref to point at
  711. message: Optional message to describe the change
  712. """
  713. self._check_refname(name)
  714. self._check_refname(other)
  715. filename = self.refpath(name)
  716. f = GitFile(filename, "wb")
  717. try:
  718. f.write(SYMREF + other + b"\n")
  719. sha = self.follow(name)[-1]
  720. self._log(
  721. name,
  722. sha,
  723. sha,
  724. committer=committer,
  725. timestamp=timestamp,
  726. timezone=timezone,
  727. message=message,
  728. )
  729. except BaseException:
  730. f.abort()
  731. raise
  732. else:
  733. f.close()
  734. def set_if_equals(
  735. self,
  736. name,
  737. old_ref,
  738. new_ref,
  739. committer=None,
  740. timestamp=None,
  741. timezone=None,
  742. message=None,
  743. ):
  744. """Set a refname to new_ref only if it currently equals old_ref.
  745. This method follows all symbolic references, and can be used to perform
  746. an atomic compare-and-swap operation.
  747. Args:
  748. name: The refname to set.
  749. old_ref: The old sha the refname must refer to, or None to set
  750. unconditionally.
  751. new_ref: The new sha the refname will refer to.
  752. message: Set message for reflog
  753. Returns: True if the set was successful, False otherwise.
  754. """
  755. self._check_refname(name)
  756. try:
  757. realnames, _ = self.follow(name)
  758. realname = realnames[-1]
  759. except (KeyError, IndexError, SymrefLoop):
  760. realname = name
  761. filename = self.refpath(realname)
  762. # make sure none of the ancestor folders is in packed refs
  763. probe_ref = os.path.dirname(realname)
  764. packed_refs = self.get_packed_refs()
  765. while probe_ref:
  766. if packed_refs.get(probe_ref, None) is not None:
  767. raise NotADirectoryError(filename)
  768. probe_ref = os.path.dirname(probe_ref)
  769. ensure_dir_exists(os.path.dirname(filename))
  770. with GitFile(filename, "wb") as f:
  771. if old_ref is not None:
  772. try:
  773. # read again while holding the lock
  774. orig_ref = self.read_loose_ref(realname)
  775. if orig_ref is None:
  776. orig_ref = self.get_packed_refs().get(realname, ZERO_SHA)
  777. if orig_ref != old_ref:
  778. f.abort()
  779. return False
  780. except OSError:
  781. f.abort()
  782. raise
  783. try:
  784. f.write(new_ref + b"\n")
  785. except OSError:
  786. f.abort()
  787. raise
  788. self._log(
  789. realname,
  790. old_ref,
  791. new_ref,
  792. committer=committer,
  793. timestamp=timestamp,
  794. timezone=timezone,
  795. message=message,
  796. )
  797. return True
  798. def add_if_new(
  799. self,
  800. name: bytes,
  801. ref: bytes,
  802. committer=None,
  803. timestamp=None,
  804. timezone=None,
  805. message: Optional[bytes] = None,
  806. ):
  807. """Add a new reference only if it does not already exist.
  808. This method follows symrefs, and only ensures that the last ref in the
  809. chain does not exist.
  810. Args:
  811. name: The refname to set.
  812. ref: The new sha the refname will refer to.
  813. message: Optional message for reflog
  814. Returns: True if the add was successful, False otherwise.
  815. """
  816. try:
  817. realnames, contents = self.follow(name)
  818. if contents is not None:
  819. return False
  820. realname = realnames[-1]
  821. except (KeyError, IndexError):
  822. realname = name
  823. self._check_refname(realname)
  824. filename = self.refpath(realname)
  825. ensure_dir_exists(os.path.dirname(filename))
  826. with GitFile(filename, "wb") as f:
  827. if os.path.exists(filename) or name in self.get_packed_refs():
  828. f.abort()
  829. return False
  830. try:
  831. f.write(ref + b"\n")
  832. except OSError:
  833. f.abort()
  834. raise
  835. else:
  836. self._log(
  837. name,
  838. None,
  839. ref,
  840. committer=committer,
  841. timestamp=timestamp,
  842. timezone=timezone,
  843. message=message,
  844. )
  845. return True
  846. def remove_if_equals(
  847. self,
  848. name,
  849. old_ref,
  850. committer=None,
  851. timestamp=None,
  852. timezone=None,
  853. message=None,
  854. ):
  855. """Remove a refname only if it currently equals old_ref.
  856. This method does not follow symbolic references. It can be used to
  857. perform an atomic compare-and-delete operation.
  858. Args:
  859. name: The refname to delete.
  860. old_ref: The old sha the refname must refer to, or None to
  861. delete unconditionally.
  862. message: Optional message
  863. Returns: True if the delete was successful, False otherwise.
  864. """
  865. self._check_refname(name)
  866. filename = self.refpath(name)
  867. ensure_dir_exists(os.path.dirname(filename))
  868. f = GitFile(filename, "wb")
  869. try:
  870. if old_ref is not None:
  871. orig_ref = self.read_loose_ref(name)
  872. if orig_ref is None:
  873. orig_ref = self.get_packed_refs().get(name, ZERO_SHA)
  874. if orig_ref != old_ref:
  875. return False
  876. # remove the reference file itself
  877. try:
  878. found = os.path.lexists(filename)
  879. except OSError:
  880. # may only be packed, or otherwise unstorable
  881. found = False
  882. if found:
  883. os.remove(filename)
  884. self._remove_packed_ref(name)
  885. self._log(
  886. name,
  887. old_ref,
  888. None,
  889. committer=committer,
  890. timestamp=timestamp,
  891. timezone=timezone,
  892. message=message,
  893. )
  894. finally:
  895. # never write, we just wanted the lock
  896. f.abort()
  897. # outside of the lock, clean-up any parent directory that might now
  898. # be empty. this ensures that re-creating a reference of the same
  899. # name of what was previously a directory works as expected
  900. parent = name
  901. while True:
  902. try:
  903. parent, _ = parent.rsplit(b"/", 1)
  904. except ValueError:
  905. break
  906. if parent == b'refs':
  907. break
  908. parent_filename = self.refpath(parent)
  909. try:
  910. os.rmdir(parent_filename)
  911. except OSError:
  912. # this can be caused by the parent directory being
  913. # removed by another process, being not empty, etc.
  914. # in any case, this is non fatal because we already
  915. # removed the reference, just ignore it
  916. break
  917. return True
  918. def _split_ref_line(line):
  919. """Split a single ref line into a tuple of SHA1 and name."""
  920. fields = line.rstrip(b"\n\r").split(b" ")
  921. if len(fields) != 2:
  922. raise PackedRefsException("invalid ref line %r" % line)
  923. sha, name = fields
  924. if not valid_hexsha(sha):
  925. raise PackedRefsException("Invalid hex sha %r" % sha)
  926. if not check_ref_format(name):
  927. raise PackedRefsException("invalid ref name %r" % name)
  928. return (sha, name)
  929. def read_packed_refs(f):
  930. """Read a packed refs file.
  931. Args:
  932. f: file-like object to read from
  933. Returns: Iterator over tuples with SHA1s and ref names.
  934. """
  935. for line in f:
  936. if line.startswith(b"#"):
  937. # Comment
  938. continue
  939. if line.startswith(b"^"):
  940. raise PackedRefsException("found peeled ref in packed-refs without peeled")
  941. yield _split_ref_line(line)
  942. def read_packed_refs_with_peeled(f):
  943. """Read a packed refs file including peeled refs.
  944. Assumes the "# pack-refs with: peeled" line was already read. Yields tuples
  945. with ref names, SHA1s, and peeled SHA1s (or None).
  946. Args:
  947. f: file-like object to read from, seek'ed to the second line
  948. """
  949. last = None
  950. for line in f:
  951. if line[0] == b"#":
  952. continue
  953. line = line.rstrip(b"\r\n")
  954. if line.startswith(b"^"):
  955. if not last:
  956. raise PackedRefsException("unexpected peeled ref line")
  957. if not valid_hexsha(line[1:]):
  958. raise PackedRefsException("Invalid hex sha %r" % line[1:])
  959. sha, name = _split_ref_line(last)
  960. last = None
  961. yield (sha, name, line[1:])
  962. else:
  963. if last:
  964. sha, name = _split_ref_line(last)
  965. yield (sha, name, None)
  966. last = line
  967. if last:
  968. sha, name = _split_ref_line(last)
  969. yield (sha, name, None)
  970. def write_packed_refs(f, packed_refs, peeled_refs=None):
  971. """Write a packed refs file.
  972. Args:
  973. f: empty file-like object to write to
  974. packed_refs: dict of refname to sha of packed refs to write
  975. peeled_refs: dict of refname to peeled value of sha
  976. """
  977. if peeled_refs is None:
  978. peeled_refs = {}
  979. else:
  980. f.write(b"# pack-refs with: peeled\n")
  981. for refname in sorted(packed_refs.keys()):
  982. f.write(git_line(packed_refs[refname], refname))
  983. if refname in peeled_refs:
  984. f.write(b"^" + peeled_refs[refname] + b"\n")
  985. def read_info_refs(f):
  986. ret = {}
  987. for line in f.readlines():
  988. (sha, name) = line.rstrip(b"\r\n").split(b"\t", 1)
  989. ret[name] = sha
  990. return ret
  991. def write_info_refs(refs, store: ObjectContainer):
  992. """Generate info refs."""
  993. # TODO: Avoid recursive import :(
  994. from .object_store import peel_sha
  995. for name, sha in sorted(refs.items()):
  996. # get_refs() includes HEAD as a special case, but we don't want to
  997. # advertise it
  998. if name == HEADREF:
  999. continue
  1000. try:
  1001. o = store[sha]
  1002. except KeyError:
  1003. continue
  1004. unpeeled, peeled = peel_sha(store, sha)
  1005. yield o.id + b"\t" + name + b"\n"
  1006. if o.id != peeled.id:
  1007. yield peeled.id + b"\t" + name + PEELED_TAG_SUFFIX + b"\n"
  1008. def is_local_branch(x):
  1009. return x.startswith(LOCAL_BRANCH_PREFIX)
  1010. def strip_peeled_refs(refs):
  1011. """Remove all peeled refs"""
  1012. return {
  1013. ref: sha
  1014. for (ref, sha) in refs.items()
  1015. if not ref.endswith(PEELED_TAG_SUFFIX)
  1016. }
  1017. def _set_origin_head(refs, origin, origin_head):
  1018. # set refs/remotes/origin/HEAD
  1019. origin_base = b"refs/remotes/" + origin + b"/"
  1020. if origin_head and origin_head.startswith(LOCAL_BRANCH_PREFIX):
  1021. origin_ref = origin_base + HEADREF
  1022. target_ref = origin_base + origin_head[len(LOCAL_BRANCH_PREFIX) :]
  1023. if target_ref in refs:
  1024. refs.set_symbolic_ref(origin_ref, target_ref)
  1025. def _set_default_branch(
  1026. refs: RefsContainer, origin: bytes, origin_head: bytes, branch: bytes,
  1027. ref_message: Optional[bytes]) -> bytes:
  1028. """Set the default branch.
  1029. """
  1030. origin_base = b"refs/remotes/" + origin + b"/"
  1031. if branch:
  1032. origin_ref = origin_base + branch
  1033. if origin_ref in refs:
  1034. local_ref = LOCAL_BRANCH_PREFIX + branch
  1035. refs.add_if_new(
  1036. local_ref, refs[origin_ref], ref_message
  1037. )
  1038. head_ref = local_ref
  1039. elif LOCAL_TAG_PREFIX + branch in refs:
  1040. head_ref = LOCAL_TAG_PREFIX + branch
  1041. else:
  1042. raise ValueError(
  1043. "%r is not a valid branch or tag" % os.fsencode(branch)
  1044. )
  1045. elif origin_head:
  1046. head_ref = origin_head
  1047. if origin_head.startswith(LOCAL_BRANCH_PREFIX):
  1048. origin_ref = origin_base + origin_head[len(LOCAL_BRANCH_PREFIX) :]
  1049. else:
  1050. origin_ref = origin_head
  1051. try:
  1052. refs.add_if_new(
  1053. head_ref, refs[origin_ref], ref_message
  1054. )
  1055. except KeyError:
  1056. pass
  1057. else:
  1058. raise ValueError('neither origin_head nor branch are provided')
  1059. return head_ref
  1060. def _set_head(refs, head_ref, ref_message):
  1061. if head_ref.startswith(LOCAL_TAG_PREFIX):
  1062. # detach HEAD at specified tag
  1063. head = refs[head_ref]
  1064. if isinstance(head, Tag):
  1065. _cls, obj = head.object
  1066. head = obj.get_object(obj).id
  1067. del refs[HEADREF]
  1068. refs.set_if_equals(
  1069. HEADREF, None, head, message=ref_message
  1070. )
  1071. else:
  1072. # set HEAD to specific branch
  1073. try:
  1074. head = refs[head_ref]
  1075. refs.set_symbolic_ref(HEADREF, head_ref)
  1076. refs.set_if_equals(HEADREF, None, head, message=ref_message)
  1077. except KeyError:
  1078. head = None
  1079. return head
  1080. def _import_remote_refs(
  1081. refs_container: RefsContainer,
  1082. remote_name: str,
  1083. refs: Dict[str, str],
  1084. message: Optional[bytes] = None,
  1085. prune: bool = False,
  1086. prune_tags: bool = False,
  1087. ):
  1088. stripped_refs = strip_peeled_refs(refs)
  1089. branches = {
  1090. n[len(LOCAL_BRANCH_PREFIX) :]: v
  1091. for (n, v) in stripped_refs.items()
  1092. if n.startswith(LOCAL_BRANCH_PREFIX)
  1093. }
  1094. refs_container.import_refs(
  1095. b"refs/remotes/" + remote_name.encode(),
  1096. branches,
  1097. message=message,
  1098. prune=prune,
  1099. )
  1100. tags = {
  1101. n[len(LOCAL_TAG_PREFIX) :]: v
  1102. for (n, v) in stripped_refs.items()
  1103. if n.startswith(LOCAL_TAG_PREFIX) and not n.endswith(PEELED_TAG_SUFFIX)
  1104. }
  1105. refs_container.import_refs(LOCAL_TAG_PREFIX, tags, message=message, prune=prune_tags)
  1106. def serialize_refs(store, refs):
  1107. # TODO: Avoid recursive import :(
  1108. from .object_store import peel_sha
  1109. ret = {}
  1110. for ref, sha in refs.items():
  1111. try:
  1112. unpeeled, peeled = peel_sha(store, sha)
  1113. except KeyError:
  1114. warnings.warn(
  1115. "ref %s points at non-present sha %s"
  1116. % (ref.decode("utf-8", "replace"), sha.decode("ascii")),
  1117. UserWarning,
  1118. )
  1119. continue
  1120. else:
  1121. if isinstance(unpeeled, Tag):
  1122. ret[ref + PEELED_TAG_SUFFIX] = peeled.id
  1123. ret[ref] = unpeeled.id
  1124. return ret