diff --git a/dvc/scm.py b/dvc/scm.py index 71eaecf0e..2c8cdf78e 100644 --- a/dvc/scm.py +++ b/dvc/scm.py @@ -80,11 +80,12 @@ class Git(Base): ignore_list = [] if os.path.exists(gitignore): ignore_list = open(gitignore, 'r').readlines() - if entry in ignore_list: + filtered = list(filter(lambda x: x.strip() == entry.strip(), ignore_list)) + if len(filtered) != 0: return content = entry - if ignore_list: + if len(ignore_list) > 0: content = '\n' + content open(gitignore, 'a').write(content) diff --git a/tests/test_scm.py b/tests/test_scm.py index 70fb50bb6..f3aaeed47 100644 --- a/tests/test_scm.py +++ b/tests/test_scm.py @@ -20,13 +20,25 @@ class TestSCMGit(TestGit): def test_is_repo(self): self.assertTrue(Git.is_repo(os.curdir)) - def test_ignore(self): - Git(self._root_dir).ignore('foo') - self.assertTrue(os.path.isfile(Git.GITIGNORE)) - self.assertTrue('foo' in open(Git.GITIGNORE, 'r').readlines()) - def test_commit(self): G = Git(self._root_dir) G.add(['foo']) G.commit('add') self.assertTrue('foo' in self.git.git.ls_files()) + + +class TestIgnore(TestGit): + def _count_gitignore(self): + with open(Git.GITIGNORE, 'r') as fd: + lines = fd.readlines() + return len(list(filter(lambda x: x.strip() == 'foo', lines))) + + def test(self): + git = Git(self._root_dir) + + git.ignore('foo') + self.assertTrue(os.path.isfile(Git.GITIGNORE)) + self.assertEqual(self._count_gitignore(), 1) + + git.ignore('foo') + self.assertEqual(self._count_gitignore(), 1)