56
53
def parse_ignore_file(f):
57
54
"""Read in all of the lines in the file and turn it into an ignore list
59
Continue in the case of utf8 decoding errors, and emit a warning when
60
such and error is found. Optimise for the common case -- no decoding
56
Continue in the case of utf8 decoding errors, and emit a warning when
57
such and error is found. Optimise for the common case -- no decoding
194
191
if tree.has_filename(ifn):
195
192
with open(ifn, 'rbU') as f:
196
193
file_contents = f.read()
197
# figure out what kind of line endings are used
198
newline = getattr(f, 'newlines', None)
199
if isinstance(newline, tuple):
201
elif newline is None:
202
newline = os.linesep.encode()
194
if file_contents.endswith(b'\r\n'):
204
199
file_contents = b""
205
200
newline = os.linesep.encode()
207
sio = BytesIO(file_contents)
202
with BytesIO(file_contents) as sio:
209
203
ignores = parse_ignore_file(sio)
213
205
# write out the updated ignores set
214
f = atomicfile.AtomicFile(ifn, 'wb')
206
with atomicfile.AtomicFile(ifn, 'wb') as f:
216
207
# write the original contents, preserving original line endings
217
f.write(newline.join(file_contents.split(b'\n')))
208
f.write(file_contents)
218
209
if len(file_contents) > 0 and not file_contents.endswith(b'\n'):
220
211
for pattern in name_pattern_list:
221
212
if not pattern in ignores:
222
213
f.write(pattern.encode('utf-8'))
228
216
if not tree.is_versioned(tree._format.ignore_filename):
229
217
tree.add([tree._format.ignore_filename])