git: check if gitignore already has file before modifying it

Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
Ruslan Kuprieiev
2017-10-31 16:03:43 +02:00
parent 54c4ce1b49
commit f91abed416
+17 -1
View File
@@ -96,6 +96,17 @@ class GitWrapperI(object):
def commit_all_changes_and_log_status(self, message):
pass
@staticmethod
def _file_contains(fname, s):
if not os.path.isfile(fname):
return False
with open(fname, 'r') as fd:
if s in fd.read():
return True
return False
@staticmethod
def modify_gitignore(flist):
for fname in flist:
@@ -104,8 +115,13 @@ class GitWrapperI(object):
# git and we will not have any problems with --porcelain output for
# specific changed files.
gitignore = os.path.join(os.path.dirname(fname), '.gitignore')
entry = os.path.basename(fname)
if GitWrapperI._file_contains(gitignore, entry + '\n'):
return
with open(gitignore, 'a') as fd:
fd.write('\n' + os.path.basename(fname))
fd.write('\n' + entry)
class GitWrapper(GitWrapperI):