Skip to content

Commit

Permalink
exp show: cleanup UI issues (#5361)
Browse files Browse the repository at this point in the history
* exp show: add -n/--num to display the last N commits from HEAD

* exp run: fix empty commit with no changes

* add test for exp show with multiple commits

* allow -n greater than num commits in repo

* review fix
  • Loading branch information
pmrowla authored Feb 2, 2021
1 parent 2ea5ef0 commit d05be36
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
11 changes: 11 additions & 0 deletions dvc/command/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def run(self):
all_tags=self.args.all_tags,
all_commits=self.args.all_commits,
sha_only=self.args.sha,
num=self.args.num,
)

if self.args.show_json:
Expand Down Expand Up @@ -674,11 +675,21 @@ def add_parser(subparsers, parent_parser):
help="Show experiments derived from all Git tags.",
)
experiments_show_parser.add_argument(
"-A",
"--all-commits",
action="store_true",
default=False,
help="Show experiments derived from all Git commits.",
)
experiments_show_parser.add_argument(
"-n",
"--num",
type=int,
default=1,
dest="num",
metavar="<num>",
help="Show the last `num` commits from HEAD.",
)
experiments_show_parser.add_argument(
"--no-pager",
action="store_true",
Expand Down
2 changes: 1 addition & 1 deletion dvc/repo/experiments/executor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def commit(
):
"""Commit stages as an experiment and return the commit SHA."""
rev = scm.get_rev()
if not scm.is_dirty(untracked_files=True):
if not scm.is_dirty():
logger.debug("No changes to commit")
raise UnchangedExperimentError(rev)

Expand Down
13 changes: 12 additions & 1 deletion dvc/repo/experiments/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from collections import OrderedDict, defaultdict
from datetime import datetime

from dvc.exceptions import InvalidArgumentError
from dvc.repo import locked
from dvc.repo.experiments.base import ExpRefInfo
from dvc.repo.metrics.show import _collect_metrics, _read_metrics
from dvc.repo.params.show import _collect_configs, _read_params
from dvc.scm.base import SCMError

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,11 +81,20 @@ def show(
revs=None,
all_commits=False,
sha_only=False,
num=1,
):
res = defaultdict(OrderedDict)

if num < 1:
raise InvalidArgumentError(f"Invalid number of commits '{num}'")

if revs is None:
revs = [repo.scm.get_rev()]
revs = []
for n in range(num):
try:
revs.append(repo.scm.resolve_rev(f"HEAD~{n}"))
except SCMError:
break

revs = OrderedDict(
(rev, None)
Expand Down
22 changes: 22 additions & 0 deletions tests/func/experiments/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,25 @@ def test_show_filter(tmp_dir, scm, dvc, exp_stage, capsys):
cap = capsys.readouterr()

assert f"{div} foo {div}" not in cap.out


def test_show_multiple_commits(tmp_dir, scm, dvc, exp_stage):
from dvc.exceptions import InvalidArgumentError

init_rev = scm.get_rev()
tmp_dir.scm_gen("file", "file", "commit")
next_rev = scm.get_rev()

with pytest.raises(InvalidArgumentError):
dvc.experiments.show(num=-1)

expected = {"workspace", init_rev, next_rev}
results = dvc.experiments.show(num=2)
assert set(results.keys()) == expected

expected = {"workspace"} | set(scm.branch_revs("master"))
results = dvc.experiments.show(all_commits=True)
assert set(results.keys()) == expected

results = dvc.experiments.show(num=100)
assert set(results.keys()) == expected
3 changes: 3 additions & 0 deletions tests/unit/command/test_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def test_experiments_show(dvc, scm, mocker):
"--all-branches",
"--all-commits",
"--sha",
"-n",
"1",
]
)
assert cli_args.func == CmdExperimentsShow
Expand All @@ -80,6 +82,7 @@ def test_experiments_show(dvc, scm, mocker):
all_branches=True,
all_commits=True,
sha_only=True,
num=1,
)


Expand Down

0 comments on commit d05be36

Please sign in to comment.