~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/patches.py

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004 - 2006, 2008 Aaron Bentley, Canonical Ltd
 
1
# Copyright (C) 2005-2010 Aaron Bentley, Canonical Ltd
2
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
13
13
#
14
14
# You should have received a copy of the GNU General Public License
15
15
# along with this program; if not, write to the Free Software
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
import re
 
18
 
 
19
 
 
20
binary_files_re = 'Binary files (.*) and (.*) differ\n'
 
21
 
 
22
 
 
23
class BinaryFiles(Exception):
 
24
 
 
25
    def __init__(self, orig_name, mod_name):
 
26
        self.orig_name = orig_name
 
27
        self.mod_name = mod_name
 
28
        Exception.__init__(self, 'Binary files section encountered.')
17
29
 
18
30
 
19
31
class PatchSyntax(Exception):
57
69
def get_patch_names(iter_lines):
58
70
    try:
59
71
        line = iter_lines.next()
 
72
        match = re.match(binary_files_re, line)
 
73
        if match is not None:
 
74
            raise BinaryFiles(match.group(1), match.group(2))
60
75
        if not line.startswith("--- "):
61
76
            raise MalformedPatchHeader("No orig name", line)
62
77
        else:
92
107
    range = int(range)
93
108
    return (pos, range)
94
109
 
95
 
 
 
110
 
96
111
def hunk_from_header(line):
97
112
    import re
98
113
    matches = re.match(r'\@\@ ([^@]*) \@\@( (.*))?\n', line)
164
179
        return InsertLine(line[1:])
165
180
    elif line.startswith("-"):
166
181
        return RemoveLine(line[1:])
167
 
    elif line == NO_NL:
168
 
        return NO_NL
169
182
    else:
170
183
        raise MalformedLine("Unknown line type", line)
171
184
__pychecker__=""
237
250
        return shift
238
251
 
239
252
 
240
 
def iter_hunks(iter_lines):
 
253
def iter_hunks(iter_lines, allow_dirty=False):
 
254
    '''
 
255
    :arg iter_lines: iterable of lines to parse for hunks
 
256
    :kwarg allow_dirty: If True, when we encounter something that is not
 
257
        a hunk header when we're looking for one, assume the rest of the lines
 
258
        are not part of the patch (comments or other junk).  Default False
 
259
    '''
241
260
    hunk = None
242
261
    for line in iter_lines:
243
262
        if line == "\n":
247
266
            continue
248
267
        if hunk is not None:
249
268
            yield hunk
250
 
        hunk = hunk_from_header(line)
 
269
        try:
 
270
            hunk = hunk_from_header(line)
 
271
        except MalformedHunkHeader:
 
272
            if allow_dirty:
 
273
                # If the line isn't a hunk header, then we've reached the end
 
274
                # of this patch and there's "junk" at the end.  Ignore the
 
275
                # rest of this patch.
 
276
                return
 
277
            raise
251
278
        orig_size = 0
252
279
        mod_size = 0
253
280
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
261
288
        yield hunk
262
289
 
263
290
 
264
 
class Patch:
 
291
class BinaryPatch(object):
265
292
    def __init__(self, oldname, newname):
266
293
        self.oldname = oldname
267
294
        self.newname = newname
 
295
 
 
296
    def __str__(self):
 
297
        return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
 
298
 
 
299
 
 
300
class Patch(BinaryPatch):
 
301
 
 
302
    def __init__(self, oldname, newname):
 
303
        BinaryPatch.__init__(self, oldname, newname)
268
304
        self.hunks = []
269
305
 
270
306
    def __str__(self):
271
 
        ret = self.get_header() 
 
307
        ret = self.get_header()
272
308
        ret += "".join([str(h) for h in self.hunks])
273
309
        return ret
274
310
 
300
336
                return None
301
337
            newpos += shift
302
338
        return newpos
303
 
            
 
339
 
304
340
    def iter_inserted(self):
305
341
        """Iteraties through inserted lines
306
 
        
 
342
 
307
343
        :return: Pair of line number, line
308
344
        :rtype: iterator of (int, InsertLine)
309
345
        """
317
353
                    pos += 1
318
354
 
319
355
 
320
 
def parse_patch(iter_lines):
321
 
    (orig_name, mod_name) = get_patch_names(iter_lines)
322
 
    patch = Patch(orig_name, mod_name)
323
 
    for hunk in iter_hunks(iter_lines):
324
 
        patch.hunks.append(hunk)
325
 
    return patch
326
 
 
327
 
 
328
 
def iter_file_patch(iter_lines):
 
356
def parse_patch(iter_lines, allow_dirty=False):
 
357
    '''
 
358
    :arg iter_lines: iterable of lines to parse
 
359
    :kwarg allow_dirty: If True, allow the patch to have trailing junk.
 
360
        Default False
 
361
    '''
 
362
    iter_lines = iter_lines_handle_nl(iter_lines)
 
363
    try:
 
364
        (orig_name, mod_name) = get_patch_names(iter_lines)
 
365
    except BinaryFiles, e:
 
366
        return BinaryPatch(e.orig_name, e.mod_name)
 
367
    else:
 
368
        patch = Patch(orig_name, mod_name)
 
369
        for hunk in iter_hunks(iter_lines, allow_dirty):
 
370
            patch.hunks.append(hunk)
 
371
        return patch
 
372
 
 
373
 
 
374
def iter_file_patch(iter_lines, allow_dirty=False):
 
375
    '''
 
376
    :arg iter_lines: iterable of lines to parse for patches
 
377
    :kwarg allow_dirty: If True, allow comments and other non-patch text
 
378
        before the first patch.  Note that the algorithm here can only find
 
379
        such text before any patches have been found.  Comments after the
 
380
        first patch are stripped away in iter_hunks() if it is also passed
 
381
        allow_dirty=True.  Default False.
 
382
    '''
 
383
    ### FIXME: Docstring is not quite true.  We allow certain comments no
 
384
    # matter what, If they startwith '===', '***', or '#' Someone should
 
385
    # reexamine this logic and decide if we should include those in
 
386
    # allow_dirty or restrict those to only being before the patch is found
 
387
    # (as allow_dirty does).
 
388
    regex = re.compile(binary_files_re)
329
389
    saved_lines = []
330
390
    orig_range = 0
 
391
    beginning = True
331
392
    for line in iter_lines:
332
393
        if line.startswith('=== ') or line.startswith('*** '):
333
394
            continue
336
397
        elif orig_range > 0:
337
398
            if line.startswith('-') or line.startswith(' '):
338
399
                orig_range -= 1
339
 
        elif line.startswith('--- '):
340
 
            if len(saved_lines) > 0:
 
400
        elif line.startswith('--- ') or regex.match(line):
 
401
            if allow_dirty and beginning:
 
402
                # Patches can have "junk" at the beginning
 
403
                # Stripping junk from the end of patches is handled when we
 
404
                # parse the patch
 
405
                beginning = False
 
406
            elif len(saved_lines) > 0:
341
407
                yield saved_lines
342
408
            saved_lines = []
343
409
        elif line.startswith('@@'):
369
435
        yield last_line
370
436
 
371
437
 
372
 
def parse_patches(iter_lines):
373
 
    iter_lines = iter_lines_handle_nl(iter_lines)
374
 
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
 
438
def parse_patches(iter_lines, allow_dirty=False):
 
439
    '''
 
440
    :arg iter_lines: iterable of lines to parse for patches
 
441
    :kwarg allow_dirty: If True, allow text that's not part of the patch at
 
442
        selected places.  This includes comments before and after a patch
 
443
        for instance.  Default False.
 
444
    '''
 
445
    return [parse_patch(f.__iter__(), allow_dirty) for f in
 
446
                        iter_file_patch(iter_lines, allow_dirty)]
375
447
 
376
448
 
377
449
def difference_index(atext, btext):