~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: Robert Collins
  • Date: 2010-04-08 04:34:03 UTC
  • mfrom: (5138 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5139.
  • Revision ID: robertc@robertcollins.net-20100408043403-56z0d07vdqrx7f3t
Update bugfix for 528114 to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Lists of ignore files, etc."""
18
18
 
19
19
import errno
20
20
 
 
21
import bzrlib
21
22
from bzrlib import (
 
23
    atomicfile,
22
24
    config,
23
25
    globbing,
24
26
    )
25
27
 
 
28
from trace import warning
 
29
 
26
30
# This was the full ignore list for bzr 0.8
27
31
# please keep these sorted (in C locale order) to aid merging
28
32
OLD_DEFAULTS = [
98
102
]
99
103
 
100
104
 
 
105
 
101
106
def parse_ignore_file(f):
102
 
    """Read in all of the lines in the file and turn it into an ignore list"""
 
107
    """Read in all of the lines in the file and turn it into an ignore list
 
108
    
 
109
    Continue in the case of utf8 decoding errors, and emit a warning when 
 
110
    such and error is found. Optimise for the common case -- no decoding 
 
111
    errors.
 
112
    """
103
113
    ignored = set()
104
 
    for line in f.read().decode('utf8').split('\n'):
 
114
    ignore_file = f.read()
 
115
    try:
 
116
        # Try and parse whole ignore file at once.
 
117
        unicode_lines = ignore_file.decode('utf8').split('\n')
 
118
    except UnicodeDecodeError:
 
119
        # Otherwise go though line by line and pick out the 'good'
 
120
        # decodable lines
 
121
        lines = ignore_file.split('\n')
 
122
        unicode_lines = []    
 
123
        for line_number, line in enumerate(lines):
 
124
            try:
 
125
                unicode_lines.append(line.decode('utf-8'))
 
126
            except UnicodeDecodeError:
 
127
                # report error about line (idx+1)
 
128
                warning('.bzrignore: On Line #%d, malformed utf8 character. '
 
129
                        'Ignoring line.' % (line_number+1))
 
130
    
 
131
    # Append each line to ignore list if it's not a comment line
 
132
    for line in unicode_lines:
105
133
        line = line.rstrip('\r\n')
106
134
        if not line or line.startswith('#'):
107
135
            continue
205
233
def get_runtime_ignores():
206
234
    """Get the current set of runtime ignores."""
207
235
    return _runtime_ignores
 
236
 
 
237
 
 
238
def tree_ignores_add_patterns(tree, name_pattern_list):
 
239
    """Retrieve a list of ignores from the ignore file in a tree.
 
240
 
 
241
    :param tree: Tree to retrieve the ignore list from.
 
242
    :return:
 
243
    """
 
244
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
 
245
    if tree.has_filename(ifn):
 
246
        f = open(ifn, 'rt')
 
247
        try:
 
248
            igns = f.read().decode('utf-8')
 
249
        finally:
 
250
            f.close()
 
251
    else:
 
252
        igns = ""
 
253
 
 
254
    # TODO: If the file already uses crlf-style termination, maybe
 
255
    # we should use that for the newly added lines?
 
256
 
 
257
    if igns and igns[-1] != '\n':
 
258
        igns += '\n'
 
259
    for name_pattern in name_pattern_list:
 
260
        igns += name_pattern + '\n'
 
261
 
 
262
    f = atomicfile.AtomicFile(ifn, 'wb')
 
263
    try:
 
264
        f.write(igns.encode('utf-8'))
 
265
        f.commit()
 
266
    finally:
 
267
        f.close()
 
268
 
 
269
    if not tree.path2id('.bzrignore'):
 
270
        tree.add(['.bzrignore'])