repo.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. # repo.py -- For dealing with git repositories.
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; version 2
  8. # of the License or (at your option) any later version of
  9. # the License.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. # MA 02110-1301, USA.
  20. """Repository access."""
  21. from cStringIO import StringIO
  22. import errno
  23. import os
  24. from dulwich.errors import (
  25. MissingCommitError,
  26. NoIndexPresent,
  27. NotBlobError,
  28. NotCommitError,
  29. NotGitRepository,
  30. NotTreeError,
  31. NotTagError,
  32. PackedRefsException,
  33. CommitError,
  34. RefFormatError,
  35. )
  36. from dulwich.file import (
  37. ensure_dir_exists,
  38. GitFile,
  39. )
  40. from dulwich.object_store import (
  41. DiskObjectStore,
  42. MemoryObjectStore,
  43. )
  44. from dulwich.objects import (
  45. Blob,
  46. Commit,
  47. ShaFile,
  48. Tag,
  49. Tree,
  50. hex_to_sha,
  51. )
  52. import warnings
  53. OBJECTDIR = 'objects'
  54. SYMREF = 'ref: '
  55. REFSDIR = 'refs'
  56. REFSDIR_TAGS = 'tags'
  57. REFSDIR_HEADS = 'heads'
  58. INDEX_FILENAME = "index"
  59. BASE_DIRECTORIES = [
  60. ["branches"],
  61. [REFSDIR],
  62. [REFSDIR, REFSDIR_TAGS],
  63. [REFSDIR, REFSDIR_HEADS],
  64. ["hooks"],
  65. ["info"]
  66. ]
  67. def read_info_refs(f):
  68. ret = {}
  69. for l in f.readlines():
  70. (sha, name) = l.rstrip("\r\n").split("\t", 1)
  71. ret[name] = sha
  72. return ret
  73. def check_ref_format(refname):
  74. """Check if a refname is correctly formatted.
  75. Implements all the same rules as git-check-ref-format[1].
  76. [1] http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  77. :param refname: The refname to check
  78. :return: True if refname is valid, False otherwise
  79. """
  80. # These could be combined into one big expression, but are listed separately
  81. # to parallel [1].
  82. if '/.' in refname or refname.startswith('.'):
  83. return False
  84. if '/' not in refname:
  85. return False
  86. if '..' in refname:
  87. return False
  88. for c in refname:
  89. if ord(c) < 040 or c in '\177 ~^:?*[':
  90. return False
  91. if refname[-1] in '/.':
  92. return False
  93. if refname.endswith('.lock'):
  94. return False
  95. if '@{' in refname:
  96. return False
  97. if '\\' in refname:
  98. return False
  99. return True
  100. class RefsContainer(object):
  101. """A container for refs."""
  102. def set_ref(self, name, other):
  103. warnings.warn("RefsContainer.set_ref() is deprecated."
  104. "Use set_symblic_ref instead.",
  105. category=DeprecationWarning, stacklevel=2)
  106. return self.set_symbolic_ref(name, other)
  107. def set_symbolic_ref(self, name, other):
  108. """Make a ref point at another ref.
  109. :param name: Name of the ref to set
  110. :param other: Name of the ref to point at
  111. """
  112. raise NotImplementedError(self.set_symbolic_ref)
  113. def get_packed_refs(self):
  114. """Get contents of the packed-refs file.
  115. :return: Dictionary mapping ref names to SHA1s
  116. :note: Will return an empty dictionary when no packed-refs file is
  117. present.
  118. """
  119. raise NotImplementedError(self.get_packed_refs)
  120. def get_peeled(self, name):
  121. """Return the cached peeled value of a ref, if available.
  122. :param name: Name of the ref to peel
  123. :return: The peeled value of the ref. If the ref is known not point to a
  124. tag, this will be the SHA the ref refers to. If the ref may point to
  125. a tag, but no cached information is available, None is returned.
  126. """
  127. return None
  128. def import_refs(self, base, other):
  129. for name, value in other.iteritems():
  130. self["%s/%s" % (base, name)] = value
  131. def allkeys(self):
  132. """All refs present in this container."""
  133. raise NotImplementedError(self.allkeys)
  134. def keys(self, base=None):
  135. """Refs present in this container.
  136. :param base: An optional base to return refs under.
  137. :return: An unsorted set of valid refs in this container, including
  138. packed refs.
  139. """
  140. if base is not None:
  141. return self.subkeys(base)
  142. else:
  143. return self.allkeys()
  144. def subkeys(self, base):
  145. """Refs present in this container under a base.
  146. :param base: The base to return refs under.
  147. :return: A set of valid refs in this container under the base; the base
  148. prefix is stripped from the ref names returned.
  149. """
  150. keys = set()
  151. base_len = len(base) + 1
  152. for refname in self.allkeys():
  153. if refname.startswith(base):
  154. keys.add(refname[base_len:])
  155. return keys
  156. def as_dict(self, base=None):
  157. """Return the contents of this container as a dictionary.
  158. """
  159. ret = {}
  160. keys = self.keys(base)
  161. if base is None:
  162. base = ""
  163. for key in keys:
  164. try:
  165. ret[key] = self[("%s/%s" % (base, key)).strip("/")]
  166. except KeyError:
  167. continue # Unable to resolve
  168. return ret
  169. def _check_refname(self, name):
  170. """Ensure a refname is valid and lives in refs or is HEAD.
  171. HEAD is not a valid refname according to git-check-ref-format, but this
  172. class needs to be able to touch HEAD. Also, check_ref_format expects
  173. refnames without the leading 'refs/', but this class requires that
  174. so it cannot touch anything outside the refs dir (or HEAD).
  175. :param name: The name of the reference.
  176. :raises KeyError: if a refname is not HEAD or is otherwise not valid.
  177. """
  178. if name == 'HEAD':
  179. return
  180. if not name.startswith('refs/') or not check_ref_format(name[5:]):
  181. raise RefFormatError(name)
  182. def read_ref(self, refname):
  183. """Read a reference without following any references.
  184. :param refname: The name of the reference
  185. :return: The contents of the ref file, or None if it does
  186. not exist.
  187. """
  188. contents = self.read_loose_ref(refname)
  189. if not contents:
  190. contents = self.get_packed_refs().get(refname, None)
  191. return contents
  192. def read_loose_ref(self, name):
  193. """Read a loose reference and return its contents.
  194. :param name: the refname to read
  195. :return: The contents of the ref file, or None if it does
  196. not exist.
  197. """
  198. raise NotImplementedError(self.read_loose_ref)
  199. def _follow(self, name):
  200. """Follow a reference name.
  201. :return: a tuple of (refname, sha), where refname is the name of the
  202. last reference in the symbolic reference chain
  203. """
  204. contents = SYMREF + name
  205. depth = 0
  206. while contents.startswith(SYMREF):
  207. refname = contents[len(SYMREF):]
  208. contents = self.read_ref(refname)
  209. if not contents:
  210. break
  211. depth += 1
  212. if depth > 5:
  213. raise KeyError(name)
  214. return refname, contents
  215. def __contains__(self, refname):
  216. if self.read_ref(refname):
  217. return True
  218. return False
  219. def __getitem__(self, name):
  220. """Get the SHA1 for a reference name.
  221. This method follows all symbolic references.
  222. """
  223. _, sha = self._follow(name)
  224. if sha is None:
  225. raise KeyError(name)
  226. return sha
  227. def set_if_equals(self, name, old_ref, new_ref):
  228. """Set a refname to new_ref only if it currently equals old_ref.
  229. This method follows all symbolic references if applicable for the
  230. subclass, and can be used to perform an atomic compare-and-swap
  231. operation.
  232. :param name: The refname to set.
  233. :param old_ref: The old sha the refname must refer to, or None to set
  234. unconditionally.
  235. :param new_ref: The new sha the refname will refer to.
  236. :return: True if the set was successful, False otherwise.
  237. """
  238. raise NotImplementedError(self.set_if_equals)
  239. def add_if_new(self, name, ref):
  240. """Add a new reference only if it does not already exist."""
  241. raise NotImplementedError(self.add_if_new)
  242. def __setitem__(self, name, ref):
  243. """Set a reference name to point to the given SHA1.
  244. This method follows all symbolic references if applicable for the
  245. subclass.
  246. :note: This method unconditionally overwrites the contents of a
  247. reference. To update atomically only if the reference has not
  248. changed, use set_if_equals().
  249. :param name: The refname to set.
  250. :param ref: The new sha the refname will refer to.
  251. """
  252. self.set_if_equals(name, None, ref)
  253. def remove_if_equals(self, name, old_ref):
  254. """Remove a refname only if it currently equals old_ref.
  255. This method does not follow symbolic references, even if applicable for
  256. the subclass. It can be used to perform an atomic compare-and-delete
  257. operation.
  258. :param name: The refname to delete.
  259. :param old_ref: The old sha the refname must refer to, or None to delete
  260. unconditionally.
  261. :return: True if the delete was successful, False otherwise.
  262. """
  263. raise NotImplementedError(self.remove_if_equals)
  264. def __delitem__(self, name):
  265. """Remove a refname.
  266. This method does not follow symbolic references, even if applicable for
  267. the subclass.
  268. :note: This method unconditionally deletes the contents of a reference.
  269. To delete atomically only if the reference has not changed, use
  270. remove_if_equals().
  271. :param name: The refname to delete.
  272. """
  273. self.remove_if_equals(name, None)
  274. class DictRefsContainer(RefsContainer):
  275. """RefsContainer backed by a simple dict.
  276. This container does not support symbolic or packed references and is not
  277. threadsafe.
  278. """
  279. def __init__(self, refs):
  280. self._refs = refs
  281. self._peeled = {}
  282. def allkeys(self):
  283. return self._refs.keys()
  284. def read_loose_ref(self, name):
  285. return self._refs.get(name, None)
  286. def get_packed_refs(self):
  287. return {}
  288. def set_symbolic_ref(self, name, other):
  289. self._refs[name] = SYMREF + other
  290. def set_if_equals(self, name, old_ref, new_ref):
  291. if old_ref is not None and self._refs.get(name, None) != old_ref:
  292. return False
  293. realname, _ = self._follow(name)
  294. self._check_refname(realname)
  295. self._refs[realname] = new_ref
  296. return True
  297. def add_if_new(self, name, ref):
  298. if name in self._refs:
  299. return False
  300. self._refs[name] = ref
  301. return True
  302. def remove_if_equals(self, name, old_ref):
  303. if old_ref is not None and self._refs.get(name, None) != old_ref:
  304. return False
  305. del self._refs[name]
  306. return True
  307. def get_peeled(self, name):
  308. return self._peeled.get(name)
  309. def _update(self, refs):
  310. """Update multiple refs; intended only for testing."""
  311. # TODO(dborowitz): replace this with a public function that uses
  312. # set_if_equal.
  313. self._refs.update(refs)
  314. def _update_peeled(self, peeled):
  315. """Update cached peeled refs; intended only for testing."""
  316. self._peeled.update(peeled)
  317. class DiskRefsContainer(RefsContainer):
  318. """Refs container that reads refs from disk."""
  319. def __init__(self, path):
  320. self.path = path
  321. self._packed_refs = None
  322. self._peeled_refs = None
  323. def __repr__(self):
  324. return "%s(%r)" % (self.__class__.__name__, self.path)
  325. def subkeys(self, base):
  326. keys = set()
  327. path = self.refpath(base)
  328. for root, dirs, files in os.walk(path):
  329. dir = root[len(path):].strip(os.path.sep).replace(os.path.sep, "/")
  330. for filename in files:
  331. refname = ("%s/%s" % (dir, filename)).strip("/")
  332. # check_ref_format requires at least one /, so we prepend the
  333. # base before calling it.
  334. if check_ref_format("%s/%s" % (base, refname)):
  335. keys.add(refname)
  336. for key in self.get_packed_refs():
  337. if key.startswith(base):
  338. keys.add(key[len(base):].strip("/"))
  339. return keys
  340. def allkeys(self):
  341. keys = set()
  342. if os.path.exists(self.refpath("HEAD")):
  343. keys.add("HEAD")
  344. path = self.refpath("")
  345. for root, dirs, files in os.walk(self.refpath("refs")):
  346. dir = root[len(path):].strip(os.path.sep).replace(os.path.sep, "/")
  347. for filename in files:
  348. refname = ("%s/%s" % (dir, filename)).strip("/")
  349. if check_ref_format(refname):
  350. keys.add(refname)
  351. keys.update(self.get_packed_refs())
  352. return keys
  353. def refpath(self, name):
  354. """Return the disk path of a ref.
  355. """
  356. if os.path.sep != "/":
  357. name = name.replace("/", os.path.sep)
  358. return os.path.join(self.path, name)
  359. def get_packed_refs(self):
  360. """Get contents of the packed-refs file.
  361. :return: Dictionary mapping ref names to SHA1s
  362. :note: Will return an empty dictionary when no packed-refs file is
  363. present.
  364. """
  365. # TODO: invalidate the cache on repacking
  366. if self._packed_refs is None:
  367. # set both to empty because we want _peeled_refs to be
  368. # None if and only if _packed_refs is also None.
  369. self._packed_refs = {}
  370. self._peeled_refs = {}
  371. path = os.path.join(self.path, 'packed-refs')
  372. try:
  373. f = GitFile(path, 'rb')
  374. except IOError, e:
  375. if e.errno == errno.ENOENT:
  376. return {}
  377. raise
  378. try:
  379. first_line = iter(f).next().rstrip()
  380. if (first_line.startswith("# pack-refs") and " peeled" in
  381. first_line):
  382. for sha, name, peeled in read_packed_refs_with_peeled(f):
  383. self._packed_refs[name] = sha
  384. if peeled:
  385. self._peeled_refs[name] = peeled
  386. else:
  387. f.seek(0)
  388. for sha, name in read_packed_refs(f):
  389. self._packed_refs[name] = sha
  390. finally:
  391. f.close()
  392. return self._packed_refs
  393. def get_peeled(self, name):
  394. """Return the cached peeled value of a ref, if available.
  395. :param name: Name of the ref to peel
  396. :return: The peeled value of the ref. If the ref is known not point to a
  397. tag, this will be the SHA the ref refers to. If the ref may point to
  398. a tag, but no cached information is available, None is returned.
  399. """
  400. self.get_packed_refs()
  401. if self._peeled_refs is None or name not in self._packed_refs:
  402. # No cache: no peeled refs were read, or this ref is loose
  403. return None
  404. if name in self._peeled_refs:
  405. return self._peeled_refs[name]
  406. else:
  407. # Known not peelable
  408. return self[name]
  409. def read_loose_ref(self, name):
  410. """Read a reference file and return its contents.
  411. If the reference file a symbolic reference, only read the first line of
  412. the file. Otherwise, only read the first 40 bytes.
  413. :param name: the refname to read, relative to refpath
  414. :return: The contents of the ref file, or None if the file does not
  415. exist.
  416. :raises IOError: if any other error occurs
  417. """
  418. filename = self.refpath(name)
  419. try:
  420. f = GitFile(filename, 'rb')
  421. try:
  422. header = f.read(len(SYMREF))
  423. if header == SYMREF:
  424. # Read only the first line
  425. return header + iter(f).next().rstrip("\r\n")
  426. else:
  427. # Read only the first 40 bytes
  428. return header + f.read(40-len(SYMREF))
  429. finally:
  430. f.close()
  431. except IOError, e:
  432. if e.errno == errno.ENOENT:
  433. return None
  434. raise
  435. def _remove_packed_ref(self, name):
  436. if self._packed_refs is None:
  437. return
  438. filename = os.path.join(self.path, 'packed-refs')
  439. # reread cached refs from disk, while holding the lock
  440. f = GitFile(filename, 'wb')
  441. try:
  442. self._packed_refs = None
  443. self.get_packed_refs()
  444. if name not in self._packed_refs:
  445. return
  446. del self._packed_refs[name]
  447. if name in self._peeled_refs:
  448. del self._peeled_refs[name]
  449. write_packed_refs(f, self._packed_refs, self._peeled_refs)
  450. f.close()
  451. finally:
  452. f.abort()
  453. def set_symbolic_ref(self, name, other):
  454. """Make a ref point at another ref.
  455. :param name: Name of the ref to set
  456. :param other: Name of the ref to point at
  457. """
  458. self._check_refname(name)
  459. self._check_refname(other)
  460. filename = self.refpath(name)
  461. try:
  462. f = GitFile(filename, 'wb')
  463. try:
  464. f.write(SYMREF + other + '\n')
  465. except (IOError, OSError):
  466. f.abort()
  467. raise
  468. finally:
  469. f.close()
  470. def set_if_equals(self, name, old_ref, new_ref):
  471. """Set a refname to new_ref only if it currently equals old_ref.
  472. This method follows all symbolic references, and can be used to perform
  473. an atomic compare-and-swap operation.
  474. :param name: The refname to set.
  475. :param old_ref: The old sha the refname must refer to, or None to set
  476. unconditionally.
  477. :param new_ref: The new sha the refname will refer to.
  478. :return: True if the set was successful, False otherwise.
  479. """
  480. self._check_refname(name)
  481. try:
  482. realname, _ = self._follow(name)
  483. except KeyError:
  484. realname = name
  485. filename = self.refpath(realname)
  486. ensure_dir_exists(os.path.dirname(filename))
  487. f = GitFile(filename, 'wb')
  488. try:
  489. if old_ref is not None:
  490. try:
  491. # read again while holding the lock
  492. orig_ref = self.read_loose_ref(realname)
  493. if orig_ref is None:
  494. orig_ref = self.get_packed_refs().get(realname, None)
  495. if orig_ref != old_ref:
  496. f.abort()
  497. return False
  498. except (OSError, IOError):
  499. f.abort()
  500. raise
  501. try:
  502. f.write(new_ref+"\n")
  503. except (OSError, IOError):
  504. f.abort()
  505. raise
  506. finally:
  507. f.close()
  508. return True
  509. def add_if_new(self, name, ref):
  510. """Add a new reference only if it does not already exist.
  511. This method follows symrefs, and only ensures that the last ref in the
  512. chain does not exist.
  513. :param name: The refname to set.
  514. :param ref: The new sha the refname will refer to.
  515. :return: True if the add was successful, False otherwise.
  516. """
  517. try:
  518. realname, contents = self._follow(name)
  519. if contents is not None:
  520. return False
  521. except KeyError:
  522. realname = name
  523. self._check_refname(realname)
  524. filename = self.refpath(realname)
  525. ensure_dir_exists(os.path.dirname(filename))
  526. f = GitFile(filename, 'wb')
  527. try:
  528. if os.path.exists(filename) or name in self.get_packed_refs():
  529. f.abort()
  530. return False
  531. try:
  532. f.write(ref+"\n")
  533. except (OSError, IOError):
  534. f.abort()
  535. raise
  536. finally:
  537. f.close()
  538. return True
  539. def remove_if_equals(self, name, old_ref):
  540. """Remove a refname only if it currently equals old_ref.
  541. This method does not follow symbolic references. It can be used to
  542. perform an atomic compare-and-delete operation.
  543. :param name: The refname to delete.
  544. :param old_ref: The old sha the refname must refer to, or None to delete
  545. unconditionally.
  546. :return: True if the delete was successful, False otherwise.
  547. """
  548. self._check_refname(name)
  549. filename = self.refpath(name)
  550. ensure_dir_exists(os.path.dirname(filename))
  551. f = GitFile(filename, 'wb')
  552. try:
  553. if old_ref is not None:
  554. orig_ref = self.read_loose_ref(name)
  555. if orig_ref is None:
  556. orig_ref = self.get_packed_refs().get(name, None)
  557. if orig_ref != old_ref:
  558. return False
  559. # may only be packed
  560. try:
  561. os.remove(filename)
  562. except OSError, e:
  563. if e.errno != errno.ENOENT:
  564. raise
  565. self._remove_packed_ref(name)
  566. finally:
  567. # never write, we just wanted the lock
  568. f.abort()
  569. return True
  570. def _split_ref_line(line):
  571. """Split a single ref line into a tuple of SHA1 and name."""
  572. fields = line.rstrip("\n").split(" ")
  573. if len(fields) != 2:
  574. raise PackedRefsException("invalid ref line '%s'" % line)
  575. sha, name = fields
  576. try:
  577. hex_to_sha(sha)
  578. except (AssertionError, TypeError), e:
  579. raise PackedRefsException(e)
  580. if not check_ref_format(name):
  581. raise PackedRefsException("invalid ref name '%s'" % name)
  582. return (sha, name)
  583. def read_packed_refs(f):
  584. """Read a packed refs file.
  585. :param f: file-like object to read from
  586. :return: Iterator over tuples with SHA1s and ref names.
  587. """
  588. for l in f:
  589. if l[0] == "#":
  590. # Comment
  591. continue
  592. if l[0] == "^":
  593. raise PackedRefsException(
  594. "found peeled ref in packed-refs without peeled")
  595. yield _split_ref_line(l)
  596. def read_packed_refs_with_peeled(f):
  597. """Read a packed refs file including peeled refs.
  598. Assumes the "# pack-refs with: peeled" line was already read. Yields tuples
  599. with ref names, SHA1s, and peeled SHA1s (or None).
  600. :param f: file-like object to read from, seek'ed to the second line
  601. """
  602. last = None
  603. for l in f:
  604. if l[0] == "#":
  605. continue
  606. l = l.rstrip("\r\n")
  607. if l[0] == "^":
  608. if not last:
  609. raise PackedRefsException("unexpected peeled ref line")
  610. try:
  611. hex_to_sha(l[1:])
  612. except (AssertionError, TypeError), e:
  613. raise PackedRefsException(e)
  614. sha, name = _split_ref_line(last)
  615. last = None
  616. yield (sha, name, l[1:])
  617. else:
  618. if last:
  619. sha, name = _split_ref_line(last)
  620. yield (sha, name, None)
  621. last = l
  622. if last:
  623. sha, name = _split_ref_line(last)
  624. yield (sha, name, None)
  625. def write_packed_refs(f, packed_refs, peeled_refs=None):
  626. """Write a packed refs file.
  627. :param f: empty file-like object to write to
  628. :param packed_refs: dict of refname to sha of packed refs to write
  629. :param peeled_refs: dict of refname to peeled value of sha
  630. """
  631. if peeled_refs is None:
  632. peeled_refs = {}
  633. else:
  634. f.write('# pack-refs with: peeled\n')
  635. for refname in sorted(packed_refs.iterkeys()):
  636. f.write('%s %s\n' % (packed_refs[refname], refname))
  637. if refname in peeled_refs:
  638. f.write('^%s\n' % peeled_refs[refname])
  639. class BaseRepo(object):
  640. """Base class for a git repository.
  641. :ivar object_store: Dictionary-like object for accessing
  642. the objects
  643. :ivar refs: Dictionary-like object with the refs in this repository
  644. """
  645. def __init__(self, object_store, refs):
  646. self.object_store = object_store
  647. self.refs = refs
  648. def _init_files(self, bare):
  649. """Initialize a default set of named files."""
  650. self._put_named_file('description', "Unnamed repository")
  651. self._put_named_file('config', ('[core]\n'
  652. 'repositoryformatversion = 0\n'
  653. 'filemode = true\n'
  654. 'bare = ' + str(bare).lower() + '\n'
  655. 'logallrefupdates = true\n'))
  656. self._put_named_file(os.path.join('info', 'exclude'), '')
  657. def get_named_file(self, path):
  658. """Get a file from the control dir with a specific name.
  659. Although the filename should be interpreted as a filename relative to
  660. the control dir in a disk-based Repo, the object returned need not be
  661. pointing to a file in that location.
  662. :param path: The path to the file, relative to the control dir.
  663. :return: An open file object, or None if the file does not exist.
  664. """
  665. raise NotImplementedError(self.get_named_file)
  666. def _put_named_file(self, path, contents):
  667. """Write a file to the control dir with the given name and contents.
  668. :param path: The path to the file, relative to the control dir.
  669. :param contents: A string to write to the file.
  670. """
  671. raise NotImplementedError(self._put_named_file)
  672. def open_index(self):
  673. """Open the index for this repository.
  674. :raises NoIndexPresent: If no index is present
  675. :return: Index instance
  676. """
  677. raise NotImplementedError(self.open_index)
  678. def fetch(self, target, determine_wants=None, progress=None):
  679. """Fetch objects into another repository.
  680. :param target: The target repository
  681. :param determine_wants: Optional function to determine what refs to
  682. fetch.
  683. :param progress: Optional progress function
  684. """
  685. if determine_wants is None:
  686. determine_wants = lambda heads: heads.values()
  687. target.object_store.add_objects(
  688. self.fetch_objects(determine_wants, target.get_graph_walker(),
  689. progress))
  690. return self.get_refs()
  691. def fetch_objects(self, determine_wants, graph_walker, progress,
  692. get_tagged=None):
  693. """Fetch the missing objects required for a set of revisions.
  694. :param determine_wants: Function that takes a dictionary with heads
  695. and returns the list of heads to fetch.
  696. :param graph_walker: Object that can iterate over the list of revisions
  697. to fetch and has an "ack" method that will be called to acknowledge
  698. that a revision is present.
  699. :param progress: Simple progress function that will be called with
  700. updated progress strings.
  701. :param get_tagged: Function that returns a dict of pointed-to sha -> tag
  702. sha for including tags.
  703. :return: iterator over objects, with __len__ implemented
  704. """
  705. wants = determine_wants(self.get_refs())
  706. if wants is None:
  707. # TODO(dborowitz): find a way to short-circuit that doesn't change
  708. # this interface.
  709. return None
  710. haves = self.object_store.find_common_revisions(graph_walker)
  711. return self.object_store.iter_shas(
  712. self.object_store.find_missing_objects(haves, wants, progress,
  713. get_tagged))
  714. def get_graph_walker(self, heads=None):
  715. if heads is None:
  716. heads = self.refs.as_dict('refs/heads').values()
  717. return self.object_store.get_graph_walker(heads)
  718. def ref(self, name):
  719. """Return the SHA1 a ref is pointing to."""
  720. return self.refs[name]
  721. def get_refs(self):
  722. """Get dictionary with all refs."""
  723. return self.refs.as_dict()
  724. def head(self):
  725. """Return the SHA1 pointed at by HEAD."""
  726. return self.refs['HEAD']
  727. def _get_object(self, sha, cls):
  728. assert len(sha) in (20, 40)
  729. ret = self.get_object(sha)
  730. if not isinstance(ret, cls):
  731. if cls is Commit:
  732. raise NotCommitError(ret)
  733. elif cls is Blob:
  734. raise NotBlobError(ret)
  735. elif cls is Tree:
  736. raise NotTreeError(ret)
  737. elif cls is Tag:
  738. raise NotTagError(ret)
  739. else:
  740. raise Exception("Type invalid: %r != %r" % (
  741. ret.type_name, cls.type_name))
  742. return ret
  743. def get_object(self, sha):
  744. return self.object_store[sha]
  745. def get_parents(self, sha):
  746. return self.commit(sha).parents
  747. def get_config(self):
  748. import ConfigParser
  749. p = ConfigParser.RawConfigParser()
  750. p.read(os.path.join(self._controldir, 'config'))
  751. return dict((section, dict(p.items(section)))
  752. for section in p.sections())
  753. def commit(self, sha):
  754. """Retrieve the commit with a particular SHA.
  755. :param sha: SHA of the commit to retrieve
  756. :raise NotCommitError: If the SHA provided doesn't point at a Commit
  757. :raise KeyError: If the SHA provided didn't exist
  758. :return: A `Commit` object
  759. """
  760. warnings.warn("Repo.commit(sha) is deprecated. Use Repo[sha] instead.",
  761. category=DeprecationWarning, stacklevel=2)
  762. return self._get_object(sha, Commit)
  763. def tree(self, sha):
  764. """Retrieve the tree with a particular SHA.
  765. :param sha: SHA of the tree to retrieve
  766. :raise NotTreeError: If the SHA provided doesn't point at a Tree
  767. :raise KeyError: If the SHA provided didn't exist
  768. :return: A `Tree` object
  769. """
  770. warnings.warn("Repo.tree(sha) is deprecated. Use Repo[sha] instead.",
  771. category=DeprecationWarning, stacklevel=2)
  772. return self._get_object(sha, Tree)
  773. def tag(self, sha):
  774. """Retrieve the tag with a particular SHA.
  775. :param sha: SHA of the tag to retrieve
  776. :raise NotTagError: If the SHA provided doesn't point at a Tag
  777. :raise KeyError: If the SHA provided didn't exist
  778. :return: A `Tag` object
  779. """
  780. warnings.warn("Repo.tag(sha) is deprecated. Use Repo[sha] instead.",
  781. category=DeprecationWarning, stacklevel=2)
  782. return self._get_object(sha, Tag)
  783. def get_blob(self, sha):
  784. """Retrieve the blob with a particular SHA.
  785. :param sha: SHA of the blob to retrieve
  786. :raise NotBlobError: If the SHA provided doesn't point at a Blob
  787. :raise KeyError: If the SHA provided didn't exist
  788. :return: A `Blob` object
  789. """
  790. warnings.warn("Repo.get_blob(sha) is deprecated. Use Repo[sha] "
  791. "instead.", category=DeprecationWarning, stacklevel=2)
  792. return self._get_object(sha, Blob)
  793. def get_peeled(self, ref):
  794. """Get the peeled value of a ref.
  795. :param ref: The refname to peel.
  796. :return: The fully-peeled SHA1 of a tag object, after peeling all
  797. intermediate tags; if the original ref does not point to a tag, this
  798. will equal the original SHA1.
  799. """
  800. cached = self.refs.get_peeled(ref)
  801. if cached is not None:
  802. return cached
  803. return self.object_store.peel_sha(self.refs[ref]).id
  804. def revision_history(self, head):
  805. """Returns a list of the commits reachable from head.
  806. Returns a list of commit objects. the first of which will be the commit
  807. of head, then following that will be the parents.
  808. Raises NotCommitError if any no commits are referenced, including if the
  809. head parameter isn't the sha of a commit.
  810. XXX: work out how to handle merges.
  811. """
  812. try:
  813. commit = self[head]
  814. except KeyError:
  815. raise MissingCommitError(head)
  816. if type(commit) != Commit:
  817. raise NotCommitError(commit)
  818. pending_commits = [commit]
  819. history = set()
  820. while pending_commits != []:
  821. commit = pending_commits.pop(0)
  822. if commit in history:
  823. continue
  824. history.add(commit)
  825. for parent in commit.parents:
  826. try:
  827. commit = self[parent]
  828. except KeyError:
  829. raise MissingCommitError(head)
  830. if type(commit) != Commit:
  831. raise NotCommitError(commit)
  832. pending_commits.append(commit)
  833. history = list(history)
  834. history.sort(key=lambda c:c.commit_time, reverse=True)
  835. return history
  836. def __getitem__(self, name):
  837. if len(name) in (20, 40):
  838. try:
  839. return self.object_store[name]
  840. except KeyError:
  841. pass
  842. try:
  843. return self.object_store[self.refs[name]]
  844. except RefFormatError:
  845. raise KeyError(name)
  846. def __contains__(self, name):
  847. if len(name) in (20, 40):
  848. return name in self.object_store or name in self.refs
  849. else:
  850. return name in self.refs
  851. def __setitem__(self, name, value):
  852. if name.startswith("refs/") or name == "HEAD":
  853. if isinstance(value, ShaFile):
  854. self.refs[name] = value.id
  855. elif isinstance(value, str):
  856. self.refs[name] = value
  857. else:
  858. raise TypeError(value)
  859. else:
  860. raise ValueError(name)
  861. def __delitem__(self, name):
  862. if name.startswith("refs") or name == "HEAD":
  863. del self.refs[name]
  864. raise ValueError(name)
  865. def do_commit(self, message, committer=None,
  866. author=None, commit_timestamp=None,
  867. commit_timezone=None, author_timestamp=None,
  868. author_timezone=None, tree=None, encoding=None):
  869. """Create a new commit.
  870. :param message: Commit message
  871. :param committer: Committer fullname
  872. :param author: Author fullname (defaults to committer)
  873. :param commit_timestamp: Commit timestamp (defaults to now)
  874. :param commit_timezone: Commit timestamp timezone (defaults to GMT)
  875. :param author_timestamp: Author timestamp (defaults to commit timestamp)
  876. :param author_timezone: Author timestamp timezone
  877. (defaults to commit timestamp timezone)
  878. :param tree: SHA1 of the tree root to use (if not specified the
  879. current index will be committed).
  880. :param encoding: Encoding
  881. :return: New commit SHA1
  882. """
  883. import time
  884. c = Commit()
  885. if tree is None:
  886. index = self.open_index()
  887. c.tree = index.commit(self.object_store)
  888. else:
  889. if len(tree) != 40:
  890. raise ValueError("tree must be a 40-byte hex sha string")
  891. c.tree = tree
  892. # TODO: Allow username to be missing, and get it from .git/config
  893. if committer is None:
  894. raise ValueError("committer not set")
  895. c.committer = committer
  896. if commit_timestamp is None:
  897. commit_timestamp = time.time()
  898. c.commit_time = int(commit_timestamp)
  899. if commit_timezone is None:
  900. # FIXME: Use current user timezone rather than UTC
  901. commit_timezone = 0
  902. c.commit_timezone = commit_timezone
  903. if author is None:
  904. author = committer
  905. c.author = author
  906. if author_timestamp is None:
  907. author_timestamp = commit_timestamp
  908. c.author_time = int(author_timestamp)
  909. if author_timezone is None:
  910. author_timezone = commit_timezone
  911. c.author_timezone = author_timezone
  912. if encoding is not None:
  913. c.encoding = encoding
  914. c.message = message
  915. try:
  916. old_head = self.refs["HEAD"]
  917. c.parents = [old_head]
  918. self.object_store.add_object(c)
  919. ok = self.refs.set_if_equals("HEAD", old_head, c.id)
  920. except KeyError:
  921. c.parents = []
  922. self.object_store.add_object(c)
  923. ok = self.refs.add_if_new("HEAD", c.id)
  924. if not ok:
  925. # Fail if the atomic compare-and-swap failed, leaving the commit and
  926. # all its objects as garbage.
  927. raise CommitError("HEAD changed during commit")
  928. return c.id
  929. class Repo(BaseRepo):
  930. """A git repository backed by local disk."""
  931. def __init__(self, root):
  932. if os.path.isdir(os.path.join(root, ".git", OBJECTDIR)):
  933. self.bare = False
  934. self._controldir = os.path.join(root, ".git")
  935. elif (os.path.isdir(os.path.join(root, OBJECTDIR)) and
  936. os.path.isdir(os.path.join(root, REFSDIR))):
  937. self.bare = True
  938. self._controldir = root
  939. else:
  940. raise NotGitRepository(root)
  941. self.path = root
  942. object_store = DiskObjectStore(os.path.join(self.controldir(),
  943. OBJECTDIR))
  944. refs = DiskRefsContainer(self.controldir())
  945. BaseRepo.__init__(self, object_store, refs)
  946. def controldir(self):
  947. """Return the path of the control directory."""
  948. return self._controldir
  949. def _put_named_file(self, path, contents):
  950. """Write a file to the control dir with the given name and contents.
  951. :param path: The path to the file, relative to the control dir.
  952. :param contents: A string to write to the file.
  953. """
  954. path = path.lstrip(os.path.sep)
  955. f = GitFile(os.path.join(self.controldir(), path), 'wb')
  956. try:
  957. f.write(contents)
  958. finally:
  959. f.close()
  960. def get_named_file(self, path):
  961. """Get a file from the control dir with a specific name.
  962. Although the filename should be interpreted as a filename relative to
  963. the control dir in a disk-based Repo, the object returned need not be
  964. pointing to a file in that location.
  965. :param path: The path to the file, relative to the control dir.
  966. :return: An open file object, or None if the file does not exist.
  967. """
  968. # TODO(dborowitz): sanitize filenames, since this is used directly by
  969. # the dumb web serving code.
  970. path = path.lstrip(os.path.sep)
  971. try:
  972. return open(os.path.join(self.controldir(), path), 'rb')
  973. except (IOError, OSError), e:
  974. if e.errno == errno.ENOENT:
  975. return None
  976. raise
  977. def index_path(self):
  978. """Return path to the index file."""
  979. return os.path.join(self.controldir(), INDEX_FILENAME)
  980. def open_index(self):
  981. """Open the index for this repository."""
  982. from dulwich.index import Index
  983. if not self.has_index():
  984. raise NoIndexPresent()
  985. return Index(self.index_path())
  986. def has_index(self):
  987. """Check if an index is present."""
  988. # Bare repos must never have index files; non-bare repos may have a
  989. # missing index file, which is treated as empty.
  990. return not self.bare
  991. def stage(self, paths):
  992. """Stage a set of paths.
  993. :param paths: List of paths, relative to the repository path
  994. """
  995. from dulwich.index import cleanup_mode
  996. index = self.open_index()
  997. for path in paths:
  998. full_path = os.path.join(self.path, path)
  999. blob = Blob()
  1000. try:
  1001. st = os.stat(full_path)
  1002. except OSError:
  1003. # File no longer exists
  1004. try:
  1005. del index[path]
  1006. except KeyError:
  1007. pass # Doesn't exist in the index either
  1008. else:
  1009. f = open(full_path, 'rb')
  1010. try:
  1011. blob.data = f.read()
  1012. finally:
  1013. f.close()
  1014. self.object_store.add_object(blob)
  1015. # XXX: Cleanup some of the other file properties as well?
  1016. index[path] = (st.st_ctime, st.st_mtime, st.st_dev, st.st_ino,
  1017. cleanup_mode(st.st_mode), st.st_uid, st.st_gid, st.st_size,
  1018. blob.id, 0)
  1019. index.write()
  1020. def __repr__(self):
  1021. return "<Repo at %r>" % self.path
  1022. @classmethod
  1023. def _init_maybe_bare(cls, path, bare):
  1024. for d in BASE_DIRECTORIES:
  1025. os.mkdir(os.path.join(path, *d))
  1026. DiskObjectStore.init(os.path.join(path, OBJECTDIR))
  1027. ret = cls(path)
  1028. ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
  1029. ret._init_files(bare)
  1030. return ret
  1031. @classmethod
  1032. def init(cls, path, mkdir=False):
  1033. if mkdir:
  1034. os.mkdir(path)
  1035. controldir = os.path.join(path, ".git")
  1036. os.mkdir(controldir)
  1037. cls._init_maybe_bare(controldir, False)
  1038. return cls(path)
  1039. @classmethod
  1040. def init_bare(cls, path):
  1041. return cls._init_maybe_bare(path, True)
  1042. create = init_bare
  1043. class MemoryRepo(BaseRepo):
  1044. """Repo that stores refs, objects, and named files in memory.
  1045. MemoryRepos are always bare: they have no working tree and no index, since
  1046. those have a stronger dependency on the filesystem.
  1047. """
  1048. def __init__(self):
  1049. BaseRepo.__init__(self, MemoryObjectStore(), DictRefsContainer({}))
  1050. self._named_files = {}
  1051. self.bare = True
  1052. def _put_named_file(self, path, contents):
  1053. """Write a file to the control dir with the given name and contents.
  1054. :param path: The path to the file, relative to the control dir.
  1055. :param contents: A string to write to the file.
  1056. """
  1057. self._named_files[path] = contents
  1058. def get_named_file(self, path):
  1059. """Get a file from the control dir with a specific name.
  1060. Although the filename should be interpreted as a filename relative to
  1061. the control dir in a disk-baked Repo, the object returned need not be
  1062. pointing to a file in that location.
  1063. :param path: The path to the file, relative to the control dir.
  1064. :return: An open file object, or None if the file does not exist.
  1065. """
  1066. contents = self._named_files.get(path, None)
  1067. if contents is None:
  1068. return None
  1069. return StringIO(contents)
  1070. def open_index(self):
  1071. """Fail to open index for this repo, since it is bare."""
  1072. raise NoIndexPresent()
  1073. @classmethod
  1074. def init_bare(cls, objects, refs):
  1075. ret = cls()
  1076. for obj in objects:
  1077. ret.object_store.add_object(obj)
  1078. for refname, sha in refs.iteritems():
  1079. ret.refs[refname] = sha
  1080. ret._init_files(bare=True)
  1081. return ret