Skip to content
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

Clean up remotes's exps #6471

Merged
merged 18 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dvc/command/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ def run(self):
exp_names=self.args.experiment,
queue=self.args.queue,
clear_all=self.args.all,
remote=self.args.git_remote,
)

return 0
Expand Down Expand Up @@ -1249,6 +1250,12 @@ def add_parser(subparsers, parent_parser):
action="store_true",
help="Remove all committed experiments.",
)
remove_group.add_argument(
"-r",
"--git-remote",
jorgeorpinel marked this conversation as resolved.
Show resolved Hide resolved
metavar="<git_remote>",
karajan1001 marked this conversation as resolved.
Show resolved Hide resolved
help="Name of the Git remote to GC all of the experiment branches.",
jorgeorpinel marked this conversation as resolved.
Show resolved Hide resolved
)
experiments_remove_parser.add_argument(
"experiment",
nargs="*",
Expand Down
15 changes: 13 additions & 2 deletions dvc/repo/experiments/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, exp_refs_by_name, remote_exp_refs, remove_exp_refs

logger = logging.getLogger(__name__)

Expand All @@ -19,16 +19,19 @@ def remove(
exp_names=None,
queue=False,
clear_all=False,
remote=None,
**kwargs,
):
if not any([exp_names, queue, clear_all]):
if not any([exp_names, queue, clear_all, remote]):
return 0

removed = 0
if queue:
removed += _clear_stash(repo)
if clear_all:
removed += _clear_all(repo)
if remote:
removed += _clear_remote(repo, remote)

if exp_names:
remained = _remove_commited_exps(repo, exp_names)
Expand All @@ -41,6 +44,14 @@ def remove(
return removed


def _clear_remote(repo, remote: str):
ref_infos = list(remote_exp_refs(repo.scm, remote))
for ref_info in ref_infos:
ref_name = str(ref_info)
repo.scm.push_refspec(remote, None, ref_name)
return len(ref_infos)


def _clear_stash(repo):
removed = len(repo.experiments.stash)
repo.experiments.stash.clear()
Expand Down
4 changes: 3 additions & 1 deletion dvc/scm/git/backend/dulwich.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,11 @@ def push_refspec(
) from exc

def update_refs(refs):
from dulwich.objects import ZERO_SHA

new_refs = {}
for ref, value in zip(dest_refs, values):
if ref in refs:
if ref in refs and value != ZERO_SHA:
karajan1001 marked this conversation as resolved.
Show resolved Hide resolved
local_sha = self.repo.refs[ref]
remote_sha = refs[ref]
try:
Expand Down
24 changes: 24 additions & 0 deletions tests/func/experiments/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,27 @@ def test_remove_all(tmp_dir, scm, dvc, exp_stage):
assert len(dvc.experiments.stash) == 2
assert scm.get_ref(str(ref_info2)) is None
assert scm.get_ref(str(ref_info1)) is None


@pytest.mark.parametrize("use_url", [True, False])
def test_remove_remote(tmp_dir, scm, dvc, exp_stage, git_upstream, use_url):
remote = git_upstream.url if use_url else git_upstream.remote

results = dvc.experiments.run(exp_stage.addressing, params=["foo=1"])
exp1 = first(results)
ref_info1 = first(exp_refs_by_rev(scm, exp1))

results = dvc.experiments.run(exp_stage.addressing, params=["foo=2"])
exp2 = first(results)
ref_info2 = first(exp_refs_by_rev(scm, exp2))

dvc.experiments.push(remote, ref_info1.name)
dvc.experiments.push(remote, ref_info2.name)
assert git_upstream.scm.get_ref(str(ref_info1)) == exp1
assert git_upstream.scm.get_ref(str(ref_info2)) == exp2

dvc.experiments.remove(experiments=[ref_info1])
dvc.experiments.remove(remote=remote)

assert git_upstream.scm.get_ref(str(ref_info1)) is None
assert git_upstream.scm.get_ref(str(ref_info2)) is None
15 changes: 9 additions & 6 deletions tests/unit/command/test_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,17 @@ def test_experiments_pull(dvc, scm, mocker):


@pytest.mark.parametrize(
"queue,clear_all",
[(True, False), (False, True)],
"queue,clear_all,remote",
[(True, False, None), (False, True, None), (False, False, True)],
)
def test_experiments_remove(dvc, scm, mocker, queue, clear_all):
def test_experiments_remove(dvc, scm, mocker, queue, clear_all, remote):
if queue:
args = "--queue"
args = ["--queue"]
if clear_all:
args = "--all"
cli_args = parse_args(["experiments", "remove", args])
args = ["--all"]
if remote:
args = ["--git-remote", "myremote"]
cli_args = parse_args(["experiments", "remove"] + args)
assert cli_args.func == CmdExperimentsRemove

cmd = cli_args.func(cli_args)
Expand All @@ -273,4 +275,5 @@ def test_experiments_remove(dvc, scm, mocker, queue, clear_all):
exp_names=[],
queue=queue,
clear_all=clear_all,
remote="myremote" if remote else None,
)