178
178
def tree_ignores_add_patterns(tree, name_pattern_list):
179
"""Add more ignore patterns to the ignore file in a tree.
180
If ignore file does not exist then it will be created.
181
The ignore file will be automatically added under version control.
179
"""Add a list of ignore patterns to the ignore file in a tree.
183
:param tree: Working tree to update the ignore list.
181
:param tree: Tree to retrieve the ignore list from.
182
:param name_pattern_list: List of ignore patterns.
185
# read in the existing ignores set
185
186
ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
186
187
if tree.has_filename(ifn):
187
188
f = open(ifn, 'rt')
189
igns = f.read().decode('utf-8')
190
# grab a copy of the raw contents of the file
191
orig_contents = f.read()
192
# then parse it from the start
194
ignores = parse_ignore_file(f)
195
# figure out what kind of line endings are used
196
newline = getattr(f, 'newlines', None)
197
if type(newline) is tuple:
199
elif newline is None:
195
# TODO: If the file already uses crlf-style termination, maybe
196
# we should use that for the newly added lines?
198
if igns and igns[-1] != '\n':
200
for name_pattern in name_pattern_list:
201
igns += name_pattern + '\n'
208
# write out the updated ignores set
203
209
f = atomicfile.AtomicFile(ifn, 'wb')
205
f.write(igns.encode('utf-8'))
211
f.write(orig_contents)
212
if len(orig_contents) > 0 and not orig_contents.endswith(newline):
214
for pattern in name_pattern_list:
215
if not pattern in ignores:
216
f.write(pattern.encode('utf-8'))
210
if not tree.path2id(bzrlib.IGNORE_FILENAME):
211
tree.add([bzrlib.IGNORE_FILENAME])
222
if not tree.path2id('.bzrignore'):
223
tree.add(['.bzrignore'])