mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-19 23:11:44 +01:00
dvc: handle double dvc init
Fixes #889 Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
@@ -86,6 +86,11 @@ def parse_args(argv=None):
|
||||
action='store_true',
|
||||
default=False,
|
||||
help="Initiate dvc in directory that is not tracked by any scm tool(e.g. git)")
|
||||
init_parser.add_argument('-f',
|
||||
'--force',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Force initialization even if \'.dvc\' exists')
|
||||
init_parser.set_defaults(func=CmdInit)
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ class CmdInit(object):
|
||||
|
||||
def run_cmd(self):
|
||||
try:
|
||||
Project.init('.', no_scm=self.args.no_scm)
|
||||
Project.init('.', no_scm=self.args.no_scm, force=self.args.force)
|
||||
except InitError as e:
|
||||
Logger.error('Failed to initiate dvc', e)
|
||||
return 1
|
||||
|
||||
+7
-1
@@ -65,7 +65,7 @@ class Project(object):
|
||||
self.updater.check()
|
||||
|
||||
@staticmethod
|
||||
def init(root_dir=os.curdir, no_scm=False):
|
||||
def init(root_dir=os.curdir, no_scm=False, force=False):
|
||||
"""
|
||||
Initiate dvc project in directory.
|
||||
|
||||
@@ -86,6 +86,12 @@ class Project(object):
|
||||
msg = "{} is not tracked by any supported scm tool(e.g. git).".format(root_dir)
|
||||
raise InitError(msg)
|
||||
|
||||
if os.path.isdir(dvc_dir):
|
||||
if not force:
|
||||
msg = "'{}' exists. Use '-f' to force."
|
||||
raise InitError(msg.format(os.path.relpath(dvc_dir)))
|
||||
shutil.rmtree(dvc_dir)
|
||||
|
||||
os.mkdir(dvc_dir)
|
||||
|
||||
config = Config.init(dvc_dir)
|
||||
|
||||
@@ -24,6 +24,21 @@ class TestInit(TestGit):
|
||||
self._test_init()
|
||||
|
||||
|
||||
class TestDoubleInit(TestInit):
|
||||
def test(self):
|
||||
ret = main(['init'])
|
||||
self.assertEqual(ret, 0)
|
||||
self._test_init()
|
||||
|
||||
ret = main(['init'])
|
||||
self.assertNotEqual(ret, 0)
|
||||
self._test_init()
|
||||
|
||||
ret = main(['init', '--force'])
|
||||
self.assertEqual(ret, 0)
|
||||
self._test_init()
|
||||
|
||||
|
||||
class TestInitNoSCMFail(TestDir):
|
||||
def test_api(self):
|
||||
with self.assertRaises(InitError):
|
||||
|
||||
Reference in New Issue
Block a user