/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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
 
from cStringIO import StringIO
24
22
 
25
 
import bzrlib
26
 
from bzrlib.lazy_import import lazy_import
 
23
import breezy
 
24
from .lazy_import import lazy_import
27
25
lazy_import(globals(), """
28
 
from bzrlib import (
 
26
from breezy import (
29
27
    atomicfile,
30
 
    config,
31
28
    globbing,
32
29
    trace,
33
30
    )
34
31
""")
 
32
from . import (
 
33
    bedding,
 
34
    )
35
35
 
36
 
# ~/.bazaar/ignore will be filled out using
 
36
# ~/.config/breezy/ignore will be filled out using
37
37
# this ignore list, if it does not exist
38
38
# please keep these sorted (in C locale order) to aid merging
39
39
USER_DEFAULTS = [
50
50
]
51
51
 
52
52
 
53
 
 
54
53
def parse_ignore_file(f):
55
54
    """Read in all of the lines in the file and turn it into an ignore list
56
 
    
57
 
    Continue in the case of utf8 decoding errors, and emit a warning when 
58
 
    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
59
58
    errors.
60
59
    """
61
60
    ignored = set()
66
65
    except UnicodeDecodeError:
67
66
        # Otherwise go though line by line and pick out the 'good'
68
67
        # decodable lines
69
 
        lines = ignore_file.split('\n')
 
68
        lines = ignore_file.split(b'\n')
70
69
        unicode_lines = []
71
70
        for line_number, line in enumerate(lines):
72
71
            try:
74
73
            except UnicodeDecodeError:
75
74
                # report error about line (idx+1)
76
75
                trace.warning(
77
 
                        '.bzrignore: On Line #%d, malformed utf8 character. '
78
 
                        'Ignoring line.' % (line_number+1))
 
76
                    '.bzrignore: On Line #%d, malformed utf8 character. '
 
77
                    'Ignoring line.' % (line_number + 1))
79
78
 
80
79
    # Append each line to ignore list if it's not a comment line
81
80
    for line in unicode_lines:
88
87
 
89
88
def get_user_ignores():
90
89
    """Get the list of user ignored files, possibly creating it."""
91
 
    path = config.user_ignore_config_filename()
 
90
    path = bedding.user_ignore_config_path()
92
91
    patterns = set(USER_DEFAULTS)
93
92
    try:
94
93
        f = open(path, 'rb')
95
 
    except (IOError, OSError), e:
 
94
    except (IOError, OSError) as e:
96
95
        # open() shouldn't return an IOError without errno, but just in case
97
96
        err = getattr(e, 'errno', None)
98
97
        if err not in (errno.ENOENT,):
102
101
        # since get_* should be a safe operation
103
102
        try:
104
103
            _set_user_ignores(USER_DEFAULTS)
105
 
        except (IOError, OSError), e:
106
 
            if e.errno not in (errno.EPERM,):
 
104
        except EnvironmentError as e:
 
105
            if e.errno not in (errno.EPERM, errno.ENOENT):
107
106
                raise
108
107
        return patterns
109
108
 
120
119
    write to the user ignore file.
121
120
    This is mostly used for testing, since it would be
122
121
    bad form to rewrite a user's ignore list.
123
 
    bzrlib only writes this file if it does not exist.
 
122
    breezy only writes this file if it does not exist.
124
123
    """
125
 
    ignore_path = config.user_ignore_config_filename()
126
 
    config.ensure_config_dir_exists()
 
124
    ignore_path = bedding.user_ignore_config_path()
 
125
    bedding.ensure_config_dir_exists()
127
126
 
128
127
    # Create an empty file
129
 
    f = open(ignore_path, 'wb')
130
 
    try:
 
128
    with open(ignore_path, 'wb') as f:
131
129
        for pattern in patterns:
132
 
            f.write(pattern.encode('utf8') + '\n')
133
 
    finally:
134
 
        f.close()
 
130
            f.write(pattern.encode('utf8') + b'\n')
135
131
 
136
132
 
137
133
def add_unique_user_ignores(new_ignores):
151
147
    if not to_add:
152
148
        return []
153
149
 
154
 
    f = open(config.user_ignore_config_filename(), 'ab')
155
 
    try:
 
150
    with open(bedding.user_ignore_config_path(), 'ab') as f:
156
151
        for pattern in to_add:
157
 
            f.write(pattern.encode('utf8') + '\n')
158
 
    finally:
159
 
        f.close()
 
152
            f.write(pattern.encode('utf8') + b'\n')
160
153
 
161
154
    return to_add
162
155
 
194
187
    :return: None
195
188
    """
196
189
    # read in the existing ignores set
197
 
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
 
190
    ifn = tree.abspath(tree._format.ignore_filename)
198
191
    if tree.has_filename(ifn):
199
 
        f = open(ifn, 'rU')
200
 
        try:
 
192
        with open(ifn, 'rb') as f:
201
193
            file_contents = f.read()
202
 
            # figure out what kind of line endings are used
203
 
            newline = getattr(f, 'newlines', None)
204
 
            if type(newline) is tuple:
205
 
                newline = newline[0]
206
 
            elif newline is None:
207
 
                newline = os.linesep
208
 
        finally:
209
 
            f.close()
 
194
            if file_contents.find(b'\r\n') != -1:
 
195
                newline = b'\r\n'
 
196
            else:
 
197
                newline = b'\n'
210
198
    else:
211
 
        file_contents = ""
212
 
        newline = os.linesep
213
 
    
214
 
    sio = StringIO(file_contents)
215
 
    try:
 
199
        file_contents = b""
 
200
        newline = os.linesep.encode()
 
201
 
 
202
    with BytesIO(file_contents) as sio:
216
203
        ignores = parse_ignore_file(sio)
217
 
    finally:
218
 
        sio.close()
219
 
    
 
204
 
220
205
    # write out the updated ignores set
221
 
    f = atomicfile.AtomicFile(ifn, 'wb')
222
 
    try:
 
206
    with atomicfile.AtomicFile(ifn, 'wb') as f:
223
207
        # write the original contents, preserving original line endings
224
 
        f.write(newline.join(file_contents.split('\n')))
225
 
        if len(file_contents) > 0 and not file_contents.endswith('\n'):
 
208
        f.write(file_contents)
 
209
        if len(file_contents) > 0 and not file_contents.endswith(b'\n'):
226
210
            f.write(newline)
227
211
        for pattern in name_pattern_list:
228
 
            if not pattern in ignores:
 
212
            if pattern not in ignores:
229
213
                f.write(pattern.encode('utf-8'))
230
214
                f.write(newline)
231
 
        f.commit()
232
 
    finally:
233
 
        f.close()
234
215
 
235
 
    if not tree.path2id(bzrlib.IGNORE_FILENAME):
236
 
        tree.add([bzrlib.IGNORE_FILENAME])
 
216
    if not tree.is_versioned(tree._format.ignore_filename):
 
217
        tree.add([tree._format.ignore_filename])