From 121d32531ae746930195ab9241a0ba0b0f9ef8b5 Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Sat, 7 Aug 2021 17:01:09 +0800 Subject: [PATCH 1/9] Remove all of the experiments in the workspace fix #5676 1. remove all the experiments in workspace 2. add tests for it --- dvc/command/experiments.py | 12 ++++++++++-- dvc/repo/experiments/remove.py | 12 ++++++++---- tests/unit/command/test_experiments.py | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/dvc/command/experiments.py b/dvc/command/experiments.py index fa79072cf5..d97272381d 100644 --- a/dvc/command/experiments.py +++ b/dvc/command/experiments.py @@ -747,7 +747,9 @@ class CmdExperimentsRemove(CmdBase): def run(self): self.repo.experiments.remove( - exp_names=self.args.experiment, queue=self.args.queue + exp_names=self.args.experiment, + queue=self.args.queue, + workspace=self.args.workspace, ) return 0 @@ -1237,9 +1239,15 @@ def add_parser(subparsers, parent_parser): help=EXPERIMENTS_REMOVE_HELP, formatter_class=argparse.RawDescriptionHelpFormatter, ) - experiments_remove_parser.add_argument( + remove_group = experiments_remove_parser.add_mutually_exclusive_group() + remove_group.add_argument( "--queue", action="store_true", help="Remove all queued experiments." ) + remove_group.add_argument( + "--workspace", + action="store_true", + help="Remove all experiments in current workspace.", + ) experiments_remove_parser.add_argument( "experiment", nargs="*", diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index 8755ac3004..87dacc6dec 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -7,21 +7,25 @@ from dvc.scm.base import RevError from .base import EXPS_NAMESPACE, ExpRefInfo -from .utils import exp_refs_by_name, remove_exp_refs +from .utils import exp_refs, exp_refs_by_name, remove_exp_refs logger = logging.getLogger(__name__) @locked @scm_context -def remove(repo, exp_names=None, queue=False, **kwargs): - if not exp_names and not queue: +def remove(repo, exp_names=None, queue=False, workspace=False, **kwargs): + if not any([exp_names, queue, workspace]): return 0 removed = 0 - if queue: + if queue or workspace: removed += len(repo.experiments.stash) repo.experiments.stash.clear() + if workspace: + ref_infos = list(exp_refs(repo.scm)) + remove_exp_refs(repo.scm, ref_infos) + removed += len(ref_infos) if exp_names: remained = _remove_commited_exps(repo, exp_names) remained = _remove_queued_exps(repo, remained) diff --git a/tests/unit/command/test_experiments.py b/tests/unit/command/test_experiments.py index e271f14644..e78b268acb 100644 --- a/tests/unit/command/test_experiments.py +++ b/tests/unit/command/test_experiments.py @@ -260,5 +260,18 @@ def test_experiments_remove(dvc, scm, mocker): m = mocker.patch("dvc.repo.experiments.remove.remove", return_value={}) assert cmd.run() == 0 + m.assert_called_once_with( + cmd.repo, exp_names=[], queue=True, workspace=False + ) + + cli_args = parse_args(["experiments", "remove", "--workspace"]) + assert cli_args.func == CmdExperimentsRemove - m.assert_called_once_with(cmd.repo, exp_names=[], queue=True) + cmd = cli_args.func(cli_args) + m = mocker.patch("dvc.repo.experiments.remove.remove", return_value={}) + + assert cmd.run() == 0 + + m.assert_called_once_with( + cmd.repo, exp_names=[], queue=False, workspace=True + ) From ccb918bbc8460e8a1a9eeac3d9045c54e152c294 Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Tue, 10 Aug 2021 16:26:36 +0800 Subject: [PATCH 2/9] Only remove baseline ones --- dvc/repo/experiments/remove.py | 5 +++-- tests/func/experiments/test_remove.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index 87dacc6dec..00c1063c22 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -7,7 +7,7 @@ from dvc.scm.base import RevError from .base import EXPS_NAMESPACE, ExpRefInfo -from .utils import exp_refs, exp_refs_by_name, remove_exp_refs +from .utils import exp_refs_by_baseline, exp_refs_by_name, remove_exp_refs logger = logging.getLogger(__name__) @@ -23,7 +23,8 @@ def remove(repo, exp_names=None, queue=False, workspace=False, **kwargs): removed += len(repo.experiments.stash) repo.experiments.stash.clear() if workspace: - ref_infos = list(exp_refs(repo.scm)) + head = repo.scm.get_ref("HEAD") + ref_infos = list(exp_refs_by_baseline(repo.scm, head)) remove_exp_refs(repo.scm, ref_infos) removed += len(ref_infos) if exp_names: diff --git a/tests/func/experiments/test_remove.py b/tests/func/experiments/test_remove.py index f12f142532..8010103be9 100644 --- a/tests/func/experiments/test_remove.py +++ b/tests/func/experiments/test_remove.py @@ -72,3 +72,22 @@ def test_remove_special_queued_experiments(tmp_dir, scm, dvc, exp_stage): assert rev3 in dvc.experiments.stash_revs assert scm.get_ref(str(ref_info1)) is None assert scm.get_ref(str(ref_info2)) is not None + + +def test_remove_workspace(tmp_dir, scm, dvc, exp_stage): + results = dvc.experiments.run(exp_stage.addressing, params=["foo=1"]) + exp = first(results) + ref_info1 = first(exp_refs_by_rev(scm, exp)) + scm.add(["dvc.yaml", "dvc.lock", "copy.py", "params.yaml", "metrics.yaml"]) + scm.commit("update baseline") + + results = dvc.experiments.run(exp_stage.addressing, params=["foo=2"]) + exp = first(results) + ref_info2 = first(exp_refs_by_rev(scm, exp)) + dvc.experiments.run(exp_stage.addressing, params=["foo=3"], queue=True) + + removed = dvc.experiments.remove(workspace=True) + assert removed == 2 + assert len(dvc.experiments.stash) == 0 + assert scm.get_ref(str(ref_info2)) is None + assert scm.get_ref(str(ref_info1)) is not None From aa89bad07c39d724dc59749bde41b6617011bfca Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Fri, 13 Aug 2021 19:03:51 +0800 Subject: [PATCH 3/9] Add a new arguments --all --- dvc/command/experiments.py | 8 +++- dvc/repo/experiments/remove.py | 64 ++++++++++++++++++++------ tests/func/experiments/test_remove.py | 35 +++++++++++--- tests/unit/command/test_experiments.py | 32 +++++++------ 4 files changed, 103 insertions(+), 36 deletions(-) diff --git a/dvc/command/experiments.py b/dvc/command/experiments.py index d97272381d..b05c2ec948 100644 --- a/dvc/command/experiments.py +++ b/dvc/command/experiments.py @@ -750,6 +750,7 @@ def run(self): exp_names=self.args.experiment, queue=self.args.queue, workspace=self.args.workspace, + all_baseline=self.args.all, ) return 0 @@ -1246,7 +1247,12 @@ def add_parser(subparsers, parent_parser): remove_group.add_argument( "--workspace", action="store_true", - help="Remove all experiments in current workspace.", + help="Remove all experiments based on current workspace.", + ) + remove_group.add_argument( + "--all", + action="store_true", + help="Remove all experiments.", ) experiments_remove_parser.add_argument( "experiment", diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index 00c1063c22..71c7de13bf 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -7,26 +7,37 @@ from dvc.scm.base import RevError from .base import EXPS_NAMESPACE, ExpRefInfo -from .utils import exp_refs_by_baseline, exp_refs_by_name, remove_exp_refs +from .utils import ( + exp_refs, + exp_refs_by_baseline, + exp_refs_by_name, + remove_exp_refs, +) logger = logging.getLogger(__name__) @locked @scm_context -def remove(repo, exp_names=None, queue=False, workspace=False, **kwargs): - if not any([exp_names, queue, workspace]): +def remove( + repo, + exp_names=None, + queue=False, + workspace=False, + all_baseline=False, + **kwargs, +): + if not any([exp_names, queue, workspace, all_baseline]): return 0 removed = 0 - if queue or workspace: - removed += len(repo.experiments.stash) - repo.experiments.stash.clear() + if queue: + removed += _clear_workspace_stash(repo) if workspace: - head = repo.scm.get_ref("HEAD") - ref_infos = list(exp_refs_by_baseline(repo.scm, head)) - remove_exp_refs(repo.scm, ref_infos) - removed += len(ref_infos) + removed += _clear_workspace(repo) + if all_baseline: + removed += _clear_all_baseline(repo) + if exp_names: remained = _remove_commited_exps(repo, exp_names) remained = _remove_queued_exps(repo, remained) @@ -38,6 +49,33 @@ def remove(repo, exp_names=None, queue=False, workspace=False, **kwargs): return removed +def _clear_workspace_stash(repo): + removed = len(repo.experiments.stash) + repo.experiments.stash.clear() + return removed + + +def _clear_workspace(repo): + head = repo.scm.get_ref("HEAD") + ref_infos = list(exp_refs_by_baseline(repo.scm, head)) + remove_exp_refs(repo.scm, ref_infos) + stashed = 0 + stash_revs = repo.experiments.stash_revs + for _, ref_info in stash_revs.items(): + if head == ref_info.baseline_rev: + repo.experiments.stash.drop(ref_info.index) + stashed += 1 + return stashed + len(ref_infos) + + +def _clear_all_baseline(repo): + ref_infos = list(exp_refs(repo.scm)) + remove_exp_refs(repo.scm, ref_infos) + removed = len(repo.experiments.stash) + repo.experiments.stash.clear() + return removed + len(ref_infos) + + def _get_exp_stash_index(repo, ref_or_rev: str) -> Optional[int]: stash_revs = repo.experiments.stash_revs for _, ref_info in stash_revs.items(): @@ -58,9 +96,9 @@ def _get_exp_ref(repo, exp_name: str) -> Optional[ExpRefInfo]: if repo.scm.get_ref(exp_name): return ExpRefInfo.from_ref(exp_name) else: - exp_refs = list(exp_refs_by_name(repo.scm, exp_name)) - if exp_refs: - return _get_ref(exp_refs, exp_name, cur_rev) + exp_ref_list = list(exp_refs_by_name(repo.scm, exp_name)) + if exp_ref_list: + return _get_ref(exp_ref_list, exp_name, cur_rev) return None diff --git a/tests/func/experiments/test_remove.py b/tests/func/experiments/test_remove.py index 8010103be9..38cfd47c24 100644 --- a/tests/func/experiments/test_remove.py +++ b/tests/func/experiments/test_remove.py @@ -76,18 +76,39 @@ def test_remove_special_queued_experiments(tmp_dir, scm, dvc, exp_stage): def test_remove_workspace(tmp_dir, scm, dvc, exp_stage): results = dvc.experiments.run(exp_stage.addressing, params=["foo=1"]) - exp = first(results) - ref_info1 = first(exp_refs_by_rev(scm, exp)) + ref_info1 = first(exp_refs_by_rev(scm, first(results))) + results = dvc.experiments.run( + exp_stage.addressing, params=["foo=2"], queue=True + ) + rev1 = first(results) scm.add(["dvc.yaml", "dvc.lock", "copy.py", "params.yaml", "metrics.yaml"]) scm.commit("update baseline") - results = dvc.experiments.run(exp_stage.addressing, params=["foo=2"]) - exp = first(results) - ref_info2 = first(exp_refs_by_rev(scm, exp)) - dvc.experiments.run(exp_stage.addressing, params=["foo=3"], queue=True) + results = dvc.experiments.run(exp_stage.addressing, params=["foo=3"]) + ref_info2 = first(exp_refs_by_rev(scm, first(results))) + dvc.experiments.run(exp_stage.addressing, params=["foo=4"], queue=True) removed = dvc.experiments.remove(workspace=True) assert removed == 2 - assert len(dvc.experiments.stash) == 0 + assert len(dvc.experiments.stash) == 1 + assert rev1 in dvc.experiments.stash_revs assert scm.get_ref(str(ref_info2)) is None assert scm.get_ref(str(ref_info1)) is not None + + +def test_remove_all(tmp_dir, scm, dvc, exp_stage): + results = dvc.experiments.run(exp_stage.addressing, params=["foo=1"]) + ref_info1 = first(exp_refs_by_rev(scm, first(results))) + dvc.experiments.run(exp_stage.addressing, params=["foo=2"], queue=True) + scm.add(["dvc.yaml", "dvc.lock", "copy.py", "params.yaml", "metrics.yaml"]) + scm.commit("update baseline") + + results = dvc.experiments.run(exp_stage.addressing, params=["foo=3"]) + ref_info2 = first(exp_refs_by_rev(scm, first(results))) + dvc.experiments.run(exp_stage.addressing, params=["foo=4"], queue=True) + + removed = dvc.experiments.remove(all_baseline=True) + assert removed == 4 + assert len(dvc.experiments.stash) == 0 + assert scm.get_ref(str(ref_info2)) is None + assert scm.get_ref(str(ref_info1)) is None diff --git a/tests/unit/command/test_experiments.py b/tests/unit/command/test_experiments.py index e78b268acb..9210530628 100644 --- a/tests/unit/command/test_experiments.py +++ b/tests/unit/command/test_experiments.py @@ -252,26 +252,28 @@ def test_experiments_pull(dvc, scm, mocker): ) -def test_experiments_remove(dvc, scm, mocker): - cli_args = parse_args(["experiments", "remove", "--queue"]) - assert cli_args.func == CmdExperimentsRemove - - cmd = cli_args.func(cli_args) - m = mocker.patch("dvc.repo.experiments.remove.remove", return_value={}) - - assert cmd.run() == 0 - m.assert_called_once_with( - cmd.repo, exp_names=[], queue=True, workspace=False - ) - - cli_args = parse_args(["experiments", "remove", "--workspace"]) +@pytest.mark.parametrize( + "queue,workspace,all_baseline", + [(True, False, False), (False, True, False), (False, False, True)], +) +def test_experiments_remove(dvc, scm, mocker, queue, workspace, all_baseline): + if queue: + args = "--queue" + if workspace: + args = "--workspace" + if all_baseline: + args = "--all" + cli_args = parse_args(["experiments", "remove", args]) assert cli_args.func == CmdExperimentsRemove cmd = cli_args.func(cli_args) m = mocker.patch("dvc.repo.experiments.remove.remove", return_value={}) assert cmd.run() == 0 - m.assert_called_once_with( - cmd.repo, exp_names=[], queue=False, workspace=True + cmd.repo, + exp_names=[], + queue=queue, + workspace=workspace, + all_baseline=all_baseline, ) From 80f3e0165be90936e65b720a46ef7ecc387546a2 Mon Sep 17 00:00:00 2001 From: Gao Date: Mon, 16 Aug 2021 14:24:17 +0800 Subject: [PATCH 4/9] Update dvc/repo/experiments/remove.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Rowlands (변기호) --- dvc/repo/experiments/remove.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index 71c7de13bf..e31acec7ca 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -49,7 +49,7 @@ def remove( return removed -def _clear_workspace_stash(repo): +def _clear_stash(repo): removed = len(repo.experiments.stash) repo.experiments.stash.clear() return removed From 04d4f86b0a4ef1f6ffc0b0cbe373dec2f6cd159b Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Mon, 16 Aug 2021 19:21:39 +0800 Subject: [PATCH 5/9] Rename and shortcut for these commands --- dvc/command/experiments.py | 4 +++- dvc/repo/experiments/remove.py | 8 ++++---- tests/func/experiments/test_remove.py | 2 +- tests/unit/command/test_experiments.py | 8 ++++---- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/dvc/command/experiments.py b/dvc/command/experiments.py index b05c2ec948..d39e55cd09 100644 --- a/dvc/command/experiments.py +++ b/dvc/command/experiments.py @@ -750,7 +750,7 @@ def run(self): exp_names=self.args.experiment, queue=self.args.queue, workspace=self.args.workspace, - all_baseline=self.args.all, + clear_all=self.args.all, ) return 0 @@ -1245,11 +1245,13 @@ def add_parser(subparsers, parent_parser): "--queue", action="store_true", help="Remove all queued experiments." ) remove_group.add_argument( + "-w", "--workspace", action="store_true", help="Remove all experiments based on current workspace.", ) remove_group.add_argument( + "-A", "--all", action="store_true", help="Remove all experiments.", diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index e31acec7ca..ab1f62d9e1 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -24,18 +24,18 @@ def remove( exp_names=None, queue=False, workspace=False, - all_baseline=False, + clear_all=False, **kwargs, ): - if not any([exp_names, queue, workspace, all_baseline]): + if not any([exp_names, queue, workspace, clear_all]): return 0 removed = 0 if queue: - removed += _clear_workspace_stash(repo) + removed += _clear_stash(repo) if workspace: removed += _clear_workspace(repo) - if all_baseline: + if clear_all: removed += _clear_all_baseline(repo) if exp_names: diff --git a/tests/func/experiments/test_remove.py b/tests/func/experiments/test_remove.py index 38cfd47c24..49bc6fece6 100644 --- a/tests/func/experiments/test_remove.py +++ b/tests/func/experiments/test_remove.py @@ -107,7 +107,7 @@ def test_remove_all(tmp_dir, scm, dvc, exp_stage): ref_info2 = first(exp_refs_by_rev(scm, first(results))) dvc.experiments.run(exp_stage.addressing, params=["foo=4"], queue=True) - removed = dvc.experiments.remove(all_baseline=True) + removed = dvc.experiments.remove(clear_all=True) assert removed == 4 assert len(dvc.experiments.stash) == 0 assert scm.get_ref(str(ref_info2)) is None diff --git a/tests/unit/command/test_experiments.py b/tests/unit/command/test_experiments.py index 9210530628..8b3ff14319 100644 --- a/tests/unit/command/test_experiments.py +++ b/tests/unit/command/test_experiments.py @@ -253,15 +253,15 @@ def test_experiments_pull(dvc, scm, mocker): @pytest.mark.parametrize( - "queue,workspace,all_baseline", + "queue,workspace,clear_all", [(True, False, False), (False, True, False), (False, False, True)], ) -def test_experiments_remove(dvc, scm, mocker, queue, workspace, all_baseline): +def test_experiments_remove(dvc, scm, mocker, queue, workspace, clear_all): if queue: args = "--queue" if workspace: args = "--workspace" - if all_baseline: + if clear_all: args = "--all" cli_args = parse_args(["experiments", "remove", args]) assert cli_args.func == CmdExperimentsRemove @@ -275,5 +275,5 @@ def test_experiments_remove(dvc, scm, mocker, queue, workspace, all_baseline): exp_names=[], queue=queue, workspace=workspace, - all_baseline=all_baseline, + clear_all=clear_all, ) From 226d9593f4eba10cc93efc39a1879c05fcc31b06 Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Mon, 16 Aug 2021 20:27:16 +0800 Subject: [PATCH 6/9] Solve the issue from removing a changing list. --- dvc/repo/experiments/remove.py | 22 ++++++++++++++++------ tests/func/experiments/test_remove.py | 3 ++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index ab1f62d9e1..cd1d1f5c62 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -55,16 +55,26 @@ def _clear_stash(repo): return removed -def _clear_workspace(repo): - head = repo.scm.get_ref("HEAD") - ref_infos = list(exp_refs_by_baseline(repo.scm, head)) - remove_exp_refs(repo.scm, ref_infos) +def _clear_workspack_stash(repo, head): stashed = 0 stash_revs = repo.experiments.stash_revs + remove_index = [] for _, ref_info in stash_revs.items(): if head == ref_info.baseline_rev: - repo.experiments.stash.drop(ref_info.index) - stashed += 1 + remove_index.append(ref_info.index) + + for index in sorted(remove_index, reverse=True): + repo.experiments.stash.drop(index) + stashed += 1 + return stashed + + +def _clear_workspace(repo): + head = repo.scm.get_ref("HEAD") + ref_infos = list(exp_refs_by_baseline(repo.scm, head)) + remove_exp_refs(repo.scm, ref_infos) + stashed = _clear_workspack_stash(repo, head) + return stashed + len(ref_infos) diff --git a/tests/func/experiments/test_remove.py b/tests/func/experiments/test_remove.py index 49bc6fece6..f201eb017f 100644 --- a/tests/func/experiments/test_remove.py +++ b/tests/func/experiments/test_remove.py @@ -87,9 +87,10 @@ def test_remove_workspace(tmp_dir, scm, dvc, exp_stage): results = dvc.experiments.run(exp_stage.addressing, params=["foo=3"]) ref_info2 = first(exp_refs_by_rev(scm, first(results))) dvc.experiments.run(exp_stage.addressing, params=["foo=4"], queue=True) + dvc.experiments.run(exp_stage.addressing, params=["foo=5"], queue=True) removed = dvc.experiments.remove(workspace=True) - assert removed == 2 + assert removed == 3 assert len(dvc.experiments.stash) == 1 assert rev1 in dvc.experiments.stash_revs assert scm.get_ref(str(ref_info2)) is None From 357176e8be4fecf579795d146576dcdacad3b6c8 Mon Sep 17 00:00:00 2001 From: Gao Date: Mon, 16 Aug 2021 20:30:43 +0800 Subject: [PATCH 7/9] Update dvc/repo/experiments/remove.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Rowlands (변기호) --- dvc/repo/experiments/remove.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index cd1d1f5c62..6fac62266e 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -36,7 +36,7 @@ def remove( if workspace: removed += _clear_workspace(repo) if clear_all: - removed += _clear_all_baseline(repo) + removed += _clear_all(repo) if exp_names: remained = _remove_commited_exps(repo, exp_names) From 768f5d9daaee312666959e0ae8ab9ebc04adf906 Mon Sep 17 00:00:00 2001 From: Gao Date: Mon, 16 Aug 2021 20:30:52 +0800 Subject: [PATCH 8/9] Update dvc/repo/experiments/remove.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Peter Rowlands (변기호) --- dvc/repo/experiments/remove.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index 6fac62266e..6cc44106a2 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -78,7 +78,7 @@ def _clear_workspace(repo): return stashed + len(ref_infos) -def _clear_all_baseline(repo): +def _clear_all(repo): ref_infos = list(exp_refs(repo.scm)) remove_exp_refs(repo.scm, ref_infos) removed = len(repo.experiments.stash) From 03470f6fbc5bf5e2d05df8112ac5538bd78696f3 Mon Sep 17 00:00:00 2001 From: karajan1001 Date: Wed, 18 Aug 2021 16:07:02 +0800 Subject: [PATCH 9/9] delete workspace option --- dvc/command/experiments.py | 9 +----- dvc/repo/experiments/remove.py | 39 ++------------------------ tests/func/experiments/test_remove.py | 27 ++---------------- tests/unit/command/test_experiments.py | 9 ++---- 4 files changed, 9 insertions(+), 75 deletions(-) diff --git a/dvc/command/experiments.py b/dvc/command/experiments.py index d39e55cd09..e1fa5d83f1 100644 --- a/dvc/command/experiments.py +++ b/dvc/command/experiments.py @@ -749,7 +749,6 @@ def run(self): self.repo.experiments.remove( exp_names=self.args.experiment, queue=self.args.queue, - workspace=self.args.workspace, clear_all=self.args.all, ) @@ -1244,17 +1243,11 @@ def add_parser(subparsers, parent_parser): remove_group.add_argument( "--queue", action="store_true", help="Remove all queued experiments." ) - remove_group.add_argument( - "-w", - "--workspace", - action="store_true", - help="Remove all experiments based on current workspace.", - ) remove_group.add_argument( "-A", "--all", action="store_true", - help="Remove all experiments.", + help="Remove all committed experiments.", ) experiments_remove_parser.add_argument( "experiment", diff --git a/dvc/repo/experiments/remove.py b/dvc/repo/experiments/remove.py index 6cc44106a2..2df54728bd 100644 --- a/dvc/repo/experiments/remove.py +++ b/dvc/repo/experiments/remove.py @@ -7,12 +7,7 @@ from dvc.scm.base import RevError from .base import EXPS_NAMESPACE, ExpRefInfo -from .utils import ( - exp_refs, - exp_refs_by_baseline, - exp_refs_by_name, - remove_exp_refs, -) +from .utils import exp_refs, exp_refs_by_name, remove_exp_refs logger = logging.getLogger(__name__) @@ -23,18 +18,15 @@ def remove( repo, exp_names=None, queue=False, - workspace=False, clear_all=False, **kwargs, ): - if not any([exp_names, queue, workspace, clear_all]): + if not any([exp_names, queue, clear_all]): return 0 removed = 0 if queue: removed += _clear_stash(repo) - if workspace: - removed += _clear_workspace(repo) if clear_all: removed += _clear_all(repo) @@ -55,35 +47,10 @@ def _clear_stash(repo): return removed -def _clear_workspack_stash(repo, head): - stashed = 0 - stash_revs = repo.experiments.stash_revs - remove_index = [] - for _, ref_info in stash_revs.items(): - if head == ref_info.baseline_rev: - remove_index.append(ref_info.index) - - for index in sorted(remove_index, reverse=True): - repo.experiments.stash.drop(index) - stashed += 1 - return stashed - - -def _clear_workspace(repo): - head = repo.scm.get_ref("HEAD") - ref_infos = list(exp_refs_by_baseline(repo.scm, head)) - remove_exp_refs(repo.scm, ref_infos) - stashed = _clear_workspack_stash(repo, head) - - return stashed + len(ref_infos) - - def _clear_all(repo): ref_infos = list(exp_refs(repo.scm)) remove_exp_refs(repo.scm, ref_infos) - removed = len(repo.experiments.stash) - repo.experiments.stash.clear() - return removed + len(ref_infos) + return len(ref_infos) def _get_exp_stash_index(repo, ref_or_rev: str) -> Optional[int]: diff --git a/tests/func/experiments/test_remove.py b/tests/func/experiments/test_remove.py index f201eb017f..909561a080 100644 --- a/tests/func/experiments/test_remove.py +++ b/tests/func/experiments/test_remove.py @@ -74,29 +74,6 @@ def test_remove_special_queued_experiments(tmp_dir, scm, dvc, exp_stage): assert scm.get_ref(str(ref_info2)) is not None -def test_remove_workspace(tmp_dir, scm, dvc, exp_stage): - results = dvc.experiments.run(exp_stage.addressing, params=["foo=1"]) - ref_info1 = first(exp_refs_by_rev(scm, first(results))) - results = dvc.experiments.run( - exp_stage.addressing, params=["foo=2"], queue=True - ) - rev1 = first(results) - scm.add(["dvc.yaml", "dvc.lock", "copy.py", "params.yaml", "metrics.yaml"]) - scm.commit("update baseline") - - results = dvc.experiments.run(exp_stage.addressing, params=["foo=3"]) - ref_info2 = first(exp_refs_by_rev(scm, first(results))) - dvc.experiments.run(exp_stage.addressing, params=["foo=4"], queue=True) - dvc.experiments.run(exp_stage.addressing, params=["foo=5"], queue=True) - - removed = dvc.experiments.remove(workspace=True) - assert removed == 3 - assert len(dvc.experiments.stash) == 1 - assert rev1 in dvc.experiments.stash_revs - assert scm.get_ref(str(ref_info2)) is None - assert scm.get_ref(str(ref_info1)) is not None - - def test_remove_all(tmp_dir, scm, dvc, exp_stage): results = dvc.experiments.run(exp_stage.addressing, params=["foo=1"]) ref_info1 = first(exp_refs_by_rev(scm, first(results))) @@ -109,7 +86,7 @@ def test_remove_all(tmp_dir, scm, dvc, exp_stage): dvc.experiments.run(exp_stage.addressing, params=["foo=4"], queue=True) removed = dvc.experiments.remove(clear_all=True) - assert removed == 4 - assert len(dvc.experiments.stash) == 0 + assert removed == 2 + assert len(dvc.experiments.stash) == 2 assert scm.get_ref(str(ref_info2)) is None assert scm.get_ref(str(ref_info1)) is None diff --git a/tests/unit/command/test_experiments.py b/tests/unit/command/test_experiments.py index 8b3ff14319..d8043af94b 100644 --- a/tests/unit/command/test_experiments.py +++ b/tests/unit/command/test_experiments.py @@ -253,14 +253,12 @@ def test_experiments_pull(dvc, scm, mocker): @pytest.mark.parametrize( - "queue,workspace,clear_all", - [(True, False, False), (False, True, False), (False, False, True)], + "queue,clear_all", + [(True, False), (False, True)], ) -def test_experiments_remove(dvc, scm, mocker, queue, workspace, clear_all): +def test_experiments_remove(dvc, scm, mocker, queue, clear_all): if queue: args = "--queue" - if workspace: - args = "--workspace" if clear_all: args = "--all" cli_args = parse_args(["experiments", "remove", args]) @@ -274,6 +272,5 @@ def test_experiments_remove(dvc, scm, mocker, queue, workspace, clear_all): cmd.repo, exp_names=[], queue=queue, - workspace=workspace, clear_all=clear_all, )