23
from cStringIO import StringIO
25
from .lazy_import import lazy_import
26
from bzrlib.lazy_import import lazy_import
26
27
lazy_import(globals(), """
34
from breezy.sixish import (
38
36
# ~/.bazaar/ignore will be filled out using
39
37
# this ignore list, if it does not exist
68
66
except UnicodeDecodeError:
69
67
# Otherwise go though line by line and pick out the 'good'
71
lines = ignore_file.split(b'\n')
69
lines = ignore_file.split('\n')
73
71
for line_number, line in enumerate(lines):
94
92
patterns = set(USER_DEFAULTS)
96
94
f = open(path, 'rb')
97
except (IOError, OSError) as e:
95
except (IOError, OSError), e:
98
96
# open() shouldn't return an IOError without errno, but just in case
99
97
err = getattr(e, 'errno', None)
100
98
if err not in (errno.ENOENT,):
104
102
# since get_* should be a safe operation
106
104
_set_user_ignores(USER_DEFAULTS)
107
except (IOError, OSError) as e:
105
except (IOError, OSError), e:
108
106
if e.errno not in (errno.EPERM,):
122
120
write to the user ignore file.
123
121
This is mostly used for testing, since it would be
124
122
bad form to rewrite a user's ignore list.
125
breezy only writes this file if it does not exist.
123
bzrlib only writes this file if it does not exist.
127
125
ignore_path = config.user_ignore_config_filename()
128
126
config.ensure_config_dir_exists()
130
128
# Create an empty file
131
with open(ignore_path, 'wb') as f:
129
f = open(ignore_path, 'wb')
132
131
for pattern in patterns:
133
f.write(pattern.encode('utf8') + b'\n')
132
f.write(pattern.encode('utf8') + '\n')
136
137
def add_unique_user_ignores(new_ignores):
153
with open(config.user_ignore_config_filename(), 'ab') as f:
154
f = open(config.user_ignore_config_filename(), 'ab')
154
156
for pattern in to_add:
155
f.write(pattern.encode('utf8') + b'\n')
157
f.write(pattern.encode('utf8') + '\n')
192
196
# read in the existing ignores set
193
ifn = tree.abspath(tree._format.ignore_filename)
197
ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
194
198
if tree.has_filename(ifn):
195
with open(ifn, 'rbU') as f:
196
201
file_contents = f.read()
197
202
# figure out what kind of line endings are used
198
203
newline = getattr(f, 'newlines', None)
199
if isinstance(newline, tuple):
204
if type(newline) is tuple:
200
205
newline = newline[0]
201
206
elif newline is None:
202
newline = os.linesep.encode()
205
newline = os.linesep.encode()
207
sio = BytesIO(file_contents)
214
sio = StringIO(file_contents)
209
216
ignores = parse_ignore_file(sio)
213
220
# write out the updated ignores set
214
221
f = atomicfile.AtomicFile(ifn, 'wb')
216
223
# write the original contents, preserving original line endings
217
f.write(newline.join(file_contents.split(b'\n')))
218
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'):
220
227
for pattern in name_pattern_list:
221
228
if not pattern in ignores: