diff --git a/dvc/scm.py b/dvc/scm.py index 2c8cdf78e..865388b0c 100644 --- a/dvc/scm.py +++ b/dvc/scm.py @@ -23,6 +23,9 @@ class Base(object): def ignore(self, path): pass + def ignore_remove(self, path): + pass + def ignore_file(self): pass @@ -68,7 +71,7 @@ class Git(Base): git_dir = os.path.join(root_dir, Git.GIT_DIR) return os.path.isdir(git_dir) - def ignore(self, path): + def _get_gitignore(self, path): entry = os.path.basename(path) gitignore = os.path.join(os.path.abspath(os.path.dirname(path)), self.GITIGNORE) @@ -77,6 +80,11 @@ class Git(Base): if not gitignore.startswith(self.root_dir): raise FileNotInRepoError() + return entry, gitignore + + def ignore(self, path): + entry, gitignore = self._get_gitignore(path) + ignore_list = [] if os.path.exists(gitignore): ignore_list = open(gitignore, 'r').readlines() @@ -90,6 +98,20 @@ class Git(Base): open(gitignore, 'a').write(content) + def ignore_remove(self, path): + entry, gitignore = self._get_gitignore(path) + + if not os.path.exists(gitignore): + return + + with open(gitignore, 'r') as fd: + lines = fd.readlines() + + filtered = list(filter(lambda x: x.strip() != entry.strip(), lines)) + + with open(gitignore, 'w') as fd: + fd.writelines(filtered) + def add(self, paths): self.repo.index.add(paths) diff --git a/dvc/stage.py b/dvc/stage.py index 29ddefbf5..0a180c67f 100644 --- a/dvc/stage.py +++ b/dvc/stage.py @@ -242,6 +242,7 @@ class Output(Dependency): self.project.logger.debug("Removing '{}'".format(self.path)) os.chmod(self.path, stat.S_IWUSR) os.unlink(self.path) + self.project.scm.ignore_remove(self.path) if os.path.exists(self.cache): os.chmod(self.cache, stat.S_IREAD) diff --git a/tests/test_scm.py b/tests/test_scm.py index f3aaeed47..aa54661eb 100644 --- a/tests/test_scm.py +++ b/tests/test_scm.py @@ -42,3 +42,6 @@ class TestIgnore(TestGit): git.ignore('foo') self.assertEqual(self._count_gitignore(), 1) + + git.ignore_remove('foo') + self.assertEqual(self._count_gitignore(), 0)