repo.py 32 KB

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