Show More
@@ -19,6 +19,8 b' from . import (' | |||
|
19 | 19 | util, |
|
20 | 20 | ) |
|
21 | 21 | |
|
22 | _missing_newline_marker = "\\ No newline at end of file\n" | |
|
23 | ||
|
22 | 24 | bdiff = policy.importmod(r'bdiff') |
|
23 | 25 | mpatch = policy.importmod(r'mpatch') |
|
24 | 26 | |
@@ -267,18 +269,13 b' def unidiff(a, ad, b, bd, fn1, fn2, opts' | |||
|
267 | 269 | fn1 = util.pconvert(fn1) |
|
268 | 270 | fn2 = util.pconvert(fn2) |
|
269 | 271 | |
|
270 | def checknonewline(lines): | |
|
271 | for text in lines: | |
|
272 | if text[-1:] != '\n': | |
|
273 | text += "\n\ No newline at end of file\n" | |
|
274 | yield text | |
|
275 | ||
|
276 | 272 | if not opts.text and check_binary and (util.binary(a) or util.binary(b)): |
|
277 | 273 | if a and b and len(a) == len(b) and a == b: |
|
278 | 274 | return sentinel |
|
279 | 275 | headerlines = [] |
|
280 | 276 | hunks = (None, ['Binary file %s has changed\n' % fn1]), |
|
281 | 277 | elif not a: |
|
278 | without_newline = b[-1] != '\n' | |
|
282 | 279 | b = splitnewlines(b) |
|
283 | 280 | if a is None: |
|
284 | 281 | l1 = '--- /dev/null%s' % datetag(epoch) |
@@ -289,8 +286,12 b' def unidiff(a, ad, b, bd, fn1, fn2, opts' | |||
|
289 | 286 | size = len(b) |
|
290 | 287 | hunkrange = (0, 0, 1, size) |
|
291 | 288 | hunklines = ["@@ -0,0 +1,%d @@\n" % size] + ["+" + e for e in b] |
|
292 | hunks = (hunkrange, checknonewline(hunklines)), | |
|
289 | if without_newline: | |
|
290 | hunklines[-1] += '\n' | |
|
291 | hunklines.append(_missing_newline_marker) | |
|
292 | hunks = (hunkrange, hunklines), | |
|
293 | 293 | elif not b: |
|
294 | without_newline = a[-1] != '\n' | |
|
294 | 295 | a = splitnewlines(a) |
|
295 | 296 | l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)) |
|
296 | 297 | if b is None: |
@@ -301,7 +302,10 b' def unidiff(a, ad, b, bd, fn1, fn2, opts' | |||
|
301 | 302 | size = len(a) |
|
302 | 303 | hunkrange = (1, size, 0, 0) |
|
303 | 304 | hunklines = ["@@ -1,%d +0,0 @@\n" % size] + ["-" + e for e in a] |
|
304 | hunks = (hunkrange, checknonewline(hunklines)), | |
|
305 | if without_newline: | |
|
306 | hunklines[-1] += '\n' | |
|
307 | hunklines.append(_missing_newline_marker) | |
|
308 | hunks = (hunkrange, hunklines), | |
|
305 | 309 | else: |
|
306 | 310 | diffhunks = _unidiff(a, b, opts=opts) |
|
307 | 311 | try: |
@@ -314,9 +318,9 b' def unidiff(a, ad, b, bd, fn1, fn2, opts' | |||
|
314 | 318 | "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)), |
|
315 | 319 | ] |
|
316 | 320 | def rewindhunks(): |
|
317 |
yield hunkrange, |
|
|
321 | yield hunkrange, hunklines | |
|
318 | 322 | for hr, hl in diffhunks: |
|
319 |
yield hr, |
|
|
323 | yield hr, hl | |
|
320 | 324 | |
|
321 | 325 | hunks = rewindhunks() |
|
322 | 326 | |
@@ -330,6 +334,8 b' def _unidiff(t1, t2, opts=defaultopts):' | |||
|
330 | 334 | form the '@@ -s1,l1 +s2,l2 @@' header and `hunklines` is a list of lines |
|
331 | 335 | of the hunk combining said header followed by line additions and |
|
332 | 336 | deletions. |
|
337 | ||
|
338 | The hunks are prefixed with a bool. | |
|
333 | 339 | """ |
|
334 | 340 | l1 = splitnewlines(t1) |
|
335 | 341 | l2 = splitnewlines(t2) |
@@ -380,6 +386,26 b' def _unidiff(t1, t2, opts=defaultopts):' | |||
|
380 | 386 | + delta |
|
381 | 387 | + [' ' + l1[x] for x in xrange(a2, aend)] |
|
382 | 388 | ) |
|
389 | # If either file ends without a newline and the last line of | |
|
390 | # that file is part of a hunk, a marker is printed. If the | |
|
391 | # last line of both files is identical and neither ends in | |
|
392 | # a newline, print only one marker. That's the only case in | |
|
393 | # which the hunk can end in a shared line without a newline. | |
|
394 | skip = False | |
|
395 | if t1[-1] != '\n' and astart + alen == len(l1) + 1: | |
|
396 | for i in xrange(len(hunklines) - 1, -1, -1): | |
|
397 | if hunklines[i][0] in ('-', ' '): | |
|
398 | if hunklines[i][0] == ' ': | |
|
399 | skip = True | |
|
400 | hunklines[i] += '\n' | |
|
401 | hunklines.insert(i + 1, _missing_newline_marker) | |
|
402 | break | |
|
403 | if not skip and t2[-1] != '\n' and bstart + blen == len(l2) + 1: | |
|
404 | for i in xrange(len(hunklines) - 1, -1, -1): | |
|
405 | if hunklines[i][0] == '+': | |
|
406 | hunklines[i] += '\n' | |
|
407 | hunklines.insert(i + 1, _missing_newline_marker) | |
|
408 | break | |
|
383 | 409 | yield hunkrange, hunklines |
|
384 | 410 | |
|
385 | 411 | # bdiff.blocks gives us the matching sequences in the files. The loop |
General Comments 0
You need to be logged in to leave comments.
Login now