/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: John Arbash Meinel
  • Date: 2011-03-15 10:28:20 UTC
  • mto: This revision was merged to the branch mainline in revision 5725.
  • Revision ID: john@arbash-meinel.com-20110315102820-51wy8wjre5ol34mu
'bzr export' needs to use 'exact' encoding.

If we are going to be writing binary bites out of stdout, then it needs to
be in binary mode, or it will corrupt the data stream.
Oddly enough, it only seemed to fail if we set '--verbose'. I didn't
bother to track into that bug.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Lists of ignore files, etc."""
18
18
 
19
19
import errno
 
20
import os
 
21
from cStringIO import StringIO
20
22
 
21
23
import bzrlib
22
24
from bzrlib import (
39
41
    '*~',
40
42
    '.#*',
41
43
    '[#]*#',
 
44
    '__pycache__',
 
45
    'bzr-orphans',
42
46
]
43
47
 
44
48
 
181
185
    The ignore file will be automatically added under version control.
182
186
 
183
187
    :param tree: Working tree to update the ignore list.
 
188
    :param name_pattern_list: List of ignore patterns.
 
189
    :return: None
184
190
    """
 
191
    # read in the existing ignores set
185
192
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
186
193
    if tree.has_filename(ifn):
187
 
        f = open(ifn, 'rt')
 
194
        f = open(ifn, 'rU')
188
195
        try:
189
 
            igns = f.read().decode('utf-8')
 
196
            file_contents = f.read()
 
197
            # figure out what kind of line endings are used
 
198
            newline = getattr(f, 'newlines', None)
 
199
            if type(newline) is tuple:
 
200
                newline = newline[0]
 
201
            elif newline is None:
 
202
                newline = os.linesep
190
203
        finally:
191
204
            f.close()
192
205
    else:
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
 
 
 
206
        file_contents = ""
 
207
        newline = os.linesep
 
208
    
 
209
    sio = StringIO(file_contents)
 
210
    try:
 
211
        ignores = parse_ignore_file(sio)
 
212
    finally:
 
213
        sio.close()
 
214
    
 
215
    # write out the updated ignores set
203
216
    f = atomicfile.AtomicFile(ifn, 'wb')
204
217
    try:
205
 
        f.write(igns.encode('utf-8'))
 
218
        # write the original contents, preserving original line endings
 
219
        f.write(newline.join(file_contents.split('\n')))
 
220
        if len(file_contents) > 0 and not file_contents.endswith('\n'):
 
221
            f.write(newline)
 
222
        for pattern in name_pattern_list:
 
223
            if not pattern in ignores:
 
224
                f.write(pattern.encode('utf-8'))
 
225
                f.write(newline)
206
226
        f.commit()
207
227
    finally:
208
228
        f.close()