refs.py 40 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. import os
  22. import warnings
  23. from contextlib import suppress
  24. from typing import Any, Dict, Optional, Set, List, Tuple
  25. from .errors import PackedRefsException, RefFormatError
  26. from .file import GitFile, ensure_dir_exists
  27. from .objects import ZERO_SHA, ObjectID, Tag, git_line, valid_hexsha
  28. from .pack import ObjectContainer
  29. Ref = bytes
  30. HEADREF = b"HEAD"
  31. SYMREF = b"ref: "
  32. LOCAL_BRANCH_PREFIX = b"refs/heads/"
  33. LOCAL_TAG_PREFIX = b"refs/tags/"
  34. LOCAL_REMOTE_PREFIX = b"refs/remotes/"
  35. BAD_REF_CHARS = set(b"\177 ~^:?*[")
  36. PEELED_TAG_SUFFIX = b"^{}"
  37. # For backwards compatibility
  38. ANNOTATED_TAG_SUFFIX = PEELED_TAG_SUFFIX
  39. class SymrefLoop(Exception):
  40. """There is a loop between one or more symrefs."""
  41. def __init__(self, ref, depth) -> None:
  42. self.ref = ref
  43. self.depth = depth
  44. def parse_symref_value(contents: bytes) -> bytes:
  45. """Parse a symref value.
  46. Args:
  47. contents: Contents to parse
  48. Returns: Destination
  49. """
  50. if contents.startswith(SYMREF):
  51. return contents[len(SYMREF) :].rstrip(b"\r\n")
  52. raise ValueError(contents)
  53. def check_ref_format(refname: Ref):
  54. """Check if a refname is correctly formatted.
  55. Implements all the same rules as git-check-ref-format[1].
  56. [1]
  57. http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  58. Args:
  59. refname: The refname to check
  60. Returns: True if refname is valid, False otherwise
  61. """
  62. # These could be combined into one big expression, but are listed
  63. # separately to parallel [1].
  64. if b"/." in refname or refname.startswith(b"."):
  65. return False
  66. if b"/" not in refname:
  67. return False
  68. if b".." in refname:
  69. return False
  70. for i, c in enumerate(refname):
  71. if ord(refname[i : i + 1]) < 0o40 or c in BAD_REF_CHARS:
  72. return False
  73. if refname[-1] in b"/.":
  74. return False
  75. if refname.endswith(b".lock"):
  76. return False
  77. if b"@{" in refname:
  78. return False
  79. if b"\\" in refname:
  80. return False
  81. return True
  82. class RefsContainer:
  83. """A container for refs."""
  84. def __init__(self, logger=None) -> None:
  85. self._logger = logger
  86. def _log(
  87. self,
  88. ref,
  89. old_sha,
  90. new_sha,
  91. committer=None,
  92. timestamp=None,
  93. timezone=None,
  94. message=None,
  95. ):
  96. if self._logger is None:
  97. return
  98. if message is None:
  99. return
  100. self._logger(ref, old_sha, new_sha, committer, timestamp, timezone, message)
  101. def set_symbolic_ref(
  102. self,
  103. name,
  104. other,
  105. committer=None,
  106. timestamp=None,
  107. timezone=None,
  108. message=None,
  109. ):
  110. """Make a ref point at another ref.
  111. Args:
  112. name: Name of the ref to set
  113. other: Name of the ref to point at
  114. message: Optional message
  115. """
  116. raise NotImplementedError(self.set_symbolic_ref)
  117. def get_packed_refs(self):
  118. """Get contents of the packed-refs file.
  119. Returns: Dictionary mapping ref names to SHA1s
  120. Note: Will return an empty dictionary when no packed-refs file is
  121. present.
  122. """
  123. raise NotImplementedError(self.get_packed_refs)
  124. def add_packed_refs(self, new_refs: Dict[Ref, Optional[ObjectID]]):
  125. """Add the given refs as packed refs.
  126. Args:
  127. new_refs: A mapping of ref names to targets; if a target is None that
  128. means remove the ref
  129. """
  130. raise NotImplementedError(self.add_packed_refs)
  131. def get_peeled(self, name):
  132. """Return the cached peeled value of a ref, if available.
  133. Args:
  134. name: Name of the ref to peel
  135. Returns: The peeled value of the ref. If the ref is known not point to
  136. a tag, this will be the SHA the ref refers to. If the ref may point
  137. to a tag, but no cached information is available, None is returned.
  138. """
  139. return None
  140. def import_refs(
  141. self,
  142. base: Ref,
  143. other: Dict[Ref, ObjectID],
  144. committer: Optional[bytes] = None,
  145. timestamp: Optional[bytes] = None,
  146. timezone: Optional[bytes] = None,
  147. message: Optional[bytes] = None,
  148. prune: bool = False,
  149. ):
  150. if prune:
  151. to_delete = set(self.subkeys(base))
  152. else:
  153. to_delete = set()
  154. for name, value in other.items():
  155. if value is None:
  156. to_delete.add(name)
  157. else:
  158. self.set_if_equals(
  159. b"/".join((base, name)), None, value, message=message
  160. )
  161. if to_delete:
  162. try:
  163. to_delete.remove(name)
  164. except KeyError:
  165. pass
  166. for ref in to_delete:
  167. self.remove_if_equals(b"/".join((base, ref)), None, message=message)
  168. def allkeys(self):
  169. """All refs present in this container."""
  170. raise NotImplementedError(self.allkeys)
  171. def __iter__(self):
  172. return iter(self.allkeys())
  173. def keys(self, base=None):
  174. """Refs present in this container.
  175. Args:
  176. base: An optional base to return refs under.
  177. Returns: An unsorted set of valid refs in this container, including
  178. packed refs.
  179. """
  180. if base is not None:
  181. return self.subkeys(base)
  182. else:
  183. return self.allkeys()
  184. def subkeys(self, base):
  185. """Refs present in this container under a base.
  186. Args:
  187. base: The base to return refs under.
  188. Returns: A set of valid refs in this container under the base; the base
  189. prefix is stripped from the ref names returned.
  190. """
  191. keys = set()
  192. base_len = len(base) + 1
  193. for refname in self.allkeys():
  194. if refname.startswith(base):
  195. keys.add(refname[base_len:])
  196. return keys
  197. def as_dict(self, base=None):
  198. """Return the contents of this container as a dictionary."""
  199. ret = {}
  200. keys = self.keys(base)
  201. if base is None:
  202. base = b""
  203. else:
  204. base = base.rstrip(b"/")
  205. for key in keys:
  206. try:
  207. ret[key] = self[(base + b"/" + key).strip(b"/")]
  208. except (SymrefLoop, KeyError):
  209. continue # Unable to resolve
  210. return ret
  211. def _check_refname(self, name):
  212. """Ensure a refname is valid and lives in refs or is HEAD.
  213. HEAD is not a valid refname according to git-check-ref-format, but this
  214. class needs to be able to touch HEAD. Also, check_ref_format expects
  215. refnames without the leading 'refs/', but this class requires that
  216. so it cannot touch anything outside the refs dir (or HEAD).
  217. Args:
  218. name: The name of the reference.
  219. Raises:
  220. KeyError: if a refname is not HEAD or is otherwise not valid.
  221. """
  222. if name in (HEADREF, b"refs/stash"):
  223. return
  224. if not name.startswith(b"refs/") or not check_ref_format(name[5:]):
  225. raise RefFormatError(name)
  226. def read_ref(self, refname):
  227. """Read a reference without following any references.
  228. Args:
  229. refname: The name of the reference
  230. Returns: The contents of the ref file, or None if it does
  231. not exist.
  232. """
  233. contents = self.read_loose_ref(refname)
  234. if not contents:
  235. contents = self.get_packed_refs().get(refname, None)
  236. return contents
  237. def read_loose_ref(self, name):
  238. """Read a loose reference and return its contents.
  239. Args:
  240. name: the refname to read
  241. Returns: The contents of the ref file, or None if it does
  242. not exist.
  243. """
  244. raise NotImplementedError(self.read_loose_ref)
  245. def follow(self, name) -> Tuple[List[bytes], bytes]:
  246. """Follow a reference name.
  247. Returns: a tuple of (refnames, sha), wheres refnames are the names of
  248. references in the chain
  249. """
  250. contents = SYMREF + name
  251. depth = 0
  252. refnames = []
  253. while contents.startswith(SYMREF):
  254. refname = contents[len(SYMREF) :]
  255. refnames.append(refname)
  256. contents = self.read_ref(refname)
  257. if not contents:
  258. break
  259. depth += 1
  260. if depth > 5:
  261. raise SymrefLoop(name, depth)
  262. return refnames, contents
  263. def __contains__(self, refname) -> bool:
  264. if self.read_ref(refname):
  265. return True
  266. return False
  267. def __getitem__(self, name):
  268. """Get the SHA1 for a reference name.
  269. This method follows all symbolic references.
  270. """
  271. _, sha = self.follow(name)
  272. if sha is None:
  273. raise KeyError(name)
  274. return sha
  275. def set_if_equals(
  276. self,
  277. name,
  278. old_ref,
  279. new_ref,
  280. committer=None,
  281. timestamp=None,
  282. timezone=None,
  283. message=None,
  284. ):
  285. """Set a refname to new_ref only if it currently equals old_ref.
  286. This method follows all symbolic references if applicable for the
  287. subclass, and can be used to perform an atomic compare-and-swap
  288. operation.
  289. Args:
  290. name: The refname to set.
  291. old_ref: The old sha the refname must refer to, or None to set
  292. unconditionally.
  293. new_ref: The new sha the refname will refer to.
  294. message: Message for reflog
  295. Returns: True if the set was successful, False otherwise.
  296. """
  297. raise NotImplementedError(self.set_if_equals)
  298. def add_if_new(
  299. self, name, ref, committer=None, timestamp=None, timezone=None, message=None
  300. ):
  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) -> None:
  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) -> None:
  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) -> None:
  370. super().__init__(logger=logger)
  371. self._refs = refs
  372. self._peeled: Dict[bytes, ObjectID] = {}
  373. self._watchers: Set[Any] = 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) -> None:
  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(f"invalid ref name {name!r}")
  505. self._peeled[name] = sha
  506. else:
  507. if not check_ref_format(name):
  508. raise ValueError(f"invalid ref name {name!r}")
  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) -> 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) -> str:
  536. return f"{self.__class__.__name__}({self.path!r})"
  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(f"invalid ref line {line!r}")
  923. sha, name = fields
  924. if not valid_hexsha(sha):
  925. raise PackedRefsException(f"Invalid hex sha {sha!r}")
  926. if not check_ref_format(name):
  927. raise PackedRefsException(f"invalid ref name {name!r}")
  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(f"Invalid hex sha {line[1:]!r}")
  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 for (ref, sha) in refs.items() if not ref.endswith(PEELED_TAG_SUFFIX)
  1014. }
  1015. def _set_origin_head(refs, origin, origin_head):
  1016. # set refs/remotes/origin/HEAD
  1017. origin_base = b"refs/remotes/" + origin + b"/"
  1018. if origin_head and origin_head.startswith(LOCAL_BRANCH_PREFIX):
  1019. origin_ref = origin_base + HEADREF
  1020. target_ref = origin_base + origin_head[len(LOCAL_BRANCH_PREFIX) :]
  1021. if target_ref in refs:
  1022. refs.set_symbolic_ref(origin_ref, target_ref)
  1023. def _set_default_branch(
  1024. refs: RefsContainer,
  1025. origin: bytes,
  1026. origin_head: Optional[bytes],
  1027. branch: bytes,
  1028. ref_message: Optional[bytes],
  1029. ) -> bytes:
  1030. """Set the default branch."""
  1031. origin_base = b"refs/remotes/" + origin + b"/"
  1032. if branch:
  1033. origin_ref = origin_base + branch
  1034. if origin_ref in refs:
  1035. local_ref = LOCAL_BRANCH_PREFIX + branch
  1036. refs.add_if_new(local_ref, refs[origin_ref], ref_message)
  1037. head_ref = local_ref
  1038. elif LOCAL_TAG_PREFIX + branch in refs:
  1039. head_ref = LOCAL_TAG_PREFIX + branch
  1040. else:
  1041. raise ValueError(f"{os.fsencode(branch)!r} is not a valid branch or tag")
  1042. elif origin_head:
  1043. head_ref = origin_head
  1044. if origin_head.startswith(LOCAL_BRANCH_PREFIX):
  1045. origin_ref = origin_base + origin_head[len(LOCAL_BRANCH_PREFIX) :]
  1046. else:
  1047. origin_ref = origin_head
  1048. try:
  1049. refs.add_if_new(head_ref, refs[origin_ref], ref_message)
  1050. except KeyError:
  1051. pass
  1052. else:
  1053. raise ValueError("neither origin_head nor branch are provided")
  1054. return head_ref
  1055. def _set_head(refs, head_ref, ref_message):
  1056. if head_ref.startswith(LOCAL_TAG_PREFIX):
  1057. # detach HEAD at specified tag
  1058. head = refs[head_ref]
  1059. if isinstance(head, Tag):
  1060. _cls, obj = head.object
  1061. head = obj.get_object(obj).id
  1062. del refs[HEADREF]
  1063. refs.set_if_equals(HEADREF, None, head, message=ref_message)
  1064. else:
  1065. # set HEAD to specific branch
  1066. try:
  1067. head = refs[head_ref]
  1068. refs.set_symbolic_ref(HEADREF, head_ref)
  1069. refs.set_if_equals(HEADREF, None, head, message=ref_message)
  1070. except KeyError:
  1071. head = None
  1072. return head
  1073. def _import_remote_refs(
  1074. refs_container: RefsContainer,
  1075. remote_name: str,
  1076. refs: Dict[str, str],
  1077. message: Optional[bytes] = None,
  1078. prune: bool = False,
  1079. prune_tags: bool = False,
  1080. ):
  1081. stripped_refs = strip_peeled_refs(refs)
  1082. branches = {
  1083. n[len(LOCAL_BRANCH_PREFIX) :]: v
  1084. for (n, v) in stripped_refs.items()
  1085. if n.startswith(LOCAL_BRANCH_PREFIX)
  1086. }
  1087. refs_container.import_refs(
  1088. b"refs/remotes/" + remote_name.encode(),
  1089. branches,
  1090. message=message,
  1091. prune=prune,
  1092. )
  1093. tags = {
  1094. n[len(LOCAL_TAG_PREFIX) :]: v
  1095. for (n, v) in stripped_refs.items()
  1096. if n.startswith(LOCAL_TAG_PREFIX) and not n.endswith(PEELED_TAG_SUFFIX)
  1097. }
  1098. refs_container.import_refs(
  1099. LOCAL_TAG_PREFIX, tags, message=message, prune=prune_tags
  1100. )
  1101. def serialize_refs(store, refs):
  1102. # TODO: Avoid recursive import :(
  1103. from .object_store import peel_sha
  1104. ret = {}
  1105. for ref, sha in refs.items():
  1106. try:
  1107. unpeeled, peeled = peel_sha(store, sha)
  1108. except KeyError:
  1109. warnings.warn(
  1110. "ref {} points at non-present sha {}".format(
  1111. ref.decode("utf-8", "replace"), sha.decode("ascii")
  1112. ),
  1113. UserWarning,
  1114. )
  1115. continue
  1116. else:
  1117. if isinstance(unpeeled, Tag):
  1118. ret[ref + PEELED_TAG_SUFFIX] = peeled.id
  1119. ret[ref] = unpeeled.id
  1120. return ret