~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to patches.py

  • Committer: Aaron Bentley
  • Date: 2005-08-17 18:19:57 UTC
  • mto: (1185.82.1 bzr-w-changeset)
  • mto: This revision was merged to the branch mainline in revision 1738.
  • Revision ID: abentley@panoramicfeedback.com-20050817181957-e9154118765b08d4
Cleaned up handling of files with no terminating \n

Show diffs side-by-side

added added

removed removed

Lines of Context:
112
112
            terminator = ''
113
113
        return leadchar + self.contents + terminator
114
114
 
115
 
    def no_nl(self):
116
 
        assert self.contents.endswith('\n')
117
 
        self.contents = self.contents[:-1]
118
115
 
119
116
class ContextLine(HunkLine):
120
117
    def __init__(self, contents):
221
218
def iter_hunks(iter_lines):
222
219
    hunk = None
223
220
    for line in iter_lines:
224
 
        if line == NO_NL:
225
 
            hunk.lines[-1].no_nl()
226
 
            yield hunk
227
 
            hunk = None
228
 
            continue
229
 
        elif line == "\n":
 
221
        if line == "\n":
230
222
            if hunk is not None:
231
223
                yield hunk
232
224
                hunk = None
238
230
        mod_size = 0
239
231
        while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
240
232
            hunk_line = parse_line(iter_lines.next())
241
 
            if hunk_line is NO_NL:
242
 
                hunk.lines[-1].no_nl()
243
 
            else:
244
 
                hunk.lines.append(hunk_line)
 
233
            hunk.lines.append(hunk_line)
245
234
            if isinstance(hunk_line, (RemoveLine, ContextLine)):
246
235
                orig_size += 1
247
236
            if isinstance(hunk_line, (InsertLine, ContextLine)):
322
311
        yield saved_lines
323
312
 
324
313
 
 
314
def iter_lines_handle_nl(iter_lines):
 
315
    """
 
316
    Iterates through lines, ensuring that lines that originally had no
 
317
    terminating \n are produced without one.  This transformation may be
 
318
    applied at any point up until hunk line parsing, and is safe to apply
 
319
    repeatedly.
 
320
    """
 
321
    last_line = None
 
322
    for line in iter_lines:
 
323
        if line == NO_NL:
 
324
            assert last_line.endswith('\n')
 
325
            last_line = last_line[:-1]
 
326
            line = None
 
327
        if last_line is not None:
 
328
            yield last_line
 
329
        last_line = line
 
330
    if last_line is not None:
 
331
        yield last_line
 
332
 
 
333
 
325
334
def parse_patches(iter_lines):
 
335
    iter_lines = iter_lines_handle_nl(iter_lines)
326
336
    return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
327
337
 
328
338
 
360
370
    if orig_lines is not None:
361
371
        orig_lines = orig_lines.__iter__()
362
372
    seen_patch = []
363
 
    patch_lines = patch_lines.__iter__()
 
373
    patch_lines = iter_lines_handle_nl(patch_lines.__iter__())
364
374
    get_patch_names(patch_lines)
365
375
    line_no = 1
366
376
    for hunk in iter_hunks(patch_lines):