#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2008 Michael Tremer & Christian Schmidt # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # 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 General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # # A small helper to make the git functions comfortable # # # ############################################################################### ############################################################################### # # Set the environment # ############################################################################### if [ -z "$EDITOR" ]; then for i in nano emacs vi; do EDITOR=$(which $i 2>/dev/null) if ! [ -z "$EDITOR" ]; then export EDITOR=$EDITOR break fi done [ -z "$EDITOR" ] && exiterror "You should have installed an editor." fi CURRENT_BRANCH=$(git branch | grep ^* | cut -c 3-) # Apply git hooks if [ -d "$BASEDIR/.git" ]; then ln -sf $BASEDIR/tools/make-git $BASEDIR/.git/hooks/pre-commit fi ############################################################################### # # A small helper to make the git functions comfortable # ############################################################################### git_pull() { if [ "$1" = "--batch" ]; then shift git pull $* | grep -q "Already up-to-date." || rm -f $FAILED [ -e $FAILED ] || distccd_restart [ "$(expr $RANDOM % 25)" -eq "0" ] && git_gc else if [ "$CURRENT_BRANCH" == "master" ]; then if ! (git status | grep -q "working directory clean"); then echo "Your \"master\" branch is not clean. This may cause a merge commit." echo -n "Do you want this? [y/N]" read [ -z $REPLY ] && return 0 for i in y Y j J; do if [ "$i" == "$REPLY" ]; then git pull return $? fi done return 0 else git pull fi else echo -n "You are not on branch \"master\". Do you want to rebase from \"master\"? [y/N]" read [ -z $REPLY ] && return 0 for i in y Y j J; do if [ "$i" == "$REPLY" ]; then git rebase -v refs/heads/master return $? fi done exiterror "\"$REPLY\" is not a valid answer." fi fi } git_push() { check_user [ "$CURRENT_BRANCH" == "master" ] || \ exiterror "You have to be in branch \"master\", if you want to push." git push ssh://${IPFIRE_USER}@git.ipfire.org/pub/git/ipfire-3.x master } git_commit() { check_sanity git commit $* [ "$?" -eq "0" ] || dialogerror "git commit $* failed." } git_diff() { DIFF_NAME="${BASEDIR}/ipfire-diff-$(date '+%Y%m%d-%0k%0M').diff" check_sanity --fix echo -ne "Make a local diff to last revision" git diff HEAD > $DIFF_NAME evaluate if [ -s $DIFF_NAME ]; then # if diff file is empty echo "Diff was successfully saved to $DIFF_NAME" git diff HEAD --stat else rm -f $DIFF_NAME return 1 fi } git_log() { echo -n "Generating changelog from repository" git log -n 500 --no-merges --pretty=medium --shortstat $(git_range) > $BASEDIR/doc/ChangeLog evaluate } git_shortlog() { git shortlog --no-merges $(git_range) } git_gc() { git gc --prune } git_export() { git archive HEAD | gzip -9 > ${SNAME}-${VERSION}.source.tar.gz } git_range() { local EXT [ -z $GIT_TAG ] || LAST_TAG=$GIT_TAG [ -z $LAST_TAG ] || EXT="$LAST_TAG..HEAD" echo $EXT } ssh_cert() { test $# -gt 0 || exiterror "You need to pass the hostname of the remote host." SSH_KEYGEN=`which ssh-keygen` if test $? -ne 0; then # Error message is printed by 'which' return 1 fi SSH_DIR=~/.ssh if ! test -d $SSH_DIR; then mkdir $SSH_DIR fi chmod 700 $SSH_DIR if [ ! -f $SSH_DIR/identity ] || [ ! -f $SSH_DIR/identity.pub ]; then echo "Generating ssh1 RSA keys - please wait..." rm -f $SSH_DIR/identity $SSH_DIR/identity.pub $SSH_KEYGEN -t rsa1 -f $SSH_DIR/identity -P '' if [ $? -ne 0 ]; then echo "Command \"$SSH_KEYGEN -t rsa1 -f $SSH_DIR/identity" \ "-P ''\" failed" 1>&2 return 1 fi else echo "ssh1 RSA key is present" fi if [ ! -f $SSH_DIR/id_dsa ] || [ ! -f $SSH_DIR/id_dsa.pub ]; then echo "Generating ssh2 DSA keys - please wait..." rm -f $SSH_DIR/id_dsa $SSH_DIR/id_dsa.pub $SSH_KEYGEN -t dsa -f $SSH_DIR/id_dsa -P '' if test $? -ne 0; then echo "Command \"$SSH_KEYGEN -t dsa -f $SSH_DIR/id_dsa" \ "-P ''\" failed" 1>&2 return 1 fi else echo "ssh2 DSA key is present" fi SSH1_RSA_KEY=`cat $SSH_DIR/identity.pub` SSH2_DSA_KEY=`cat $SSH_DIR/id_dsa.pub` for IP in $*; do echo "You will now be asked for password for $IP" ssh -oStrictHostKeyChecking=no $IP "mkdir -p ~/.ssh; chmod 700 ~/.ssh; \ echo \"$SSH1_RSA_KEY\" >> ~/.ssh/authorized_keys; \ echo \"$SSH2_DSA_KEY\" >> ~/.ssh/authorized_keys2; \ chmod 600 ~/.ssh/authorized_keys ~/.ssh/authorized_keys2" if test $? -eq 0; then echo "Keys were put successfully" else echo "Error putting keys to $IP" 1>&2 fi done for IP in $*; do for ver in 1 2; do echo -n "Checking $IP connectivity by ssh$ver... " ssh -q -oProtocol=${ver} -oBatchMode=yes \ -oStrictHostKeyChecking=no $IP /bin/true evaluate done done } git_hook_pre_commit() { local USERNAME USERMAIL USERNAME=$(git config --list | grep ^user.name) USERNAME=${USERNAME#user.name=} USERMAIL=$(git config --list | grep ^user.email) USERMAIL=${USERMAIL#user.email=} if [[ ! "$USERMAIL" =~ "@ipfire\.org$" ]]; then echo "User email is not from ipfire.org. Can't commit." return 1 fi COUNTER=0 for i in $USERNAME; do COUNTER=$[ $COUNTER + 1 ] done if [ ! "$COUNTER" -ge 2 ]; then echo "Setup user name as your real name. Can't commit." return 1 fi } # Run hooks if [ "$(basename $0)" = "pre-commit" ]; then git_hook_pre_commit exit $? fi