diff options
author | Thomas Kjeldahl Nilsson <thomas@kjeldahlnilsson.net> | 2012-06-25 18:25:09 (GMT) |
---|---|---|
committer | Thomas Kjeldahl Nilsson <thomas@kjeldahlnilsson.net> | 2012-06-29 07:51:57 (GMT) |
commit | a7ff8e60d2546697577ccf4fd6e36913e0027609 (patch) | |
tree | aa351996805962f6e8d13024ff604a2ba9fcc547 /data | |
parent | 1073131e8e3045915cb3b7c380df0a6ad16df69c (diff) |
Add custom serverside git hooks
Custom pre-receive,post-receive,update hooks now included, see
the .sample files in /data/hooks. Shared by all repositories,
called after Gitorious has run its regular hooks. Enable them
by copying from {pre-receive,post-receive,update}.sample to
{pre-receive,post-receive,update}. Documentation and sample
debug code included in each sample file.
Diffstat (limited to 'data')
-rwxr-xr-x | data/hooks/custom-post-receive.sample | 38 | ||||
-rwxr-xr-x | data/hooks/custom-pre-receive.sample | 37 | ||||
-rwxr-xr-x | data/hooks/custom-update.sample | 36 | ||||
-rwxr-xr-x | data/hooks/post-receive | 32 | ||||
-rwxr-xr-x | data/hooks/pre-receive | 39 | ||||
-rwxr-xr-x[-rw-r--r--] | data/hooks/update | 126 |
6 files changed, 233 insertions, 75 deletions
diff --git a/data/hooks/custom-post-receive.sample b/data/hooks/custom-post-receive.sample new file mode 100755 index 0000000..b00a7a3 --- /dev/null +++ b/data/hooks/custom-post-receive.sample @@ -0,0 +1,38 @@ +#!/usr/bin/env ruby + +# Custom post-receive hook +# ----------------------- + +# Enable by renaming from custom-post-receive.sample to +# custom-post-receive. Once renamed, it can be customized freely and is +# not version controlled. Content of this script can be changed to +# regular shellscript, no need to be ruby code. + +# Called at the end of the standard Gitorious post-receive hook. +# Like the regular post-receive hook, the custom post-receive hook +# receives pushed references on stdin, one line per pushed reference. + +# Example: pushing 'master' and 'testbranch' will pass something like +# this to stdin +# +# remote: b506db4d7520f0257dfe389ab729fa0976ced289 1cbd892e83f597cb6c33a53a45c38a1ba6bbef27 refs/heads/master +# remote: 5577de353b5799f4c7bd65561b630c4cc8eb4b1c 19df9a1b5c973b004f23f7989ce2773ba638acd9 refs/heads/testbranch + +# Anything written to stdout (e.g 'puts("foo")') and stderr will be +# passed back over the line to the user who started the git push. + +# exit!(EXITCODE), or raising exceptions will cause an abort at this +# point, but note that the actual git push operation has already gone +# through so doing an unexpected exit serves little or no purpose. + +# More on the "pre-receive", "post-receive" and "update" serverside hooks: +# http://git-scm.com/book/en/Customizing-Git-Git-Hooks#Server-Side-Hooks + +puts "[DEBUG] CUSTOM POST-RECEIVE HOOK STARTS" + +puts "[DEBUG] REFERENCES PUSHED:" +while data = gets + puts data # writes each pushed reference to stdout +end + +puts "[DEBUG] CUSTOM POST-RECEIVE HOOK ENDS" diff --git a/data/hooks/custom-pre-receive.sample b/data/hooks/custom-pre-receive.sample new file mode 100755 index 0000000..ba4d35a --- /dev/null +++ b/data/hooks/custom-pre-receive.sample @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +# Custom pre-receive hook +# ----------------------- + +# Enable by renaming from custom-pre-receive.sample to +# custom-pre-receive. Once renamed, it can be customized freely and is +# not version controlled. Content of this script can be changed to +# regular shellscript, no need to be ruby code. + +# Called at the end of the standard Gitorious pre-receive hook. +# Like the regular pre-receive hook, the custom pre-receive hook +# receives pushed references on stdin, one line per pushed reference. + +# Example: pushing 'master' and 'testbranch' will pass something like +# this to stdin +# +# remote: b506db4d7520f0257dfe389ab729fa0976ced289 1cbd892e83f597cb6c33a53a45c38a1ba6bbef27 refs/heads/master +# remote: 5577de353b5799f4c7bd65561b630c4cc8eb4b1c 19df9a1b5c973b004f23f7989ce2773ba638acd9 refs/heads/testbranch + +# Anything written to stdout (e.g 'puts("foo")') and stderr will be +# passed back over the line to the user who started the git push. + +# exit!(EXITCODE), or raising exceptions will cause the push operation +# to fail immediately. + +# More on the "pre-receive", "post-receive" and "update" serverside hooks: +# http://git-scm.com/book/en/Customizing-Git-Git-Hooks#Server-Side-Hooks + +puts "[DEBUG] CUSTOM PRE-RECEIVE HOOK STARTS" + +puts "[DEBUG] REFERENCES PUSHED:" +while data = gets + puts data # writes each pushed reference to stdout +end + +puts "[DEBUG] CUSTOM PRE-RECEIVE HOOK ENDS" diff --git a/data/hooks/custom-update.sample b/data/hooks/custom-update.sample new file mode 100755 index 0000000..8dd204f --- /dev/null +++ b/data/hooks/custom-update.sample @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby + +# Custom update hook +# ----------------------- + +# Enable by renaming from custom-update.sample to custom-update. Once +# renamed, it can be customized freely and is not version +# controlled. Content of this script can be changed to regular +# shellscript, no need to be ruby code. + +# Called at the end of the standard Gitorious update hook. Like the +# regular update hook, the custom update hook is called once per +# pushed reference, taking the name of the reference, original commit +# sha and attempted new commit sha as params. + +# Anything written to stdout (e.g 'puts("foo")') and stderr will be +# passed back over the line to the user who started the git push. + +# exit!(EXITCODE), or raising exceptions, will abort push of +# the current reference (and only that reference, others may be pushed +# just fine.) + +# More on the "pre-receive", "update" and "update" serverside hooks: +# http://git-scm.com/book/en/Customizing-Git-Git-Hooks#Server-Side-Hooks + +puts "[DEBUG] CUSTOM UPDATE HOOK STARTS" + +reference_name = ARGV[0] +sha_before = ARGV[1] +sha_after = ARGV[2] + +puts "[DEBUG] REFERENCE PUSHED: #{reference_name}" +puts "[DEBUG] ORIGINAL SHA: #{sha_before}" +puts "[DEBUG] REFERENCE PUSHED: #{sha_after}" + +puts "[DEBUG] CUSTOM UPDATE HOOK ENDS" diff --git a/data/hooks/post-receive b/data/hooks/post-receive index bffc95c..5c5d215 100755 --- a/data/hooks/post-receive +++ b/data/hooks/post-receive @@ -33,8 +33,40 @@ hashed_dir = gitdir.split('/')[-3,3].join('/').split('.').first # compat with the old-style paths hashed_dir = hashed_dir.sub(/^repositories\//, "") +hook_input_lines = "" + while line = gets + hook_input_lines << line @publisher.post_message({:gitdir => hashed_dir, :message => line.chomp, :username => ENV['GITORIOUS_USER']}) end +require 'pathname' +def custom_hook_path + real_path_of_this = Pathname.new(File.dirname(__FILE__)).realpath + full_path_of_this = File.expand_path(real_path_of_this) + custom_prereceive_path = full_path_of_this + "/custom-post-receive" +end + +def run_custom_hook_if_present(input) + if File.exist?(custom_hook_path) + cmd = %Q{#{custom_hook_path} 2>&1} + IO.popen(cmd, 'w+') do |subprocess| + subprocess.write(input) + subprocess.close_write + subprocess.read.split("\n").each do |l| + puts "#{l}\n" + end + end + + custom_hook_status = $? + if !custom_hook_status.success? + puts "not success, exiting with its code" + exit(custom_hook_status.exitstatus) + end + end +end + +run_custom_hook_if_present(hook_input_lines) + + puts "[OK]" diff --git a/data/hooks/pre-receive b/data/hooks/pre-receive index 48794ec..ebc0cf0 100755 --- a/data/hooks/pre-receive +++ b/data/hooks/pre-receive @@ -28,7 +28,11 @@ logger = if ENV["GITORIOUS_BASE_DIR"] Logger.new("/tmp/pre_receive_guard.log") end + +hook_input_lines = "" + while data = gets + hook_input_lines << data begin pre_receive_guard = Gitorious::SSH::PreReceiveGuard.new(ENV.to_hash, data) oldrev, newrev, ref = data.chomp.split(" ") @@ -84,4 +88,39 @@ while data = gets pre_receive_guard.gitorious_says "Temporary interuption. Please try again shortly" exit!(1) end + + +end + +require 'pathname' +def custom_hook_path + real_path_of_this = Pathname.new(File.dirname(__FILE__)).realpath + full_path_of_this = File.expand_path(real_path_of_this) + custom_prereceive_path = full_path_of_this + "/custom-pre-receive" end + +def run_custom_hook_if_present(input) + if File.exist?(custom_hook_path) + cmd = %Q{#{custom_hook_path} 2>&1} + IO.popen(cmd, 'w+') do |subprocess| + subprocess.write(input) + subprocess.close_write + subprocess.read.split("\n").each do |l| + puts "#{l}\n" + end + end + + custom_hook_status = $? + if !custom_hook_status.success? + puts "not success, exiting with its code" + exit(custom_hook_status.exitstatus) + end + end +end + +run_custom_hook_if_present(hook_input_lines) + + + + + diff --git a/data/hooks/update b/data/hooks/update index 3cc5ba7..0201c3a 100644..100755 --- a/data/hooks/update +++ b/data/hooks/update @@ -1,78 +1,54 @@ -#!/bin/sh -# -# An example hook script to blocks unannotated tags from entering. -# Called by git-receive-pack with arguments: refname sha1-old sha1-new +#!/usr/bin/env ruby + +#-- +# Copyright (C) 2012 Gitorious AS # -# To enable this hook, make this file executable by "chmod +x update". +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. # -# Config -# ------ -# hooks.allowunannotated -# This boolean sets whether unannotated tags will be allowed into the -# repository. By default they won't be. +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. # - -# --- Command line -refname="$1" -oldrev="$2" -newrev="$3" - -# --- Safety check -if [ -z "$GIT_DIR" ]; then - echo "Don't run this script from the command line." >&2 - echo " (if you want, you could supply GIT_DIR then run" >&2 - echo " $0 <ref> <oldrev> <newrev>)" >&2 - exit 1 -fi - -if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then - echo "Usage: $0 <ref> <oldrev> <newrev>" >&2 - exit 1 -fi - -# --- Config -allowunannotated=$(git repo-config --bool hooks.allowunannotated) - -# check for no description -projectdesc=$(sed -e '1p' "$GIT_DIR/description") -if [ -z "$projectdesc" -o "$projectdesc" = "Unnamed repository; edit this file to name it for gitweb" ]; then - echo "*** Project description file hasn't been set" >&2 - exit 1 -fi - -# --- Check types -# if $newrev is 0000...0000, it's a commit to delete a branch -if [ -z "${newrev##0*}" ]; then - newrev_type=commit -else - newrev_type=$(git cat-file -t $newrev) -fi - -case "$refname","$newrev_type" in - refs/tags/*,commit) - # un-annotated tag - short_refname=${refname##refs/tags/} - if [ "$allowunannotated" != "true" ]; then - echo "*** The un-annotated tag, $short_refname, is not allowed in this repository" >&2 - echo "*** Use 'git tag [ -a | -s ]' for tags you want to propagate." >&2 - exit 1 - fi - ;; - refs/tags/*,tag) - # annotated tag - ;; - refs/heads/*,commit) - # branch - ;; - refs/remotes/*,commit) - # tracking branch - ;; - *) - # Anything else (is there anything else?) - echo "*** Update hook: unknown type of update to ref $refname of type $newrev_type" >&2 - exit 1 - ;; -esac - -# --- Finished -exit 0 +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +#++ + +reference_name = ARGV[0] +sha_before = ARGV[1] +sha_after = ARGV[2] + +# NOTE: Any core Gitorious update operations goes here at the top (none so far). + +require 'pathname' +def custom_hook_path + real_path_of_this = Pathname.new(File.dirname(__FILE__)).realpath + full_path_of_this = File.expand_path(real_path_of_this) + custom_prereceive_path = full_path_of_this + "/custom-update" +end + +# TODO pass the 3 arguments instead of stdin lines + +def run_custom_hook_if_present(ref, sha1, sha2) + if File.exist?(custom_hook_path) + cmd = %Q{#{custom_hook_path} #{ref} #{sha1} #{sha2} 2>&1} + IO.popen(cmd, 'w+') do |subprocess| + subprocess.write("") + subprocess.close_write + subprocess.read.split("\n").each do |l| + puts "#{l}\n" + end + end + + custom_hook_status = $? + if !custom_hook_status.success? + puts "not success, exiting with its code" + exit(custom_hook_status.exitstatus) + end + end +end + +run_custom_hook_if_present(reference_name, sha_before, sha_after) |