lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



I didn't find a utility to count the added/modded/deleted lines, as listed on the Lua Wiki PowerPatches page.

Here is a quick tool for doing that.


--
-- COUNT-LINES.LUA
--
-- Counts new, changed, deleted lines separately for a patch.
--
-- Usage: lua count-lines.lua fn.patch
--

local fn= arg[1]
if not fn then
    io.stderr:write "Usage: lua count-lines.lua fn.patch\n\n"
    os.exit(-1)
end

local mod_sum=0
local add_sum=0
local del_sum=0

local prev_line_start

--
-- PRINT( str )
-- PRINT( lines_uint, whatdone_str )
--
local function PRINT( a, b )
    if type(a)=="number" then
        print( "  "..a.." lines "..b )
    else
        print( a )
    end
end

-- Enable 'PRINT_LOG()' for debugging
--
local PRINT_LOG= function() end
                or PRINT

-- Lines starting with '-' and thereafter '+' that we're currently scanning
--
local minus_lines= 0
local plus_lines= 0

for line in io.lines(fn) do
    local a= line:sub(1,3)  -- "---" or "+++" when file changes
    local regular=true

    if a~="---" and a~="+++" then
        local b= line:sub(1,1)
        if b=="-" then
            assert( plus_lines==0 )
            minus_lines= minus_lines+1
            regular= false

        elseif b=="+" then
            plus_lines= plus_lines+1
            regular= false
        end
    end

    if not regular then
        PRINT_LOG(line)
    end

    -- Regular lines end diff portions & update them to whole stats
    --
    if regular and ((minus_lines>0) or (plus_lines>0)) then
        if plus_lines==0 then
            del_sum= del_sum+minus_lines    -- block removed
            PRINT_LOG( minus_lines, "deleted" )
        elseif minus_lines==0 then
            add_sum= add_sum+plus_lines     -- block added
            PRINT_LOG( plus_lines, "added" )
        else
local t= math.min(minus_lines, plus_lines) -- lines both '-' and '+'
            mod_sum= mod_sum+t

            PRINT_LOG( t, "modified" )

            if plus_lines>minus_lines then
                add_sum= add_sum+ (plus_lines-minus_lines)
                PRINT_LOG( plus_lines-minus_lines, "added" )
            elseif minus_lines>plus_lines then
                del_sum= del_sum+ (minus_lines-plus_lines)
                PRINT_LOG( minus_lines-plus_lines, "deleted" )
            end
        end
        PRINT_LOG ""

        minus_lines= 0
        plus_lines= 0
    end

    prev_line_start= a:sub(1,1)
end

PRINT( mod_sum, "modified" )
PRINT( add_sum, "added" )
PRINT( del_sum, "deleted" )