summaryrefslogtreecommitdiff
path: root/bin/gitano-post-receive-hook.in
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-30 14:11:05 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-30 14:11:05 +0100
commita797a8a9d11b0f0ca9bc738192475b06d5aacc9a (patch)
tree91d4ee70d1842d3487f32712278b7c9c56139005 /bin/gitano-post-receive-hook.in
parentf853bbf411c5bddfc3c4b46ad1c0593fcf56d93b (diff)
downloadgitano-a797a8a9d11b0f0ca9bc738192475b06d5aacc9a.tar.gz
BUILD: Start of a build system and thus installer
Diffstat (limited to 'bin/gitano-post-receive-hook.in')
-rw-r--r--bin/gitano-post-receive-hook.in138
1 files changed, 138 insertions, 0 deletions
diff --git a/bin/gitano-post-receive-hook.in b/bin/gitano-post-receive-hook.in
new file mode 100644
index 0000000..9b5ae37
--- /dev/null
+++ b/bin/gitano-post-receive-hook.in
@@ -0,0 +1,138 @@
+-- @@SHEBANG
+-- -*- Lua -*-
+-- gitano-post-receive-hook
+--
+-- Git (with) Augmented network operations -- Post-receive hook handler
+--
+-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
+--
+--
+
+-- @@GITANO_LUA_PATH
+
+local gitano = require "gitano"
+local luxio = require "luxio"
+local sio = require "luxio.simple"
+local sp = require "luxio.subprocess"
+
+local start_log_level = gitano.log.get_level()
+-- Clamp level at info until we have checked if the caller
+-- is an admin or not
+gitano.log.cap_level(gitano.log.level.INFO)
+
+local repo_root = luxio.getenv("GITANO_ROOT")
+local username = luxio.getenv("GITANO_USER") or "gitano/anonymous"
+local keytag = luxio.getenv("GITANO_KEYTAG") or "unknown"
+local project = luxio.getenv("GITANO_PROJECT") or ""
+local source = luxio.getenv("GITANO_SOURCE") or "ssh"
+
+-- Now load the administration data
+
+local admin_repo = gitano.git.repository.new((repo_root or "") .. "/gitano-admin.git")
+
+if not admin_repo then
+ gitano.log.fatal("Unable to locate administration repository. Cannot continue");
+end
+
+local admin_head = admin_repo:get(admin_repo.HEAD)
+
+if not admin_head then
+ gitano.log.fatal("Unable to find the HEAD of the administration repository. Cannot continue");
+end
+
+local config, msg = gitano.config.parse(admin_head)
+
+if not config then
+ gitano.log.critical("Unable to parse administration repository.")
+ gitano.log.critical(" * " .. (msg or "No error?"))
+ gitano.log.fatal("Cannot continue")
+end
+
+-- Now, are we an admin?
+if config.groups["gitano-admin"].filtered_members[username] then
+ -- Yep, so blithely reset logging level
+ gitano.log.set_level(start_log_level)
+end
+
+if not config.global.silent then
+ -- Not silent, bump to chatty level automatically
+ gitano.log.bump_level(gitano.log.level.CHAT)
+end
+
+local repo, msg = gitano.repository.find(config, project)
+if not repo then
+ gitano.log.critical("Unable to locate repository.")
+ gitano.log.critical(" * " .. (tostring(msg)))
+ gitano.log.fatal("Cannot continue")
+end
+
+if repo.is_nascent then
+ gitano.log.fatal("Repository " .. repo.name .. " is nascent")
+end
+
+-- Post-receive is not allowed to prevent updates, instead on stdin we get
+-- a list of the refs updated and we get to react to that (such as sending
+-- emails, ensuring that new rules are applied, etc)
+
+local updates = {}
+for oldsha, newsha, refname in
+ (sio.stdin:read("*a")):gmatch("([^ ]+) ([^ ]+) ([^\n]+)") do
+ gitano.log.ddebug("post-receive:", oldsha, newsha, refname)
+ updates[refname] = {oldsha, newsha, oldsha=oldsha, newsha=newsha}
+end
+
+-- updates to refs/gitano/admin will *already* have been processed thanks
+-- to the repository being found above. So if that's present, just note
+-- that the updates (if any) will have been applied
+
+if updates["refs/gitano/admin"] then
+ gitano.log.chat("<" .. repo.name .. ">",
+ "Any changes to admin ref have been applied.")
+end
+
+local function report_repo(reponame, repo, msg)
+ if repo then
+ gitano.log.chat("<" .. reponame .. ">",
+ "Any changes to hooks etc have been applied")
+ else
+ gitano.log.crit("<" .. reponame ..">", "Unable to process:", msg)
+ end
+end
+
+if repo.name == "gitano-admin" and updates[admin_repo.HEAD] then
+ -- Updating the 'master' of gitano-admin, let's iterate all the repositories
+ gitano.log.chat("Scanning repositories to apply hook/rules updates...")
+ local ok, msg = gitano.repository.foreach(config, report_repo)
+ if not ok then
+ gitano.log.crit(msg)
+ end
+ gitano.log.chat("All repositories updated where possible.")
+ local proc = sp.spawn({
+ config.global.bin_path .. "/gitano-update-ssh",
+ config.global.repository_root
+ })
+ local how, why = proc:wait()
+ if how ~= "exit" or why ~= 0 then
+ gitano.log.crit("Updating SSH keys didn't work?")
+ end
+elseif repo.name ~= "gitano-admin" then
+ -- Not gitano-admin at all, so run the update-server-info stuff
+ gitano.log.chat("Updating server info for dumb HTTP transport")
+ local ok, err = repo.git:update_server_info()
+ if not ok then
+ gitano.log.warn(err)
+ end
+end
+
+if repo:uses_hook("post-receive") then
+ gitano.log.debug("Configuring for post-receive hook")
+ gitano.actions.set_supple_globals("post-receive")
+ gitano.log.info("Running repository post-receive hook")
+ local ok, msg = gitano.supple.run_hook(repo, "post-receive", updates)
+ if not ok then
+ gitano.log.crit(msg or "No reason given, but errored somehow.")
+ end
+ gitano.log.info("Finished")
+end
+
+return 0