Skip to content

Commit 4e64dc0

Browse files
committed
Added file and hunk headers.
1 parent f2dd00a commit 4e64dc0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

xtra/DIFF.LUA

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ local function diff_u(fn1,fn2)
99
local function eol()if c=='\r'then local d=f:read(1)if not d then return true end f:seek("cur",-1)return d~='\n'end return c=='\n'end
1010
o=o+1 table.insert(l,c)if eol()then break end end
1111
if #l==0 then break end l=table.concat(l)local ln,c=#l,crc32(l)p=p..string.pack(PF,ln,c,ls)end f:close()return p end
12-
local f1,f2,ps,la=open(fn1),open(fn2),string.packsize(PF)
12+
local f1,f2,ps,la,fh=open(fn1),open(fn2),string.packsize(PF)
1313
local function cmp(x,y)
1414
local l1,c1,o1,l2,c2,o2=string.unpack(PF,f1,(x-1)*ps+1)l2,c2,o2=string.unpack(PF,f2,(y-1)*ps+1)
1515
if l1~=l2 or c1~=c2 then return false end
@@ -19,6 +19,7 @@ local function diff_u(fn1,fn2)
1919
local b1,e,b2=line(fn1,o1,l1)if not b1 then error(e)end b2,e=line(fn2,o2,l2)if not b2 then error(e)end
2020
return b1==b2 end
2121
local h,w,m=#f1//ps,#f2//ps,{}for i=1,h do for j=1,w do if cmp(i,j)then table.insert(m,j+(h*(i-1)))end end end
22+
if #m==0 then return end
2223
local function scr(x,y)local i,j=y+h*(x-1),0 for k=1,#m do if m[k]<=i then j=j+1 else return j,m[k]end end return j end
2324
local x,y,path=h,w,{}while x>0 and y>0 do
2425
local s=scr(x,y)if scr(x,y-1)==s-1 then table.insert(path,1,{x=x,y=y})x,y=x-1,y-1
@@ -27,14 +28,23 @@ local function diff_u(fn1,fn2)
2728
local l,_,o=string.unpack(PF,p,(i-1)*ps+1)
2829
local f=assert(io.open(fn,"rb"))f:seek("set",o)
2930
local r=f:read(l)f:close()return r end
30-
local function pfl(p,s)print(p..s:gsub('\n$',''):gsub('\r$',''))end
31-
local i,j=1,1 for _,p in ipairs(path)do
32-
while i<p.x do pfl("-",get(fn1,f1,i))i=i+1 end
33-
while j<p.y do pfl("+",get(fn2,f2,j))j=j+1 end
34-
pfl(" ",get(fn1,f1,i))i,j=i+1,j+1 end
35-
local function nlc() if la then la=la:sub(-1)if la~='\n'and la~='\r'then print("\\ No newline at end of file")end end end
36-
while i<=h do la=get(fn1,f1,i,ps)pfl("-",la)i=i+1 end nlc()
37-
while j<=w do la=get(fn2,f2,j,ps)pfl("+",la)j=j+1 end nlc()end
31+
local function pfl(p,s)return(p..s:gsub('\n$',''):gsub('\r$',''))end
32+
local i,j,u=1,1
33+
local function flush()if #u.l>0 then
34+
table.insert(u.l,1,"@@ -"..u.o.s..(u.o.l==1 and""or","..u.o.l).." +"..u.n.s..(u.n.l==1 and""or","..u.n.l).. " @@")
35+
if not fh then
36+
local function fn(s)return string.match(s,"%s")and '"'..s..'"'or s end
37+
table.insert(u.l,1,"+++ "..fn(fn2))table.insert(u.l,1,"--- "..fn(fn1))fh=true end
38+
for _,v in ipairs(u.l) do print(v)end end u=nil end
39+
local function init()if not u then u={e=0,l={},o={s=i,l=0},n={s=j,l=0}}end end
40+
for _,p in ipairs(path)do
41+
while i<p.x do init()table.insert(u.l,pfl("-",get(fn1,f1,i)))i,u.o.l,u.e=i+1,u.o.l+1,0 end
42+
while j<p.y do init()table.insert(u.l,pfl("+",get(fn2,f2,j)))j,u.n.l,u.e=j+1,u.n.l+1,0 end
43+
if u then if u.e<3 then table.insert(u.l,pfl(" ",get(fn1,f1,i)))u.e,u.o.l,u.n.l=u.e+1,u.o.l+1,u.n.l+1 else flush()end end i,j=i+1,j+1 end
44+
local function nlc() if la then la=la:sub(-1)if la~='\n'and la~='\r'then table.insert(u.l,"\\ No newline at end of file")end end end
45+
while i<=h do init()la=get(fn1,f1,i,ps)table.insert(u.l,pfl("-",la))i,u.o.l,u.e=i+1,u.o.l+1,0 end nlc()
46+
while j<=w do init()la=get(fn2,f2,j,ps)table.insert(u.l,pfl("+",la))j,u.n.l,u.e=j+1,u.n.l+1,0 end nlc()
47+
if u then flush()end end
3848
if#arg<2 or#arg%2~=0 then print(arg[-1].." "..arg[0]..[[ old new...
3949
4050
Compare differences between files, print them as unified context.

0 commit comments

Comments
 (0)