Details
Details
- Reviewers
- None
- Group Reviewers
hg-reviewers - Commits
- rHG6a33e81e4c5e: mdiff: remove rewindhunk by yielding a bool first to indicate data
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/mdiff.py (21 lines) |
Status | Author | Revision | |
---|---|---|---|
Closed | joerg.sonnenberger | ||
Closed | joerg.sonnenberger | ||
Closed | joerg.sonnenberger |
size = len(a) | size = len(a) | ||||
hunkrange = (1, size, 0, 0) | hunkrange = (1, size, 0, 0) | ||||
hunklines = ["@@ -1,%d +0,0 @@\n" % size] + ["-" + e for e in a] | hunklines = ["@@ -1,%d +0,0 @@\n" % size] + ["-" + e for e in a] | ||||
if without_newline: | if without_newline: | ||||
hunklines[-1] += '\n' | hunklines[-1] += '\n' | ||||
hunklines.append(_missing_newline_marker) | hunklines.append(_missing_newline_marker) | ||||
hunks = (hunkrange, hunklines), | hunks = (hunkrange, hunklines), | ||||
else: | else: | ||||
diffhunks = _unidiff(a, b, opts=opts) | hunks = _unidiff(a, b, opts=opts) | ||||
try: | if not next(hunks): | ||||
hunkrange, hunklines = next(diffhunks) | |||||
except StopIteration: | |||||
return sentinel | return sentinel | ||||
headerlines = [ | headerlines = [ | ||||
"--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)), | "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)), | ||||
"+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)), | "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)), | ||||
] | ] | ||||
def rewindhunks(): | |||||
yield hunkrange, hunklines | |||||
for hr, hl in diffhunks: | |||||
yield hr, hl | |||||
hunks = rewindhunks() | |||||
return headerlines, hunks | return headerlines, hunks | ||||
def _unidiff(t1, t2, opts=defaultopts): | def _unidiff(t1, t2, opts=defaultopts): | ||||
"""Yield hunks of a headerless unified diff from t1 and t2 texts. | """Yield hunks of a headerless unified diff from t1 and t2 texts. | ||||
Each hunk consists of a (hunkrange, hunklines) tuple where `hunkrange` is a | Each hunk consists of a (hunkrange, hunklines) tuple where `hunkrange` is a | ||||
tuple (s1, l1, s2, l2) representing the range information of the hunk to | tuple (s1, l1, s2, l2) representing the range information of the hunk to | ||||
yield hunkrange, hunklines | yield hunkrange, hunklines | ||||
# bdiff.blocks gives us the matching sequences in the files. The loop | # bdiff.blocks gives us the matching sequences in the files. The loop | ||||
# below finds the spaces between those matching sequences and translates | # below finds the spaces between those matching sequences and translates | ||||
# them into diff output. | # them into diff output. | ||||
# | # | ||||
hunk = None | hunk = None | ||||
ignoredlines = 0 | ignoredlines = 0 | ||||
has_hunks = False | |||||
for s, stype in allblocks(t1, t2, opts, l1, l2): | for s, stype in allblocks(t1, t2, opts, l1, l2): | ||||
a1, a2, b1, b2 = s | a1, a2, b1, b2 = s | ||||
if stype != '!': | if stype != '!': | ||||
if stype == '~': | if stype == '~': | ||||
# The diff context lines are based on t1 content. When | # The diff context lines are based on t1 content. When | ||||
# blank lines are ignored, the new lines offsets must | # blank lines are ignored, the new lines offsets must | ||||
# be adjusted as if equivalent blocks ('~') had the | # be adjusted as if equivalent blocks ('~') had the | ||||
# same sizes on both sides. | # same sizes on both sides. | ||||
prev = None | prev = None | ||||
if hunk: | if hunk: | ||||
# join with the previous hunk if it falls inside the context | # join with the previous hunk if it falls inside the context | ||||
if astart < hunk[1] + opts.context + 1: | if astart < hunk[1] + opts.context + 1: | ||||
prev = hunk | prev = hunk | ||||
astart = hunk[1] | astart = hunk[1] | ||||
bstart = hunk[3] | bstart = hunk[3] | ||||
else: | else: | ||||
if not has_hunks: | |||||
has_hunks = True | |||||
yield True | |||||
for x in yieldhunk(hunk): | for x in yieldhunk(hunk): | ||||
yield x | yield x | ||||
if prev: | if prev: | ||||
# we've joined the previous hunk, record the new ending points. | # we've joined the previous hunk, record the new ending points. | ||||
hunk[1] = a2 | hunk[1] = a2 | ||||
hunk[3] = b2 | hunk[3] = b2 | ||||
delta = hunk[4] | delta = hunk[4] | ||||
else: | else: | ||||
# create a new hunk | # create a new hunk | ||||
hunk = [astart, a2, bstart, b2, delta] | hunk = [astart, a2, bstart, b2, delta] | ||||
delta[len(delta):] = [' ' + x for x in l1[astart:a1]] | delta[len(delta):] = [' ' + x for x in l1[astart:a1]] | ||||
delta[len(delta):] = ['-' + x for x in old] | delta[len(delta):] = ['-' + x for x in old] | ||||
delta[len(delta):] = ['+' + x for x in new] | delta[len(delta):] = ['+' + x for x in new] | ||||
if hunk: | if hunk: | ||||
if not has_hunks: | |||||
has_hunks = True | |||||
yield True | |||||
for x in yieldhunk(hunk): | for x in yieldhunk(hunk): | ||||
yield x | yield x | ||||
elif not has_hunks: | |||||
yield False | |||||
def b85diff(to, tn): | def b85diff(to, tn): | ||||
'''print base85-encoded binary diff''' | '''print base85-encoded binary diff''' | ||||
def fmtline(line): | def fmtline(line): | ||||
l = len(line) | l = len(line) | ||||
if l <= 26: | if l <= 26: | ||||
l = chr(ord('A') + l - 1) | l = chr(ord('A') + l - 1) | ||||
else: | else: |