refs.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. # refs.py -- For dealing with git refs
  2. # Copyright (C) 2008-2013 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License or (at your option) any later version of
  8. # the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. # MA 02110-1301, USA.
  19. """Ref handling.
  20. """
  21. import errno
  22. import os
  23. import sys
  24. from dulwich.errors import (
  25. PackedRefsException,
  26. RefFormatError,
  27. )
  28. from dulwich.objects import (
  29. git_line,
  30. valid_hexsha,
  31. )
  32. from dulwich.file import (
  33. GitFile,
  34. ensure_dir_exists,
  35. )
  36. SYMREF = b'ref: '
  37. LOCAL_BRANCH_PREFIX = b'refs/heads/'
  38. BAD_REF_CHARS = set(b'\177 ~^:?*[')
  39. def check_ref_format(refname):
  40. """Check if a refname is correctly formatted.
  41. Implements all the same rules as git-check-ref-format[1].
  42. [1] http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  43. :param refname: The refname to check
  44. :return: True if refname is valid, False otherwise
  45. """
  46. # These could be combined into one big expression, but are listed separately
  47. # to parallel [1].
  48. if b'/.' in refname or refname.startswith(b'.'):
  49. return False
  50. if b'/' not in refname:
  51. return False
  52. if b'..' in refname:
  53. return False
  54. for i, c in enumerate(refname):
  55. if ord(refname[i:i+1]) < 0o40 or c in BAD_REF_CHARS:
  56. return False
  57. if refname[-1] in b'/.':
  58. return False
  59. if refname.endswith(b'.lock'):
  60. return False
  61. if b'@{' in refname:
  62. return False
  63. if b'\\' in refname:
  64. return False
  65. return True
  66. class RefsContainer(object):
  67. """A container for refs."""
  68. def set_symbolic_ref(self, name, other):
  69. """Make a ref point at another ref.
  70. :param name: Name of the ref to set
  71. :param other: Name of the ref to point at
  72. """
  73. raise NotImplementedError(self.set_symbolic_ref)
  74. def get_packed_refs(self):
  75. """Get contents of the packed-refs file.
  76. :return: Dictionary mapping ref names to SHA1s
  77. :note: Will return an empty dictionary when no packed-refs file is
  78. present.
  79. """
  80. raise NotImplementedError(self.get_packed_refs)
  81. def get_peeled(self, name):
  82. """Return the cached peeled value of a ref, if available.
  83. :param name: Name of the ref to peel
  84. :return: The peeled value of the ref. If the ref is known not point to a
  85. tag, this will be the SHA the ref refers to. If the ref may point to
  86. a tag, but no cached information is available, None is returned.
  87. """
  88. return None
  89. def import_refs(self, base, other):
  90. for name, value in other.items():
  91. self[b'/'.join((base, name))] = value
  92. def allkeys(self):
  93. """All refs present in this container."""
  94. raise NotImplementedError(self.allkeys)
  95. def keys(self, base=None):
  96. """Refs present in this container.
  97. :param base: An optional base to return refs under.
  98. :return: An unsorted set of valid refs in this container, including
  99. packed refs.
  100. """
  101. if base is not None:
  102. return self.subkeys(base)
  103. else:
  104. return self.allkeys()
  105. def subkeys(self, base):
  106. """Refs present in this container under a base.
  107. :param base: The base to return refs under.
  108. :return: A set of valid refs in this container under the base; the base
  109. prefix is stripped from the ref names returned.
  110. """
  111. keys = set()
  112. base_len = len(base) + 1
  113. for refname in self.allkeys():
  114. if refname.startswith(base):
  115. keys.add(refname[base_len:])
  116. return keys
  117. def as_dict(self, base=None):
  118. """Return the contents of this container as a dictionary.
  119. """
  120. ret = {}
  121. keys = self.keys(base)
  122. if base is None:
  123. base = b''
  124. for key in keys:
  125. try:
  126. ret[key] = self[(base + b'/' + key).strip(b'/')]
  127. except KeyError:
  128. continue # Unable to resolve
  129. return ret
  130. def _check_refname(self, name):
  131. """Ensure a refname is valid and lives in refs or is HEAD.
  132. HEAD is not a valid refname according to git-check-ref-format, but this
  133. class needs to be able to touch HEAD. Also, check_ref_format expects
  134. refnames without the leading 'refs/', but this class requires that
  135. so it cannot touch anything outside the refs dir (or HEAD).
  136. :param name: The name of the reference.
  137. :raises KeyError: if a refname is not HEAD or is otherwise not valid.
  138. """
  139. if name in (b'HEAD', b'refs/stash'):
  140. return
  141. if not name.startswith(b'refs/') or not check_ref_format(name[5:]):
  142. raise RefFormatError(name)
  143. def read_ref(self, refname):
  144. """Read a reference without following any references.
  145. :param refname: The name of the reference
  146. :return: The contents of the ref file, or None if it does
  147. not exist.
  148. """
  149. contents = self.read_loose_ref(refname)
  150. if not contents:
  151. contents = self.get_packed_refs().get(refname, None)
  152. return contents
  153. def read_loose_ref(self, name):
  154. """Read a loose reference and return its contents.
  155. :param name: the refname to read
  156. :return: The contents of the ref file, or None if it does
  157. not exist.
  158. """
  159. raise NotImplementedError(self.read_loose_ref)
  160. def _follow(self, name):
  161. """Follow a reference name.
  162. :return: a tuple of (refname, sha), where refname is the name of the
  163. last reference in the symbolic reference chain
  164. """
  165. contents = SYMREF + name
  166. depth = 0
  167. while contents.startswith(SYMREF):
  168. refname = contents[len(SYMREF):]
  169. contents = self.read_ref(refname)
  170. if not contents:
  171. break
  172. depth += 1
  173. if depth > 5:
  174. raise KeyError(name)
  175. return refname, contents
  176. def __contains__(self, refname):
  177. if self.read_ref(refname):
  178. return True
  179. return False
  180. def __getitem__(self, name):
  181. """Get the SHA1 for a reference name.
  182. This method follows all symbolic references.
  183. """
  184. _, sha = self._follow(name)
  185. if sha is None:
  186. raise KeyError(name)
  187. return sha
  188. def set_if_equals(self, name, old_ref, new_ref):
  189. """Set a refname to new_ref only if it currently equals old_ref.
  190. This method follows all symbolic references if applicable for the
  191. subclass, and can be used to perform an atomic compare-and-swap
  192. operation.
  193. :param name: The refname to set.
  194. :param old_ref: The old sha the refname must refer to, or None to set
  195. unconditionally.
  196. :param new_ref: The new sha the refname will refer to.
  197. :return: True if the set was successful, False otherwise.
  198. """
  199. raise NotImplementedError(self.set_if_equals)
  200. def add_if_new(self, name, ref):
  201. """Add a new reference only if it does not already exist."""
  202. raise NotImplementedError(self.add_if_new)
  203. def __setitem__(self, name, ref):
  204. """Set a reference name to point to the given SHA1.
  205. This method follows all symbolic references if applicable for the
  206. subclass.
  207. :note: This method unconditionally overwrites the contents of a
  208. reference. To update atomically only if the reference has not
  209. changed, use set_if_equals().
  210. :param name: The refname to set.
  211. :param ref: The new sha the refname will refer to.
  212. """
  213. self.set_if_equals(name, None, ref)
  214. def remove_if_equals(self, name, old_ref):
  215. """Remove a refname only if it currently equals old_ref.
  216. This method does not follow symbolic references, even if applicable for
  217. the subclass. It can be used to perform an atomic compare-and-delete
  218. operation.
  219. :param name: The refname to delete.
  220. :param old_ref: The old sha the refname must refer to, or None to delete
  221. unconditionally.
  222. :return: True if the delete was successful, False otherwise.
  223. """
  224. raise NotImplementedError(self.remove_if_equals)
  225. def __delitem__(self, name):
  226. """Remove a refname.
  227. This method does not follow symbolic references, even if applicable for
  228. the subclass.
  229. :note: This method unconditionally deletes the contents of a reference.
  230. To delete atomically only if the reference has not changed, use
  231. remove_if_equals().
  232. :param name: The refname to delete.
  233. """
  234. self.remove_if_equals(name, None)
  235. class DictRefsContainer(RefsContainer):
  236. """RefsContainer backed by a simple dict.
  237. This container does not support symbolic or packed references and is not
  238. threadsafe.
  239. """
  240. def __init__(self, refs):
  241. self._refs = refs
  242. self._peeled = {}
  243. def allkeys(self):
  244. return self._refs.keys()
  245. def read_loose_ref(self, name):
  246. return self._refs.get(name, None)
  247. def get_packed_refs(self):
  248. return {}
  249. def set_symbolic_ref(self, name, other):
  250. self._refs[name] = SYMREF + other
  251. def set_if_equals(self, name, old_ref, new_ref):
  252. if old_ref is not None and self._refs.get(name, None) != old_ref:
  253. return False
  254. realname, _ = self._follow(name)
  255. self._check_refname(realname)
  256. self._refs[realname] = new_ref
  257. return True
  258. def add_if_new(self, name, ref):
  259. if name in self._refs:
  260. return False
  261. self._refs[name] = ref
  262. return True
  263. def remove_if_equals(self, name, old_ref):
  264. if old_ref is not None and self._refs.get(name, None) != old_ref:
  265. return False
  266. del self._refs[name]
  267. return True
  268. def get_peeled(self, name):
  269. return self._peeled.get(name)
  270. def _update(self, refs):
  271. """Update multiple refs; intended only for testing."""
  272. # TODO(dborowitz): replace this with a public function that uses
  273. # set_if_equal.
  274. self._refs.update(refs)
  275. def _update_peeled(self, peeled):
  276. """Update cached peeled refs; intended only for testing."""
  277. self._peeled.update(peeled)
  278. class InfoRefsContainer(RefsContainer):
  279. """Refs container that reads refs from a info/refs file."""
  280. def __init__(self, f):
  281. self._refs = {}
  282. self._peeled = {}
  283. for l in f.readlines():
  284. sha, name = l.rstrip(b'\n').split(b'\t')
  285. if name.endswith(b'^{}'):
  286. name = name[:-3]
  287. if not check_ref_format(name):
  288. raise ValueError("invalid ref name %r" % name)
  289. self._peeled[name] = sha
  290. else:
  291. if not check_ref_format(name):
  292. raise ValueError("invalid ref name %r" % name)
  293. self._refs[name] = sha
  294. def allkeys(self):
  295. return self._refs.keys()
  296. def read_loose_ref(self, name):
  297. return self._refs.get(name, None)
  298. def get_packed_refs(self):
  299. return {}
  300. def get_peeled(self, name):
  301. try:
  302. return self._peeled[name]
  303. except KeyError:
  304. return self._refs[name]
  305. class DiskRefsContainer(RefsContainer):
  306. """Refs container that reads refs from disk."""
  307. def __init__(self, path):
  308. self.path = path
  309. self._packed_refs = None
  310. self._peeled_refs = None
  311. def __repr__(self):
  312. return "%s(%r)" % (self.__class__.__name__, self.path)
  313. def subkeys(self, base):
  314. subkeys = set()
  315. path = self.refpath(base)
  316. for root, dirs, files in os.walk(path):
  317. dir = root[len(path):].strip(os.path.sep).replace(os.path.sep, "/")
  318. for filename in files:
  319. refname = (("%s/%s" % (dir, filename))
  320. .strip("/").encode(sys.getfilesystemencoding()))
  321. # check_ref_format requires at least one /, so we prepend the
  322. # base before calling it.
  323. if check_ref_format(base + b'/' + refname):
  324. subkeys.add(refname)
  325. for key in self.get_packed_refs():
  326. if key.startswith(base):
  327. subkeys.add(key[len(base):].strip(b'/'))
  328. return subkeys
  329. def allkeys(self):
  330. allkeys = set()
  331. if os.path.exists(self.refpath(b'HEAD')):
  332. allkeys.add(b'HEAD')
  333. path = self.refpath(b'')
  334. for root, dirs, files in os.walk(self.refpath(b'refs')):
  335. dir = root[len(path):].strip(os.path.sep).replace(os.path.sep, "/")
  336. for filename in files:
  337. refname = ("%s/%s" % (dir, filename)).encode(sys.getfilesystemencoding())
  338. if check_ref_format(refname):
  339. allkeys.add(refname)
  340. allkeys.update(self.get_packed_refs())
  341. return allkeys
  342. def refpath(self, name):
  343. """Return the disk path of a ref.
  344. """
  345. if getattr(self.path, "encode", None) and getattr(name, "decode", None):
  346. name = name.decode(sys.getfilesystemencoding())
  347. if os.path.sep != "/":
  348. name = name.replace("/", os.path.sep)
  349. return os.path.join(self.path, name)
  350. def get_packed_refs(self):
  351. """Get contents of the packed-refs file.
  352. :return: Dictionary mapping ref names to SHA1s
  353. :note: Will return an empty dictionary when no packed-refs file is
  354. present.
  355. """
  356. # TODO: invalidate the cache on repacking
  357. if self._packed_refs is None:
  358. # set both to empty because we want _peeled_refs to be
  359. # None if and only if _packed_refs is also None.
  360. self._packed_refs = {}
  361. self._peeled_refs = {}
  362. path = os.path.join(self.path, 'packed-refs')
  363. try:
  364. f = GitFile(path, 'rb')
  365. except IOError as e:
  366. if e.errno == errno.ENOENT:
  367. return {}
  368. raise
  369. with f:
  370. first_line = next(iter(f)).rstrip()
  371. if (first_line.startswith(b'# pack-refs') and b' peeled' in
  372. first_line):
  373. for sha, name, peeled in read_packed_refs_with_peeled(f):
  374. self._packed_refs[name] = sha
  375. if peeled:
  376. self._peeled_refs[name] = peeled
  377. else:
  378. f.seek(0)
  379. for sha, name in read_packed_refs(f):
  380. self._packed_refs[name] = sha
  381. return self._packed_refs
  382. def get_peeled(self, name):
  383. """Return the cached peeled value of a ref, if available.
  384. :param name: Name of the ref to peel
  385. :return: The peeled value of the ref. If the ref is known not point to a
  386. tag, this will be the SHA the ref refers to. If the ref may point to
  387. a tag, but no cached information is available, None is returned.
  388. """
  389. self.get_packed_refs()
  390. if self._peeled_refs is None or name not in self._packed_refs:
  391. # No cache: no peeled refs were read, or this ref is loose
  392. return None
  393. if name in self._peeled_refs:
  394. return self._peeled_refs[name]
  395. else:
  396. # Known not peelable
  397. return self[name]
  398. def read_loose_ref(self, name):
  399. """Read a reference file and return its contents.
  400. If the reference file a symbolic reference, only read the first line of
  401. the file. Otherwise, only read the first 40 bytes.
  402. :param name: the refname to read, relative to refpath
  403. :return: The contents of the ref file, or None if the file does not
  404. exist.
  405. :raises IOError: if any other error occurs
  406. """
  407. filename = self.refpath(name)
  408. try:
  409. with GitFile(filename, 'rb') as f:
  410. header = f.read(len(SYMREF))
  411. if header == SYMREF:
  412. # Read only the first line
  413. return header + next(iter(f)).rstrip(b'\r\n')
  414. else:
  415. # Read only the first 40 bytes
  416. return header + f.read(40 - len(SYMREF))
  417. except IOError as e:
  418. if e.errno == errno.ENOENT:
  419. return None
  420. raise
  421. def _remove_packed_ref(self, name):
  422. if self._packed_refs is None:
  423. return
  424. filename = os.path.join(self.path, 'packed-refs')
  425. # reread cached refs from disk, while holding the lock
  426. f = GitFile(filename, 'wb')
  427. try:
  428. self._packed_refs = None
  429. self.get_packed_refs()
  430. if name not in self._packed_refs:
  431. return
  432. del self._packed_refs[name]
  433. if name in self._peeled_refs:
  434. del self._peeled_refs[name]
  435. write_packed_refs(f, self._packed_refs, self._peeled_refs)
  436. f.close()
  437. finally:
  438. f.abort()
  439. def set_symbolic_ref(self, name, other):
  440. """Make a ref point at another ref.
  441. :param name: Name of the ref to set
  442. :param other: Name of the ref to point at
  443. """
  444. self._check_refname(name)
  445. self._check_refname(other)
  446. filename = self.refpath(name)
  447. try:
  448. f = GitFile(filename, 'wb')
  449. try:
  450. f.write(SYMREF + other + b'\n')
  451. except (IOError, OSError):
  452. f.abort()
  453. raise
  454. finally:
  455. f.close()
  456. def set_if_equals(self, name, old_ref, new_ref):
  457. """Set a refname to new_ref only if it currently equals old_ref.
  458. This method follows all symbolic references, and can be used to perform
  459. an atomic compare-and-swap operation.
  460. :param name: The refname to set.
  461. :param old_ref: The old sha the refname must refer to, or None to set
  462. unconditionally.
  463. :param new_ref: The new sha the refname will refer to.
  464. :return: True if the set was successful, False otherwise.
  465. """
  466. self._check_refname(name)
  467. try:
  468. realname, _ = self._follow(name)
  469. except KeyError:
  470. realname = name
  471. filename = self.refpath(realname)
  472. ensure_dir_exists(os.path.dirname(filename))
  473. with GitFile(filename, 'wb') as f:
  474. if old_ref is not None:
  475. try:
  476. # read again while holding the lock
  477. orig_ref = self.read_loose_ref(realname)
  478. if orig_ref is None:
  479. orig_ref = self.get_packed_refs().get(realname, None)
  480. if orig_ref != old_ref:
  481. f.abort()
  482. return False
  483. except (OSError, IOError):
  484. f.abort()
  485. raise
  486. try:
  487. f.write(new_ref + b'\n')
  488. except (OSError, IOError):
  489. f.abort()
  490. raise
  491. return True
  492. def add_if_new(self, name, ref):
  493. """Add a new reference only if it does not already exist.
  494. This method follows symrefs, and only ensures that the last ref in the
  495. chain does not exist.
  496. :param name: The refname to set.
  497. :param ref: The new sha the refname will refer to.
  498. :return: True if the add was successful, False otherwise.
  499. """
  500. try:
  501. realname, contents = self._follow(name)
  502. if contents is not None:
  503. return False
  504. except KeyError:
  505. realname = name
  506. self._check_refname(realname)
  507. filename = self.refpath(realname)
  508. ensure_dir_exists(os.path.dirname(filename))
  509. with GitFile(filename, 'wb') as f:
  510. if os.path.exists(filename) or name in self.get_packed_refs():
  511. f.abort()
  512. return False
  513. try:
  514. f.write(ref + b'\n')
  515. except (OSError, IOError):
  516. f.abort()
  517. raise
  518. return True
  519. def remove_if_equals(self, name, old_ref):
  520. """Remove a refname only if it currently equals old_ref.
  521. This method does not follow symbolic references. It can be used to
  522. perform an atomic compare-and-delete operation.
  523. :param name: The refname to delete.
  524. :param old_ref: The old sha the refname must refer to, or None to delete
  525. unconditionally.
  526. :return: True if the delete was successful, False otherwise.
  527. """
  528. self._check_refname(name)
  529. filename = self.refpath(name)
  530. ensure_dir_exists(os.path.dirname(filename))
  531. f = GitFile(filename, 'wb')
  532. try:
  533. if old_ref is not None:
  534. orig_ref = self.read_loose_ref(name)
  535. if orig_ref is None:
  536. orig_ref = self.get_packed_refs().get(name, None)
  537. if orig_ref != old_ref:
  538. return False
  539. # may only be packed
  540. try:
  541. os.remove(filename)
  542. except OSError as e:
  543. if e.errno != errno.ENOENT:
  544. raise
  545. self._remove_packed_ref(name)
  546. finally:
  547. # never write, we just wanted the lock
  548. f.abort()
  549. return True
  550. def _split_ref_line(line):
  551. """Split a single ref line into a tuple of SHA1 and name."""
  552. fields = line.rstrip(b'\n').split(b' ')
  553. if len(fields) != 2:
  554. raise PackedRefsException("invalid ref line %r" % line)
  555. sha, name = fields
  556. if not valid_hexsha(sha):
  557. raise PackedRefsException("Invalid hex sha %r" % sha)
  558. if not check_ref_format(name):
  559. raise PackedRefsException("invalid ref name %r" % name)
  560. return (sha, name)
  561. def read_packed_refs(f):
  562. """Read a packed refs file.
  563. :param f: file-like object to read from
  564. :return: Iterator over tuples with SHA1s and ref names.
  565. """
  566. for l in f:
  567. if l.startswith(b'#'):
  568. # Comment
  569. continue
  570. if l.startswith(b'^'):
  571. raise PackedRefsException(
  572. "found peeled ref in packed-refs without peeled")
  573. yield _split_ref_line(l)
  574. def read_packed_refs_with_peeled(f):
  575. """Read a packed refs file including peeled refs.
  576. Assumes the "# pack-refs with: peeled" line was already read. Yields tuples
  577. with ref names, SHA1s, and peeled SHA1s (or None).
  578. :param f: file-like object to read from, seek'ed to the second line
  579. """
  580. last = None
  581. for l in f:
  582. if l[0] == b'#':
  583. continue
  584. l = l.rstrip(b'\r\n')
  585. if l.startswith(b'^'):
  586. if not last:
  587. raise PackedRefsException("unexpected peeled ref line")
  588. if not valid_hexsha(l[1:]):
  589. raise PackedRefsException("Invalid hex sha %r" % l[1:])
  590. sha, name = _split_ref_line(last)
  591. last = None
  592. yield (sha, name, l[1:])
  593. else:
  594. if last:
  595. sha, name = _split_ref_line(last)
  596. yield (sha, name, None)
  597. last = l
  598. if last:
  599. sha, name = _split_ref_line(last)
  600. yield (sha, name, None)
  601. def write_packed_refs(f, packed_refs, peeled_refs=None):
  602. """Write a packed refs file.
  603. :param f: empty file-like object to write to
  604. :param packed_refs: dict of refname to sha of packed refs to write
  605. :param peeled_refs: dict of refname to peeled value of sha
  606. """
  607. if peeled_refs is None:
  608. peeled_refs = {}
  609. else:
  610. f.write(b'# pack-refs with: peeled\n')
  611. for refname in sorted(packed_refs.keys()):
  612. f.write(git_line(packed_refs[refname], refname))
  613. if refname in peeled_refs:
  614. f.write(b'^' + peeled_refs[refname] + b'\n')
  615. def read_info_refs(f):
  616. ret = {}
  617. for l in f.readlines():
  618. (sha, name) = l.rstrip("\r\n").split("\t", 1)
  619. ret[name] = sha
  620. return ret
  621. def write_info_refs(refs, store):
  622. """Generate info refs."""
  623. for name, sha in sorted(refs.items()):
  624. # get_refs() includes HEAD as a special case, but we don't want to
  625. # advertise it
  626. if name == b'HEAD':
  627. continue
  628. try:
  629. o = store[sha]
  630. except KeyError:
  631. continue
  632. peeled = store.peel_sha(sha)
  633. yield o.id + b'\t' + name + b'\n'
  634. if o.id != peeled.id:
  635. yield peeled.id + b'\t' + name + b'^{}\n'
  636. is_local_branch = lambda x: x.startswith(b'refs/heads/')