From 0bacd1cccf1f515244e38a96047d12a23fc43fc3 Mon Sep 17 00:00:00 2001 From: gsvolt Date: Sat, 26 Sep 2020 22:44:00 -0400 Subject: [PATCH 1/2] scm: Git: _add_entry_to_gitignore: writes unique entry to .gitignore Fixes #4327 --- dvc/scm/git.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dvc/scm/git.py b/dvc/scm/git.py index a3317218ea..acc4bc0526 100644 --- a/dvc/scm/git.py +++ b/dvc/scm/git.py @@ -210,6 +210,7 @@ def _add_entry_to_gitignore(self, entry, gitignore): entry = GitWildMatchPattern.escape(entry) with open(gitignore, "a+", encoding="utf-8") as fobj: + unique_lines=set(fobj.readlines()) fobj.seek(0, os.SEEK_END) if fobj.tell() == 0: # Empty file @@ -218,7 +219,9 @@ def _add_entry_to_gitignore(self, entry, gitignore): fobj.seek(fobj.tell() - 1, os.SEEK_SET) last = fobj.read(1) prefix = "" if last == "\n" else "\n" - fobj.write(f"{prefix}{entry}\n") + new_entry=f"{prefix}{entry}\n" + if new_entry not in unique_lines: + fobj.write(new_entry) def ignore_remove(self, path): entry, gitignore = self._get_gitignore(path) From 01e2a7f87d2bb2d58bef6e30be6d7c126d9995ff Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 27 Sep 2020 02:55:33 +0000 Subject: [PATCH 2/2] Restyled by black --- dvc/scm/git.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dvc/scm/git.py b/dvc/scm/git.py index acc4bc0526..d5d72c1b84 100644 --- a/dvc/scm/git.py +++ b/dvc/scm/git.py @@ -210,7 +210,7 @@ def _add_entry_to_gitignore(self, entry, gitignore): entry = GitWildMatchPattern.escape(entry) with open(gitignore, "a+", encoding="utf-8") as fobj: - unique_lines=set(fobj.readlines()) + unique_lines = set(fobj.readlines()) fobj.seek(0, os.SEEK_END) if fobj.tell() == 0: # Empty file @@ -219,7 +219,7 @@ def _add_entry_to_gitignore(self, entry, gitignore): fobj.seek(fobj.tell() - 1, os.SEEK_SET) last = fobj.read(1) prefix = "" if last == "\n" else "\n" - new_entry=f"{prefix}{entry}\n" + new_entry = f"{prefix}{entry}\n" if new_entry not in unique_lines: fobj.write(new_entry)