From 25559e6edd23adbb1194672e1e965a8de61feda5 Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Sun, 8 Oct 2017 17:09:04 +0200 Subject: [PATCH] Import a Git repository hook --- doc/hooks/post-receive | 97 ++++++++++++++++++++++++++++++++++++++++++ doc/hooks/project.conf | 1 + doc/project.conf | 2 + project.conf | 2 +- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100755 doc/hooks/post-receive create mode 100644 doc/hooks/project.conf create mode 100644 doc/project.conf diff --git a/doc/hooks/post-receive b/doc/hooks/post-receive new file mode 100755 index 0000000..24df684 --- /dev/null +++ b/doc/hooks/post-receive @@ -0,0 +1,97 @@ +#!/bin/sh +#Copyright (c) 2014-2017 Pierre Pronchery +# +#Redistribution and use in source and binary forms, with or without +#modification, are permitted provided that the following conditions +#are met: +#1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +#2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +#THIS SOFTWARE IS PROVIDED BY THE EDGEBSD PROJECT AND CONTRIBUTORS +#``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +#TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +#PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +#BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +#CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +#SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +#INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +#CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +#ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +#POSSIBILITY OF SUCH DAMAGE. + + + +#variables +GIT_GITWEB="https://git.defora.org/gitweb" +GIT_MIRROR="/home/defora/git" +GIT_REMOTE="origin" +IRC_CHANNEL="#DeforaOS" +IRC_SERVER="irc.oftc.net" +#executables +GIT="/usr/bin/git" +HOOK="/usr/local/libexec/deforaos-git-hook.sh" +IRC="/usr/local/libexec/deforaos-irc.sh -N -s $IRC_SERVER -c $IRC_CHANNEL -n defora" +MKTEMP="/bin/mktemp" +RM="/bin/rm -f" + + +#functions +#hook_irc +_hook_irc() +{ + $HOOK -O GITWEB="$GIT_GITWEB" "post-receive" | $IRC + #ignore errors + return 0 +} + + +#hook_mirror +_hook_mirror() +{ + ret=0 + + [ -d "$GIT_MIRROR" ] || return 2 + while read oldrev newrev refname; do + branch= + case "$refname" in + refs/heads/*) + branch=${refname#refs/heads/} + ;; + esac + [ "$branch" = "master" ] || continue + mirror="$GIT_MIRROR/${GL_REPO}.git" + if [ ! -d "$mirror" ]; then + #clone the repository + $GIT clone "$HOME/repositories/${GL_REPO}.git" "$mirror" + if [ $? -ne 0 ]; then + echo "$GL_REPO: Could not create mirror" 1>&2 + continue + fi + fi + #refresh the repository + (unset GIT_DIR; + cd "$mirror" && + $GIT fetch -q "$GIT_REMOTE" && + $GIT reset -q --hard "$GIT_REMOTE/$branch") || ret=2 + done + return $ret +} + + +#main +#save a copy of the output +tmpfile=$($MKTEMP) +[ $? -eq 0 ] || exit 2 +while read line; do + echo "$line" >> "$tmpfile" +done +#chain the hooks +_hook_mirror < "$tmpfile" +_hook_irc < "$tmpfile" +#clean up +$RM -- "$tmpfile" || exit 2 + +exit 0 diff --git a/doc/hooks/project.conf b/doc/hooks/project.conf new file mode 100644 index 0000000..b098958 --- /dev/null +++ b/doc/hooks/project.conf @@ -0,0 +1 @@ +dist=Makefile,post-receive diff --git a/doc/project.conf b/doc/project.conf new file mode 100644 index 0000000..49724c8 --- /dev/null +++ b/doc/project.conf @@ -0,0 +1,2 @@ +subdirs=hooks +dist=Makefile diff --git a/project.conf b/project.conf index 406a20f..d72a5ec 100644 --- a/project.conf +++ b/project.conf @@ -1,5 +1,5 @@ package=scripts version=0.0.0 -subdirs=src,targets,tests +subdirs=doc,src,targets,tests dist=Makefile,COPYING