-
Notifications
You must be signed in to change notification settings - Fork 1.3k
exp show: add --rev flag (#7152)
#7204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| """Manages source control systems (e.g. Git).""" | ||
| from contextlib import contextmanager | ||
| from typing import TYPE_CHECKING, Iterator | ||
| from functools import partial | ||
| from typing import TYPE_CHECKING, Iterator, List, Mapping, Optional | ||
|
|
||
| from funcy import group_by | ||
| from scmrepo.base import Base # noqa: F401, pylint: disable=unused-import | ||
| from scmrepo.git import Git | ||
| from scmrepo.noscm import NoSCM | ||
|
|
||
| from dvc.exceptions import DvcException | ||
| from dvc.exceptions import DvcException, InvalidArgumentError | ||
| from dvc.progress import Tqdm | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -123,3 +125,56 @@ def resolve_rev(scm: "Git", rev: str) -> str: | |
| if len(ref_infos) > 1: | ||
| raise RevError(f"ambiguous Git revision '{rev}'") | ||
| raise RevError(str(exc)) | ||
|
|
||
|
|
||
| def iter_revs( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any name to suggest?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can modify these in #7245 |
||
| scm: "Git", | ||
| head_revs: Optional[List[str]] = None, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not name it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can call it |
||
| num: int = 1, | ||
| all_branches: bool = False, | ||
| all_tags: bool = False, | ||
| all_commits: bool = False, | ||
| all_experiments: bool = False, | ||
| ) -> Mapping[str, List[str]]: | ||
| from dvc.repo.experiments.utils import fix_exp_head | ||
|
|
||
| if num < 1 and num != -1: | ||
| raise InvalidArgumentError(f"Invalid number of commits '{num}'") | ||
|
|
||
| if not any( | ||
| [head_revs, all_branches, all_tags, all_commits, all_experiments] | ||
| ): | ||
| return {} | ||
|
|
||
| head_revs = head_revs or [] | ||
| revs = [] | ||
| for rev in head_revs: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wasn't this mutually exclusive with
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't this be done separately with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the previous version
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How is this supposed to work? If I do So it seems like I don't feel strongly about whether we allow these to be combined. It seems strange to combine them, but if we can explain the behavior clearly, it might be fine to allow users to combine them if they want. |
||
| revs.append(rev) | ||
| n = 1 | ||
| while True: | ||
| if num == n: | ||
| break | ||
| try: | ||
| head = fix_exp_head(scm, f"{rev}~{n}") | ||
| assert head | ||
| revs.append(resolve_rev(scm, head)) | ||
| except RevError: | ||
| break | ||
| n += 1 | ||
|
|
||
| if all_commits: | ||
| revs.extend(scm.list_all_commits()) | ||
| else: | ||
| if all_branches: | ||
| revs.extend(scm.list_branches()) | ||
|
|
||
| if all_tags: | ||
| revs.extend(scm.list_tags()) | ||
|
|
||
| if all_experiments: | ||
| from dvc.repo.experiments.utils import exp_commits | ||
|
|
||
| revs.extend(exp_commits(scm)) | ||
|
|
||
| rev_resolver = partial(resolve_rev, scm) | ||
| return group_by(rev_resolver, revs) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from dvc.repo.experiments import ExpRefInfo | ||
| from dvc.scm import iter_revs | ||
|
|
||
|
|
||
| def test_iter_revs( | ||
| tmp_dir, | ||
| scm, | ||
| ): | ||
| """ | ||
| new other | ||
| β β | ||
| old (tag) βββββ | ||
| β | ||
| root | ||
| """ | ||
| old = scm.active_branch() | ||
| tmp_dir.scm_gen("foo", "init", commit="init") | ||
| rev_root = scm.get_rev() | ||
| tmp_dir.scm_gen("foo", "old", commit="old") | ||
| rev_old = scm.get_rev() | ||
| scm.checkout("new", create_new=True) | ||
| tmp_dir.scm_gen("foo", "new", commit="new") | ||
| rev_new = scm.get_rev() | ||
| scm.checkout(old) | ||
| scm.tag("tag") | ||
| scm.checkout("other", create_new=True) | ||
| tmp_dir.scm_gen("foo", "other", commit="new") | ||
| rev_other = scm.get_rev() | ||
|
|
||
| ref = ExpRefInfo(rev_root, "exp1") | ||
| scm.set_ref(str(ref), rev_new) | ||
| ref = ExpRefInfo(rev_root, "exp2") | ||
| scm.set_ref(str(ref), rev_old) | ||
|
|
||
| gen = iter_revs(scm, [rev_root, "new"], 1) | ||
| assert gen == {rev_root: [rev_root], rev_new: ["new"]} | ||
| gen = iter_revs(scm, ["new"], 2) | ||
| assert gen == {rev_new: ["new"], rev_old: [rev_old]} | ||
| gen = iter_revs(scm, ["other"], -1) | ||
| assert gen == { | ||
| rev_other: ["other"], | ||
| rev_old: [rev_old], | ||
| rev_root: [rev_root], | ||
| } | ||
| gen = iter_revs(scm, ["tag"]) | ||
| assert gen == {rev_old: ["tag"]} | ||
| gen = iter_revs(scm, all_branches=True) | ||
| assert gen == {rev_old: [old], rev_new: ["new"], rev_other: ["other"]} | ||
| gen = iter_revs(scm, all_tags=True) | ||
| assert gen == {rev_old: ["tag"]} | ||
| gen = iter_revs(scm, all_commits=True) | ||
| assert gen == { | ||
| rev_old: [rev_old], | ||
| rev_new: [rev_new], | ||
| rev_other: [rev_other], | ||
| rev_root: [rev_root], | ||
| } | ||
| gen = iter_revs(scm, all_experiments=True) | ||
| assert gen == { | ||
| rev_new: [rev_new], | ||
| rev_old: [rev_old], | ||
| rev_root: [rev_root], | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.