experiments: fix bug where wrong git branch checked-out before pull (#4450)

This commit is contained in:
Peter Rowlands (변기호)
2020-08-24 15:47:25 +03:00
committed by GitHub
parent bfb98e9288
commit f789cdff89
2 changed files with 41 additions and 2 deletions
+14 -2
View File
@@ -162,13 +162,25 @@ class Experiments:
def _scm_checkout(self, rev):
self.scm.repo.git.reset(hard=True)
if self.scm.repo.head.is_detached:
# switch back to default branch
self.scm.repo.heads[0].checkout()
self._checkout_default_branch()
if not Git.is_sha(rev) or not self.scm.has_rev(rev):
self.scm.pull()
logger.debug("Checking out experiment commit '%s'", rev)
self.scm.checkout(rev)
def _checkout_default_branch(self):
# switch to default branch
git_repo = self.scm.repo
origin_refs = git_repo.remotes["origin"].refs
ref = origin_refs["HEAD"].reference
branch_name = ref.name.split("/")[-1]
if branch_name in git_repo.heads:
branch = git_repo.heads[branch_name]
else:
branch = git_repo.create_head(branch_name, ref)
branch.set_tracking_branch(ref)
branch.checkout()
def _stash_exp(self, *args, params: Optional[dict] = None, **kwargs):
"""Stash changes from the current (parent) workspace as an experiment.
@@ -22,3 +22,30 @@ def test_new_simple(tmp_dir, scm, dvc, mocker):
assert (
tmp_dir / ".dvc" / "experiments" / "metrics.yaml"
).read_text() == "foo: 2"
def test_update_with_pull(tmp_dir, scm, dvc, mocker):
tmp_dir.gen("copy.py", COPY_SCRIPT)
tmp_dir.gen("params.yaml", "foo: 1")
stage = dvc.run(
cmd="python copy.py params.yaml metrics.yaml",
metrics_no_cache=["metrics.yaml"],
params=["foo"],
name="copy-file",
)
scm.add(["dvc.yaml", "dvc.lock", "copy.py", "params.yaml", "metrics.yaml"])
scm.commit("init")
expected_revs = [scm.get_rev()]
tmp_dir.gen("params.yaml", "foo: 2")
dvc.reproduce(stage.addressing, experiment=True)
scm.add(["dvc.yaml", "dvc.lock", "params.yaml", "metrics.yaml"])
scm.commit("promote experiment")
expected_revs.append(scm.get_rev())
tmp_dir.gen("params.yaml", "foo: 3")
dvc.reproduce(stage.addressing, experiment=True)
exp_scm = dvc.experiments.scm
for rev in expected_revs:
assert exp_scm.has_rev(rev)