/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Lists of ignore files, etc."""
18
18
 
19
 
from __future__ import absolute_import
20
 
 
21
19
import errno
22
 
from io import BytesIO
23
 
import os
24
20
 
25
 
import breezy
26
 
from .lazy_import import lazy_import
27
 
lazy_import(globals(), """
28
 
from breezy import (
 
21
import bzrlib
 
22
from bzrlib import (
29
23
    atomicfile,
30
24
    config,
31
25
    globbing,
32
 
    trace,
33
26
    )
34
 
""")
 
27
 
 
28
from trace import warning
35
29
 
36
30
# ~/.bazaar/ignore will be filled out using
37
31
# this ignore list, if it does not exist
45
39
    '*~',
46
40
    '.#*',
47
41
    '[#]*#',
48
 
    '__pycache__',
49
 
    'bzr-orphans',
50
42
]
51
43
 
52
44
 
 
45
 
53
46
def parse_ignore_file(f):
54
47
    """Read in all of the lines in the file and turn it into an ignore list
55
 
 
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
 
48
    
 
49
    Continue in the case of utf8 decoding errors, and emit a warning when 
 
50
    such and error is found. Optimise for the common case -- no decoding 
58
51
    errors.
59
52
    """
60
53
    ignored = set()
65
58
    except UnicodeDecodeError:
66
59
        # Otherwise go though line by line and pick out the 'good'
67
60
        # decodable lines
68
 
        lines = ignore_file.split(b'\n')
 
61
        lines = ignore_file.split('\n')
69
62
        unicode_lines = []
70
63
        for line_number, line in enumerate(lines):
71
64
            try:
72
65
                unicode_lines.append(line.decode('utf-8'))
73
66
            except UnicodeDecodeError:
74
67
                # report error about line (idx+1)
75
 
                trace.warning(
76
 
                    '.bzrignore: On Line #%d, malformed utf8 character. '
77
 
                    'Ignoring line.' % (line_number + 1))
 
68
                warning('.bzrignore: On Line #%d, malformed utf8 character. '
 
69
                        'Ignoring line.' % (line_number+1))
78
70
 
79
71
    # Append each line to ignore list if it's not a comment line
80
72
    for line in unicode_lines:
91
83
    patterns = set(USER_DEFAULTS)
92
84
    try:
93
85
        f = open(path, 'rb')
94
 
    except (IOError, OSError) as e:
 
86
    except (IOError, OSError), e:
95
87
        # open() shouldn't return an IOError without errno, but just in case
96
88
        err = getattr(e, 'errno', None)
97
89
        if err not in (errno.ENOENT,):
101
93
        # since get_* should be a safe operation
102
94
        try:
103
95
            _set_user_ignores(USER_DEFAULTS)
104
 
        except (IOError, OSError) as e:
 
96
        except (IOError, OSError), e:
105
97
            if e.errno not in (errno.EPERM,):
106
98
                raise
107
99
        return patterns
119
111
    write to the user ignore file.
120
112
    This is mostly used for testing, since it would be
121
113
    bad form to rewrite a user's ignore list.
122
 
    breezy only writes this file if it does not exist.
 
114
    bzrlib only writes this file if it does not exist.
123
115
    """
124
116
    ignore_path = config.user_ignore_config_filename()
125
117
    config.ensure_config_dir_exists()
126
118
 
127
119
    # Create an empty file
128
 
    with open(ignore_path, 'wb') as f:
 
120
    f = open(ignore_path, 'wb')
 
121
    try:
129
122
        for pattern in patterns:
130
 
            f.write(pattern.encode('utf8') + b'\n')
 
123
            f.write(pattern.encode('utf8') + '\n')
 
124
    finally:
 
125
        f.close()
131
126
 
132
127
 
133
128
def add_unique_user_ignores(new_ignores):
147
142
    if not to_add:
148
143
        return []
149
144
 
150
 
    with open(config.user_ignore_config_filename(), 'ab') as f:
 
145
    f = open(config.user_ignore_config_filename(), 'ab')
 
146
    try:
151
147
        for pattern in to_add:
152
 
            f.write(pattern.encode('utf8') + b'\n')
 
148
            f.write(pattern.encode('utf8') + '\n')
 
149
    finally:
 
150
        f.close()
153
151
 
154
152
    return to_add
155
153
 
183
181
    The ignore file will be automatically added under version control.
184
182
 
185
183
    :param tree: Working tree to update the ignore list.
186
 
    :param name_pattern_list: List of ignore patterns.
187
 
    :return: None
188
184
    """
189
 
    # read in the existing ignores set
190
 
    ifn = tree.abspath(tree._format.ignore_filename)
 
185
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
191
186
    if tree.has_filename(ifn):
192
 
        with open(ifn, 'rb') as f:
193
 
            file_contents = f.read()
194
 
            if file_contents.find(b'\r\n') != -1:
195
 
                newline = b'\r\n'
196
 
            else:
197
 
                newline = b'\n'
 
187
        f = open(ifn, 'rt')
 
188
        try:
 
189
            igns = f.read().decode('utf-8')
 
190
        finally:
 
191
            f.close()
198
192
    else:
199
 
        file_contents = b""
200
 
        newline = os.linesep.encode()
201
 
 
202
 
    with BytesIO(file_contents) as sio:
203
 
        ignores = parse_ignore_file(sio)
204
 
 
205
 
    # write out the updated ignores set
206
 
    with atomicfile.AtomicFile(ifn, 'wb') as f:
207
 
        # 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'):
210
 
            f.write(newline)
211
 
        for pattern in name_pattern_list:
212
 
            if pattern not in ignores:
213
 
                f.write(pattern.encode('utf-8'))
214
 
                f.write(newline)
215
 
 
216
 
    if not tree.is_versioned(tree._format.ignore_filename):
217
 
        tree.add([tree._format.ignore_filename])
 
193
        igns = ""
 
194
 
 
195
    # TODO: If the file already uses crlf-style termination, maybe
 
196
    # we should use that for the newly added lines?
 
197
 
 
198
    if igns and igns[-1] != '\n':
 
199
        igns += '\n'
 
200
    for name_pattern in name_pattern_list:
 
201
        igns += name_pattern + '\n'
 
202
 
 
203
    f = atomicfile.AtomicFile(ifn, 'wb')
 
204
    try:
 
205
        f.write(igns.encode('utf-8'))
 
206
        f.commit()
 
207
    finally:
 
208
        f.close()
 
209
 
 
210
    if not tree.path2id(bzrlib.IGNORE_FILENAME):
 
211
        tree.add([bzrlib.IGNORE_FILENAME])