repo.py 34 KB

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