exp branch: Make branch arg optional.

Default to `f"{ref_info.name}-branch"` where ref_info is inferred from the `exp_rev` argument.

Co-authored-by: Dave Berenbaum <[email protected]>
This commit is contained in:
David de la Iglesia Castro
2023-03-24 19:11:06 +01:00
co-authored by Dave Berenbaum
parent 5485275b92
commit e9fb0b92aa
4 changed files with 24 additions and 7 deletions
+9 -1
View File
@@ -26,5 +26,13 @@ def add_parser(experiments_subparsers, parent_parser):
experiments_branch_parser.add_argument(
"experiment", help="Experiment to be promoted."
)
experiments_branch_parser.add_argument("branch", help="Name for the new Git branch")
experiments_branch_parser.add_argument(
"branch",
nargs="?",
default=None,
help=(
"Optional name for the new Git branch. "
"Defaults to '{experiment-name}-branch'."
),
)
experiments_branch_parser.set_defaults(func=CmdExperimentsBranch)
+3 -1
View File
@@ -13,7 +13,7 @@ logger = logging.getLogger(__name__)
@locked
@scm_context
def branch(repo, exp_rev, branch_name, *args, **kwargs):
def branch(repo, exp_rev, branch_name=None, **kwargs):
from dvc.scm import resolve_rev
try:
@@ -46,6 +46,8 @@ def branch(repo, exp_rev, branch_name, *args, **kwargs):
if not ref_info:
raise InvalidExpRevError(exp_rev)
branch_name = branch_name or f"{ref_info.name}-branch"
branch_ref = f"refs/heads/{branch_name}"
if repo.scm.get_ref(branch_ref):
raise InvalidArgumentError(f"Git branch '{branch_name}' already exists.")
+2 -1
View File
@@ -246,11 +246,12 @@ def test_branch(tmp_dir, scm, dvc, exp_stage):
with pytest.raises(InvalidArgumentError):
dvc.experiments.branch("foo", "branch-exists")
dvc.experiments.branch("foo")
dvc.experiments.branch("foo", "branch-name")
dvc.experiments.branch(exp_a, "branch-rev")
dvc.experiments.branch(ref_a, "branch-ref")
for name in ["branch-name", "branch-rev", "branch-ref"]:
for name in ["foo-branch", "branch-name", "branch-rev", "branch-ref"]:
assert name in scm.list_branches()
assert scm.resolve_rev(name) == exp_a
+10 -4
View File
@@ -187,15 +187,21 @@ def test_experiments_gc(dvc, scm, mocker):
def test_experiments_branch(dvc, scm, mocker):
cli_args = parse_args(["experiments", "branch", "expname", "branchname"])
m = mocker.patch("dvc.repo.experiments.branch.branch", return_value={})
cli_args = parse_args(["experiments", "branch", "expname"])
assert cli_args.func == CmdExperimentsBranch
cmd = cli_args.func(cli_args)
m = mocker.patch("dvc.repo.experiments.branch.branch", return_value={})
assert cmd.run() == 0
m.assert_called_once_with(cmd.repo, "expname", "branchname")
m.assert_called_with(cmd.repo, "expname", None)
cli_args = parse_args(["experiments", "branch", "expname", "branchname"])
cmd = cli_args.func(cli_args)
assert cmd.run() == 0
m.assert_called_with(cmd.repo, "expname", "branchname")
def test_experiments_list(dvc, scm, mocker):