From 93246f0658f5be6422b7e2d5e78bc9aeb90c370d Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Thu, 19 Mar 2020 16:51:34 +0545 Subject: [PATCH] version: fix bug when .git dir is removed, show more info regarding repo (#3508) * version: fix error when .git dir is removed * version: show dvc/git information * version: remove introducing no_scm params in Repo api --- dvc/command/version.py | 16 ++++++++++++++++ tests/func/test_version.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/dvc/command/version.py b/dvc/command/version.py index 3a4fe042d..539c7b1ef 100644 --- a/dvc/command/version.py +++ b/dvc/command/version.py @@ -15,6 +15,7 @@ from dvc.utils import is_binary, relpath from dvc.command.base import CmdBaseNoRepo, append_doc_link from dvc.version import __version__ from dvc.exceptions import DvcException, NotDvcRepoError +from dvc.scm.base import SCMError from dvc.system import System from dvc.utils.pkg import PKG @@ -62,6 +63,11 @@ class CmdVersion(CmdBaseNoRepo): except NotDvcRepoError: root_directory = os.getcwd() + except SCMError: + root_directory = os.getcwd() + info.append("Repo: dvc, git (broken)") + else: + info.append("Repo: {}".format(_get_dvc_repo_info(repo))) if psutil: fs_root = self.get_fs_type(os.path.abspath(root_directory)) @@ -125,6 +131,16 @@ class CmdVersion(CmdBaseNoRepo): return ", ".join(supported_remotes) +def _get_dvc_repo_info(repo): + if repo.config.get("core", {}).get("no_scm", False): + return "dvc (no_scm)" + + if repo.root_dir != repo.scm.root_dir: + return "dvc (subdir), git" + + return "dvc, git" + + def add_parser(subparsers, parent_parser): VERSION_HELP = "Show DVC version and system/environment information." diff --git a/tests/func/test_version.py b/tests/func/test_version.py index 44e44d552..9c31b3f84 100644 --- a/tests/func/test_version.py +++ b/tests/func/test_version.py @@ -1,5 +1,6 @@ import os import re +import shutil import pytest @@ -7,9 +8,12 @@ from dvc.command.version import psutil from dvc.main import main -def test_info_in_repo(tmp_dir, dvc, caplog): +@pytest.mark.parametrize("scm_init", [True, False]) +def test_info_in_repo(scm_init, tmp_dir, caplog): + tmp_dir.init(scm=scm_init, dvc=True) # Create `.dvc/cache`, that is needed to check supported link types. - os.mkdir(dvc.cache.local.cache_dir) + os.mkdir(tmp_dir.dvc.cache.local.cache_dir) + assert main(["version"]) == 0 assert re.search(r"DVC version: \d+\.\d+\.\d+", caplog.text) @@ -22,6 +26,32 @@ def test_info_in_repo(tmp_dir, dvc, caplog): r"(Cache: (.*link - (not )?supported(,\s)?){3})", caplog.text ) + if scm_init: + assert "Repo: dvc, git" in caplog.text + else: + assert "Repo: dvc (no_scm)" in caplog.text + + +def test_info_in_subdir(tmp_dir, scm, caplog): + dvc_subdir = tmp_dir / "subdir" + dvc_subdir.mkdir() + + with dvc_subdir.chdir(): + dvc_subdir.init(scm=False, dvc=True) + with dvc_subdir.dvc.config.edit() as conf: + del conf["core"]["no_scm"] + + assert main(["version"]) == 0 + + assert "Repo: dvc (subdir), git" in caplog.text + + +def test_info_in_broken_git_repo(tmp_dir, dvc, scm, caplog): + shutil.rmtree(dvc.scm.dir) + assert main(["version"]) == 0 + + assert "Repo: dvc, git (broken)" in caplog.text + @pytest.mark.skipif(psutil is None, reason="No psutil.") def test_fs_info_in_repo(tmp_dir, dvc, caplog): @@ -42,6 +72,7 @@ def test_info_outside_of_repo(tmp_dir, caplog): assert re.search(r"Package: .*", caplog.text) assert re.search(r"Supported remotes: .*", caplog.text) assert not re.search(r"(Cache: (.*link - (not )?(,\s)?){3})", caplog.text) + assert "Repo:" not in caplog.text @pytest.mark.skipif(psutil is None, reason="No psutil.")