~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ignores.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Lists of ignore files, etc."""
18
18
 
 
19
from __future__ import absolute_import
 
20
 
19
21
import errno
 
22
import os
 
23
from cStringIO import StringIO
20
24
 
21
25
import bzrlib
 
26
from bzrlib.lazy_import import lazy_import
 
27
lazy_import(globals(), """
22
28
from bzrlib import (
23
29
    atomicfile,
24
30
    config,
25
31
    globbing,
 
32
    trace,
26
33
    )
27
 
 
28
 
from trace import warning
 
34
""")
29
35
 
30
36
# ~/.bazaar/ignore will be filled out using
31
37
# this ignore list, if it does not exist
39
45
    '*~',
40
46
    '.#*',
41
47
    '[#]*#',
 
48
    '__pycache__',
 
49
    'bzr-orphans',
42
50
]
43
51
 
44
52
 
65
73
                unicode_lines.append(line.decode('utf-8'))
66
74
            except UnicodeDecodeError:
67
75
                # report error about line (idx+1)
68
 
                warning('.bzrignore: On Line #%d, malformed utf8 character. '
 
76
                trace.warning(
 
77
                        '.bzrignore: On Line #%d, malformed utf8 character. '
69
78
                        'Ignoring line.' % (line_number+1))
70
79
 
71
80
    # Append each line to ignore list if it's not a comment line
181
190
    The ignore file will be automatically added under version control.
182
191
 
183
192
    :param tree: Working tree to update the ignore list.
 
193
    :param name_pattern_list: List of ignore patterns.
 
194
    :return: None
184
195
    """
 
196
    # read in the existing ignores set
185
197
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
186
198
    if tree.has_filename(ifn):
187
 
        f = open(ifn, 'rt')
 
199
        f = open(ifn, 'rU')
188
200
        try:
189
 
            igns = f.read().decode('utf-8')
 
201
            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
190
208
        finally:
191
209
            f.close()
192
210
    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
 
 
 
211
        file_contents = ""
 
212
        newline = os.linesep
 
213
    
 
214
    sio = StringIO(file_contents)
 
215
    try:
 
216
        ignores = parse_ignore_file(sio)
 
217
    finally:
 
218
        sio.close()
 
219
    
 
220
    # write out the updated ignores set
203
221
    f = atomicfile.AtomicFile(ifn, 'wb')
204
222
    try:
205
 
        f.write(igns.encode('utf-8'))
 
223
        # 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'):
 
226
            f.write(newline)
 
227
        for pattern in name_pattern_list:
 
228
            if not pattern in ignores:
 
229
                f.write(pattern.encode('utf-8'))
 
230
                f.write(newline)
206
231
        f.commit()
207
232
    finally:
208
233
        f.close()