~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: Andrew Bennetts
  • Date: 2011-02-25 08:45:27 UTC
  • mto: This revision was merged to the branch mainline in revision 5695.
  • Revision ID: andrew.bennetts@canonical.com-20110225084527-0ucp7p00d00hoqon
Add another test.

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()