|
1 | | -#!/usr/bin/env lua |
| 1 | +#!/usr/bin/env lua |
2 | 2 |
|
3 | | -CL = tonumber(os.getenv("DIFF_CONTEXT")) or 3 |
4 | | -local function diff_u(fn1, fn2) |
5 | | - local function open(fn) local f, e = io.open(fn) if not f then print(e) os.exit(1) end return f end |
6 | | - local f1, f2, pre, add, del, p1, p2, x, y, h, l1, l2 = open(fn1), open(fn2), {}, {}, {}, 0, 0, 0, 0 |
7 | | - local buf = {add = 0, del = 0, line = {}} |
8 | | - local function flush_pre() |
9 | | - if not h then print("--- " .. fn1 .. "\n+++ " .. fn2) h = true end |
10 | | - if #pre > 0 then |
11 | | - for _, v in ipairs(pre) do table.insert(buf.line, " " .. v) end |
12 | | - buf.ls, buf.rs = x - #pre, y - #pre |
13 | | - end |
14 | | - pre = {} |
15 | | - end |
16 | | - local function flush_hunk() |
17 | | - local pol = {} |
18 | | - local function optimize() |
19 | | - local i, j = 1 |
20 | | - repeat |
21 | | - j = 1 |
22 | | - repeat |
23 | | - if del[i] == add[j] then |
24 | | - table.insert(pol, table.remove(del, i)) table.remove(add, j) |
25 | | - break |
26 | | - end |
27 | | - j = j + 1 |
28 | | - until j > #add |
29 | | - i = i + 1 |
30 | | - until i > #del |
31 | | - end |
32 | | - |
33 | | - if #del > 0 and #add > 0 then optimize() end |
34 | | - for _, v in ipairs(del) do table.insert(buf.line, "-" .. v) end |
35 | | - for _, v in ipairs(add) do table.insert(buf.line, "+" .. v) end |
36 | | - for _, v in ipairs(pol) do table.insert(buf.line, " " .. v) end |
37 | | - buf.add, buf.del = buf.add + #add, buf.del + #del del, add = {}, {} |
38 | | - end |
39 | | - local function flush_buf() |
40 | | - if #buf.line > 0 then |
41 | | - buf.del, buf.add = #buf.line - buf.add, #buf.line - buf.del |
42 | | - local header = "@@ -" .. (buf.ls or buf.del > 0 and "1" or "0") .. |
43 | | - (buf.del == 1 and "" or "," .. buf.del) .. |
44 | | - " +" .. (buf.rs or buf.add > 0 and "1" or "0") .. |
45 | | - (buf.add == 1 and "" or "," .. buf.add) .. " @@" |
46 | | - |
47 | | - table.insert(buf.line, 1, header) |
48 | | - for _, v in ipairs(buf.line) do print(v) end |
49 | | - end |
50 | | - buf.line, buf.add, buf.del, buf.ls, buf.rs = {}, 0, 0, nil, nil |
51 | | - end |
52 | | - local function diagonal() |
53 | | - flush_hunk() |
54 | | - if #pre > CL then table.remove(pre, 1) end |
55 | | - if p1 > 0 or p2 > 0 then |
56 | | - if p1 > 0 and p2 > 0 then |
57 | | - if p1 > CL or p2 > CL then table.insert(buf.line, " " .. l2) end |
58 | | - p1, p2 = p1 - 1, p2 - 1 |
59 | | - elseif p1 > 0 then |
60 | | - if p1 > CL then table.insert(buf.line, " " .. l1) end |
61 | | - p1 = p1 - 1 |
62 | | - elseif p2 > 0 then |
63 | | - if p2 > CL then table.insert(buf.line, " " .. l2) end |
64 | | - p2 = p2 - 1 |
65 | | - elseif p2 <= CL then table.insert(pre, l2) |
66 | | - elseif p1 <= CL then table.insert(pre, l1) end |
67 | | - |
68 | | - if p1 == 0 or p2 == 0 then flush_buf() table.insert(pre, l2) end |
69 | | - else |
70 | | - table.insert(pre, l2) |
71 | | - end |
72 | | - x, y, l1, l2 = x + 1, y + 1, f1:read("*l"), f2:read("*l") |
73 | | - end |
74 | | - local function right() |
75 | | - flush_pre() p1 = (CL * 2) + 1 |
76 | | - table.insert(del, l1) |
77 | | - x, l1 = x + 1, f1:read("*l") |
78 | | - end |
79 | | - local function down() |
80 | | - flush_pre() p2 = (CL * 2) + 1 |
81 | | - table.insert(add, l2) |
82 | | - y, l2 = y + 1, f2:read("*l") |
83 | | - end |
84 | | - repeat |
85 | | - if l1 == l2 then diagonal() |
86 | | - elseif not l2 then right() |
87 | | - elseif not l1 then down() |
88 | | - else right() down() end |
89 | | - until not l1 and not l2 |
90 | | - |
91 | | - f1:close() f2:close() |
92 | | - if #add > 0 or #del > 0 then flush_pre() flush_hunk() end |
93 | | - if #buf.line > 0 then flush_buf() end |
94 | | -end |
95 | | - |
96 | | -if #arg < 2 or #arg % 2 ~= 0 then |
97 | | - print(arg[-1] .. " " .. arg[0] .. [[ old new ... |
| 3 | +CL=tonumber(os.getenv("DIFF_CONTEXT"))or 3 |
| 4 | +local function diff_u(fn1,fn2) |
| 5 | + local function open(fn)local f,e=io.open(fn)if not f then print(e)os.exit(1)end return f end |
| 6 | + local f1,f2,p,a,d,p1,p2,x,y,B,h,l1,l2=open(fn1),open(fn2),{},{},{},0,0,0,0,{a=0,d=0,l={}} |
| 7 | + local function FP() |
| 8 | + if not h then print("--- "..fn1.."\n+++ "..fn2)h=true end |
| 9 | + if#p>0 then for _,v in ipairs(p)do table.insert(B.l," "..v)end B.o,B.n=x-#p,y-#p end |
| 10 | + p={}end |
| 11 | + local function FH() |
| 12 | + local pol={} |
| 13 | + local function optimize()local i,j=1 repeat j=1 repeat if d[i]== a[j]then table.insert(pol,table.remove(d,i))table.remove(a,j)break end j=j+1 until j>#a i=i+1 until i>#d end |
| 14 | + if#d>0 and#a>0 then optimize()end |
| 15 | + for _,v in ipairs(d)do table.insert(B.l,"-"..v)end |
| 16 | + for _,v in ipairs(a)do table.insert(B.l,"+"..v)end |
| 17 | + for _,v in ipairs(pol)do table.insert(B.l," "..v)end |
| 18 | + B.a,B.d=B.a+#a,B.d+#d |
| 19 | + d,a={},{}end |
| 20 | + local function FB() |
| 21 | + if#B.l>0 then B.d,B.a=#B.l-B.a,#B.l-B.d |
| 22 | + local he="@@-"..(B.o or B.d>0 and "1" or "0")..(B.d==1 and "" or ","..B.d).."+"..(B.n or B.a>0 and "1" or "0")..(B.a==1 and "" or ","..B.a).." @@" |
| 23 | + table.insert(B.l,1,he)for _,v in ipairs(B.l)do print(v)end end |
| 24 | + B.l,B.a,B.d,B.o,B.n={},0,0,nil,nil end |
| 25 | + local function di() |
| 26 | + FH()if#p>CL then table.remove(p,1)end |
| 27 | + if p1>0 or p2>0 then |
| 28 | + if p1>0 and p2>0 then if p1>CL or p2>CL then table.insert(B.l," "..l2)end p1,p2=p1-1,p2-1 |
| 29 | + elseif p1>0 then if p1>CL then table.insert(B.l," "..l1)end p1=p1-1 |
| 30 | + elseif p2>0 then if p2>CL then table.insert(B.l," "..l2)end p2=p2-1 |
| 31 | + elseif p2<=CL then table.insert(p,l2) |
| 32 | + elseif p1<=CL then table.insert(p,l1)end |
| 33 | + if p1==0 or p2==0 then FB()table.insert(p,l2)end |
| 34 | + else table.insert(p,l2)end x,y,l1,l2=x+1,y+1,f1:read("*l"),f2:read("*l")end |
| 35 | + local function ri()FP()p1=(CL*2)+1 table.insert(d,l1)x,l1=x+1,f1:read("*l")end |
| 36 | + local function dn()FP()p2=(CL*2)+1 table.insert(a,l2)y,l2=y+1,f2:read("*l")end |
| 37 | + repeat if l1==l2 then di()elseif not l2 then ri()elseif not l1 then dn()else ri()dn()end until not l1 and not l2 f1:close()f2:close()if#a>0 or#d>0 then FP()FH()end if#B.l>0 then FB()end end |
| 38 | +if#arg<2 or#arg%2~=0 then print(arg[-1].." "..arg[0]..[[ old new... |
98 | 39 |
|
99 | 40 | Compare differences between old and new files and print them as unified context. |
100 | | -To compare multiple sets of files at a time, write as sequential parameters. |
101 | | -Example: ']] .. arg[0] .. [[ old1 new1 old2 new2 old3 new3' etc...]]) |
102 | | - os.exit(1) |
103 | | -end |
104 | | -for i = 1, #arg, 2 do |
105 | | - diff_u(arg[i], arg[i + 1]) |
106 | | -end |
| 41 | +To compare multiple sets of files at a time,write as sequential parameters. |
| 42 | +Example: ']]..arg[0]..[[ old1 new1 old2 new2 old3 new3' etc...]])os.exit(1)end |
| 43 | +for i=1,#arg,2 do diff_u(arg[i],arg[i+1])end |
0 commit comments