17
17
"""Lists of ignore files, etc."""
19
from __future__ import absolute_import
20
from io import BytesIO
23
from cStringIO import StringIO
24
from .lazy_import import lazy_import
26
from bzrlib.lazy_import import lazy_import
25
27
lazy_import(globals(), """
36
# ~/.config/breezy/ignore will be filled out using
36
# ~/.bazaar/ignore will be filled out using
37
37
# this ignore list, if it does not exist
38
38
# please keep these sorted (in C locale order) to aid merging
53
54
def parse_ignore_file(f):
54
55
"""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
57
Continue in the case of utf8 decoding errors, and emit a warning when
58
such and error is found. Optimise for the common case -- no decoding
65
66
except UnicodeDecodeError:
66
67
# Otherwise go though line by line and pick out the 'good'
68
lines = ignore_file.split(b'\n')
69
lines = ignore_file.split('\n')
70
71
for line_number, line in enumerate(lines):
73
74
except UnicodeDecodeError:
74
75
# report error about line (idx+1)
76
'.bzrignore: On Line #%d, malformed utf8 character. '
77
'Ignoring line.' % (line_number + 1))
77
'.bzrignore: On Line #%d, malformed utf8 character. '
78
'Ignoring line.' % (line_number+1))
79
80
# Append each line to ignore list if it's not a comment line
80
81
for line in unicode_lines:
88
89
def get_user_ignores():
89
90
"""Get the list of user ignored files, possibly creating it."""
90
path = bedding.user_ignore_config_path()
91
path = config.user_ignore_config_filename()
91
92
patterns = set(USER_DEFAULTS)
93
94
f = open(path, 'rb')
94
except (IOError, OSError) as e:
95
except (IOError, OSError), e:
95
96
# open() shouldn't return an IOError without errno, but just in case
96
97
err = getattr(e, 'errno', None)
97
98
if err not in (errno.ENOENT,):
101
102
# since get_* should be a safe operation
103
104
_set_user_ignores(USER_DEFAULTS)
104
except EnvironmentError as e:
105
if e.errno not in (errno.EPERM, errno.ENOENT):
105
except (IOError, OSError), e:
106
if e.errno not in (errno.EPERM,):
119
120
write to the user ignore file.
120
121
This is mostly used for testing, since it would be
121
122
bad form to rewrite a user's ignore list.
122
breezy only writes this file if it does not exist.
123
bzrlib only writes this file if it does not exist.
124
ignore_path = bedding.user_ignore_config_path()
125
bedding.ensure_config_dir_exists()
125
ignore_path = config.user_ignore_config_filename()
126
config.ensure_config_dir_exists()
127
128
# Create an empty file
128
with open(ignore_path, 'wb') as f:
129
f = open(ignore_path, 'wb')
129
131
for pattern in patterns:
130
f.write(pattern.encode('utf8') + b'\n')
132
f.write(pattern.encode('utf8') + '\n')
133
137
def add_unique_user_ignores(new_ignores):
150
with open(bedding.user_ignore_config_path(), 'ab') as f:
154
f = open(config.user_ignore_config_filename(), 'ab')
151
156
for pattern in to_add:
152
f.write(pattern.encode('utf8') + b'\n')
157
f.write(pattern.encode('utf8') + '\n')
189
196
# read in the existing ignores set
190
ifn = tree.abspath(tree._format.ignore_filename)
197
ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
191
198
if tree.has_filename(ifn):
192
with open(ifn, 'rb') as f:
193
201
file_contents = f.read()
194
if file_contents.find(b'\r\n') != -1:
202
# figure out what kind of line endings are used
203
newline = getattr(f, 'newlines', None)
204
if type(newline) is tuple:
206
elif newline is None:
200
newline = os.linesep.encode()
202
with BytesIO(file_contents) as sio:
214
sio = StringIO(file_contents)
203
216
ignores = parse_ignore_file(sio)
205
220
# write out the updated ignores set
206
with atomicfile.AtomicFile(ifn, 'wb') as f:
221
f = atomicfile.AtomicFile(ifn, 'wb')
207
223
# write the original contents, preserving original line endings
208
f.write(file_contents)
209
if len(file_contents) > 0 and not file_contents.endswith(b'\n'):
224
f.write(newline.join(file_contents.split('\n')))
225
if len(file_contents) > 0 and not file_contents.endswith('\n'):
211
227
for pattern in name_pattern_list:
212
if pattern not in ignores:
228
if not pattern in ignores:
213
229
f.write(pattern.encode('utf-8'))
216
if not tree.is_versioned(tree._format.ignore_filename):
217
tree.add([tree._format.ignore_filename])
235
if not tree.path2id(bzrlib.IGNORE_FILENAME):
236
tree.add([bzrlib.IGNORE_FILENAME])