|
5 | 5 |
|
6 | 6 | __all__ = ["Commit"] |
7 | 7 |
|
8 | | -from collections import defaultdict |
| 8 | +from collections import defaultdict, deque |
9 | 9 | import datetime |
10 | 10 | from io import BytesIO |
| 11 | +from itertools import chain, islice |
11 | 12 | import logging |
12 | 13 | import os |
13 | 14 | from subprocess import Popen |
|
36 | 37 |
|
37 | 38 | from typing import ( |
38 | 39 | Any, |
| 40 | + Deque, |
39 | 41 | Dict, |
40 | 42 | IO, |
41 | 43 | Iterator, |
@@ -480,24 +482,66 @@ def iter_parents(self, paths: Union[PathLike, Sequence[PathLike]] = "", **kwargs |
480 | 482 | contain at least one of the paths. |
481 | 483 |
|
482 | 484 | :param kwargs: |
483 | | - All arguments allowed by :manpage:`git-rev-list(1)`. |
| 485 | + All arguments allowed by :manpage:`git-rev-list(1)`. ``skip`` values of |
| 486 | + 0 and 1 skip only ``self``; larger values also skip matching ancestors. |
484 | 487 |
|
485 | 488 | :return: |
486 | 489 | Iterator yielding :class:`Commit` objects which are parents of ``self`` |
487 | 490 | """ |
488 | | - # skip ourselves |
489 | | - skip = kwargs.get("skip", 1) |
490 | | - if skip == 0: # skip ourselves |
491 | | - skip = 1 |
492 | | - if paths or kwargs.keys() - {"skip", "max_count"}: |
493 | | - # rev-list leaves us out when we don't match the paths or filters, so there |
494 | | - # is nothing of ours to skip. |
495 | | - probe = {**kwargs, "skip": 0, "max_count": 1} |
496 | | - if skip is not None and next(self.iter_items(self.repo, self, paths, **probe), None) != self: |
497 | | - skip = int(skip) - 1 |
498 | | - kwargs["skip"] = skip |
499 | | - |
500 | | - return self.iter_items(self.repo, self, paths, **kwargs) |
| 491 | + skip = max(0, int(kwargs.pop("skip", None) or 1) - 1) |
| 492 | + count_options = {"max_count", "max-count", "n"} |
| 493 | + if not paths and kwargs.keys() <= count_options: |
| 494 | + return self.iter_items(self.repo, self, paths, skip=skip + 1, **kwargs) |
| 495 | + |
| 496 | + # Keep Git's option order when resolving alternate spellings of max_count. |
| 497 | + limits = {key: kwargs.pop(key) for key in tuple(kwargs) if key in count_options} |
| 498 | + count_args = self.repo.git.transform_kwargs(**limits) if limits else [] |
| 499 | + max_count = int(count_args[-1].rsplit("=", 1)[-1]) if count_args else -1 |
| 500 | + if max_count == 0: |
| 501 | + return self.iter_items(self.repo, self, paths, max_count=0, **kwargs) |
| 502 | + |
| 503 | + reverse_args = self.repo.git.transform_kwargs(reverse=kwargs["reverse"]) if "reverse" in kwargs else [] |
| 504 | + reverse = bool(reverse_args) and all(arg == "--reverse" for arg in reverse_args) |
| 505 | + if reverse and max_count < 0: |
| 506 | + commits = self.iter_items(self.repo, self, paths, **kwargs) |
| 507 | + |
| 508 | + def skip_from_end() -> Iterator["Commit"]: |
| 509 | + # Git applies skip before reversing. Hold back just the commits that |
| 510 | + # might need to be skipped, including self if it is last. |
| 511 | + tail: Deque[Commit] = deque() |
| 512 | + for commit in commits: |
| 513 | + tail.append(commit) |
| 514 | + if len(tail) > skip + 1: |
| 515 | + yield tail.popleft() |
| 516 | + if len(tail) == skip + 1 and tail[-1] != self: |
| 517 | + yield tail[0] |
| 518 | + |
| 519 | + return skip_from_end() |
| 520 | + |
| 521 | + if reverse: |
| 522 | + del kwargs["reverse"] |
| 523 | + if max_count > 0: |
| 524 | + # Allow room for self even though filters may leave it out. |
| 525 | + kwargs["max_count"] = skip + max_count + 1 |
| 526 | + commits = self.iter_items(self.repo, self, paths, **kwargs) |
| 527 | + |
| 528 | + def skip_from_start() -> Iterator["Commit"]: |
| 529 | + first = next(commits, None) |
| 530 | + if first is None: |
| 531 | + return |
| 532 | + start = skip + (first == self) |
| 533 | + stop = start + max_count if max_count > 0 else None |
| 534 | + parents = islice(chain((first,), commits), start, stop) |
| 535 | + if reverse: |
| 536 | + yield from reversed(list(parents)) |
| 537 | + else: |
| 538 | + yield from parents |
| 539 | + if stop is not None: |
| 540 | + # Finish the walk, consuming the one extra result if self was filtered out. |
| 541 | + for _ in commits: |
| 542 | + pass |
| 543 | + |
| 544 | + return skip_from_start() |
501 | 545 |
|
502 | 546 | @property |
503 | 547 | def stats(self) -> Stats: |
|
0 commit comments