25
26
lazy_import(globals(), """
26
27
from breezy import (
34
from breezy.sixish import (
36
# ~/.config/breezy/ignore will be filled out using
38
# ~/.bazaar/ignore will be filled out using
37
39
# this ignore list, if it does not exist
38
40
# please keep these sorted (in C locale order) to aid merging
53
56
def parse_ignore_file(f):
54
57
"""Read in all of the lines in the file and turn it into an ignore list
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
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
73
76
except UnicodeDecodeError:
74
77
# report error about line (idx+1)
76
'.bzrignore: On Line #%d, malformed utf8 character. '
77
'Ignoring line.' % (line_number + 1))
79
'.bzrignore: On Line #%d, malformed utf8 character. '
80
'Ignoring line.' % (line_number+1))
79
82
# Append each line to ignore list if it's not a comment line
80
83
for line in unicode_lines:
88
91
def get_user_ignores():
89
92
"""Get the list of user ignored files, possibly creating it."""
90
path = bedding.user_ignore_config_path()
93
path = config.user_ignore_config_filename()
91
94
patterns = set(USER_DEFAULTS)
93
96
f = open(path, 'rb')
101
104
# since get_* should be a safe operation
103
106
_set_user_ignores(USER_DEFAULTS)
104
except EnvironmentError as e:
105
if e.errno not in (errno.EPERM, errno.ENOENT):
107
except (IOError, OSError) as e:
108
if e.errno not in (errno.EPERM,):
121
124
bad form to rewrite a user's ignore list.
122
125
breezy only writes this file if it does not exist.
124
ignore_path = bedding.user_ignore_config_path()
125
bedding.ensure_config_dir_exists()
127
ignore_path = config.user_ignore_config_filename()
128
config.ensure_config_dir_exists()
127
130
# Create an empty file
128
131
with open(ignore_path, 'wb') as f:
189
192
# read in the existing ignores set
190
ifn = tree.abspath(tree._format.ignore_filename)
193
ifn = tree.abspath(breezy.IGNORE_FILENAME)
191
194
if tree.has_filename(ifn):
192
with open(ifn, 'rb') as f:
195
with open(ifn, 'rbU') as f:
193
196
file_contents = f.read()
194
if file_contents.find(b'\r\n') != -1:
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()
199
204
file_contents = b""
200
205
newline = os.linesep.encode()
202
with BytesIO(file_contents) as sio:
207
sio = BytesIO(file_contents)
203
209
ignores = parse_ignore_file(sio)
205
213
# write out the updated ignores set
206
with atomicfile.AtomicFile(ifn, 'wb') as f:
214
f = atomicfile.AtomicFile(ifn, 'wb')
207
216
# write the original contents, preserving original line endings
208
f.write(file_contents)
217
f.write(newline.join(file_contents.split(b'\n')))
209
218
if len(file_contents) > 0 and not file_contents.endswith(b'\n'):
211
220
for pattern in name_pattern_list:
212
if pattern not in ignores:
221
if not pattern in ignores:
213
222
f.write(pattern.encode('utf-8'))
216
if not tree.is_versioned(tree._format.ignore_filename):
217
tree.add([tree._format.ignore_filename])
228
if not tree.is_versioned(breezy.IGNORE_FILENAME):
229
tree.add([breezy.IGNORE_FILENAME])