repo.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. # repo.py -- For dealing wih 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. import errno
  22. import os
  23. from dulwich.errors import (
  24. MissingCommitError,
  25. NoIndexPresent,
  26. NotBlobError,
  27. NotCommitError,
  28. NotGitRepository,
  29. NotTreeError,
  30. NotTagError,
  31. PackedRefsException,
  32. )
  33. from dulwich.file import (
  34. ensure_dir_exists,
  35. GitFile,
  36. )
  37. from dulwich.object_store import (
  38. DiskObjectStore,
  39. )
  40. from dulwich.objects import (
  41. Blob,
  42. Commit,
  43. ShaFile,
  44. Tag,
  45. Tree,
  46. hex_to_sha,
  47. num_type_map,
  48. )
  49. OBJECTDIR = 'objects'
  50. SYMREF = 'ref: '
  51. REFSDIR = 'refs'
  52. REFSDIR_TAGS = 'tags'
  53. REFSDIR_HEADS = 'heads'
  54. INDEX_FILENAME = "index"
  55. BASE_DIRECTORIES = [
  56. [OBJECTDIR],
  57. [OBJECTDIR, "info"],
  58. [OBJECTDIR, "pack"],
  59. ["branches"],
  60. [REFSDIR],
  61. [REFSDIR, REFSDIR_TAGS],
  62. [REFSDIR, REFSDIR_HEADS],
  63. ["hooks"],
  64. ["info"]
  65. ]
  66. def read_info_refs(f):
  67. ret = {}
  68. for l in f.readlines():
  69. (sha, name) = l.rstrip("\n").split("\t", 1)
  70. ret[name] = sha
  71. return ret
  72. def check_ref_format(refname):
  73. """Check if a refname is correctly formatted.
  74. Implements all the same rules as git-check-ref-format[1].
  75. [1] http://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  76. :param refname: The refname to check
  77. :return: True if refname is valid, False otherwise
  78. """
  79. # These could be combined into one big expression, but are listed separately
  80. # to parallel [1].
  81. if '/.' in refname or refname.startswith('.'):
  82. return False
  83. if '/' not in refname:
  84. return False
  85. if '..' in refname:
  86. return False
  87. for c in refname:
  88. if ord(c) < 040 or c in '\177 ~^:?*[':
  89. return False
  90. if refname[-1] in '/.':
  91. return False
  92. if refname.endswith('.lock'):
  93. return False
  94. if '@{' in refname:
  95. return False
  96. if '\\' in refname:
  97. return False
  98. return True
  99. class RefsContainer(object):
  100. """A container for refs."""
  101. def set_ref(self, name, other):
  102. """Make a ref point at another ref.
  103. :param name: Name of the ref to set
  104. :param other: Name of the ref to point at
  105. """
  106. self[name] = SYMREF + other + '\n'
  107. def get_packed_refs(self):
  108. """Get contents of the packed-refs file.
  109. :return: Dictionary mapping ref names to SHA1s
  110. :note: Will return an empty dictionary when no packed-refs file is
  111. present.
  112. """
  113. raise NotImplementedError(self.get_packed_refs)
  114. def get_peeled(self, name):
  115. """Return the cached peeled value of a ref, if available.
  116. :param name: Name of the ref to peel
  117. :return: The peeled value of the ref. If the ref is known not point to a
  118. tag, this will be the SHA the ref refers to. If the ref may point to
  119. a tag, but no cached information is available, None is returned.
  120. """
  121. return None
  122. def import_refs(self, base, other):
  123. for name, value in other.iteritems():
  124. self["%s/%s" % (base, name)] = value
  125. def keys(self, base=None):
  126. """Refs present in this container.
  127. :param base: An optional base to return refs under
  128. :return: An unsorted set of valid refs in this container, including
  129. packed refs.
  130. """
  131. if base is not None:
  132. return self.subkeys(base)
  133. else:
  134. return self.allkeys()
  135. def subkeys(self, base):
  136. keys = set()
  137. for refname in self.allkeys():
  138. if refname.startswith(base):
  139. keys.add(refname)
  140. return keys
  141. def as_dict(self, base=None):
  142. """Return the contents of this container as a dictionary.
  143. """
  144. ret = {}
  145. keys = self.keys(base)
  146. if base is None:
  147. base = ""
  148. for key in keys:
  149. try:
  150. ret[key] = self[("%s/%s" % (base, key)).strip("/")]
  151. except KeyError:
  152. continue # Unable to resolve
  153. return ret
  154. def _check_refname(self, name):
  155. """Ensure a refname is valid and lives in refs or is HEAD.
  156. HEAD is not a valid refname according to git-check-ref-format, but this
  157. class needs to be able to touch HEAD. Also, check_ref_format expects
  158. refnames without the leading 'refs/', but this class requires that
  159. so it cannot touch anything outside the refs dir (or HEAD).
  160. :param name: The name of the reference.
  161. :raises KeyError: if a refname is not HEAD or is otherwise not valid.
  162. """
  163. if name == 'HEAD':
  164. return
  165. if not name.startswith('refs/') or not check_ref_format(name[5:]):
  166. raise KeyError(name)
  167. def read_loose_ref(self, name):
  168. """Read a loose reference and return its contents.
  169. :param name: the refname to read
  170. :return: The contents of the ref file, or None if it does
  171. not exist.
  172. """
  173. raise NotImplementedError(self.read_loose_ref)
  174. def _follow(self, name):
  175. """Follow a reference name.
  176. :return: a tuple of (refname, sha), where refname is the name of the
  177. last reference in the symbolic reference chain
  178. """
  179. self._check_refname(name)
  180. contents = SYMREF + name
  181. depth = 0
  182. while contents.startswith(SYMREF):
  183. refname = contents[len(SYMREF):]
  184. contents = self.read_loose_ref(refname)
  185. if not contents:
  186. contents = self.get_packed_refs().get(refname, None)
  187. if not contents:
  188. break
  189. depth += 1
  190. if depth > 5:
  191. raise KeyError(name)
  192. return refname, contents
  193. def __getitem__(self, name):
  194. """Get the SHA1 for a reference name.
  195. This method follows all symbolic references.
  196. """
  197. _, sha = self._follow(name)
  198. if sha is None:
  199. raise KeyError(name)
  200. return sha
  201. class DictRefsContainer(RefsContainer):
  202. def __init__(self, refs):
  203. self._refs = refs
  204. def allkeys(self):
  205. return self._refs.keys()
  206. def read_loose_ref(self, name):
  207. return self._refs[name]
  208. class DiskRefsContainer(RefsContainer):
  209. """Refs container that reads refs from disk."""
  210. def __init__(self, path):
  211. self.path = path
  212. self._packed_refs = None
  213. self._peeled_refs = None
  214. def __repr__(self):
  215. return "%s(%r)" % (self.__class__.__name__, self.path)
  216. def subkeys(self, base):
  217. keys = set()
  218. path = self.refpath(base)
  219. for root, dirs, files in os.walk(path):
  220. dir = root[len(path):].strip(os.path.sep).replace(os.path.sep, "/")
  221. for filename in files:
  222. refname = ("%s/%s" % (dir, filename)).strip("/")
  223. # check_ref_format requires at least one /, so we prepend the
  224. # base before calling it.
  225. if check_ref_format("%s/%s" % (base, refname)):
  226. keys.add(refname)
  227. for key in self.get_packed_refs():
  228. if key.startswith(base):
  229. keys.add(key[len(base):].strip("/"))
  230. return keys
  231. def allkeys(self):
  232. keys = set()
  233. if os.path.exists(self.refpath("HEAD")):
  234. keys.add("HEAD")
  235. path = self.refpath("")
  236. for root, dirs, files in os.walk(self.refpath("refs")):
  237. dir = root[len(path):].strip(os.path.sep).replace(os.path.sep, "/")
  238. for filename in files:
  239. refname = ("%s/%s" % (dir, filename)).strip("/")
  240. if check_ref_format(refname):
  241. keys.add(refname)
  242. keys.update(self.get_packed_refs())
  243. return keys
  244. def refpath(self, name):
  245. """Return the disk path of a ref.
  246. """
  247. if os.path.sep != "/":
  248. name = name.replace("/", os.path.sep)
  249. return os.path.join(self.path, name)
  250. def get_packed_refs(self):
  251. """Get contents of the packed-refs file.
  252. :return: Dictionary mapping ref names to SHA1s
  253. :note: Will return an empty dictionary when no packed-refs file is
  254. present.
  255. """
  256. # TODO: invalidate the cache on repacking
  257. if self._packed_refs is None:
  258. self._packed_refs = {}
  259. path = os.path.join(self.path, 'packed-refs')
  260. try:
  261. f = GitFile(path, 'rb')
  262. except IOError, e:
  263. if e.errno == errno.ENOENT:
  264. return {}
  265. raise
  266. try:
  267. first_line = iter(f).next().rstrip()
  268. if (first_line.startswith("# pack-refs") and " peeled" in
  269. first_line):
  270. self._peeled_refs = {}
  271. for sha, name, peeled in read_packed_refs_with_peeled(f):
  272. self._packed_refs[name] = sha
  273. if peeled:
  274. self._peeled_refs[name] = peeled
  275. else:
  276. f.seek(0)
  277. for sha, name in read_packed_refs(f):
  278. self._packed_refs[name] = sha
  279. finally:
  280. f.close()
  281. return self._packed_refs
  282. def get_peeled(self, name):
  283. """Return the cached peeled value of a ref, if available.
  284. :param name: Name of the ref to peel
  285. :return: The peeled value of the ref. If the ref is known not point to a
  286. tag, this will be the SHA the ref refers to. If the ref may point to
  287. a tag, but no cached information is available, None is returned.
  288. """
  289. self.get_packed_refs()
  290. if self._peeled_refs is None or name not in self._packed_refs:
  291. # No cache: no peeled refs were read, or this ref is loose
  292. return None
  293. if name in self._peeled_refs:
  294. return self._peeled_refs[name]
  295. else:
  296. # Known not peelable
  297. return self[name]
  298. def read_loose_ref(self, name):
  299. """Read a reference file and return its contents.
  300. If the reference file a symbolic reference, only read the first line of
  301. the file. Otherwise, only read the first 40 bytes.
  302. :param name: the refname to read, relative to refpath
  303. :return: The contents of the ref file, or None if the file does not
  304. exist.
  305. :raises IOError: if any other error occurs
  306. """
  307. filename = self.refpath(name)
  308. try:
  309. f = GitFile(filename, 'rb')
  310. try:
  311. header = f.read(len(SYMREF))
  312. if header == SYMREF:
  313. # Read only the first line
  314. return header + iter(f).next().rstrip("\n")
  315. else:
  316. # Read only the first 40 bytes
  317. return header + f.read(40-len(SYMREF))
  318. finally:
  319. f.close()
  320. except IOError, e:
  321. if e.errno == errno.ENOENT:
  322. return None
  323. raise
  324. def _remove_packed_ref(self, name):
  325. if self._packed_refs is None:
  326. return
  327. filename = os.path.join(self.path, 'packed-refs')
  328. # reread cached refs from disk, while holding the lock
  329. f = GitFile(filename, 'wb')
  330. try:
  331. self._packed_refs = None
  332. self.get_packed_refs()
  333. if name not in self._packed_refs:
  334. return
  335. del self._packed_refs[name]
  336. if name in self._peeled_refs:
  337. del self._peeled_refs[name]
  338. write_packed_refs(f, self._packed_refs, self._peeled_refs)
  339. f.close()
  340. finally:
  341. f.abort()
  342. def set_if_equals(self, name, old_ref, new_ref):
  343. """Set a refname to new_ref only if it currently equals old_ref.
  344. This method follows all symbolic references, and can be used to perform
  345. an atomic compare-and-swap operation.
  346. :param name: The refname to set.
  347. :param old_ref: The old sha the refname must refer to, or None to set
  348. unconditionally.
  349. :param new_ref: The new sha the refname will refer to.
  350. :return: True if the set was successful, False otherwise.
  351. """
  352. try:
  353. realname, _ = self._follow(name)
  354. except KeyError:
  355. realname = name
  356. filename = self.refpath(realname)
  357. ensure_dir_exists(os.path.dirname(filename))
  358. f = GitFile(filename, 'wb')
  359. try:
  360. if old_ref is not None:
  361. try:
  362. # read again while holding the lock
  363. orig_ref = self.read_loose_ref(realname)
  364. if orig_ref is None:
  365. orig_ref = self.get_packed_refs().get(realname, None)
  366. if orig_ref != old_ref:
  367. f.abort()
  368. return False
  369. except (OSError, IOError):
  370. f.abort()
  371. raise
  372. try:
  373. f.write(new_ref+"\n")
  374. except (OSError, IOError):
  375. f.abort()
  376. raise
  377. finally:
  378. f.close()
  379. return True
  380. def add_if_new(self, name, ref):
  381. """Add a new reference only if it does not already exist."""
  382. self._check_refname(name)
  383. filename = self.refpath(name)
  384. ensure_dir_exists(os.path.dirname(filename))
  385. f = GitFile(filename, 'wb')
  386. try:
  387. if os.path.exists(filename) or name in self.get_packed_refs():
  388. f.abort()
  389. return False
  390. try:
  391. f.write(ref+"\n")
  392. except (OSError, IOError):
  393. f.abort()
  394. raise
  395. finally:
  396. f.close()
  397. return True
  398. def __setitem__(self, name, ref):
  399. """Set a reference name to point to the given SHA1.
  400. This method follows all symbolic references.
  401. :note: This method unconditionally overwrites the contents of a reference
  402. on disk. To update atomically only if the reference has not changed
  403. on disk, use set_if_equals().
  404. """
  405. self.set_if_equals(name, None, ref)
  406. def remove_if_equals(self, name, old_ref):
  407. """Remove a refname only if it currently equals old_ref.
  408. This method does not follow symbolic references. It can be used to
  409. perform an atomic compare-and-delete operation.
  410. :param name: The refname to delete.
  411. :param old_ref: The old sha the refname must refer to, or None to delete
  412. unconditionally.
  413. :return: True if the delete was successful, False otherwise.
  414. """
  415. self._check_refname(name)
  416. filename = self.refpath(name)
  417. ensure_dir_exists(os.path.dirname(filename))
  418. f = GitFile(filename, 'wb')
  419. try:
  420. if old_ref is not None:
  421. orig_ref = self.read_loose_ref(name)
  422. if orig_ref is None:
  423. orig_ref = self.get_packed_refs().get(name, None)
  424. if orig_ref != old_ref:
  425. return False
  426. # may only be packed
  427. try:
  428. os.remove(filename)
  429. except OSError, e:
  430. if e.errno != errno.ENOENT:
  431. raise
  432. self._remove_packed_ref(name)
  433. finally:
  434. # never write, we just wanted the lock
  435. f.abort()
  436. return True
  437. def __delitem__(self, name):
  438. """Remove a refname.
  439. This method does not follow symbolic references.
  440. :note: This method unconditionally deletes the contents of a reference
  441. on disk. To delete atomically only if the reference has not changed
  442. on disk, use set_if_equals().
  443. """
  444. self.remove_if_equals(name, None)
  445. def _split_ref_line(line):
  446. """Split a single ref line into a tuple of SHA1 and name."""
  447. fields = line.rstrip("\n").split(" ")
  448. if len(fields) != 2:
  449. raise PackedRefsException("invalid ref line '%s'" % line)
  450. sha, name = fields
  451. try:
  452. hex_to_sha(sha)
  453. except (AssertionError, TypeError), e:
  454. raise PackedRefsException(e)
  455. if not check_ref_format(name):
  456. raise PackedRefsException("invalid ref name '%s'" % name)
  457. return (sha, name)
  458. def read_packed_refs(f):
  459. """Read a packed refs file.
  460. Yields tuples with SHA1s and ref names.
  461. :param f: file-like object to read from
  462. """
  463. for l in f:
  464. if l[0] == "#":
  465. # Comment
  466. continue
  467. if l[0] == "^":
  468. raise PackedRefsException(
  469. "found peeled ref in packed-refs without peeled")
  470. yield _split_ref_line(l)
  471. def read_packed_refs_with_peeled(f):
  472. """Read a packed refs file including peeled refs.
  473. Assumes the "# pack-refs with: peeled" line was already read. Yields tuples
  474. with ref names, SHA1s, and peeled SHA1s (or None).
  475. :param f: file-like object to read from, seek'ed to the second line
  476. """
  477. last = None
  478. for l in f:
  479. if l[0] == "#":
  480. continue
  481. l = l.rstrip("\n")
  482. if l[0] == "^":
  483. if not last:
  484. raise PackedRefsException("unexpected peeled ref line")
  485. try:
  486. hex_to_sha(l[1:])
  487. except (AssertionError, TypeError), e:
  488. raise PackedRefsException(e)
  489. sha, name = _split_ref_line(last)
  490. last = None
  491. yield (sha, name, l[1:])
  492. else:
  493. if last:
  494. sha, name = _split_ref_line(last)
  495. yield (sha, name, None)
  496. last = l
  497. if last:
  498. sha, name = _split_ref_line(last)
  499. yield (sha, name, None)
  500. def write_packed_refs(f, packed_refs, peeled_refs=None):
  501. """Write a packed refs file.
  502. :param f: empty file-like object to write to
  503. :param packed_refs: dict of refname to sha of packed refs to write
  504. :param peeled_refs: dict of refname to peeled value of sha
  505. """
  506. if peeled_refs is None:
  507. peeled_refs = {}
  508. else:
  509. f.write('# pack-refs with: peeled\n')
  510. for refname in sorted(packed_refs.iterkeys()):
  511. f.write('%s %s\n' % (packed_refs[refname], refname))
  512. if refname in peeled_refs:
  513. f.write('^%s\n' % peeled_refs[refname])
  514. class BaseRepo(object):
  515. """Base class for a git repository.
  516. :ivar object_store: Dictionary-like object for accessing
  517. the objects
  518. :ivar refs: Dictionary-like object with the refs in this repository
  519. """
  520. def __init__(self, object_store, refs):
  521. self.object_store = object_store
  522. self.refs = refs
  523. def get_named_file(self, path):
  524. """Get a file from the control dir with a specific name.
  525. Although the filename should be interpreted as a filename relative to
  526. the control dir in a disk-baked Repo, the object returned need not be
  527. pointing to a file in that location.
  528. :param path: The path to the file, relative to the control dir.
  529. :return: An open file object, or None if the file does not exist.
  530. """
  531. raise NotImplementedError(self.get_named_file)
  532. def open_index(self):
  533. """Open the index for this repository.
  534. :raises NoIndexPresent: If no index is present
  535. :return: Index instance
  536. """
  537. raise NotImplementedError(self.open_index)
  538. def fetch(self, target, determine_wants=None, progress=None):
  539. """Fetch objects into another repository.
  540. :param target: The target repository
  541. :param determine_wants: Optional function to determine what refs to
  542. fetch.
  543. :param progress: Optional progress function
  544. """
  545. if determine_wants is None:
  546. determine_wants = lambda heads: heads.values()
  547. target.object_store.add_objects(
  548. self.fetch_objects(determine_wants, target.get_graph_walker(),
  549. progress))
  550. return self.get_refs()
  551. def fetch_objects(self, determine_wants, graph_walker, progress,
  552. get_tagged=None):
  553. """Fetch the missing objects required for a set of revisions.
  554. :param determine_wants: Function that takes a dictionary with heads
  555. and returns the list of heads to fetch.
  556. :param graph_walker: Object that can iterate over the list of revisions
  557. to fetch and has an "ack" method that will be called to acknowledge
  558. that a revision is present.
  559. :param progress: Simple progress function that will be called with
  560. updated progress strings.
  561. :param get_tagged: Function that returns a dict of pointed-to sha -> tag
  562. sha for including tags.
  563. :return: iterator over objects, with __len__ implemented
  564. """
  565. wants = determine_wants(self.get_refs())
  566. haves = self.object_store.find_common_revisions(graph_walker)
  567. return self.object_store.iter_shas(
  568. self.object_store.find_missing_objects(haves, wants, progress,
  569. get_tagged))
  570. def get_graph_walker(self, heads=None):
  571. if heads is None:
  572. heads = self.refs.as_dict('refs/heads').values()
  573. return self.object_store.get_graph_walker(heads)
  574. def ref(self, name):
  575. """Return the SHA1 a ref is pointing to."""
  576. return self.refs[name]
  577. def get_refs(self):
  578. """Get dictionary with all refs."""
  579. return self.refs.as_dict()
  580. def head(self):
  581. """Return the SHA1 pointed at by HEAD."""
  582. return self.refs['HEAD']
  583. def _get_object(self, sha, cls):
  584. assert len(sha) in (20, 40)
  585. ret = self.get_object(sha)
  586. if ret._type != cls._type:
  587. if cls is Commit:
  588. raise NotCommitError(ret)
  589. elif cls is Blob:
  590. raise NotBlobError(ret)
  591. elif cls is Tree:
  592. raise NotTreeError(ret)
  593. elif cls is Tag:
  594. raise NotTagError(ret)
  595. else:
  596. raise Exception("Type invalid: %r != %r" % (ret._type, cls._type))
  597. return ret
  598. def get_object(self, sha):
  599. return self.object_store[sha]
  600. def get_parents(self, sha):
  601. return self.commit(sha).parents
  602. def get_config(self):
  603. import ConfigParser
  604. p = ConfigParser.RawConfigParser()
  605. p.read(os.path.join(self._controldir, 'config'))
  606. return dict((section, dict(p.items(section)))
  607. for section in p.sections())
  608. def commit(self, sha):
  609. return self._get_object(sha, Commit)
  610. def tree(self, sha):
  611. return self._get_object(sha, Tree)
  612. def tag(self, sha):
  613. return self._get_object(sha, Tag)
  614. def get_peeled(self, ref):
  615. """Get the peeled value of a ref.
  616. :param ref: the refname to peel
  617. :return: the fully-peeled SHA1 of a tag object, after peeling all
  618. intermediate tags; if the original ref does not point to a tag, this
  619. will equal the original SHA1.
  620. """
  621. cached = self.refs.get_peeled(ref)
  622. if cached is not None:
  623. return cached
  624. obj = self[ref]
  625. obj_type = num_type_map[obj.type]
  626. while obj_type == Tag:
  627. obj_type, sha = obj.object
  628. obj = self.get_object(sha)
  629. return obj.id
  630. def get_blob(self, sha):
  631. return self._get_object(sha, Blob)
  632. def revision_history(self, head):
  633. """Returns a list of the commits reachable from head.
  634. Returns a list of commit objects. the first of which will be the commit
  635. of head, then following theat will be the parents.
  636. Raises NotCommitError if any no commits are referenced, including if the
  637. head parameter isn't the sha of a commit.
  638. XXX: work out how to handle merges.
  639. """
  640. # We build the list backwards, as parents are more likely to be older
  641. # than children
  642. pending_commits = [head]
  643. history = []
  644. while pending_commits != []:
  645. head = pending_commits.pop(0)
  646. try:
  647. commit = self.commit(head)
  648. except KeyError:
  649. raise MissingCommitError(head)
  650. if commit in history:
  651. continue
  652. i = 0
  653. for known_commit in history:
  654. if known_commit.commit_time > commit.commit_time:
  655. break
  656. i += 1
  657. history.insert(i, commit)
  658. pending_commits += commit.parents
  659. history.reverse()
  660. return history
  661. def __getitem__(self, name):
  662. if len(name) in (20, 40):
  663. return self.object_store[name]
  664. return self.object_store[self.refs[name]]
  665. def __setitem__(self, name, value):
  666. if name.startswith("refs/") or name == "HEAD":
  667. if isinstance(value, ShaFile):
  668. self.refs[name] = value.id
  669. elif isinstance(value, str):
  670. self.refs[name] = value
  671. else:
  672. raise TypeError(value)
  673. else:
  674. raise ValueError(name)
  675. def __delitem__(self, name):
  676. if name.startswith("refs") or name == "HEAD":
  677. del self.refs[name]
  678. raise ValueError(name)
  679. def do_commit(self, committer, message,
  680. author=None, commit_timestamp=None,
  681. commit_timezone=None, author_timestamp=None,
  682. author_timezone=None, tree=None):
  683. """Create a new commit.
  684. :param committer: Committer fullname
  685. :param message: Commit message
  686. :param author: Author fullname (defaults to committer)
  687. :param commit_timestamp: Commit timestamp (defaults to now)
  688. :param commit_timezone: Commit timestamp timezone (defaults to GMT)
  689. :param author_timestamp: Author timestamp (defaults to commit timestamp)
  690. :param author_timezone: Author timestamp timezone
  691. (defaults to commit timestamp timezone)
  692. :param tree: SHA1 of the tree root to use (if not specified the current index will be committed).
  693. :return: New commit SHA1
  694. """
  695. from dulwich.index import commit_index
  696. import time
  697. index = self.open_index()
  698. c = Commit()
  699. if tree is None:
  700. c.tree = commit_index(self.object_store, index)
  701. else:
  702. c.tree = tree
  703. c.committer = committer
  704. if commit_timestamp is None:
  705. commit_timestamp = time.time()
  706. c.commit_time = int(commit_timestamp)
  707. if commit_timezone is None:
  708. commit_timezone = 0
  709. c.commit_timezone = commit_timezone
  710. if author is None:
  711. author = committer
  712. c.author = author
  713. if author_timestamp is None:
  714. author_timestamp = commit_timestamp
  715. c.author_time = int(author_timestamp)
  716. if author_timezone is None:
  717. author_timezone = commit_timezone
  718. c.author_timezone = author_timezone
  719. c.message = message
  720. self.object_store.add_object(c)
  721. self.refs["HEAD"] = c.id
  722. return c.id
  723. class Repo(BaseRepo):
  724. """A git repository backed by local disk."""
  725. def __init__(self, root):
  726. if os.path.isdir(os.path.join(root, ".git", OBJECTDIR)):
  727. self.bare = False
  728. self._controldir = os.path.join(root, ".git")
  729. elif (os.path.isdir(os.path.join(root, OBJECTDIR)) and
  730. os.path.isdir(os.path.join(root, REFSDIR))):
  731. self.bare = True
  732. self._controldir = root
  733. else:
  734. raise NotGitRepository(root)
  735. self.path = root
  736. object_store = DiskObjectStore(
  737. os.path.join(self.controldir(), OBJECTDIR))
  738. refs = DiskRefsContainer(self.controldir())
  739. BaseRepo.__init__(self, object_store, refs)
  740. def controldir(self):
  741. """Return the path of the control directory."""
  742. return self._controldir
  743. def _put_named_file(self, path, contents):
  744. """Write a file from the control dir with a specific name and contents.
  745. """
  746. f = GitFile(os.path.join(self.controldir(), path), 'wb')
  747. try:
  748. f.write(contents)
  749. finally:
  750. f.close()
  751. def get_named_file(self, path):
  752. """Get a file from the control dir with a specific name.
  753. Although the filename should be interpreted as a filename relative to
  754. the control dir in a disk-baked Repo, the object returned need not be
  755. pointing to a file in that location.
  756. :param path: The path to the file, relative to the control dir.
  757. :return: An open file object, or None if the file does not exist.
  758. """
  759. try:
  760. return open(os.path.join(self.controldir(), path.lstrip('/')), 'rb')
  761. except (IOError, OSError), e:
  762. if e.errno == errno.ENOENT:
  763. return None
  764. raise
  765. def index_path(self):
  766. """Return path to the index file."""
  767. return os.path.join(self.controldir(), INDEX_FILENAME)
  768. def open_index(self):
  769. """Open the index for this repository."""
  770. from dulwich.index import Index
  771. if not self.has_index():
  772. raise NoIndexPresent()
  773. return Index(self.index_path())
  774. def has_index(self):
  775. """Check if an index is present."""
  776. return os.path.exists(self.index_path())
  777. def __repr__(self):
  778. return "<Repo at %r>" % self.path
  779. @classmethod
  780. def init(cls, path, mkdir=True):
  781. controldir = os.path.join(path, ".git")
  782. os.mkdir(controldir)
  783. cls.init_bare(controldir)
  784. return cls(path)
  785. @classmethod
  786. def init_bare(cls, path, mkdir=True):
  787. for d in BASE_DIRECTORIES:
  788. os.mkdir(os.path.join(path, *d))
  789. ret = cls(path)
  790. ret.refs.set_ref("HEAD", "refs/heads/master")
  791. ret._put_named_file('description', "Unnamed repository")
  792. ret._put_named_file('config', """[core]
  793. repositoryformatversion = 0
  794. filemode = true
  795. bare = false
  796. logallrefupdates = true
  797. """)
  798. ret._put_named_file(os.path.join('info', 'excludes'), '')
  799. return ret
  800. create = init_bare