]> git.ipfire.org Git - people/stevee/ipfire-3.x.git/blob - tools/make-git
Fixes on last commit.
[people/stevee/ipfire-3.x.git] / tools / make-git
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2008 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 # A small helper to make the git functions comfortable #
21 # #
22 ###############################################################################
23 ###############################################################################
24 #
25 # Set the environment
26 #
27 ###############################################################################
28
29 if [ -z "$EDITOR" ]; then
30 for i in nano emacs vi; do
31 EDITOR=$(which $i 2>/dev/null)
32 if ! [ -z "$EDITOR" ]; then
33 export EDITOR=$EDITOR
34 break
35 fi
36 done
37 [ -z "$EDITOR" ] && exiterror "You should have installed an editor."
38 fi
39
40 CURRENT_BRANCH=$(git branch | grep ^* | cut -c 3-)
41
42 # Apply git hooks
43 if [ -d "$BASEDIR/.git" ]; then
44 ln -sf $BASEDIR/tools/make-git $BASEDIR/.git/hooks/pre-commit
45 fi
46
47 ###############################################################################
48 #
49 # A small helper to make the git functions comfortable
50 #
51 ###############################################################################
52
53 git_pull() {
54 if [ "$1" = "--batch" ]; then
55 shift
56 git pull $* | grep -q "Already up-to-date." || rm -f $FAILED
57 [ -e $FAILED ] || distccd_restart
58 [ "$(expr $RANDOM % 25)" -eq "0" ] && git_gc
59 else
60 if [ "$CURRENT_BRANCH" == "master" ]; then
61 if ! (git status | grep -q "working directory clean"); then
62 echo "Your \"master\" branch is not clean. This may cause a merge commit."
63 echo -n "Do you want this? [y/N]"
64 read
65 [ -z $REPLY ] && return 0
66 for i in y Y j J; do
67 if [ "$i" == "$REPLY" ]; then
68 git pull
69 return $?
70 fi
71 done
72 return 0
73 else
74 git pull
75 fi
76 else
77 echo -n "You are not on branch \"master\". Do you want to rebase from \"master\"? [y/N]"
78 read
79 [ -z $REPLY ] && return 0
80 for i in y Y j J; do
81 if [ "$i" == "$REPLY" ]; then
82 git rebase -v refs/heads/master
83 return $?
84 fi
85 done
86 exiterror "\"$REPLY\" is not a valid answer."
87 fi
88 fi
89 }
90
91 git_push() {
92 check_user
93
94 [ "$CURRENT_BRANCH" == "master" ] || \
95 exiterror "You have to be in branch \"master\", if you want to push."
96 git push ssh://${IPFIRE_USER}@git.ipfire.org/pub/git/ipfire-3.x master
97 }
98
99 git_commit() {
100 check_sanity
101
102 git commit $*
103
104 [ "$?" -eq "0" ] || exiterror "git commit $* failed."
105 }
106
107 git_diff() {
108 DIFF_NAME="${BASEDIR}/ipfire-diff-$(date '+%Y%m%d-%0k%0M').diff"
109
110 check_sanity --fix
111
112 echo -ne "Make a local diff to last revision"
113 git diff HEAD > $DIFF_NAME
114 evaluate
115 if [ -s $DIFF_NAME ]; then # if diff file is empty
116 echo "Diff was successfully saved to $DIFF_NAME"
117 git diff HEAD --stat
118 else
119 rm -f $DIFF_NAME
120 return 1
121 fi
122 }
123
124 git_log() {
125 echo -n "Generating changelog from repository"
126
127 [ -z $GIT_TAG ] || LAST_TAG=$GIT_TAG
128 [ -z $LAST_TAG ] || EXT="$LAST_TAG..HEAD"
129
130 git log -n 500 --no-merges --pretty=medium --shortstat $EXT > $BASEDIR/doc/ChangeLog
131 evaluate
132 }
133
134 git_gc() {
135 git gc --prune
136 }
137
138 git_export() {
139 git archive HEAD | gzip -9 > ${SNAME}-${VERSION}.source.tar.gz
140 }
141
142 ssh_cert() {
143 test $# -gt 0 || exiterror "You need to pass the hostname of the remote host."
144
145 SSH_KEYGEN=`which ssh-keygen`
146 if test $? -ne 0; then
147 # Error message is printed by 'which'
148 return 1
149 fi
150
151 SSH_DIR=~/.ssh
152 if ! test -d $SSH_DIR; then
153 mkdir $SSH_DIR
154 fi
155 chmod 700 $SSH_DIR
156
157 if [ ! -f $SSH_DIR/identity ] || [ ! -f $SSH_DIR/identity.pub ]; then
158 echo "Generating ssh1 RSA keys - please wait..."
159 rm -f $SSH_DIR/identity $SSH_DIR/identity.pub
160 $SSH_KEYGEN -t rsa1 -f $SSH_DIR/identity -P ''
161 if [ $? -ne 0 ]; then
162 echo "Command \"$SSH_KEYGEN -t rsa1 -f $SSH_DIR/identity" \
163 "-P ''\" failed" 1>&2
164 return 1
165 fi
166 else
167 echo "ssh1 RSA key is present"
168 fi
169
170 if [ ! -f $SSH_DIR/id_dsa ] || [ ! -f $SSH_DIR/id_dsa.pub ]; then
171 echo "Generating ssh2 DSA keys - please wait..."
172 rm -f $SSH_DIR/id_dsa $SSH_DIR/id_dsa.pub
173 $SSH_KEYGEN -t dsa -f $SSH_DIR/id_dsa -P ''
174 if test $? -ne 0; then
175 echo "Command \"$SSH_KEYGEN -t dsa -f $SSH_DIR/id_dsa" \
176 "-P ''\" failed" 1>&2
177 return 1
178 fi
179 else
180 echo "ssh2 DSA key is present"
181 fi
182
183 SSH1_RSA_KEY=`cat $SSH_DIR/identity.pub`
184 SSH2_DSA_KEY=`cat $SSH_DIR/id_dsa.pub`
185
186 for IP in $*; do
187 echo "You will now be asked for password for $IP"
188 ssh -oStrictHostKeyChecking=no $IP "mkdir -p ~/.ssh; chmod 700 ~/.ssh; \
189 echo \"$SSH1_RSA_KEY\" >> ~/.ssh/authorized_keys; \
190 echo \"$SSH2_DSA_KEY\" >> ~/.ssh/authorized_keys2; \
191 chmod 600 ~/.ssh/authorized_keys ~/.ssh/authorized_keys2"
192 if test $? -eq 0; then
193 echo "Keys were put successfully"
194 else
195 echo "Error putting keys to $IP" 1>&2
196 fi
197 done
198
199 for IP in $*; do
200 for ver in 1 2; do
201 echo -n "Checking $IP connectivity by ssh$ver... "
202 ssh -q -oProtocol=${ver} -oBatchMode=yes \
203 -oStrictHostKeyChecking=no $IP /bin/true
204 evaluate
205 done
206 done
207 }
208
209 git_hook_pre_commit() {
210 local USERNAME USERMAIL
211 USERNAME=$(git config --list | grep ^user.name)
212 USERNAME=${USERNAME#user.name=}
213
214 USERMAIL=$(git config --list | grep ^user.email)
215 USERMAIL=${USERMAIL#user.email=}
216
217 if [[ ! "$USERMAIL" =~ "@ipfire\.org$" ]]; then
218 echo "User email is not from ipfire.org. Can't commit."
219 return 1
220 fi
221
222 COUNTER=0
223 for i in $USERNAME; do
224 COUNTER=$[ $COUNTER + 1 ]
225 done
226
227 if [ ! "$COUNTER" -ge 2 ]; then
228 echo "Setup user name as your real name. Can't commit."
229 return 1
230 fi
231 }
232
233 # Run hooks
234 if [ "$(basename $0)" = "pre-commit" ]; then
235 git_hook_pre_commit
236 exit $?
237 fi