/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-08-14 21:37:46 UTC
  • mto: This revision was merged to the branch mainline in revision 7083.
  • Revision ID: jelmer@jelmer.uk-20180814213746-oxx6tcn57k8k98ed
Fix an ignore test. Make AtomicFile a contextmanager.

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
22
23
import os
23
24
 
24
25
import breezy
31
32
    trace,
32
33
    )
33
34
""")
34
 
from breezy.sixish import (
35
 
    BytesIO,
36
 
    )
37
35
 
38
36
# ~/.bazaar/ignore will be filled out using
39
37
# this ignore list, if it does not exist
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()
194
191
    if tree.has_filename(ifn):
195
192
        with open(ifn, 'rbU') 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.endswith(b'\r\n'):
 
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
201
 
207
 
    sio = BytesIO(file_contents)
208
 
    try:
 
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
212
            if not pattern 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
216
    if not tree.is_versioned(tree._format.ignore_filename):
229
217
        tree.add([tree._format.ignore_filename])