From 55cb677229548a0f23e6210c8d4bd90a9820b5b4 Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Mon, 10 Feb 2020 15:23:46 +0545 Subject: [PATCH 1/2] repo: throw if root_dir in Repo.__init__() is not a directory path When Repo is instantiated with invalid urls such as 'http://github.com/iterative/dvc.git', it does os.path.realpath which is not intelligent enough and just concatenates with current working directory, which is then traversed outward. It might happen that the outward folder is a dvc directory which is not really what we want. So, a check for root to be a directory is added. Otherwise, NotDvcRepoError is thrown with a custom message. --- dvc/exceptions.py | 4 ++-- dvc/repo/__init__.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/dvc/exceptions.py b/dvc/exceptions.py index 357f8c7cfb..1ab9390116 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -118,8 +118,8 @@ class NotDvcRepoError(DvcException): root (str): path to the directory. """ - def __init__(self, root): - msg = ( + def __init__(self, root, msg=None): + msg = msg or ( "you are not inside of a DVC repository " "(checked up to mount point '{}')" ) diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index 9bdb04cd76..a7a79ffb2b 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -129,19 +129,19 @@ def __repr__(self): @classmethod def find_root(cls, root=None): - if root is None: - root = os.getcwd() - else: - root = os.path.abspath(os.path.realpath(root)) + root_dir = os.path.abspath(os.path.realpath(root or os.getcwd())) + + if not os.path.isdir(root_dir): + raise NotDvcRepoError(root, msg="folder {} does not exist") while True: - dvc_dir = os.path.join(root, cls.DVC_DIR) + dvc_dir = os.path.join(root_dir, cls.DVC_DIR) if os.path.isdir(dvc_dir): - return root - if os.path.ismount(root): + return root_dir + if os.path.ismount(root_dir): break - root = os.path.dirname(root) - raise NotDvcRepoError(root) + root_dir = os.path.dirname(root_dir) + raise NotDvcRepoError(root_dir) @classmethod def find_dvc_dir(cls, root=None): From 648785d45feddc41a7d05e3a5a760e4719cdf29b Mon Sep 17 00:00:00 2001 From: Saugat Pachhai Date: Mon, 10 Feb 2020 19:48:56 +0545 Subject: [PATCH 2/2] repo: set message from raise for NotDvcRepoError --- dvc/exceptions.py | 13 +------------ dvc/repo/__init__.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/dvc/exceptions.py b/dvc/exceptions.py index 1ab9390116..4af6111080 100644 --- a/dvc/exceptions.py +++ b/dvc/exceptions.py @@ -112,18 +112,7 @@ def __init__(self, path): class NotDvcRepoError(DvcException): - """Thrown if a directory is not a DVC repo. - - Args: - root (str): path to the directory. - """ - - def __init__(self, root, msg=None): - msg = msg or ( - "you are not inside of a DVC repository " - "(checked up to mount point '{}')" - ) - super().__init__(msg.format(root)) + """Thrown if a directory is not a DVC repo""" class DvcParserError(DvcException): diff --git a/dvc/repo/__init__.py b/dvc/repo/__init__.py index a7a79ffb2b..16062042fd 100644 --- a/dvc/repo/__init__.py +++ b/dvc/repo/__init__.py @@ -129,10 +129,10 @@ def __repr__(self): @classmethod def find_root(cls, root=None): - root_dir = os.path.abspath(os.path.realpath(root or os.getcwd())) + root_dir = os.path.realpath(root or os.curdir) if not os.path.isdir(root_dir): - raise NotDvcRepoError(root, msg="folder {} does not exist") + raise NotDvcRepoError("directory '{}' does not exist".format(root)) while True: dvc_dir = os.path.join(root_dir, cls.DVC_DIR) @@ -141,7 +141,12 @@ def find_root(cls, root=None): if os.path.ismount(root_dir): break root_dir = os.path.dirname(root_dir) - raise NotDvcRepoError(root_dir) + + message = ( + "you are not inside of a DVC repository " + "(checked up to mount point '{}')" + ).format(root_dir) + raise NotDvcRepoError(message) @classmethod def find_dvc_dir(cls, root=None):