From 1fb79e8d678346b71fa85f361f2c6099daf6ba11 Mon Sep 17 00:00:00 2001 From: Jan Pokorný Date: Wed, 9 Oct 2013 18:46:31 +0200 Subject: Allow offset to be change by manual specification (at least :) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan Pokorný --- fix-offsets | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/fix-offsets b/fix-offsets index 5635c8b..d9737e2 100755 --- a/fix-offsets +++ b/fix-offsets @@ -49,6 +49,21 @@ def hunk_worker(hunk, header, tres, i, cmd, **kwargs): def proceed(opts, args): if not args: args.append('-') + expr_dict = {} + popped = 0 + for i, (o, a) in enumerate(opts[:]): + if o == '--expr': + expr = opts.pop(i - popped)[1] + popped += 1 + try: + for filespec in expr.split('::'): + f, rest = filespec.split(':', 1) + f_spec = expr_dict.setdefault(f, []) + f_spec.extend([map(int, x.split('/')) for x in rest.split(':')]) + except: + print >>sys.stderr, "invalid --expr specification" + return 1 + cmd_args = reduce(lambda a, x: a + list(x), opts, []) #print >>sys.stderr, "cmdargs: {0}, opts: {1}".format(cmd_args, opts) @@ -71,13 +86,21 @@ def proceed(opts, args): p.source, p.target) print header tres = [0 for hunk in p.hunks] - ts = [True for hunk in p.hunks] + if expr_dict: + for f in expr_dict.keys(): + if p.source[-len(f):] == f: + for hunk, delta in expr_dict[f]: + tres[hunk - 1] = delta + break + ts = [len(expr_dict) == 0 for hunk in p.hunks] tmax = 10 avg = 0.0 cnt = 0 avg_limit = 50.0 len_ts = len(ts) while any(ts): + # crazy per-single-hunk matching logic (to get more control?) + # not used if nonempty --expr specified # ts[i] = False if not to continue with that or bool(x) == True # otherwise (None is a temporary local state!) for i in xrange(len_ts): @@ -115,6 +138,7 @@ def proceed(opts, args): for i in xrange(len_ts): hunk = p.hunks[i] if i > 0 and 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): @@ -145,9 +169,10 @@ def proceed(opts, args): if __name__ == '__main__': # see PATCH(1) - opts, args = getopt.getopt(sys.argv[1:], 'p:f:h', ['strip=', 'fuzz=']) + opts, args = getopt.getopt(sys.argv[1:], 'p:f:h', ['strip=', 'fuzz=', 'expr=']) if ('-h', '') in opts: - print >>sys.stderr, "Usage: {0} {{[-p|-f]}} [patch-or-stdin]" \ + print >>sys.stderr, "Usage: {0} {{[-p|-f]}} [--expr=EXPR] [patch-or-stdin]" \ .format(sys.argv[0]) + print >>sys.stderr, " where EXPR is: FILE1:HUNK1/OFFSET1[:...][::FILE2:...]" sys.exit(0) sys.exit(proceed(opts, args)) -- cgit