Skip to content

Commit ef5e06f

Browse files
committed
Squished PATCH.LUA
1 parent 3e89ae2 commit ef5e06f

File tree

1 file changed

+41
-110
lines changed

1 file changed

+41
-110
lines changed

xtra/PATCH.LUA

Lines changed: 41 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,45 @@
11
#!/usr/bin/env lua
22

3+
M="malformed"
34
local function patch(p)
4-
local pf, e = io.open(p)
5-
local ol, bl, cp, of, bf, hunk = 0, 0
6-
if not pf then print(e) os.exit(1) end
7-
8-
local function parseFileName(l)
9-
local first = l:sub(5,5)
10-
if first == '"' or first == "'" then
11-
return l:match(first .. "([^" .. first .. "]+)" .. first, 5)
12-
else
13-
local words = {}
14-
for v in string.gmatch(l, "%S+") do table.insert(words, v) end
15-
return words[2]
16-
end
17-
end
18-
5+
local pf,e=io.open(p)local ol,bl,cp,of,bf,h=0,0
6+
if not pf then print(e)os.exit(1)end
7+
local function parseFileName(l)local first=l:sub(5,5)
8+
if first=='"'or first=="'"then return l:match(first.."([^"..first.."]+)"..first,5)
9+
else local words={}for v in string.gmatch(l,"%S+")do table.insert(words,v)end return words[2]end end
1910
local function setPatchFile(f)
20-
local o, b
21-
local function abort()
22-
print(e)
23-
if o then o:close() end
24-
if b then b:close() end
25-
return false
26-
end
27-
o, e = io.open(f)if not o then return abort end
28-
b, e = io.open(f..".orig", "w")if not b then return abort end
29-
for m in o:lines() do b:write(m..'\n') end o:close() b:close()
30-
o, e = io.open(f, "w") if not o then return abort end
31-
b, e = io.open(f..".orig") if not b then return abort end
32-
if of then of:close() end if bf then bf:close() end of = o bf = b
33-
return true
34-
end
35-
36-
local function writeRemainder()
37-
if of then
38-
local rest = bf:read("*l")
39-
while rest do
40-
of:write(rest .. '\n')
41-
rest = bf:read("*l")
42-
end
43-
of:close()
44-
end
45-
bf:close()
46-
end
47-
48-
local n=pf:read("*l")
49-
while n do
50-
local l=n n=pf:read("*l")
51-
local ls = l:sub(1, 1)
52-
if ls == "+" then
53-
if l:sub(2,4) == "++ " then
54-
--TODO: Just ignore this line?
55-
else
56-
if of then
57-
local nl = n and n:match("^\\ No newline at end of file")
58-
of:write(l:sub(2)..(nl and""or'\n')) ol = ol + 1
59-
end
60-
end
61-
elseif ls == '-' then
62-
if l:sub(2,4) == "-- " then
63-
local filename = parseFileName(l)
64-
if bf then writeRemainder() end
65-
if setPatchFile(filename) then cp = filename print("\n'" .. cp .. "':") end
66-
else
67-
local cmp = bf:read("*l") bl = bl + 1
68-
if cmp ~= l:sub(2) then print("malformed")os.exit(1) end
69-
end
70-
elseif ls == ' ' then
71-
--TODO: context line verification
72-
local line = l:sub(2)
73-
if bf then
74-
local cmp = bf:read("*l") bl = bl + 1
75-
if cmp == line then
76-
if of then of:write(line .. '\n') ol = ol + 1 end
77-
else
78-
print("malformed")os.exit(1)
79-
end
80-
end
81-
82-
elseif ls == '@' then
83-
if l:sub(2,3) == "@ " then
84-
local function parseHunkHeader()
85-
local function split(i,d)
86-
local r={}
87-
for m in (i..d):gmatch("(.-)"..d)do table.insert(r,m)end
88-
return r
89-
end
90-
local words,old,new=split(l," "),{},{}
91-
if words[2] then old=split(words[2],",")end
92-
if words[3] then new=split(words[3],",")end
93-
if old[1] then old[1]=old[1]:sub(2)end
94-
if new[1] then new[1]=new[1]:sub(2)end
95-
return {
96-
old = { start = tonumber(old[1]), lines = tonumber(old[2]) or 1 },
97-
new = { start = tonumber(new[1]), lines = tonumber(new[2]) or 1 }
98-
}
99-
end
100-
if hunk then if hunk.old.lines~=bl-(hunk.old.start-1)or hunk.new.lines~=ol-(hunk.new.start-1)then print("malformed")os.exit(1)else print("patched")end end
101-
hunk=parseHunkHeader()if hunk then
102-
io.write("\t@@ -"..hunk.old.start..","..hunk.old.lines.." +"..hunk.new.start..","..hunk.new.lines.." @@: ")
103-
local skip=hunk.old.start>0 and(hunk.old.start-1)-bl or 0
104-
if skip<0 then print("malformed (overlapping hunk)")os.exit(1)end
105-
for _=1,skip do
106-
local ctx=bf:read("*l")if not ctx then print("malformed (unexpected EOF in context)")os.exit(1)end
107-
bl=bl+1 if of then of:write(ctx.."\n")ol=ol+1 end end end end end end
108-
pf:close()
109-
if hunk then print("patched") end
110-
if bf then writeRemainder() end
111-
end
112-
113-
if #arg < 1 then os.exit(1) end
114-
patch(arg[1], arg[2])
11+
local o,b local function abort()print(e)if o then o:close()end if b then b:close()end return false end
12+
o,e=io.open(f)if not o then return abort end
13+
b,e=io.open(f..".orig","w")if not b then return abort end
14+
for m in o:lines()do b:write(m..'\n')end o:close()b:close()
15+
o,e=io.open(f,"w")if not o then return abort end
16+
b,e=io.open(f..".orig")if not b then return abort end
17+
if of then of:close()end if bf then bf:close()end of=o bf=b return true end
18+
local function writeRemainder()if of then local rest=bf:read("*l")while rest do of:write(rest..'\n')rest=bf:read("*l")end of:close()end bf:close()end
19+
local n=pf:read("*l")while n do
20+
local l,ls=n n=pf:read("*l")ls=l:sub(1,1)
21+
if ls=='+'then
22+
if l:sub(2,4)=="++ " then --ignore
23+
else if of then local nl=n and n:match("^\\ No newline at end of file")of:write(l:sub(2)..(nl and""or'\n'))ol=ol + 1 end end
24+
elseif ls=='-'then
25+
if l:sub(2,4)=="-- " then local filename=parseFileName(l)if bf then writeRemainder()end
26+
if setPatchFile(filename)then cp=filename print("\n'"..cp.."':")end
27+
else local cmp=bf:read("*l")bl=bl + 1 if cmp ~=l:sub(2)then print(M)os.exit(1)end end
28+
elseif ls==' 'then
29+
if bf then local cmp,line=bf:read("*l"),l:sub(2)bl=bl + 1 if cmp==line then if of then of:write(line..'\n')ol=ol + 1 end else print(M)os.exit(1)end end
30+
elseif ls=='@'then if l:sub(2,3)=="@ " then
31+
local function parseHunkHeader()
32+
local function split(i,d)local r={}for m in (i..d):gmatch("(.-)"..d)do table.insert(r,m)end return r end
33+
local words,o,n=split(l," "),{},{}
34+
if words[2]then o=split(words[2],",")end if words[3]then n=split(words[3],",")end
35+
if o[1]then o[1]=o[1]:sub(2)end if n[1]then n[1]=n[1]:sub(2)end
36+
return{o={s=tonumber(o[1]),l=tonumber(o[2])or 1},n={s=tonumber(n[1]),l=tonumber(n[2])or 1}}end
37+
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("patched")end end
38+
h=parseHunkHeader()if h then
39+
io.write("\t@@ -"..h.o.s..","..h.o.l.." +"..h.n.s..","..h.n.l.." @@: ")
40+
local s=h.o.s>0 and(h.o.s-1)-bl or 0 if s<0 then print(M)os.exit(1)end
41+
for _=1,s do
42+
local ctx=bf:read("*l")if not ctx then print(M)os.exit(1)end
43+
bl=bl+1 if of then of:write(ctx.."\n")ol=ol+1 end end end end end end
44+
pf:close()if h then print("patched")end if bf then writeRemainder()end end
45+
if #arg<1 then os.exit(1)end patch(arg[1])

0 commit comments

Comments
 (0)