--[[ This example client-side loose Extension runs before a 'p4 add' command and checks that the files being added are not over an arbitrary size limit. ]]-- local maxSize = 123 function fileSize( name ) local fh = io.open( name, "rb" ) if fh == nil then return -1 end local size = fh:seek( "end" ) fh:close() return size end --[[ Client-side Extensions have two callbacks that they can define - one to run before a command is executed, and one afterwards. The preCommand callback function can choose to 0) let the command PASS and continue to run, 1) REPLACE the command (pretend the command ran but not actually do it on the assumption that the Extension did something in its place), or 2) FAIL the command. The postCommand hook can only FAIL and PASS. ]]-- function preCommand() local func = Helix.Core.Client.GetVar( "func" ) -- The callbacks are run for every command, so check that this is a 'p4 add'. if func ~= "add" then return Helix.Core.Client.Action.PASS end local argc = Helix.Core.Client.GetVar( "argc" ) local argv = Helix.Core.Client.GetVar( "argv" ) local msg = "" for i, v in ipairs( argv ) do local fileSize = fileSize( v ) if fileSize > maxSize then msg = msg .. v .. " " .. fileSize if i < argc - 1 then msg = msg .. ", " end end end if string.len( msg ) > 0 then msg = "Files over size limit: " .. msg Helix.Core.Client.ClientError( msg ) return Helix.Core.Client.Action.FAIL end return Helix.Core.Client.Action.PASS end