/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 breezy/ignores.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from __future__ import absolute_import
20
20
 
21
21
import errno
22
 
from io import BytesIO
23
22
import os
24
23
 
25
24
import breezy
27
26
lazy_import(globals(), """
28
27
from breezy import (
29
28
    atomicfile,
 
29
    config,
30
30
    globbing,
31
31
    trace,
32
32
    )
33
33
""")
34
 
from . import (
35
 
    bedding,
 
34
from breezy.sixish import (
 
35
    BytesIO,
36
36
    )
37
37
 
38
 
# ~/.config/breezy/ignore will be filled out using
 
38
# ~/.bazaar/ignore will be filled out using
39
39
# this ignore list, if it does not exist
40
40
# please keep these sorted (in C locale order) to aid merging
41
41
USER_DEFAULTS = [
52
52
]
53
53
 
54
54
 
 
55
 
55
56
def parse_ignore_file(f):
56
57
    """Read in all of the lines in the file and turn it into an ignore list
57
 
 
58
 
    Continue in the case of utf8 decoding errors, and emit a warning when
59
 
    such and error is found. Optimise for the common case -- no decoding
 
58
    
 
59
    Continue in the case of utf8 decoding errors, and emit a warning when 
 
60
    such and error is found. Optimise for the common case -- no decoding 
60
61
    errors.
61
62
    """
62
63
    ignored = set()
75
76
            except UnicodeDecodeError:
76
77
                # report error about line (idx+1)
77
78
                trace.warning(
78
 
                    '.bzrignore: On Line #%d, malformed utf8 character. '
79
 
                    'Ignoring line.' % (line_number + 1))
 
79
                        '.bzrignore: On Line #%d, malformed utf8 character. '
 
80
                        'Ignoring line.' % (line_number+1))
80
81
 
81
82
    # Append each line to ignore list if it's not a comment line
82
83
    for line in unicode_lines:
89
90
 
90
91
def get_user_ignores():
91
92
    """Get the list of user ignored files, possibly creating it."""
92
 
    path = bedding.user_ignore_config_path()
 
93
    path = config.user_ignore_config_filename()
93
94
    patterns = set(USER_DEFAULTS)
94
95
    try:
95
96
        f = open(path, 'rb')
103
104
        # since get_* should be a safe operation
104
105
        try:
105
106
            _set_user_ignores(USER_DEFAULTS)
106
 
        except EnvironmentError as e:
107
 
            if e.errno not in (errno.EPERM, errno.ENOENT):
 
107
        except (IOError, OSError) as e:
 
108
            if e.errno not in (errno.EPERM,):
108
109
                raise
109
110
        return patterns
110
111
 
123
124
    bad form to rewrite a user's ignore list.
124
125
    breezy only writes this file if it does not exist.
125
126
    """
126
 
    ignore_path = bedding.user_ignore_config_path()
127
 
    bedding.ensure_config_dir_exists()
 
127
    ignore_path = config.user_ignore_config_filename()
 
128
    config.ensure_config_dir_exists()
128
129
 
129
130
    # Create an empty file
130
131
    with open(ignore_path, 'wb') as f:
149
150
    if not to_add:
150
151
        return []
151
152
 
152
 
    with open(bedding.user_ignore_config_path(), 'ab') as f:
 
153
    with open(config.user_ignore_config_filename(), 'ab') as f:
153
154
        for pattern in to_add:
154
155
            f.write(pattern.encode('utf8') + b'\n')
155
156
 
191
192
    # read in the existing ignores set
192
193
    ifn = tree.abspath(tree._format.ignore_filename)
193
194
    if tree.has_filename(ifn):
194
 
        with open(ifn, 'rb') as f:
 
195
        with open(ifn, 'rbU') as f:
195
196
            file_contents = f.read()
196
 
            if file_contents.find(b'\r\n') != -1:
197
 
                newline = b'\r\n'
198
 
            else:
199
 
                newline = b'\n'
 
197
            # figure out what kind of line endings are used
 
198
            newline = getattr(f, 'newlines', None)
 
199
            if isinstance(newline, tuple):
 
200
                newline = newline[0]
 
201
            elif newline is None:
 
202
                newline = os.linesep.encode()
200
203
    else:
201
204
        file_contents = b""
202
205
        newline = os.linesep.encode()
203
206
 
204
 
    with BytesIO(file_contents) as sio:
 
207
    sio = BytesIO(file_contents)
 
208
    try:
205
209
        ignores = parse_ignore_file(sio)
 
210
    finally:
 
211
        sio.close()
206
212
 
207
213
    # write out the updated ignores set
208
 
    with atomicfile.AtomicFile(ifn, 'wb') as f:
 
214
    f = atomicfile.AtomicFile(ifn, 'wb')
 
215
    try:
209
216
        # write the original contents, preserving original line endings
210
 
        f.write(file_contents)
 
217
        f.write(newline.join(file_contents.split(b'\n')))
211
218
        if len(file_contents) > 0 and not file_contents.endswith(b'\n'):
212
219
            f.write(newline)
213
220
        for pattern in name_pattern_list:
214
 
            if pattern not in ignores:
 
221
            if not pattern in ignores:
215
222
                f.write(pattern.encode('utf-8'))
216
223
                f.write(newline)
 
224
        f.commit()
 
225
    finally:
 
226
        f.close()
217
227
 
218
228
    if not tree.is_versioned(tree._format.ignore_filename):
219
229
        tree.add([tree._format.ignore_filename])