walk.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. # walk.py -- General implementation of walking commits and their contents.
  2. # Copyright (C) 2010 Google, Inc.
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """General implementation of walking commits and their contents."""
  21. from collections import defaultdict
  22. import collections
  23. import heapq
  24. from itertools import chain
  25. from dulwich.diff_tree import (
  26. RENAME_CHANGE_TYPES,
  27. tree_changes,
  28. tree_changes_for_merge,
  29. RenameDetector,
  30. )
  31. from dulwich.objects import (
  32. Tag,
  33. )
  34. ORDER_DATE = 'date'
  35. ORDER_TOPO = 'topo'
  36. ALL_ORDERS = (ORDER_DATE, ORDER_TOPO)
  37. # Maximum number of commits to walk past a commit time boundary.
  38. _MAX_EXTRA_COMMITS = 5
  39. class MissingCommitError(Exception):
  40. """Indicates that a commit was not found in the repository"""
  41. def __init__(self, sha, *args, **kwargs):
  42. self.sha = sha
  43. Exception.__init__(self, "%s is not in the revision store" % sha)
  44. class WalkEntry(object):
  45. """Object encapsulating a single result from a walk."""
  46. def __init__(self, walker, commit):
  47. self.commit = commit
  48. self._store = walker.store
  49. self._get_parents = walker.get_parents
  50. self._changes = {}
  51. self._rename_detector = walker.rename_detector
  52. def changes(self, path_prefix=None):
  53. """Get the tree changes for this entry.
  54. :param path_prefix: Portion of the path in the repository to
  55. use to filter changes. Must be a directory name. Must be
  56. a full, valid, path reference (no partial names or wildcards).
  57. :return: For commits with up to one parent, a list of TreeChange
  58. objects; if the commit has no parents, these will be relative to
  59. the empty tree. For merge commits, a list of lists of TreeChange
  60. objects; see dulwich.diff.tree_changes_for_merge.
  61. """
  62. cached = self._changes.get(path_prefix)
  63. if cached is None:
  64. commit = self.commit
  65. if not self._get_parents(commit):
  66. changes_func = tree_changes
  67. parent = None
  68. elif len(self._get_parents(commit)) == 1:
  69. changes_func = tree_changes
  70. parent = self._store[self._get_parents(commit)[0]].tree
  71. if path_prefix:
  72. mode, subtree_sha = parent.lookup_path(
  73. self._store.__getitem__,
  74. path_prefix,
  75. )
  76. parent = self._store[subtree_sha]
  77. else:
  78. changes_func = tree_changes_for_merge
  79. parent = [
  80. self._store[p].tree for p in self._get_parents(commit)]
  81. if path_prefix:
  82. parent_trees = [self._store[p] for p in parent]
  83. parent = []
  84. for p in parent_trees:
  85. try:
  86. mode, st = p.lookup_path(
  87. self._store.__getitem__,
  88. path_prefix,
  89. )
  90. except KeyError:
  91. pass
  92. else:
  93. parent.append(st)
  94. commit_tree_sha = commit.tree
  95. if path_prefix:
  96. commit_tree = self._store[commit_tree_sha]
  97. mode, commit_tree_sha = commit_tree.lookup_path(
  98. self._store.__getitem__,
  99. path_prefix,
  100. )
  101. cached = list(changes_func(
  102. self._store, parent, commit_tree_sha,
  103. rename_detector=self._rename_detector))
  104. self._changes[path_prefix] = cached
  105. return self._changes[path_prefix]
  106. def __repr__(self):
  107. return '<WalkEntry commit=%s, changes=%r>' % (
  108. self.commit.id, self.changes())
  109. class _CommitTimeQueue(object):
  110. """Priority queue of WalkEntry objects by commit time."""
  111. def __init__(self, walker):
  112. self._walker = walker
  113. self._store = walker.store
  114. self._get_parents = walker.get_parents
  115. self._excluded = walker.excluded
  116. self._pq = []
  117. self._pq_set = set()
  118. self._seen = set()
  119. self._done = set()
  120. self._min_time = walker.since
  121. self._last = None
  122. self._extra_commits_left = _MAX_EXTRA_COMMITS
  123. self._is_finished = False
  124. for commit_id in chain(walker.include, walker.excluded):
  125. self._push(commit_id)
  126. def _push(self, object_id):
  127. try:
  128. obj = self._store[object_id]
  129. except KeyError:
  130. raise MissingCommitError(object_id)
  131. if isinstance(obj, Tag):
  132. self._push(obj.object[1])
  133. return
  134. # TODO(jelmer): What to do about non-Commit and non-Tag objects?
  135. commit = obj
  136. if commit.id not in self._pq_set and commit.id not in self._done:
  137. heapq.heappush(self._pq, (-commit.commit_time, commit))
  138. self._pq_set.add(commit.id)
  139. self._seen.add(commit.id)
  140. def _exclude_parents(self, commit):
  141. excluded = self._excluded
  142. seen = self._seen
  143. todo = [commit]
  144. while todo:
  145. commit = todo.pop()
  146. for parent in self._get_parents(commit):
  147. if parent not in excluded and parent in seen:
  148. # TODO: This is inefficient unless the object store does
  149. # some caching (which DiskObjectStore currently does not).
  150. # We could either add caching in this class or pass around
  151. # parsed queue entry objects instead of commits.
  152. todo.append(self._store[parent])
  153. excluded.add(parent)
  154. def next(self):
  155. if self._is_finished:
  156. return None
  157. while self._pq:
  158. _, commit = heapq.heappop(self._pq)
  159. sha = commit.id
  160. self._pq_set.remove(sha)
  161. if sha in self._done:
  162. continue
  163. self._done.add(sha)
  164. for parent_id in self._get_parents(commit):
  165. self._push(parent_id)
  166. reset_extra_commits = True
  167. is_excluded = sha in self._excluded
  168. if is_excluded:
  169. self._exclude_parents(commit)
  170. if self._pq and all(c.id in self._excluded
  171. for _, c in self._pq):
  172. _, n = self._pq[0]
  173. if self._last and n.commit_time >= self._last.commit_time:
  174. # If the next commit is newer than the last one, we
  175. # need to keep walking in case its parents (which we
  176. # may not have seen yet) are excluded. This gives the
  177. # excluded set a chance to "catch up" while the commit
  178. # is still in the Walker's output queue.
  179. reset_extra_commits = True
  180. else:
  181. reset_extra_commits = False
  182. if (self._min_time is not None and
  183. commit.commit_time < self._min_time):
  184. # We want to stop walking at min_time, but commits at the
  185. # boundary may be out of order with respect to their parents.
  186. # So we walk _MAX_EXTRA_COMMITS more commits once we hit this
  187. # boundary.
  188. reset_extra_commits = False
  189. if reset_extra_commits:
  190. # We're not at a boundary, so reset the counter.
  191. self._extra_commits_left = _MAX_EXTRA_COMMITS
  192. else:
  193. self._extra_commits_left -= 1
  194. if not self._extra_commits_left:
  195. break
  196. if not is_excluded:
  197. self._last = commit
  198. return WalkEntry(self._walker, commit)
  199. self._is_finished = True
  200. return None
  201. __next__ = next
  202. class Walker(object):
  203. """Object for performing a walk of commits in a store.
  204. Walker objects are initialized with a store and other options and can then
  205. be treated as iterators of Commit objects.
  206. """
  207. def __init__(self, store, include, exclude=None, order=ORDER_DATE,
  208. reverse=False, max_entries=None, paths=None,
  209. rename_detector=None, follow=False, since=None, until=None,
  210. get_parents=lambda commit: commit.parents,
  211. queue_cls=_CommitTimeQueue):
  212. """Constructor.
  213. :param store: ObjectStore instance for looking up objects.
  214. :param include: Iterable of SHAs of commits to include along with their
  215. ancestors.
  216. :param exclude: Iterable of SHAs of commits to exclude along with their
  217. ancestors, overriding includes.
  218. :param order: ORDER_* constant specifying the order of results.
  219. Anything other than ORDER_DATE may result in O(n) memory usage.
  220. :param reverse: If True, reverse the order of output, requiring O(n)
  221. memory.
  222. :param max_entries: The maximum number of entries to yield, or None for
  223. no limit.
  224. :param paths: Iterable of file or subtree paths to show entries for.
  225. :param rename_detector: diff.RenameDetector object for detecting
  226. renames.
  227. :param follow: If True, follow path across renames/copies. Forces a
  228. default rename_detector.
  229. :param since: Timestamp to list commits after.
  230. :param until: Timestamp to list commits before.
  231. :param get_parents: Method to retrieve the parents of a commit
  232. :param queue_cls: A class to use for a queue of commits, supporting the
  233. iterator protocol. The constructor takes a single argument, the
  234. Walker.
  235. """
  236. # Note: when adding arguments to this method, please also update
  237. # dulwich.repo.BaseRepo.get_walker
  238. if order not in ALL_ORDERS:
  239. raise ValueError('Unknown walk order %s' % order)
  240. self.store = store
  241. if isinstance(include, bytes):
  242. # TODO(jelmer): Really, this should require a single type.
  243. # Print deprecation warning here?
  244. include = [include]
  245. self.include = include
  246. self.excluded = set(exclude or [])
  247. self.order = order
  248. self.reverse = reverse
  249. self.max_entries = max_entries
  250. self.paths = paths and set(paths) or None
  251. if follow and not rename_detector:
  252. rename_detector = RenameDetector(store)
  253. self.rename_detector = rename_detector
  254. self.get_parents = get_parents
  255. self.follow = follow
  256. self.since = since
  257. self.until = until
  258. self._num_entries = 0
  259. self._queue = queue_cls(self)
  260. self._out_queue = collections.deque()
  261. def _path_matches(self, changed_path):
  262. if changed_path is None:
  263. return False
  264. for followed_path in self.paths:
  265. if changed_path == followed_path:
  266. return True
  267. if (changed_path.startswith(followed_path) and
  268. changed_path[len(followed_path)] == b'/'[0]):
  269. return True
  270. return False
  271. def _change_matches(self, change):
  272. if not change:
  273. return False
  274. old_path = change.old.path
  275. new_path = change.new.path
  276. if self._path_matches(new_path):
  277. if self.follow and change.type in RENAME_CHANGE_TYPES:
  278. self.paths.add(old_path)
  279. self.paths.remove(new_path)
  280. return True
  281. elif self._path_matches(old_path):
  282. return True
  283. return False
  284. def _should_return(self, entry):
  285. """Determine if a walk entry should be returned..
  286. :param entry: The WalkEntry to consider.
  287. :return: True if the WalkEntry should be returned by this walk, or
  288. False otherwise (e.g. if it doesn't match any requested paths).
  289. """
  290. commit = entry.commit
  291. if self.since is not None and commit.commit_time < self.since:
  292. return False
  293. if self.until is not None and commit.commit_time > self.until:
  294. return False
  295. if commit.id in self.excluded:
  296. return False
  297. if self.paths is None:
  298. return True
  299. if len(self.get_parents(commit)) > 1:
  300. for path_changes in entry.changes():
  301. # For merge commits, only include changes with conflicts for
  302. # this path. Since a rename conflict may include different
  303. # old.paths, we have to check all of them.
  304. for change in path_changes:
  305. if self._change_matches(change):
  306. return True
  307. else:
  308. for change in entry.changes():
  309. if self._change_matches(change):
  310. return True
  311. return None
  312. def _next(self):
  313. max_entries = self.max_entries
  314. while max_entries is None or self._num_entries < max_entries:
  315. entry = next(self._queue)
  316. if entry is not None:
  317. self._out_queue.append(entry)
  318. if entry is None or len(self._out_queue) > _MAX_EXTRA_COMMITS:
  319. if not self._out_queue:
  320. return None
  321. entry = self._out_queue.popleft()
  322. if self._should_return(entry):
  323. self._num_entries += 1
  324. return entry
  325. return None
  326. def _reorder(self, results):
  327. """Possibly reorder a results iterator.
  328. :param results: An iterator of WalkEntry objects, in the order returned
  329. from the queue_cls.
  330. :return: An iterator or list of WalkEntry objects, in the order
  331. required by the Walker.
  332. """
  333. if self.order == ORDER_TOPO:
  334. results = _topo_reorder(results, self.get_parents)
  335. if self.reverse:
  336. results = reversed(list(results))
  337. return results
  338. def __iter__(self):
  339. return iter(self._reorder(iter(self._next, None)))
  340. def _topo_reorder(entries, get_parents=lambda commit: commit.parents):
  341. """Reorder an iterable of entries topologically.
  342. This works best assuming the entries are already in almost-topological
  343. order, e.g. in commit time order.
  344. :param entries: An iterable of WalkEntry objects.
  345. :param get_parents: Optional function for getting the parents of a commit.
  346. :return: iterator over WalkEntry objects from entries in FIFO order, except
  347. where a parent would be yielded before any of its children.
  348. """
  349. todo = collections.deque()
  350. pending = {}
  351. num_children = defaultdict(int)
  352. for entry in entries:
  353. todo.append(entry)
  354. for p in get_parents(entry.commit):
  355. num_children[p] += 1
  356. while todo:
  357. entry = todo.popleft()
  358. commit = entry.commit
  359. commit_id = commit.id
  360. if num_children[commit_id]:
  361. pending[commit_id] = entry
  362. continue
  363. for parent_id in get_parents(commit):
  364. num_children[parent_id] -= 1
  365. if not num_children[parent_id]:
  366. parent_entry = pending.pop(parent_id, None)
  367. if parent_entry:
  368. todo.appendleft(parent_entry)
  369. yield entry