diff --git a/dvc/command/add.py b/dvc/command/add.py index 79c523ace..eb0ac3af4 100644 --- a/dvc/command/add.py +++ b/dvc/command/add.py @@ -9,7 +9,11 @@ class CmdAdd(CmdBase): def run(self): for target in self.args.targets: try: - self.repo.add(target, recursive=self.args.recursive) + self.repo.add( + target, + recursive=self.args.recursive, + no_commit=self.args.no_commit, + ) except DvcException: logger.error("failed to add file") return 1 @@ -28,6 +32,12 @@ def add_parser(subparsers, parent_parser): default=False, help="Recursively add each file under the directory.", ) + add_parser.add_argument( + "--no-commit", + action="store_true", + default=False, + help="Don't put files/directories into cache.", + ) add_parser.add_argument( "targets", nargs="+", help="Input files/directories." ) diff --git a/dvc/repo/add.py b/dvc/repo/add.py index 6c1138cb1..ff82b17aa 100644 --- a/dvc/repo/add.py +++ b/dvc/repo/add.py @@ -1,7 +1,7 @@ import os -def add(self, fname, recursive=False): +def add(self, fname, recursive=False, no_commit=False): from dvc.stage import Stage fnames = [] @@ -30,7 +30,8 @@ def add(self, fname, recursive=False): continue stage.save() - stage.commit() + if not no_commit: + stage.commit() stages.append(stage) self.check_dag(self.stages() + stages) diff --git a/tests/test_add.py b/tests/test_add.py index 3fa99a326..bceb84aa6 100644 --- a/tests/test_add.py +++ b/tests/test_add.py @@ -264,3 +264,11 @@ class TestShouldUpdateStateEntryForDirectoryAfterAdd(TestDvc): ) self.assertEqual(ret, 0) self.assertEqual(file_md5_counter.mock.call_count, 3) + + +class TestAddNoCommit(TestDvc): + def test(self): + ret = main(["add", self.FOO, "--no-commit"]) + self.assertEqual(ret, 0) + self.assertTrue(os.path.isfile(self.FOO)) + self.assertEqual(len(os.listdir(self.dvc.cache.local.cache_dir)), 0)