/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: Gordon Tyler
  • Date: 2010-05-01 03:55:47 UTC
  • mto: This revision was merged to the branch mainline in revision 5207.
  • Revision ID: gordon@doxxx.net-20100501035547-n6xvp9exaseo303d
Updated NEWS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
176
176
 
177
177
 
178
178
def tree_ignores_add_patterns(tree, name_pattern_list):
179
 
    """Add more ignore patterns to the ignore file in a tree.
180
 
    If ignore file does not exist then it will be created.
181
 
    The ignore file will be automatically added under version control.
 
179
    """Add a list of ignore patterns to the ignore file in a tree.
182
180
 
183
 
    :param tree: Working tree to update the ignore list.
 
181
    :param tree: Tree to retrieve the ignore list from.
 
182
    :param name_pattern_list: List of ignore patterns.
 
183
    :return: None
184
184
    """
 
185
    # read in the existing ignores set
185
186
    ifn = tree.abspath(bzrlib.IGNORE_FILENAME)
186
187
    if tree.has_filename(ifn):
187
188
        f = open(ifn, 'rt')
188
189
        try:
189
 
            igns = f.read().decode('utf-8')
 
190
            # grab a copy of the raw contents of the file
 
191
            orig_contents = f.read()
 
192
            # then parse it from the start
 
193
            f.seek(0)
 
194
            ignores = parse_ignore_file(f)
 
195
            # figure out what kind of line endings are used
 
196
            newline = getattr(f, 'newlines', None)
 
197
            if type(newline) is tuple:
 
198
                newline = newline[0]
 
199
            elif newline is None:
 
200
                newline = '\n'
190
201
        finally:
191
202
            f.close()
192
203
    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
 
 
 
204
        orig_contents = ''
 
205
        ignores = set()
 
206
        newline = '\n'
 
207
 
 
208
    # write out the updated ignores set
203
209
    f = atomicfile.AtomicFile(ifn, 'wb')
204
210
    try:
205
 
        f.write(igns.encode('utf-8'))
 
211
        f.write(orig_contents)
 
212
        if len(orig_contents) > 0 and not orig_contents.endswith(newline):
 
213
            f.write(newline)
 
214
        for pattern in name_pattern_list:
 
215
            if not pattern in ignores:
 
216
                f.write(pattern.encode('utf-8'))
 
217
                f.write(newline)
206
218
        f.commit()
207
219
    finally:
208
220
        f.close()
209
221
 
210
 
    if not tree.path2id(bzrlib.IGNORE_FILENAME):
211
 
        tree.add([bzrlib.IGNORE_FILENAME])
 
222
    if not tree.path2id('.bzrignore'):
 
223
        tree.add(['.bzrignore'])