Mercurial > hg
comparison mercurial/mdiff.py @ 35952:9e641c4525a2
mdiff: use str.startswith/endswith() instead of slicing
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 04 Feb 2018 10:33:14 +0900 |
parents | 8b6dd3922f70 |
children | 29dd37a418aa |
comparison
equal
deleted
inserted
replaced
35951:8b6dd3922f70 | 35952:9e641c4525a2 |
---|---|
272 if a and b and len(a) == len(b) and a == b: | 272 if a and b and len(a) == len(b) and a == b: |
273 return sentinel | 273 return sentinel |
274 headerlines = [] | 274 headerlines = [] |
275 hunks = (None, ['Binary file %s has changed\n' % fn1]), | 275 hunks = (None, ['Binary file %s has changed\n' % fn1]), |
276 elif not a: | 276 elif not a: |
277 without_newline = b[-1:] != '\n' | 277 without_newline = not b.endswith('\n') |
278 b = splitnewlines(b) | 278 b = splitnewlines(b) |
279 if a is None: | 279 if a is None: |
280 l1 = '--- /dev/null%s' % datetag(epoch) | 280 l1 = '--- /dev/null%s' % datetag(epoch) |
281 else: | 281 else: |
282 l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)) | 282 l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)) |
288 if without_newline: | 288 if without_newline: |
289 hunklines[-1] += '\n' | 289 hunklines[-1] += '\n' |
290 hunklines.append(_missing_newline_marker) | 290 hunklines.append(_missing_newline_marker) |
291 hunks = (hunkrange, hunklines), | 291 hunks = (hunkrange, hunklines), |
292 elif not b: | 292 elif not b: |
293 without_newline = a[-1:] != '\n' | 293 without_newline = not a.endswith('\n') |
294 a = splitnewlines(a) | 294 a = splitnewlines(a) |
295 l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)) | 295 l1 = "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)) |
296 if b is None: | 296 if b is None: |
297 l2 = '+++ /dev/null%s' % datetag(epoch) | 297 l2 = '+++ /dev/null%s' % datetag(epoch) |
298 else: | 298 else: |
381 # that file is part of a hunk, a marker is printed. If the | 381 # that file is part of a hunk, a marker is printed. If the |
382 # last line of both files is identical and neither ends in | 382 # last line of both files is identical and neither ends in |
383 # a newline, print only one marker. That's the only case in | 383 # a newline, print only one marker. That's the only case in |
384 # which the hunk can end in a shared line without a newline. | 384 # which the hunk can end in a shared line without a newline. |
385 skip = False | 385 skip = False |
386 if t1[-1:] != '\n' and astart + alen == len(l1) + 1: | 386 if not t1.endswith('\n') and astart + alen == len(l1) + 1: |
387 for i in xrange(len(hunklines) - 1, -1, -1): | 387 for i in xrange(len(hunklines) - 1, -1, -1): |
388 if hunklines[i][0:1] in ('-', ' '): | 388 if hunklines[i].startswith(('-', ' ')): |
389 if hunklines[i][0:1] == ' ': | 389 if hunklines[i].startswith(' '): |
390 skip = True | 390 skip = True |
391 hunklines[i] += '\n' | 391 hunklines[i] += '\n' |
392 hunklines.insert(i + 1, _missing_newline_marker) | 392 hunklines.insert(i + 1, _missing_newline_marker) |
393 break | 393 break |
394 if not skip and t2[-1:] != '\n' and bstart + blen == len(l2) + 1: | 394 if not skip and not t2.endswith('\n') and bstart + blen == len(l2) + 1: |
395 for i in xrange(len(hunklines) - 1, -1, -1): | 395 for i in xrange(len(hunklines) - 1, -1, -1): |
396 if hunklines[i][0:1] == '+': | 396 if hunklines[i].startswith('+'): |
397 hunklines[i] += '\n' | 397 hunklines[i] += '\n' |
398 hunklines.insert(i + 1, _missing_newline_marker) | 398 hunklines.insert(i + 1, _missing_newline_marker) |
399 break | 399 break |
400 yield hunkrange, hunklines | 400 yield hunkrange, hunklines |
401 | 401 |