object_store.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. # object_store.py -- Object store for git objects
  2. # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # or (at your option) a later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Git object store interfaces and implementation."""
  19. import errno
  20. import itertools
  21. import os
  22. import stat
  23. import tempfile
  24. import urllib2
  25. from dulwich.diff_tree import (
  26. tree_changes,
  27. walk_trees,
  28. )
  29. from dulwich.errors import (
  30. NotTreeError,
  31. )
  32. from dulwich.file import GitFile
  33. from dulwich.objects import (
  34. Commit,
  35. ShaFile,
  36. Tag,
  37. Tree,
  38. ZERO_SHA,
  39. hex_to_sha,
  40. sha_to_hex,
  41. hex_to_filename,
  42. S_ISGITLINK,
  43. object_class,
  44. )
  45. from dulwich.pack import (
  46. Pack,
  47. PackData,
  48. ThinPackData,
  49. iter_sha1,
  50. load_pack_index,
  51. write_pack,
  52. write_pack_index_v2,
  53. write_pack_objects,
  54. )
  55. INFODIR = 'info'
  56. PACKDIR = 'pack'
  57. class BaseObjectStore(object):
  58. """Object store interface."""
  59. def determine_wants_all(self, refs):
  60. return [sha for (ref, sha) in refs.iteritems()
  61. if not sha in self and not ref.endswith("^{}") and
  62. not sha == ZERO_SHA]
  63. def iter_shas(self, shas):
  64. """Iterate over the objects for the specified shas.
  65. :param shas: Iterable object with SHAs
  66. :return: Object iterator
  67. """
  68. return ObjectStoreIterator(self, shas)
  69. def contains_loose(self, sha):
  70. """Check if a particular object is present by SHA1 and is loose."""
  71. raise NotImplementedError(self.contains_loose)
  72. def contains_packed(self, sha):
  73. """Check if a particular object is present by SHA1 and is packed."""
  74. raise NotImplementedError(self.contains_packed)
  75. def __contains__(self, sha):
  76. """Check if a particular object is present by SHA1.
  77. This method makes no distinction between loose and packed objects.
  78. """
  79. return self.contains_packed(sha) or self.contains_loose(sha)
  80. @property
  81. def packs(self):
  82. """Iterable of pack objects."""
  83. raise NotImplementedError
  84. def get_raw(self, name):
  85. """Obtain the raw text for an object.
  86. :param name: sha for the object.
  87. :return: tuple with numeric type and object contents.
  88. """
  89. raise NotImplementedError(self.get_raw)
  90. def __getitem__(self, sha):
  91. """Obtain an object by SHA1."""
  92. type_num, uncomp = self.get_raw(sha)
  93. return ShaFile.from_raw_string(type_num, uncomp)
  94. def __iter__(self):
  95. """Iterate over the SHAs that are present in this store."""
  96. raise NotImplementedError(self.__iter__)
  97. def add_object(self, obj):
  98. """Add a single object to this object store.
  99. """
  100. raise NotImplementedError(self.add_object)
  101. def add_objects(self, objects):
  102. """Add a set of objects to this object store.
  103. :param objects: Iterable over a list of objects.
  104. """
  105. raise NotImplementedError(self.add_objects)
  106. def tree_changes(self, source, target, want_unchanged=False):
  107. """Find the differences between the contents of two trees
  108. :param object_store: Object store to use for retrieving tree contents
  109. :param tree: SHA1 of the root tree
  110. :param want_unchanged: Whether unchanged files should be reported
  111. :return: Iterator over tuples with
  112. (oldpath, newpath), (oldmode, newmode), (oldsha, newsha)
  113. """
  114. for change in tree_changes(self, source, target,
  115. want_unchanged=want_unchanged):
  116. yield ((change.old.path, change.new.path),
  117. (change.old.mode, change.new.mode),
  118. (change.old.sha, change.new.sha))
  119. def iter_tree_contents(self, tree_id, include_trees=False):
  120. """Iterate the contents of a tree and all subtrees.
  121. Iteration is depth-first pre-order, as in e.g. os.walk.
  122. :param tree_id: SHA1 of the tree.
  123. :param include_trees: If True, include tree objects in the iteration.
  124. :return: Iterator over TreeEntry namedtuples for all the objects in a
  125. tree.
  126. """
  127. for entry, _ in walk_trees(self, tree_id, None):
  128. if not stat.S_ISDIR(entry.mode) or include_trees:
  129. yield entry
  130. def find_missing_objects(self, haves, wants, progress=None,
  131. get_tagged=None):
  132. """Find the missing objects required for a set of revisions.
  133. :param haves: Iterable over SHAs already in common.
  134. :param wants: Iterable over SHAs of objects to fetch.
  135. :param progress: Simple progress function that will be called with
  136. updated progress strings.
  137. :param get_tagged: Function that returns a dict of pointed-to sha -> tag
  138. sha for including tags.
  139. :return: Iterator over (sha, path) pairs.
  140. """
  141. finder = MissingObjectFinder(self, haves, wants, progress, get_tagged)
  142. return iter(finder.next, None)
  143. def find_common_revisions(self, graphwalker):
  144. """Find which revisions this store has in common using graphwalker.
  145. :param graphwalker: A graphwalker object.
  146. :return: List of SHAs that are in common
  147. """
  148. haves = []
  149. sha = graphwalker.next()
  150. while sha:
  151. if sha in self:
  152. haves.append(sha)
  153. graphwalker.ack(sha)
  154. sha = graphwalker.next()
  155. return haves
  156. def get_graph_walker(self, heads):
  157. """Obtain a graph walker for this object store.
  158. :param heads: Local heads to start search with
  159. :return: GraphWalker object
  160. """
  161. return ObjectStoreGraphWalker(heads, lambda sha: self[sha].parents)
  162. def generate_pack_contents(self, have, want, progress=None):
  163. """Iterate over the contents of a pack file.
  164. :param have: List of SHA1s of objects that should not be sent
  165. :param want: List of SHA1s of objects that should be sent
  166. :param progress: Optional progress reporting method
  167. """
  168. return self.iter_shas(self.find_missing_objects(have, want, progress))
  169. def peel_sha(self, sha):
  170. """Peel all tags from a SHA.
  171. :param sha: The object SHA to peel.
  172. :return: The fully-peeled SHA1 of a tag object, after peeling all
  173. intermediate tags; if the original ref does not point to a tag, this
  174. will equal the original SHA1.
  175. """
  176. obj = self[sha]
  177. obj_class = object_class(obj.type_name)
  178. while obj_class is Tag:
  179. obj_class, sha = obj.object
  180. obj = self[sha]
  181. return obj
  182. class PackBasedObjectStore(BaseObjectStore):
  183. def __init__(self):
  184. self._pack_cache = None
  185. def contains_packed(self, sha):
  186. """Check if a particular object is present by SHA1 and is packed."""
  187. for pack in self.packs:
  188. if sha in pack:
  189. return True
  190. return False
  191. def _load_packs(self):
  192. raise NotImplementedError(self._load_packs)
  193. def _pack_cache_stale(self):
  194. """Check whether the pack cache is stale."""
  195. raise NotImplementedError(self._pack_cache_stale)
  196. def _add_known_pack(self, pack):
  197. """Add a newly appeared pack to the cache by path.
  198. """
  199. if self._pack_cache is not None:
  200. self._pack_cache.append(pack)
  201. @property
  202. def packs(self):
  203. """List with pack objects."""
  204. if self._pack_cache is None or self._pack_cache_stale():
  205. self._pack_cache = self._load_packs()
  206. return self._pack_cache
  207. def _iter_loose_objects(self):
  208. """Iterate over the SHAs of all loose objects."""
  209. raise NotImplementedError(self._iter_loose_objects)
  210. def _get_loose_object(self, sha):
  211. raise NotImplementedError(self._get_loose_object)
  212. def _remove_loose_object(self, sha):
  213. raise NotImplementedError(self._remove_loose_object)
  214. def pack_loose_objects(self):
  215. """Pack loose objects.
  216. :return: Number of objects packed
  217. """
  218. objects = set()
  219. for sha in self._iter_loose_objects():
  220. objects.add((self._get_loose_object(sha), None))
  221. self.add_objects(list(objects))
  222. for obj, path in objects:
  223. self._remove_loose_object(obj.id)
  224. return len(objects)
  225. def __iter__(self):
  226. """Iterate over the SHAs that are present in this store."""
  227. iterables = self.packs + [self._iter_loose_objects()]
  228. return itertools.chain(*iterables)
  229. def contains_loose(self, sha):
  230. """Check if a particular object is present by SHA1 and is loose."""
  231. return self._get_loose_object(sha) is not None
  232. def get_raw(self, name):
  233. """Obtain the raw text for an object.
  234. :param name: sha for the object.
  235. :return: tuple with numeric type and object contents.
  236. """
  237. if len(name) == 40:
  238. sha = hex_to_sha(name)
  239. hexsha = name
  240. elif len(name) == 20:
  241. sha = name
  242. hexsha = None
  243. else:
  244. raise AssertionError("Invalid object name %r" % name)
  245. for pack in self.packs:
  246. try:
  247. return pack.get_raw(sha)
  248. except KeyError:
  249. pass
  250. if hexsha is None:
  251. hexsha = sha_to_hex(name)
  252. ret = self._get_loose_object(hexsha)
  253. if ret is not None:
  254. return ret.type_num, ret.as_raw_string()
  255. raise KeyError(hexsha)
  256. def add_objects(self, objects):
  257. """Add a set of objects to this object store.
  258. :param objects: Iterable over objects, should support __len__.
  259. :return: Pack object of the objects written.
  260. """
  261. if len(objects) == 0:
  262. # Don't bother writing an empty pack file
  263. return
  264. f, commit = self.add_pack()
  265. write_pack_objects(f, objects)
  266. return commit()
  267. class DiskObjectStore(PackBasedObjectStore):
  268. """Git-style object store that exists on disk."""
  269. def __init__(self, path):
  270. """Open an object store.
  271. :param path: Path of the object store.
  272. """
  273. super(DiskObjectStore, self).__init__()
  274. self.path = path
  275. self.pack_dir = os.path.join(self.path, PACKDIR)
  276. self._pack_cache_time = 0
  277. def _load_packs(self):
  278. pack_files = []
  279. try:
  280. self._pack_cache_time = os.stat(self.pack_dir).st_mtime
  281. pack_dir_contents = os.listdir(self.pack_dir)
  282. for name in pack_dir_contents:
  283. # TODO: verify that idx exists first
  284. if name.startswith("pack-") and name.endswith(".pack"):
  285. filename = os.path.join(self.pack_dir, name)
  286. pack_files.append((os.stat(filename).st_mtime, filename))
  287. except OSError, e:
  288. if e.errno == errno.ENOENT:
  289. return []
  290. raise
  291. pack_files.sort(reverse=True)
  292. suffix_len = len(".pack")
  293. return [Pack(f[:-suffix_len]) for _, f in pack_files]
  294. def _pack_cache_stale(self):
  295. try:
  296. return os.stat(self.pack_dir).st_mtime > self._pack_cache_time
  297. except OSError, e:
  298. if e.errno == errno.ENOENT:
  299. return True
  300. raise
  301. def _get_shafile_path(self, sha):
  302. # Check from object dir
  303. return hex_to_filename(self.path, sha)
  304. def _iter_loose_objects(self):
  305. for base in os.listdir(self.path):
  306. if len(base) != 2:
  307. continue
  308. for rest in os.listdir(os.path.join(self.path, base)):
  309. yield base+rest
  310. def _get_loose_object(self, sha):
  311. path = self._get_shafile_path(sha)
  312. try:
  313. return ShaFile.from_path(path)
  314. except (OSError, IOError), e:
  315. if e.errno == errno.ENOENT:
  316. return None
  317. raise
  318. def _remove_loose_object(self, sha):
  319. os.remove(self._get_shafile_path(sha))
  320. def move_in_thin_pack(self, path):
  321. """Move a specific file containing a pack into the pack directory.
  322. :note: The file should be on the same file system as the
  323. packs directory.
  324. :param path: Path to the pack file.
  325. """
  326. data = ThinPackData(self.get_raw, path)
  327. # Write index for the thin pack (do we really need this?)
  328. temppath = os.path.join(self.pack_dir,
  329. sha_to_hex(urllib2.randombytes(20))+".tempidx")
  330. data.create_index_v2(temppath)
  331. p = Pack.from_objects(data, load_pack_index(temppath))
  332. try:
  333. # Write a full pack version
  334. temppath = os.path.join(self.pack_dir,
  335. sha_to_hex(urllib2.randombytes(20))+".temppack")
  336. write_pack(temppath, p.pack_tuples())
  337. finally:
  338. p.close()
  339. pack_sha = load_pack_index(temppath+".idx").objects_sha1()
  340. newbasename = os.path.join(self.pack_dir, "pack-%s" % pack_sha)
  341. os.rename(temppath+".pack", newbasename+".pack")
  342. os.rename(temppath+".idx", newbasename+".idx")
  343. final_pack = Pack(newbasename)
  344. self._add_known_pack(final_pack)
  345. return final_pack
  346. def move_in_pack(self, path):
  347. """Move a specific file containing a pack into the pack directory.
  348. :note: The file should be on the same file system as the
  349. packs directory.
  350. :param path: Path to the pack file.
  351. """
  352. p = PackData(path)
  353. entries = p.sorted_entries()
  354. basename = os.path.join(self.pack_dir,
  355. "pack-%s" % iter_sha1(entry[0] for entry in entries))
  356. f = GitFile(basename+".idx", "wb")
  357. try:
  358. write_pack_index_v2(f, entries, p.get_stored_checksum())
  359. finally:
  360. f.close()
  361. p.close()
  362. os.rename(path, basename + ".pack")
  363. final_pack = Pack(basename)
  364. self._add_known_pack(final_pack)
  365. return final_pack
  366. def add_thin_pack(self):
  367. """Add a new thin pack to this object store.
  368. Thin packs are packs that contain deltas with parents that exist
  369. in a different pack.
  370. """
  371. fd, path = tempfile.mkstemp(dir=self.pack_dir, suffix=".pack")
  372. f = os.fdopen(fd, 'wb')
  373. def commit():
  374. os.fsync(fd)
  375. f.close()
  376. if os.path.getsize(path) > 0:
  377. return self.move_in_thin_pack(path)
  378. else:
  379. os.remove(path)
  380. return None
  381. return f, commit
  382. def add_pack(self):
  383. """Add a new pack to this object store.
  384. :return: Fileobject to write to and a commit function to
  385. call when the pack is finished.
  386. """
  387. fd, path = tempfile.mkstemp(dir=self.pack_dir, suffix=".pack")
  388. f = os.fdopen(fd, 'wb')
  389. def commit():
  390. os.fsync(fd)
  391. f.close()
  392. if os.path.getsize(path) > 0:
  393. return self.move_in_pack(path)
  394. else:
  395. os.remove(path)
  396. return None
  397. return f, commit
  398. def add_object(self, obj):
  399. """Add a single object to this object store.
  400. :param obj: Object to add
  401. """
  402. dir = os.path.join(self.path, obj.id[:2])
  403. try:
  404. os.mkdir(dir)
  405. except OSError, e:
  406. if e.errno != errno.EEXIST:
  407. raise
  408. path = os.path.join(dir, obj.id[2:])
  409. if os.path.exists(path):
  410. return # Already there, no need to write again
  411. f = GitFile(path, 'wb')
  412. try:
  413. f.write(obj.as_legacy_object())
  414. finally:
  415. f.close()
  416. @classmethod
  417. def init(cls, path):
  418. try:
  419. os.mkdir(path)
  420. except OSError, e:
  421. if e.errno != errno.EEXIST:
  422. raise
  423. os.mkdir(os.path.join(path, "info"))
  424. os.mkdir(os.path.join(path, PACKDIR))
  425. return cls(path)
  426. class MemoryObjectStore(BaseObjectStore):
  427. """Object store that keeps all objects in memory."""
  428. def __init__(self):
  429. super(MemoryObjectStore, self).__init__()
  430. self._data = {}
  431. def contains_loose(self, sha):
  432. """Check if a particular object is present by SHA1 and is loose."""
  433. return sha in self._data
  434. def contains_packed(self, sha):
  435. """Check if a particular object is present by SHA1 and is packed."""
  436. return False
  437. def __iter__(self):
  438. """Iterate over the SHAs that are present in this store."""
  439. return self._data.iterkeys()
  440. @property
  441. def packs(self):
  442. """List with pack objects."""
  443. return []
  444. def get_raw(self, name):
  445. """Obtain the raw text for an object.
  446. :param name: sha for the object.
  447. :return: tuple with numeric type and object contents.
  448. """
  449. return self[name].as_raw_string()
  450. def __getitem__(self, name):
  451. return self._data[name]
  452. def __delitem__(self, name):
  453. """Delete an object from this store, for testing only."""
  454. del self._data[name]
  455. def add_object(self, obj):
  456. """Add a single object to this object store.
  457. """
  458. self._data[obj.id] = obj
  459. def add_objects(self, objects):
  460. """Add a set of objects to this object store.
  461. :param objects: Iterable over a list of objects.
  462. """
  463. for obj, path in objects:
  464. self._data[obj.id] = obj
  465. class ObjectImporter(object):
  466. """Interface for importing objects."""
  467. def __init__(self, count):
  468. """Create a new ObjectImporter.
  469. :param count: Number of objects that's going to be imported.
  470. """
  471. self.count = count
  472. def add_object(self, object):
  473. """Add an object."""
  474. raise NotImplementedError(self.add_object)
  475. def finish(self, object):
  476. """Finish the import and write objects to disk."""
  477. raise NotImplementedError(self.finish)
  478. class ObjectIterator(object):
  479. """Interface for iterating over objects."""
  480. def iterobjects(self):
  481. raise NotImplementedError(self.iterobjects)
  482. class ObjectStoreIterator(ObjectIterator):
  483. """ObjectIterator that works on top of an ObjectStore."""
  484. def __init__(self, store, sha_iter):
  485. """Create a new ObjectIterator.
  486. :param store: Object store to retrieve from
  487. :param sha_iter: Iterator over (sha, path) tuples
  488. """
  489. self.store = store
  490. self.sha_iter = sha_iter
  491. self._shas = []
  492. def __iter__(self):
  493. """Yield tuple with next object and path."""
  494. for sha, path in self.itershas():
  495. yield self.store[sha], path
  496. def iterobjects(self):
  497. """Iterate over just the objects."""
  498. for o, path in self:
  499. yield o
  500. def itershas(self):
  501. """Iterate over the SHAs."""
  502. for sha in self._shas:
  503. yield sha
  504. for sha in self.sha_iter:
  505. self._shas.append(sha)
  506. yield sha
  507. def __contains__(self, needle):
  508. """Check if an object is present.
  509. :note: This checks if the object is present in
  510. the underlying object store, not if it would
  511. be yielded by the iterator.
  512. :param needle: SHA1 of the object to check for
  513. """
  514. return needle in self.store
  515. def __getitem__(self, key):
  516. """Find an object by SHA1.
  517. :note: This retrieves the object from the underlying
  518. object store. It will also succeed if the object would
  519. not be returned by the iterator.
  520. """
  521. return self.store[key]
  522. def __len__(self):
  523. """Return the number of objects."""
  524. return len(list(self.itershas()))
  525. def tree_lookup_path(lookup_obj, root_sha, path):
  526. """Lookup an object in a Git tree.
  527. :param lookup_obj: Callback for retrieving object by SHA1
  528. :param root_sha: SHA1 of the root tree
  529. :param path: Path to lookup
  530. """
  531. parts = path.split("/")
  532. sha = root_sha
  533. mode = None
  534. for p in parts:
  535. obj = lookup_obj(sha)
  536. if not isinstance(obj, Tree):
  537. raise NotTreeError(sha)
  538. if p == '':
  539. continue
  540. mode, sha = obj[p]
  541. return mode, sha
  542. class MissingObjectFinder(object):
  543. """Find the objects missing from another object store.
  544. :param object_store: Object store containing at least all objects to be
  545. sent
  546. :param haves: SHA1s of commits not to send (already present in target)
  547. :param wants: SHA1s of commits to send
  548. :param progress: Optional function to report progress to.
  549. :param get_tagged: Function that returns a dict of pointed-to sha -> tag
  550. sha for including tags.
  551. :param tagged: dict of pointed-to sha -> tag sha for including tags
  552. """
  553. def __init__(self, object_store, haves, wants, progress=None,
  554. get_tagged=None):
  555. haves = set(haves)
  556. self.sha_done = haves
  557. self.objects_to_send = set([(w, None, False) for w in wants
  558. if w not in haves])
  559. self.object_store = object_store
  560. if progress is None:
  561. self.progress = lambda x: None
  562. else:
  563. self.progress = progress
  564. self._tagged = get_tagged and get_tagged() or {}
  565. def add_todo(self, entries):
  566. self.objects_to_send.update([e for e in entries
  567. if not e[0] in self.sha_done])
  568. def parse_tree(self, tree):
  569. self.add_todo([(sha, name, not stat.S_ISDIR(mode))
  570. for name, mode, sha in tree.iteritems()
  571. if not S_ISGITLINK(mode)])
  572. def parse_commit(self, commit):
  573. self.add_todo([(commit.tree, "", False)])
  574. self.add_todo([(p, None, False) for p in commit.parents])
  575. def parse_tag(self, tag):
  576. self.add_todo([(tag.object[1], None, False)])
  577. def next(self):
  578. if not self.objects_to_send:
  579. return None
  580. (sha, name, leaf) = self.objects_to_send.pop()
  581. if not leaf:
  582. o = self.object_store[sha]
  583. if isinstance(o, Commit):
  584. self.parse_commit(o)
  585. elif isinstance(o, Tree):
  586. self.parse_tree(o)
  587. elif isinstance(o, Tag):
  588. self.parse_tag(o)
  589. if sha in self._tagged:
  590. self.add_todo([(self._tagged[sha], None, True)])
  591. self.sha_done.add(sha)
  592. self.progress("counting objects: %d\r" % len(self.sha_done))
  593. return (sha, name)
  594. class ObjectStoreGraphWalker(object):
  595. """Graph walker that finds what commits are missing from an object store.
  596. :ivar heads: Revisions without descendants in the local repo
  597. :ivar get_parents: Function to retrieve parents in the local repo
  598. """
  599. def __init__(self, local_heads, get_parents):
  600. """Create a new instance.
  601. :param local_heads: Heads to start search with
  602. :param get_parents: Function for finding the parents of a SHA1.
  603. """
  604. self.heads = set(local_heads)
  605. self.get_parents = get_parents
  606. self.parents = {}
  607. def ack(self, sha):
  608. """Ack that a revision and its ancestors are present in the source."""
  609. ancestors = set([sha])
  610. # stop if we run out of heads to remove
  611. while self.heads:
  612. for a in ancestors:
  613. if a in self.heads:
  614. self.heads.remove(a)
  615. # collect all ancestors
  616. new_ancestors = set()
  617. for a in ancestors:
  618. if a in self.parents:
  619. new_ancestors.update(self.parents[a])
  620. # no more ancestors; stop
  621. if not new_ancestors:
  622. break
  623. ancestors = new_ancestors
  624. def next(self):
  625. """Iterate over ancestors of heads in the target."""
  626. if self.heads:
  627. ret = self.heads.pop()
  628. ps = self.get_parents(ret)
  629. self.parents[ret] = ps
  630. self.heads.update(ps)
  631. return ret
  632. return None