|
2 | 2 |
|
3 | 3 | P,M="patched","malformed" |
4 | 4 | local function patch(pf) |
5 | | - local ol,bl,cp,of,bf,h,e=0,0 |
| 5 | + local wl,rl,cp,wf,rf,h,e=0,0 |
6 | 6 | local function parseFileName(l)local f=l:sub(5,5) |
7 | 7 | if f=='"'or f=="'"then return l:match(f.."([^"..f.."]+)"..f,5) |
8 | 8 | else f={}for v in string.gmatch(l,"%S+")do table.insert(f,v)end return f[2]end end |
9 | 9 | local function setPatchFile(f) --TODO: write patch to temporary file first |
10 | | - local o,b local function abort()print(e)if o then o:close()end if b then b:close()end return false end |
11 | | - o,e=io.open(f)if not o then return abort()end |
12 | | - b,e=io.open(f..".orig","w")if not b then return abort()end |
13 | | - for m in o:lines()do b:write(m..'\n')end o:close()b:close() |
14 | | - o,e=io.open(f,"w")if not o then return abort()end |
15 | | - b,e=io.open(f..".orig")if not b then return abort()end |
16 | | - if of then of:close()end if bf then bf:close()end of,ol,bf,bl,h=o,0,b,0,nil return true end |
17 | | - local function writeRemainder()if h then if(h.o.l>0 and h.o.s>0)and(h.o.l~=bl-(h.o.s-1)or h.n.l~=ol-(h.n.s-1))then print(M)os.exit(1)else print(P)end end |
18 | | - if of then local rest=bf:read("*l")while rest do of:write(rest..'\n')rest=bf:read("*l")end of:close()of,ol=nil,0 end bf:close()bf,bl,h=nil,0,nil end |
| 10 | + local r,w local function abort()print(e)if r then r:close()end if w then w:close()end return false end |
| 11 | + r,e=io.open(f)if not r then return abort()end |
| 12 | + w,e=io.open(f..".orig","w")if not w then return abort()end |
| 13 | + for m in r:lines()do w:write(m..'\n')end r:close()w:close() |
| 14 | + w,e=io.open(f,"w")if not r then return abort()end |
| 15 | + r,e=io.open(f..".orig")if not w then return abort()end |
| 16 | + if wf then wf:close()end if rf then rf:close()end wf,wl,rf,rl,h=w,0,r,0,nil return true end |
| 17 | + local function writeRemainder()if h then if(h.o.l>0 and h.o.s>0)and(h.o.l~=rl-(h.o.s-1)or h.n.l~=wl-(h.n.s-1))then print(M)os.exit(1)else print(P)end end |
| 18 | + if wf then local rest=rf:read("*l")while rest do wf:write(rest..'\n')rest=rf:read("*l")end wf:close()wf,wl=nil,0 end rf:close()rf,rl,h=nil,0,nil end |
19 | 19 | local n=pf:read("*l")while n do |
20 | 20 | local l,ls=n n=pf:read("*l")ls=l:sub(1,1) |
21 | 21 | if ls=='+'then |
22 | | - if l:sub(2,4)~="++ "then of:write(l:sub(2)..(n and n:match("^\\ No newline at end of file")and""or'\n'))ol=ol+1 end |
| 22 | + if l:sub(2,4)~="++ "then wf:write(l:sub(2)..(n and n:match("^\\ No newline at end of file")and""or'\n'))wl=wl+1 end |
23 | 23 | elseif ls=='-'then |
24 | | - if l:sub(2,4)=="-- "then local fn=parseFileName(l)if bf then writeRemainder()end if setPatchFile(fn)then cp=fn print("\n'"..cp.."':")end |
25 | | - else bl=bl+1 if bf:read("*l")~=l:sub(2)then print(M)os.exit(1)end end |
| 24 | + if l:sub(2,4)=="-- "then local fn=parseFileName(l)if rf then writeRemainder()end if setPatchFile(fn)then cp=fn print("\n'"..cp.."':")end |
| 25 | + else rl=rl+1 if rf:read("*l")~=l:sub(2)then print(M)os.exit(1)end end |
26 | 26 | elseif ls==' 'then |
27 | | - if bf then local c,i=bf:read("*l"),l:sub(2)bl=bl+1 if c==i then if of then of:write(i..'\n')ol=ol+1 end else print(M)os.exit(1)end end |
| 27 | + if rf then local c,i=rf:read("*l"),l:sub(2)rl=rl+1 if c==i then if wf then wf:write(i..'\n')wl=wl+1 end else print(M)os.exit(1)end end |
28 | 28 | elseif ls=='@'then if l:sub(2,3)=="@ "then |
29 | 29 | local function parseHunkHeader() |
30 | 30 | local function split(i,d)local r={}for m in(i..d):gmatch("(.-)"..d)do table.insert(r,m)end return r end |
31 | 31 | local s,o,w=split(l," "),{},{}if s[2]then o=split(s[2],",")end if s[3]then w=split(s[3],",")end |
32 | 32 | if o[1]then o[1]=o[1]:sub(2)end if w[1]then w[1]=w[1]:sub(2)end |
33 | 33 | return{o={s=tonumber(o[1]),l=tonumber(o[2])or 1},n={s=tonumber(w[1]),l=tonumber(w[2])or 1}}end |
34 | | - if h then if h.o.l~=bl-(h.o.s-1)or h.n.l~=ol-(h.n.s-1)then print(M)os.exit(1)else print(P)end end |
| 34 | + if h then if h.o.l~=rl-(h.o.s-1)or h.n.l~=wl-(h.n.s-1)then print(M)os.exit(1)else print(P)end end |
35 | 35 | h=parseHunkHeader()if h then |
36 | 36 | io.write("\t@@ -"..h.o.s..","..h.o.l.." +"..h.n.s..","..h.n.l.." @@: ") |
37 | | - local s=h.o.s>0 and(h.o.s-1)-bl or 0 if s<0 then print(M)os.exit(1)end |
| 37 | + local s=h.o.s>0 and(h.o.s-1)-rl or 0 if s<0 then print(M)os.exit(1)end |
38 | 38 | for _=1,s do |
39 | | - local ctx=bf:read("*l")if not ctx then print(M)os.exit(1)end |
40 | | - bl=bl+1 if of then of:write(ctx.."\n")ol=ol+1 end end end end end end |
41 | | - if bf then writeRemainder()end end |
| 39 | + local ctx=rf:read("*l")if not ctx then print(M)os.exit(1)end |
| 40 | + rl=rl+1 if wf then wf:write(ctx.."\n")wl=wl+1 end end end end end end |
| 41 | + if rf then writeRemainder()end end |
42 | 42 | if #arg<1 then print(arg[-1].." "..arg[0]..[[ diff... |
43 | 43 |
|
44 | 44 | Patch unified difference file(s)]])os.exit(1)end |
|
0 commit comments