#!/opt/Lua51/bin/lua local cjson = require "cjson" local invoker = require "invoker" local gfmt = string.format local function goodstr(s) if type(s) ~= "string" then return false end if string.len(s) == 0 then return false end return true end local function tagman_load(cfgfile) local cfgd = invoker.readfile(cfgfile, TAGMAN_MAXSIZE) if not cfgd then io.stderr:write(gfmt("Error, failed to read file: %s\n", cfgfile)) io.stderr:flush() return false end local okay, jdat = pcall(cjson.decode, cfgd) cfgd = nil -- free memory early if not okay or type(jdat) ~= "table" then io.stderr:write(gfmt("Error, failed to decode as JSON: %s\n", cfgfile)) io.stderr:flush() return false end local repos = jdat["repolist"] if type(repos) ~= "table" then io.stderr:write(gfmt("Error, repolist not found in %s\n", cfgfile)) io.stderr:flush() return false end local pathidx = {} local repidx, repoinfo = 0, {} while true do repidx = repidx + 1 local rinfo = repos[repidx] if type(rinfo) ~= "table" then break end -- extract Git repo information local path_g, branch, tagv = rinfo['path'], rinfo['branch'], rinfo['tag'] if not goodstr(path_g) then io.stderr:write(gfmt("Error, invalid git repository information at index: %d\n", repidx)) io.stderr:flush() return false end local dotgit = gfmt("%s/.git", path_g) if invoker.statfile(path_g) ~= "directory" or invoker.statfile(dotgit) ~= "directory" then io.stderr:write(gfmt("Error, invalid git repository path: %s\n", path_g)) io.stderr:flush() return false end if pathidx[path_g] then -- check for git duplication io.stderr:write(gfmt("Error, duplicated git repository: %s\n", path_g)) io.stderr:flush() return false end if not goodstr(branch) then io.stderr:write(gfmt("Error, branch not specified for %s\n", path_g)) io.stderr:flush() return false end if not goodstr(tagv) then tagv = nil end pathidx[path_g] = repidx repoinfo[path_g] = { ["branch"] = branch, ["tag"] = tagv, } end if repidx <= 0 then return nil end return repoinfo end local function mainfunc(arg1, arg2) if not invoker.statfile(arg1) then if type(arg1) ~= "string" then arg1 = "nil" end io.stderr:write(gfmt("Error, first json file not found: %s\n", arg1)) io.stderr:flush() return false end if not invoker.statfile(arg2) then if type(arg2) ~= "string" then arg2 = "nil" end io.stderr:write(gfmt("Error, second json file not found: %s\n", arg2)) io.stderr:flush() return false end local r1info = tagman_load(arg1) if not r1info then io.stderr:write(gfmt("Error, failed to load branch/tag file: %s\n", arg1)) io.stderr:flush() return false end local r2info = tagman_load(arg2) if not r1info then io.stderr:write(gfmt("Error, failed to load branch/tag file: %s\n", arg2)) io.stderr:flush() return false end local sep0 = "============================================================\n" local sep1 = "************************************************************\n" for path_i, info_2 in pairs(r2info) do local info_1 = r1info[path_i] if type(info_1) == "table" then local tag1, tag2 = info_1["tag"], info_2["tag"] if goodstr(tag1) and goodstr(tag2) then if tag1 ~= tag2 then io.stdout:write(sep0) io.stdout:write(gfmt("ChangeLog for %s\n", path_i)) io.stdout:flush() if not invoker.statfile(gfmt("%s/ChangeLog", path_i)) then io.stderr:write(gfmt("Error, ChangeLog file not found: %s\n", path_i)) io.stderr:flush() else invoker.invoke(0, "bash", "-c", gfmt("cd \"%s\" && (git diff \"%s\" \"%s\" -- ChangeLog | cat)", path_i, tag1, tag2)) end io.stdout:write(sep1) io.stdout:flush() end else io.stderr:write(gfmt("Error, Git repository \"%s\" tag missing\n", path_i)) io.stderr:flush() end else io.stderr:write(gfmt("Error, Git repository \"%s\" not found in \"%s\"\n", path_i, arg1)) io.stderr:flush() end r1info[path_i] = nil end return true end os.exit(mainfunc(arg[1], arg[2]) and 0 or 1)