33P ,M = " patched" ," malformed"
44local function patch (pf )
55 local wl ,rl ,cp ,wf ,rf ,h ,e = 0 ,0
6- local function parseFileName (l )local f = l :sub (5 ,5 )
6+ local function PFN (l )local f = l :sub (5 ,5 )
77 if f == ' "' or f == " '" then return l :match (f .. " ([^" .. f .. " ]+)" .. f ,5 )
88 else f = {}for v in string.gmatch (l ," %S+" )do table.insert (f ,v )end return f [2 ]end end
9- local function setPatchFile (f ) -- TODO: write patch to temporary file first
9+ local function SPF (f ) -- TODO: write patch to temporary file first
1010 local r ,w local function abort ()print (e )if r then r :close ()end if w then w :close ()end return false end
1111 r ,e = io.open (f )if not r then return abort ()end
1212 w ,e = io.open (f .. " .orig" ," w" )if not w then return abort ()end
@@ -16,32 +16,27 @@ local function patch(pf)
1616 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
1717 local function R ()rl = rl + 1 return rf :read (" *l" )end
1818 local function W (s )wl = wl + 1 return wf :write (s )end
19- 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
20- if wf then local rest = R ()while rest do W (rest .. ' \n ' )rest = R ()end wf :close ()wf ,wl = nil ,0 end rf :close ()rf ,rl ,h = nil ,0 ,nil end
21- local n = pf :read (" *l" )while n do
22- local l ,ls = n n = pf :read (" *l" )ls = l :sub (1 ,1 )
23- if ls == ' +' then
19+ local function WR ()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
20+ if wf then local r = R ()while r do W (r .. ' \n ' )r = R ()end wf :close ()wf ,wl = nil ,0 end rf :close ()rf ,rl ,h = nil ,0 ,nil end
21+ local n = pf :read (" *l" )while n do local l ,f = n n = pf :read (" *l" )f = l :sub (1 ,1 )if f == ' +' then
2422 if l :sub (2 ,4 )~= " ++ " then W (l :sub (2 ).. (n and n :match (" ^\\ No newline at end of file" )and " " or ' \n ' ))end
25- elseif ls == ' -' then
26- 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
23+ elseif f == ' -' then
24+ if l :sub (2 ,4 )== " -- " then local fn = PFN (l )if rf then WR ()end if SPF (fn )then cp = fn print (" \t " .. cp .. " :" )end
2725 elseif R ()~= l :sub (2 )then print (M )os.exit (1 )end
28- elseif ls == ' ' then
26+ elseif f == ' ' then
2927 if rf then local c ,i = R (),l :sub (2 )if c == i then if wf then W (i .. ' \n ' )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 s ,o ,w = split (l ," " ),{},{}if s [2 ]then o = split (s [2 ]," ," )end if s [3 ]then w = split (s [3 ]," ," )end
28+ elseif f == ' @' then if l :sub (2 ,3 )== " @ " then
29+ local function PHH () local function S (i ,d )local r = {}for m in (i .. d ):gmatch (" (.-)" .. d )do table.insert (r ,m )end return r end
30+ local s ,o ,w = S (l ," " ),{},{}if s [2 ]then o = S (s [2 ]," ," )end if s [3 ]then w = S (s [3 ]," ," )end
3431 if o [1 ]then o [1 ]= o [1 ]:sub (2 )end if w [1 ]then w [1 ]= w [1 ]:sub (2 )end
3532 return {o = {s = tonumber (o [1 ]),l = tonumber (o [2 ])or 1 },n = {s = tonumber (w [1 ]),l = tonumber (w [2 ])or 1 }}end
3633 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
37- h = parseHunkHeader ()if h then
38- io.write (" \t @@ -" .. h .o .s .. " ," .. h .o .l .. " +" .. h .n .s .. " ," .. h .n .l .. " @@: " )
39- local s = h .o .s > 0 and (h .o .s - 1 )- rl or 0 if s < 0 then print (M )os.exit (1 )end
34+ h = PHH ()if h then io.write (" \t\t @@ -" .. h .o .s .. " ," .. h .o .l .. " +" .. h .n .s .. " ," .. h .n .l .. " @@: " )local s = h .o .s > 0 and (h .o .s - 1 )- rl or 0 if s < 0 then print (M )os.exit (1 )end
4035 for _ = 1 ,s do
41- local ctx = R ()if not ctx then print (M )os.exit (1 )end
42- if wf then W (ctx .. " \n " )end end end end end end
43- if rf then writeRemainder ()end end
36+ local r = R ()if not r then print (M )os.exit (1 )end
37+ if wf then W (r .. " \n " )end end end end end end
38+ if rf then WR ()end end
4439if # arg < 1 then print (arg [- 1 ].. " " .. arg [0 ].. [[ diff...
4540
4641Patch unified difference file(s)]] )os.exit (1 )end
47- for _ ,a in ipairs (arg )do local f ,e = io.open (a )if f then patch (f )f :close ()else print (e )end end
42+ for _ ,a in ipairs (arg )do local f ,e = io.open (a )if f then print ( a .. ' : ' ) patch (f )f :close ()else print (e )end end
0 commit comments