repo: raise exception if root_dir is not a directory path (#3295)

* 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.

* repo: set message from raise for NotDvcRepoError
This commit is contained in:
Saugat Pachhai
2020-02-10 20:57:34 +02:00
committed by GitHub
parent 7821fa6866
commit 8610c0c95e
2 changed files with 15 additions and 21 deletions
+1 -12
View File
@@ -112,18 +112,7 @@ class MoveNotDataSourceError(DvcException):
class NotDvcRepoError(DvcException):
"""Thrown if a directory is not a DVC repo.
Args:
root (str): path to the directory.
"""
def __init__(self, root):
msg = (
"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):
+14 -9
View File
@@ -129,19 +129,24 @@ class Repo(object):
@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.realpath(root or os.curdir)
if not os.path.isdir(root_dir):
raise NotDvcRepoError("directory '{}' does not exist".format(root))
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)
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):