/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: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

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