Merge pull request #1700 from pared/1699

add: prevent adding git files
This commit is contained in:
Ruslan Kuprieiev
2019-03-13 22:22:39 +02:00
committed by GitHub
7 changed files with 69 additions and 10 deletions
+4
View File
@@ -415,3 +415,7 @@ class Repo(object):
raise OutputNotFoundError(path)
return matched
def is_dvc_internal(self, path):
path_parts = os.path.normpath(path).split(os.path.sep)
return self.DVC_DIR in path_parts
+14 -10
View File
@@ -11,16 +11,7 @@ def add(repo, target, recursive=False, no_commit=False, fname=None):
if recursive and fname:
raise RecursiveAddingWhileUsingFilename()
targets = [target]
if os.path.isdir(target) and recursive:
targets = [
file
for file in walk_files(target)
if not Stage.is_stage_file(file)
if os.path.basename(file) != repo.scm.ignore_file
if not repo.scm.is_tracked(file)
]
targets = _find_all_targets(repo, target, recursive)
stages = _create_stages(repo, targets, fname, no_commit)
@@ -31,6 +22,19 @@ def add(repo, target, recursive=False, no_commit=False, fname=None):
return stages
def _find_all_targets(repo, target, recursive):
if os.path.isdir(target) and recursive:
return [
file
for file in walk_files(target)
if not repo.is_dvc_internal(file)
if not Stage.is_stage_file(file)
if not repo.scm.belongs_to_scm(file)
if not repo.scm.is_tracked(file)
]
return [target]
def _create_stages(repo, targets, fname, no_commit):
stages = []
+3
View File
@@ -162,3 +162,6 @@ class Base(object):
Method to add file to mechanism that will remind user
to track new files
"""
def belongs_to_scm(self, path):
"""Return boolean whether file belongs to scm"""
+5
View File
@@ -198,3 +198,8 @@ class Git(Base):
def track_file(self, path):
self.files_to_track.append(path)
def belongs_to_scm(self, path):
basename = os.path.basename(path)
path_parts = os.path.normpath(path).split(os.path.sep)
return basename == self.ignore_file or Git.GIT_DIR in path_parts
+13
View File
@@ -476,3 +476,16 @@ class TestShouldCleanUpAfterFailedAdd(TestDvc):
gitignore_content = get_gitignore_content()
self.assertFalse(any(self.BAR in line for line in gitignore_content))
class TestShouldNotTrackGitInternalFiles(TestDvc):
def test(self):
stage_creator_spy = spy(dvc.repo.add._create_stages)
with patch.object(dvc.repo.add, "_create_stages", stage_creator_spy):
ret = main(["add", "-R", self.dvc.root_dir])
self.assertEqual(0, ret)
created_stages_filenames = stage_creator_spy.mock.call_args[0][0]
for fname in created_stages_filenames:
self.assertNotIn(".git", fname)
+13
View File
@@ -0,0 +1,13 @@
import os
from tests.basic_env import TestDvc
class TestIsDvcInternal(TestDvc):
def test_return_false_on_non_dvc_file(self):
path = os.path.join("path", "to-non-.dvc", "file")
self.assertFalse(self.dvc.is_dvc_internal(path))
def test_return_true_on_dvc_internal_file(self):
path = os.path.join("path", "to", ".dvc", "file")
self.assertTrue(path)
+17
View File
@@ -0,0 +1,17 @@
import os
from tests.basic_env import TestDvc
class TestGit(TestDvc):
def test_belongs_to_scm_true_on_gitignore(self):
path = os.path.join("path", "to", ".gitignore")
self.assertTrue(self.dvc.scm.belongs_to_scm(path))
def test_belongs_to_scm_true_on_git_internal(self):
path = os.path.join("path", "to", ".git", "internal", "file")
self.assertTrue(self.dvc.scm.belongs_to_scm(path))
def test_belongs_to_scm_false(self):
path = os.path.join("some", "non-.git", "file")
self.assertFalse(self.dvc.scm.belongs_to_scm(path))