/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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
22
20
 
23
21
import bzrlib
24
22
from bzrlib import (
41
39
    '*~',
42
40
    '.#*',
43
41
    '[#]*#',
44
 
    '__pycache__',
45
 
    'bzr-orphans',
46
42
]
47
43
 
48
44
 
185
181
    The ignore file will be automatically added under version control.
186
182
 
187
183
    :param tree: Working tree to update the ignore list.
188
 
    :param name_pattern_list: List of ignore patterns.
189
 
    :return: None
190
184
    """
191
 
    # read in the existing ignores set
192
185
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
193
186
    if tree.has_filename(ifn):
194
 
        f = open(ifn, 'rU')
 
187
        f = open(ifn, 'rt')
195
188
        try:
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
 
189
            igns = f.read().decode('utf-8')
203
190
        finally:
204
191
            f.close()
205
192
    else:
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
 
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
 
216
203
    f = atomicfile.AtomicFile(ifn, 'wb')
217
204
    try:
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)
 
205
        f.write(igns.encode('utf-8'))
226
206
        f.commit()
227
207
    finally:
228
208
        f.close()