repo.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. # repo.py -- For dealing with git repositories.
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. # Copyright (C) 2008-2013 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. This module contains the base class for git repositories
  22. (BaseRepo) and an implementation which uses a repository on
  23. local disk (Repo).
  24. """
  25. from cStringIO import StringIO
  26. import errno
  27. import os
  28. from dulwich.errors import (
  29. NoIndexPresent,
  30. NotBlobError,
  31. NotCommitError,
  32. NotGitRepository,
  33. NotTreeError,
  34. NotTagError,
  35. CommitError,
  36. RefFormatError,
  37. HookError,
  38. )
  39. from dulwich.file import (
  40. GitFile,
  41. )
  42. from dulwich.object_store import (
  43. DiskObjectStore,
  44. MemoryObjectStore,
  45. )
  46. from dulwich.objects import (
  47. Blob,
  48. Commit,
  49. ShaFile,
  50. Tag,
  51. Tree,
  52. )
  53. from dulwich.hooks import (
  54. PreCommitShellHook,
  55. PostCommitShellHook,
  56. CommitMsgShellHook,
  57. )
  58. from dulwich.refs import (
  59. check_ref_format,
  60. RefsContainer,
  61. DictRefsContainer,
  62. InfoRefsContainer,
  63. DiskRefsContainer,
  64. read_packed_refs,
  65. read_packed_refs_with_peeled,
  66. write_packed_refs,
  67. SYMREF,
  68. )
  69. import warnings
  70. OBJECTDIR = 'objects'
  71. REFSDIR = 'refs'
  72. REFSDIR_TAGS = 'tags'
  73. REFSDIR_HEADS = 'heads'
  74. INDEX_FILENAME = "index"
  75. BASE_DIRECTORIES = [
  76. ["branches"],
  77. [REFSDIR],
  78. [REFSDIR, REFSDIR_TAGS],
  79. [REFSDIR, REFSDIR_HEADS],
  80. ["hooks"],
  81. ["info"]
  82. ]
  83. class BaseRepo(object):
  84. """Base class for a git repository.
  85. :ivar object_store: Dictionary-like object for accessing
  86. the objects
  87. :ivar refs: Dictionary-like object with the refs in this
  88. repository
  89. """
  90. def __init__(self, object_store, refs):
  91. """Open a repository.
  92. This shouldn't be called directly, but rather through one of the
  93. base classes, such as MemoryRepo or Repo.
  94. :param object_store: Object store to use
  95. :param refs: Refs container to use
  96. """
  97. self.object_store = object_store
  98. self.refs = refs
  99. self.hooks = {}
  100. def _init_files(self, bare):
  101. """Initialize a default set of named files."""
  102. from dulwich.config import ConfigFile
  103. self._put_named_file('description', "Unnamed repository")
  104. f = StringIO()
  105. cf = ConfigFile()
  106. cf.set("core", "repositoryformatversion", "0")
  107. cf.set("core", "filemode", "true")
  108. cf.set("core", "bare", str(bare).lower())
  109. cf.set("core", "logallrefupdates", "true")
  110. cf.write_to_file(f)
  111. self._put_named_file('config', f.getvalue())
  112. self._put_named_file(os.path.join('info', 'exclude'), '')
  113. def get_named_file(self, path):
  114. """Get a file from the control dir with a specific name.
  115. Although the filename should be interpreted as a filename relative to
  116. the control dir in a disk-based Repo, the object returned need not be
  117. pointing to a file in that location.
  118. :param path: The path to the file, relative to the control dir.
  119. :return: An open file object, or None if the file does not exist.
  120. """
  121. raise NotImplementedError(self.get_named_file)
  122. def _put_named_file(self, path, contents):
  123. """Write a file to the control dir with the given name and contents.
  124. :param path: The path to the file, relative to the control dir.
  125. :param contents: A string to write to the file.
  126. """
  127. raise NotImplementedError(self._put_named_file)
  128. def open_index(self):
  129. """Open the index for this repository.
  130. :raise NoIndexPresent: If no index is present
  131. :return: The matching `Index`
  132. """
  133. raise NotImplementedError(self.open_index)
  134. def fetch(self, target, determine_wants=None, progress=None):
  135. """Fetch objects into another repository.
  136. :param target: The target repository
  137. :param determine_wants: Optional function to determine what refs to
  138. fetch.
  139. :param progress: Optional progress function
  140. :return: The local refs
  141. """
  142. if determine_wants is None:
  143. determine_wants = lambda heads: heads.values()
  144. target.object_store.add_objects(
  145. self.fetch_objects(determine_wants, target.get_graph_walker(),
  146. progress))
  147. return self.get_refs()
  148. def fetch_objects(self, determine_wants, graph_walker, progress,
  149. get_tagged=None):
  150. """Fetch the missing objects required for a set of revisions.
  151. :param determine_wants: Function that takes a dictionary with heads
  152. and returns the list of heads to fetch.
  153. :param graph_walker: Object that can iterate over the list of revisions
  154. to fetch and has an "ack" method that will be called to acknowledge
  155. that a revision is present.
  156. :param progress: Simple progress function that will be called with
  157. updated progress strings.
  158. :param get_tagged: Function that returns a dict of pointed-to sha -> tag
  159. sha for including tags.
  160. :return: iterator over objects, with __len__ implemented
  161. """
  162. wants = determine_wants(self.get_refs())
  163. if type(wants) is not list:
  164. raise TypeError("determine_wants() did not return a list")
  165. if wants == []:
  166. # TODO(dborowitz): find a way to short-circuit that doesn't change
  167. # this interface.
  168. return []
  169. haves = self.object_store.find_common_revisions(graph_walker)
  170. return self.object_store.iter_shas(
  171. self.object_store.find_missing_objects(haves, wants, progress,
  172. get_tagged))
  173. def get_graph_walker(self, heads=None):
  174. """Retrieve a graph walker.
  175. A graph walker is used by a remote repository (or proxy)
  176. to find out which objects are present in this repository.
  177. :param heads: Repository heads to use (optional)
  178. :return: A graph walker object
  179. """
  180. if heads is None:
  181. heads = self.refs.as_dict('refs/heads').values()
  182. return self.object_store.get_graph_walker(heads)
  183. def ref(self, name):
  184. """Return the SHA1 a ref is pointing to.
  185. :param name: Name of the ref to look up
  186. :raise KeyError: when the ref (or the one it points to) does not exist
  187. :return: SHA1 it is pointing at
  188. """
  189. return self.refs[name]
  190. def get_refs(self):
  191. """Get dictionary with all refs.
  192. :return: A ``dict`` mapping ref names to SHA1s
  193. """
  194. return self.refs.as_dict()
  195. def head(self):
  196. """Return the SHA1 pointed at by HEAD."""
  197. return self.refs['HEAD']
  198. def _get_object(self, sha, cls):
  199. assert len(sha) in (20, 40)
  200. ret = self.get_object(sha)
  201. if not isinstance(ret, cls):
  202. if cls is Commit:
  203. raise NotCommitError(ret)
  204. elif cls is Blob:
  205. raise NotBlobError(ret)
  206. elif cls is Tree:
  207. raise NotTreeError(ret)
  208. elif cls is Tag:
  209. raise NotTagError(ret)
  210. else:
  211. raise Exception("Type invalid: %r != %r" % (
  212. ret.type_name, cls.type_name))
  213. return ret
  214. def get_object(self, sha):
  215. """Retrieve the object with the specified SHA.
  216. :param sha: SHA to retrieve
  217. :return: A ShaFile object
  218. :raise KeyError: when the object can not be found
  219. """
  220. return self.object_store[sha]
  221. def get_parents(self, sha):
  222. """Retrieve the parents of a specific commit.
  223. :param sha: SHA of the commit for which to retrieve the parents
  224. :return: List of parents
  225. """
  226. return self.commit(sha).parents
  227. def get_config(self):
  228. """Retrieve the config object.
  229. :return: `ConfigFile` object for the ``.git/config`` file.
  230. """
  231. raise NotImplementedError(self.get_config)
  232. def get_description(self):
  233. """Retrieve the description for this repository.
  234. :return: String with the description of the repository
  235. as set by the user.
  236. """
  237. raise NotImplementedError(self.get_description)
  238. def set_description(self, description):
  239. """Set the description for this repository.
  240. :param description: Text to set as description for this repository.
  241. """
  242. raise NotImplementedError(self.set_description)
  243. def get_config_stack(self):
  244. """Return a config stack for this repository.
  245. This stack accesses the configuration for both this repository
  246. itself (.git/config) and the global configuration, which usually
  247. lives in ~/.gitconfig.
  248. :return: `Config` instance for this repository
  249. """
  250. from dulwich.config import StackedConfig
  251. backends = [self.get_config()] + StackedConfig.default_backends()
  252. return StackedConfig(backends, writable=backends[0])
  253. def commit(self, sha):
  254. """Retrieve the commit with a particular SHA.
  255. :param sha: SHA of the commit to retrieve
  256. :raise NotCommitError: If the SHA provided doesn't point at a Commit
  257. :raise KeyError: If the SHA provided didn't exist
  258. :return: A `Commit` object
  259. """
  260. warnings.warn("Repo.commit(sha) is deprecated. Use Repo[sha] instead.",
  261. category=DeprecationWarning, stacklevel=2)
  262. return self._get_object(sha, Commit)
  263. def tree(self, sha):
  264. """Retrieve the tree with a particular SHA.
  265. :param sha: SHA of the tree to retrieve
  266. :raise NotTreeError: If the SHA provided doesn't point at a Tree
  267. :raise KeyError: If the SHA provided didn't exist
  268. :return: A `Tree` object
  269. """
  270. warnings.warn("Repo.tree(sha) is deprecated. Use Repo[sha] instead.",
  271. category=DeprecationWarning, stacklevel=2)
  272. return self._get_object(sha, Tree)
  273. def tag(self, sha):
  274. """Retrieve the tag with a particular SHA.
  275. :param sha: SHA of the tag to retrieve
  276. :raise NotTagError: If the SHA provided doesn't point at a Tag
  277. :raise KeyError: If the SHA provided didn't exist
  278. :return: A `Tag` object
  279. """
  280. warnings.warn("Repo.tag(sha) is deprecated. Use Repo[sha] instead.",
  281. category=DeprecationWarning, stacklevel=2)
  282. return self._get_object(sha, Tag)
  283. def get_blob(self, sha):
  284. """Retrieve the blob with a particular SHA.
  285. :param sha: SHA of the blob to retrieve
  286. :raise NotBlobError: If the SHA provided doesn't point at a Blob
  287. :raise KeyError: If the SHA provided didn't exist
  288. :return: A `Blob` object
  289. """
  290. warnings.warn("Repo.get_blob(sha) is deprecated. Use Repo[sha] "
  291. "instead.", category=DeprecationWarning, stacklevel=2)
  292. return self._get_object(sha, Blob)
  293. def get_peeled(self, ref):
  294. """Get the peeled value of a ref.
  295. :param ref: The refname to peel.
  296. :return: The fully-peeled SHA1 of a tag object, after peeling all
  297. intermediate tags; if the original ref does not point to a tag, this
  298. will equal the original SHA1.
  299. """
  300. cached = self.refs.get_peeled(ref)
  301. if cached is not None:
  302. return cached
  303. return self.object_store.peel_sha(self.refs[ref]).id
  304. def get_walker(self, include=None, *args, **kwargs):
  305. """Obtain a walker for this repository.
  306. :param include: Iterable of SHAs of commits to include along with their
  307. ancestors. Defaults to [HEAD]
  308. :param exclude: Iterable of SHAs of commits to exclude along with their
  309. ancestors, overriding includes.
  310. :param order: ORDER_* constant specifying the order of results. Anything
  311. other than ORDER_DATE may result in O(n) memory usage.
  312. :param reverse: If True, reverse the order of output, requiring O(n)
  313. memory.
  314. :param max_entries: The maximum number of entries to yield, or None for
  315. no limit.
  316. :param paths: Iterable of file or subtree paths to show entries for.
  317. :param rename_detector: diff.RenameDetector object for detecting
  318. renames.
  319. :param follow: If True, follow path across renames/copies. Forces a
  320. default rename_detector.
  321. :param since: Timestamp to list commits after.
  322. :param until: Timestamp to list commits before.
  323. :param queue_cls: A class to use for a queue of commits, supporting the
  324. iterator protocol. The constructor takes a single argument, the
  325. Walker.
  326. :return: A `Walker` object
  327. """
  328. from dulwich.walk import Walker
  329. if include is None:
  330. include = [self.head()]
  331. if isinstance(include, str):
  332. include = [include]
  333. return Walker(self.object_store, include, *args, **kwargs)
  334. def revision_history(self, head):
  335. """Returns a list of the commits reachable from head.
  336. :param head: The SHA of the head to list revision history for.
  337. :return: A list of commit objects reachable from head, starting with
  338. head itself, in descending commit time order.
  339. :raise MissingCommitError: if any missing commits are referenced,
  340. including if the head parameter isn't the SHA of a commit.
  341. """
  342. warnings.warn("Repo.revision_history() is deprecated."
  343. "Use dulwich.walker.Walker(repo) instead.",
  344. category=DeprecationWarning, stacklevel=2)
  345. return [e.commit for e in self.get_walker(include=[head])]
  346. def __getitem__(self, name):
  347. """Retrieve a Git object by SHA1 or ref.
  348. :param name: A Git object SHA1 or a ref name
  349. :return: A `ShaFile` object, such as a Commit or Blob
  350. :raise KeyError: when the specified ref or object does not exist
  351. """
  352. if len(name) in (20, 40):
  353. try:
  354. return self.object_store[name]
  355. except (KeyError, ValueError):
  356. pass
  357. try:
  358. return self.object_store[self.refs[name]]
  359. except RefFormatError:
  360. raise KeyError(name)
  361. def __contains__(self, name):
  362. """Check if a specific Git object or ref is present.
  363. :param name: Git object SHA1 or ref name
  364. """
  365. if len(name) in (20, 40):
  366. return name in self.object_store or name in self.refs
  367. else:
  368. return name in self.refs
  369. def __setitem__(self, name, value):
  370. """Set a ref.
  371. :param name: ref name
  372. :param value: Ref value - either a ShaFile object, or a hex sha
  373. """
  374. if name.startswith("refs/") or name == "HEAD":
  375. if isinstance(value, ShaFile):
  376. self.refs[name] = value.id
  377. elif isinstance(value, str):
  378. self.refs[name] = value
  379. else:
  380. raise TypeError(value)
  381. else:
  382. raise ValueError(name)
  383. def __delitem__(self, name):
  384. """Remove a ref.
  385. :param name: Name of the ref to remove
  386. """
  387. if name.startswith("refs/") or name == "HEAD":
  388. del self.refs[name]
  389. else:
  390. raise ValueError(name)
  391. def _get_user_identity(self):
  392. """Determine the identity to use for new commits.
  393. """
  394. config = self.get_config_stack()
  395. return "%s <%s>" % (
  396. config.get(("user", ), "name"),
  397. config.get(("user", ), "email"))
  398. def do_commit(self, message=None, committer=None,
  399. author=None, commit_timestamp=None,
  400. commit_timezone=None, author_timestamp=None,
  401. author_timezone=None, tree=None, encoding=None,
  402. ref='HEAD', merge_heads=None):
  403. """Create a new commit.
  404. :param message: Commit message
  405. :param committer: Committer fullname
  406. :param author: Author fullname (defaults to committer)
  407. :param commit_timestamp: Commit timestamp (defaults to now)
  408. :param commit_timezone: Commit timestamp timezone (defaults to GMT)
  409. :param author_timestamp: Author timestamp (defaults to commit timestamp)
  410. :param author_timezone: Author timestamp timezone
  411. (defaults to commit timestamp timezone)
  412. :param tree: SHA1 of the tree root to use (if not specified the
  413. current index will be committed).
  414. :param encoding: Encoding
  415. :param ref: Optional ref to commit to (defaults to current branch)
  416. :param merge_heads: Merge heads (defaults to .git/MERGE_HEADS)
  417. :return: New commit SHA1
  418. """
  419. import time
  420. c = Commit()
  421. if tree is None:
  422. index = self.open_index()
  423. c.tree = index.commit(self.object_store)
  424. else:
  425. if len(tree) != 40:
  426. raise ValueError("tree must be a 40-byte hex sha string")
  427. c.tree = tree
  428. try:
  429. self.hooks['pre-commit'].execute()
  430. except HookError, e:
  431. raise CommitError(e)
  432. except KeyError: # no hook defined, silent fallthrough
  433. pass
  434. if merge_heads is None:
  435. # FIXME: Read merge heads from .git/MERGE_HEADS
  436. merge_heads = []
  437. if committer is None:
  438. # FIXME: Support GIT_COMMITTER_NAME/GIT_COMMITTER_EMAIL environment
  439. # variables
  440. committer = self._get_user_identity()
  441. c.committer = committer
  442. if commit_timestamp is None:
  443. # FIXME: Support GIT_COMMITTER_DATE environment variable
  444. commit_timestamp = time.time()
  445. c.commit_time = int(commit_timestamp)
  446. if commit_timezone is None:
  447. # FIXME: Use current user timezone rather than UTC
  448. commit_timezone = 0
  449. c.commit_timezone = commit_timezone
  450. if author is None:
  451. # FIXME: Support GIT_AUTHOR_NAME/GIT_AUTHOR_EMAIL environment
  452. # variables
  453. author = committer
  454. c.author = author
  455. if author_timestamp is None:
  456. # FIXME: Support GIT_AUTHOR_DATE environment variable
  457. author_timestamp = commit_timestamp
  458. c.author_time = int(author_timestamp)
  459. if author_timezone is None:
  460. author_timezone = commit_timezone
  461. c.author_timezone = author_timezone
  462. if encoding is not None:
  463. c.encoding = encoding
  464. if message is None:
  465. # FIXME: Try to read commit message from .git/MERGE_MSG
  466. raise ValueError("No commit message specified")
  467. try:
  468. c.message = self.hooks['commit-msg'].execute(message)
  469. if c.message is None:
  470. c.message = message
  471. except HookError, e:
  472. raise CommitError(e)
  473. except KeyError: # no hook defined, message not modified
  474. c.message = message
  475. try:
  476. old_head = self.refs[ref]
  477. c.parents = [old_head] + merge_heads
  478. self.object_store.add_object(c)
  479. ok = self.refs.set_if_equals(ref, old_head, c.id)
  480. except KeyError:
  481. c.parents = merge_heads
  482. self.object_store.add_object(c)
  483. ok = self.refs.add_if_new(ref, c.id)
  484. if not ok:
  485. # Fail if the atomic compare-and-swap failed, leaving the commit and
  486. # all its objects as garbage.
  487. raise CommitError("%s changed during commit" % (ref,))
  488. try:
  489. self.hooks['post-commit'].execute()
  490. except HookError, e: # silent failure
  491. warnings.warn("post-commit hook failed: %s" % e, UserWarning)
  492. except KeyError: # no hook defined, silent fallthrough
  493. pass
  494. return c.id
  495. class Repo(BaseRepo):
  496. """A git repository backed by local disk.
  497. To open an existing repository, call the contructor with
  498. the path of the repository.
  499. To create a new repository, use the Repo.init class method.
  500. """
  501. def __init__(self, root):
  502. if os.path.isdir(os.path.join(root, ".git", OBJECTDIR)):
  503. self.bare = False
  504. self._controldir = os.path.join(root, ".git")
  505. elif (os.path.isdir(os.path.join(root, OBJECTDIR)) and
  506. os.path.isdir(os.path.join(root, REFSDIR))):
  507. self.bare = True
  508. self._controldir = root
  509. elif (os.path.isfile(os.path.join(root, ".git"))):
  510. import re
  511. f = open(os.path.join(root, ".git"), 'r')
  512. try:
  513. _, path = re.match('(gitdir: )(.+$)', f.read()).groups()
  514. finally:
  515. f.close()
  516. self.bare = False
  517. self._controldir = os.path.join(root, path)
  518. else:
  519. raise NotGitRepository(
  520. "No git repository was found at %(path)s" % dict(path=root)
  521. )
  522. self.path = root
  523. object_store = DiskObjectStore(os.path.join(self.controldir(),
  524. OBJECTDIR))
  525. refs = DiskRefsContainer(self.controldir())
  526. BaseRepo.__init__(self, object_store, refs)
  527. self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())
  528. self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
  529. self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
  530. def controldir(self):
  531. """Return the path of the control directory."""
  532. return self._controldir
  533. def _put_named_file(self, path, contents):
  534. """Write a file to the control dir with the given name and contents.
  535. :param path: The path to the file, relative to the control dir.
  536. :param contents: A string to write to the file.
  537. """
  538. path = path.lstrip(os.path.sep)
  539. f = GitFile(os.path.join(self.controldir(), path), 'wb')
  540. try:
  541. f.write(contents)
  542. finally:
  543. f.close()
  544. def get_named_file(self, path):
  545. """Get a file from the control dir with a specific name.
  546. Although the filename should be interpreted as a filename relative to
  547. the control dir in a disk-based Repo, the object returned need not be
  548. pointing to a file in that location.
  549. :param path: The path to the file, relative to the control dir.
  550. :return: An open file object, or None if the file does not exist.
  551. """
  552. # TODO(dborowitz): sanitize filenames, since this is used directly by
  553. # the dumb web serving code.
  554. path = path.lstrip(os.path.sep)
  555. try:
  556. return open(os.path.join(self.controldir(), path), 'rb')
  557. except (IOError, OSError), e:
  558. if e.errno == errno.ENOENT:
  559. return None
  560. raise
  561. def index_path(self):
  562. """Return path to the index file."""
  563. return os.path.join(self.controldir(), INDEX_FILENAME)
  564. def open_index(self):
  565. """Open the index for this repository.
  566. :raise NoIndexPresent: If no index is present
  567. :return: The matching `Index`
  568. """
  569. from dulwich.index import Index
  570. if not self.has_index():
  571. raise NoIndexPresent()
  572. return Index(self.index_path())
  573. def has_index(self):
  574. """Check if an index is present."""
  575. # Bare repos must never have index files; non-bare repos may have a
  576. # missing index file, which is treated as empty.
  577. return not self.bare
  578. def stage(self, paths):
  579. """Stage a set of paths.
  580. :param paths: List of paths, relative to the repository path
  581. """
  582. if isinstance(paths, basestring):
  583. paths = [paths]
  584. from dulwich.index import index_entry_from_stat
  585. index = self.open_index()
  586. for path in paths:
  587. full_path = os.path.join(self.path, path)
  588. try:
  589. st = os.stat(full_path)
  590. except OSError:
  591. # File no longer exists
  592. try:
  593. del index[path]
  594. except KeyError:
  595. pass # already removed
  596. else:
  597. blob = Blob()
  598. f = open(full_path, 'rb')
  599. try:
  600. blob.data = f.read()
  601. finally:
  602. f.close()
  603. self.object_store.add_object(blob)
  604. index[path] = index_entry_from_stat(st, blob.id, 0)
  605. index.write()
  606. def clone(self, target_path, mkdir=True, bare=False,
  607. origin="origin"):
  608. """Clone this repository.
  609. :param target_path: Target path
  610. :param mkdir: Create the target directory
  611. :param bare: Whether to create a bare repository
  612. :param origin: Base name for refs in target repository
  613. cloned from this repository
  614. :return: Created repository as `Repo`
  615. """
  616. if not bare:
  617. target = self.init(target_path, mkdir=mkdir)
  618. else:
  619. target = self.init_bare(target_path)
  620. self.fetch(target)
  621. target.refs.import_refs(
  622. 'refs/remotes/' + origin, self.refs.as_dict('refs/heads'))
  623. target.refs.import_refs(
  624. 'refs/tags', self.refs.as_dict('refs/tags'))
  625. try:
  626. target.refs.add_if_new(
  627. 'refs/heads/master',
  628. self.refs['refs/heads/master'])
  629. except KeyError:
  630. pass
  631. # Update target head
  632. head, head_sha = self.refs._follow('HEAD')
  633. if head is not None and head_sha is not None:
  634. target.refs.set_symbolic_ref('HEAD', head)
  635. target['HEAD'] = head_sha
  636. if not bare:
  637. # Checkout HEAD to target dir
  638. target._build_tree()
  639. return target
  640. def _build_tree(self):
  641. from dulwich.index import build_index_from_tree
  642. config = self.get_config()
  643. honor_filemode = config.get_boolean('core', 'filemode', os.name != "nt")
  644. return build_index_from_tree(self.path, self.index_path(),
  645. self.object_store, self['HEAD'].tree,
  646. honor_filemode=honor_filemode)
  647. def get_config(self):
  648. """Retrieve the config object.
  649. :return: `ConfigFile` object for the ``.git/config`` file.
  650. """
  651. from dulwich.config import ConfigFile
  652. path = os.path.join(self._controldir, 'config')
  653. try:
  654. return ConfigFile.from_path(path)
  655. except (IOError, OSError), e:
  656. if e.errno != errno.ENOENT:
  657. raise
  658. ret = ConfigFile()
  659. ret.path = path
  660. return ret
  661. def get_description(self):
  662. """Retrieve the description of this repository.
  663. :return: A string describing the repository or None.
  664. """
  665. path = os.path.join(self._controldir, 'description')
  666. try:
  667. f = GitFile(path, 'rb')
  668. try:
  669. return f.read()
  670. finally:
  671. f.close()
  672. except (IOError, OSError), e:
  673. if e.errno != errno.ENOENT:
  674. raise
  675. return None
  676. def __repr__(self):
  677. return "<Repo at %r>" % self.path
  678. def set_description(self, description):
  679. """Set the description for this repository.
  680. :param description: Text to set as description for this repository.
  681. """
  682. path = os.path.join(self._controldir, 'description')
  683. f = open(path, 'w')
  684. try:
  685. f.write(description)
  686. finally:
  687. f.close()
  688. @classmethod
  689. def _init_maybe_bare(cls, path, bare):
  690. for d in BASE_DIRECTORIES:
  691. os.mkdir(os.path.join(path, *d))
  692. DiskObjectStore.init(os.path.join(path, OBJECTDIR))
  693. ret = cls(path)
  694. ret.refs.set_symbolic_ref("HEAD", "refs/heads/master")
  695. ret._init_files(bare)
  696. return ret
  697. @classmethod
  698. def init(cls, path, mkdir=False):
  699. """Create a new repository.
  700. :param path: Path in which to create the repository
  701. :param mkdir: Whether to create the directory
  702. :return: `Repo` instance
  703. """
  704. if mkdir:
  705. os.mkdir(path)
  706. controldir = os.path.join(path, ".git")
  707. os.mkdir(controldir)
  708. cls._init_maybe_bare(controldir, False)
  709. return cls(path)
  710. @classmethod
  711. def init_bare(cls, path):
  712. """Create a new bare repository.
  713. ``path`` should already exist and be an emty directory.
  714. :param path: Path to create bare repository in
  715. :return: a `Repo` instance
  716. """
  717. return cls._init_maybe_bare(path, True)
  718. create = init_bare
  719. class MemoryRepo(BaseRepo):
  720. """Repo that stores refs, objects, and named files in memory.
  721. MemoryRepos are always bare: they have no working tree and no index, since
  722. those have a stronger dependency on the filesystem.
  723. """
  724. def __init__(self):
  725. BaseRepo.__init__(self, MemoryObjectStore(), DictRefsContainer({}))
  726. self._named_files = {}
  727. self.bare = True
  728. def _put_named_file(self, path, contents):
  729. """Write a file to the control dir with the given name and contents.
  730. :param path: The path to the file, relative to the control dir.
  731. :param contents: A string to write to the file.
  732. """
  733. self._named_files[path] = contents
  734. def get_named_file(self, path):
  735. """Get a file from the control dir with a specific name.
  736. Although the filename should be interpreted as a filename relative to
  737. the control dir in a disk-baked Repo, the object returned need not be
  738. pointing to a file in that location.
  739. :param path: The path to the file, relative to the control dir.
  740. :return: An open file object, or None if the file does not exist.
  741. """
  742. contents = self._named_files.get(path, None)
  743. if contents is None:
  744. return None
  745. return StringIO(contents)
  746. def open_index(self):
  747. """Fail to open index for this repo, since it is bare.
  748. :raise NoIndexPresent: Raised when no index is present
  749. """
  750. raise NoIndexPresent()
  751. def get_config(self):
  752. """Retrieve the config object.
  753. :return: `ConfigFile` object.
  754. """
  755. from dulwich.config import ConfigFile
  756. return ConfigFile()
  757. def get_description(self):
  758. """Retrieve the repository description.
  759. This defaults to None, for no description.
  760. """
  761. return None
  762. @classmethod
  763. def init_bare(cls, objects, refs):
  764. """Create a new bare repository in memory.
  765. :param objects: Objects for the new repository,
  766. as iterable
  767. :param refs: Refs as dictionary, mapping names
  768. to object SHA1s
  769. """
  770. ret = cls()
  771. for obj in objects:
  772. ret.object_store.add_object(obj)
  773. for refname, sha in refs.iteritems():
  774. ret.refs[refname] = sha
  775. ret._init_files(bare=True)
  776. return ret