summaryrefslogtreecommitdiffstats
path: root/fix-offsets
diff options
context:
space:
mode:
Diffstat (limited to 'fix-offsets')
-rwxr-xr-xfix-offsets58
1 files changed, 47 insertions, 11 deletions
diff --git a/fix-offsets b/fix-offsets
index 7035b05..23cc1a1 100755
--- a/fix-offsets
+++ b/fix-offsets
@@ -27,6 +27,7 @@ re_h = re.compile(
use_threading = True
#use_threading = False
+
def adjustment(ret, stderrdata):
print >>sys.stderr, "ret = {0}\nstderrdata = {1}".format(ret, stderrdata)
for match in re_h.finditer(stderrdata):
@@ -46,6 +47,7 @@ def hunk_worker(hunk, header, tres, i, cmd, **kwargs):
delta = adjustment(proc.wait(), stderrdata)
tres[i] = delta
+
def proceed(opts, args):
if not args:
args.append('-')
@@ -93,6 +95,7 @@ def proceed(opts, args):
tres[hunk - 1] = delta
break
ts = [len(expr_dict) == 0 for hunk in p.hunks]
+ misorder = [0 for hunk in p.hunks]
tmax = 10
avg = 0.0
cnt = 0
@@ -106,6 +109,8 @@ def proceed(opts, args):
for i in xrange(len_ts):
if not ts[i]:
continue
+ # XXX: should take total number of per-file-hunks into
+ # account (start with 3+ if allowed?)
if not use_threading:
hunk_worker(p.hunks[i], header, tres, i, cmd, **kwargs)
ts[i] = None
@@ -140,10 +145,26 @@ def proceed(opts, args):
if hunk.startsrc < p.hunks[i-1].startsrc: #+ p.hunks[i-1].linessrc:
# "misordered hunks" guard
#print >>sys.stderr, "Critical adjustment: {0}".format(i+1)
- p.hunks[i-1].startsrc = hunk.startsrc - p.hunks[i-1].linessrc
- for ii in xrange(i-1, len_ts):
- ts[ii] = True
- break
+ if misorder[i-1] > 2:
+ tres[i] = int(avg)
+ else:
+ misorder[i-1] += 1
+ delta = int(hunk.startsrc - (p.hunks[i-1].startsrc + misorder[i-1] * p.hunks[i-1].linessrc))
+ p.hunks[i-1].startsrc += delta
+ if p.hunks[i-1].startsrc < 0:
+ p.hunks[i-1].startsrc = 0
+ else:
+ p.hunks[i-1].starttgt += delta
+ if i < len_ts + 1:
+ delta = -delta
+ delta /= 2
+ hunk.startsrc += delta
+ hunk.starttgt += delta
+ for ii in xrange(i-1, len_ts):
+ ts[ii] = True
+ break
+ else:
+ misorder[i-1] = 0
ts[i] = ts[i] or abs(tres[i] - avg) > avg_limit
ts[i] = ts[i] or abs(tres[i] - tres[i-1]) > (avg_limit/2)
if i < len_ts - 1:
@@ -155,17 +176,32 @@ def proceed(opts, args):
else:
continue
avg_limit *= 1.25
- for i in xrange(len_ts):
- delta = tres[i]
- hunk = p.hunks[i]
- if delta:
- hunk.startsrc += delta
- hunk.starttgt += delta
- print str(hunk),
+ check_and_print_hunk(len_ts - 1, 999999, p.hunks, tres, misorder)
null.close()
+def check_and_print_hunk(i, test, hunks, tres, misorder):
+ """non-tail recursion to eval offset (hi->lo) + dump hunks (lo->hi) if ok"""
+ # tail version seems not to be conveniently possible
+ if i < 0:
+ return
+ delta = tres[i]
+ tres[i] = 0
+ if misorder[i] < 2:
+ hunk = hunks[i]
+ if delta:
+ hunk.startsrc += delta
+ hunk.starttgt += delta
+ str_hunk = str(hunk)
+ check_and_print_hunk(i-1, min(hunk.startsrc, test), hunks, tres, misorder)
+ if test <= hunk.startsrc:
+ print >>sys.stderr, "Duplicate: {0}".format(str_hunk)
+ else:
+ print str_hunk,
+ return
+
+
if __name__ == '__main__':
# see PATCH(1)
opts, args = getopt.getopt(sys.argv[1:], 'p:f:h', ['strip=', 'fuzz=', 'expr='])