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

external repo: checkout revision before initializing dvc repo #2852

Merged
merged 8 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 16 additions & 3 deletions dvc/external_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ def _external_repo(url=None, rev=None, cache_dir=None):
REPO_CACHE[url, None, None] = clean_clone_path

# Adjust new clone/copy to fit rev and cache_dir

# Checkout needs to be done first because current branch might not be
# DVC repository
if rev is not None:
_git_checkout(new_path, rev)

repo = Repo(new_path)
try:
if rev is not None:
repo.scm.checkout(rev)

if cache_dir is not None:
cache_config = CacheConfig(repo.config)
cache_config.set_dir(cache_dir, level=Config.LEVEL_LOCAL)
Comment on lines 72 to 74
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can skip creating Repo and use Config directly here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it can be done with just config, though it would require a change in test_get_from_non_dvc_repo. I'll create an issue for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not require changing any tests. This should be a self-contained change.

Expand All @@ -77,6 +80,16 @@ def _external_repo(url=None, rev=None, cache_dir=None):
return new_path


def _git_checkout(repo_path, revision):
from dvc.scm import Git

git = Git(repo_path)
try:
git.checkout(revision)
finally:
git.close()


def clean_repos():
# Outside code should not see cache while we are removing
repo_paths = list(REPO_CACHE.values())
Expand Down
18 changes: 18 additions & 0 deletions tests/func/test_get.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

import filecmp
import logging
import os

import pytest
Expand All @@ -11,6 +12,7 @@
from dvc.repo import Repo
from dvc.system import System
from dvc.utils import makedirs
from dvc.utils.compat import fspath
from tests.utils import trees_equal


Expand Down Expand Up @@ -87,3 +89,19 @@ def test_get_to_dir(dname, erepo):

assert os.path.isdir(dname)
assert filecmp.cmp(erepo.FOO, dst, shallow=False)


def test_get_from_non_dvc_master(erepo, tmp_path, monkeypatch, caplog):
monkeypatch.chdir(fspath(tmp_path))
erepo.dvc.scm.repo.index.remove([".dvc"], r=True)
efiop marked this conversation as resolved.
Show resolved Hide resolved
erepo.dvc.scm.commit("remove .dvc")

caplog.clear()
imported_file = "foo_imported"
with caplog.at_level(logging.INFO, logger="dvc"):
Repo.get(erepo._root_dir, erepo.FOO, out=imported_file, rev="branch")

assert caplog.text == ""
Comment on lines +101 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, why do we test that there is no info output to logger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#2858
I think that we should use caplog more often in order to prevent spawning of UI issues that are quite common recently. Do you think I should also check capsys?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably get progress bars in capsys, not easy to test, especially automatically, but your call, I don't have a strong opinion here πŸ™‚

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pbars go to stderr by default, so shouldn't be an issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why Repo.get() has not output though.

assert filecmp.cmp(
os.path.join(erepo._root_dir, erepo.FOO), imported_file, shallow=False
)