1
# Copyright (C) 2005, 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Lists of ignore files, etc."""
28
# This was the full ignore list for bzr 0.8
29
# please keep these sorted (in C locale order) to aid merging
51
# Our setup tests dump .python-eggs in the bzr source tree root
65
'.sw[nop]', # vim editing nameless file
88
# ~/.bazaar/ignore will be filled out using
89
# this ignore list, if it does not exist
90
# please keep these sorted (in C locale order) to aid merging
103
def parse_ignore_file(f):
104
"""Read in all of the lines in the file and turn it into an ignore list"""
106
for line in f.read().decode('utf8').split('\n'):
107
line = line.rstrip('\r\n')
108
if not line or line.startswith('#'):
110
ignored.add(globbing.normalize_pattern(line))
114
def get_user_ignores():
115
"""Get the list of user ignored files, possibly creating it."""
116
path = config.user_ignore_config_filename()
117
patterns = set(USER_DEFAULTS)
120
except (IOError, OSError), e:
121
# open() shouldn't return an IOError without errno, but just in case
122
err = getattr(e, 'errno', None)
123
if err not in (errno.ENOENT,):
125
# Create the ignore file, and just return the default
126
# We want to ignore if we can't write to the file
127
# since get_* should be a safe operation
129
_set_user_ignores(USER_DEFAULTS)
130
except (IOError, OSError), e:
131
if e.errno not in (errno.EPERM,):
136
return parse_ignore_file(f)
141
def _set_user_ignores(patterns):
142
"""Fill out the user ignore file with the given patterns
144
This may raise an error if it doesn't have permission to
145
write to the user ignore file.
146
This is mostly used for testing, since it would be
147
bad form to rewrite a user's ignore list.
148
bzrlib only writes this file if it does not exist.
150
ignore_path = config.user_ignore_config_filename()
151
config.ensure_config_dir_exists()
153
# Create an empty file
154
f = open(ignore_path, 'wb')
156
for pattern in patterns:
157
f.write(pattern.encode('utf8') + '\n')
162
def add_unique_user_ignores(new_ignores):
163
"""Add entries to the user's ignore list if not present.
165
:param new_ignores: A list of ignore patterns
166
:return: The list of ignores that were added
168
ignored = get_user_ignores()
170
for ignore in new_ignores:
171
ignore = globbing.normalize_pattern(ignore)
172
if ignore not in ignored:
174
to_add.append(ignore)
179
f = open(config.user_ignore_config_filename(), 'ab')
181
for pattern in to_add:
182
f.write(pattern.encode('utf8') + '\n')
189
_runtime_ignores = set()
192
def add_runtime_ignores(ignores):
193
"""Add some ignore patterns that only exists in memory.
195
This is used by some plugins that want bzr to ignore files,
196
but don't want to change a users ignore list.
197
(Such as a conversion script that needs to ignore temporary files,
198
but does not want to modify the project's ignore list.)
200
:param ignores: A list or generator of ignore patterns.
203
global _runtime_ignores
204
_runtime_ignores.update(set(ignores))
207
def get_runtime_ignores():
208
"""Get the current set of runtime ignores."""
209
return _runtime_ignores
212
def tree_ignores_add_patterns(tree, name_pattern_list):
213
"""Retrieve a list of ignores from the ignore file in a tree.
215
:param tree: Tree to retrieve the ignore list from.
218
ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
219
if tree.has_filename(ifn):
222
igns = f.read().decode('utf-8')
228
# TODO: If the file already uses crlf-style termination, maybe
229
# we should use that for the newly added lines?
231
if igns and igns[-1] != '\n':
233
for name_pattern in name_pattern_list:
234
igns += name_pattern + '\n'
236
f = atomicfile.AtomicFile(ifn, 'wb')
238
f.write(igns.encode('utf-8'))
243
if not tree.path2id('.bzrignore'):
244
tree.add(['.bzrignore'])