]> git.ipfire.org Git - ipfire-3.x.git/blob - tools/make-git
Added new package: dosfstools.
[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" ] || dialogerror "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 git log -n 500 --no-merges --pretty=medium --shortstat $(git_range) > $BASEDIR/doc/ChangeLog
128 evaluate
129 }
130
131 git_shortlog() {
132 git shortlog --no-merges $(git_range)
133 }
134
135 git_gc() {
136 git gc --prune
137 }
138
139 git_export() {
140 git archive HEAD | gzip -9 > ${SNAME}-${VERSION}.source.tar.gz
141 }
142
143 git_range() {
144 local EXT
145 [ -z $GIT_TAG ] || LAST_TAG=$GIT_TAG
146 [ -z $LAST_TAG ] || EXT="$LAST_TAG..HEAD"
147 echo $EXT
148 }
149
150 ssh_cert() {
151 test $# -gt 0 || exiterror "You need to pass the hostname of the remote host."
152
153 SSH_KEYGEN=`which ssh-keygen`
154 if test $? -ne 0; then
155 # Error message is printed by 'which'
156 return 1
157 fi
158
159 SSH_DIR=~/.ssh
160 if ! test -d $SSH_DIR; then
161 mkdir $SSH_DIR
162 fi
163 chmod 700 $SSH_DIR
164
165 if [ ! -f $SSH_DIR/identity ] || [ ! -f $SSH_DIR/identity.pub ]; then
166 echo "Generating ssh1 RSA keys - please wait..."
167 rm -f $SSH_DIR/identity $SSH_DIR/identity.pub
168 $SSH_KEYGEN -t rsa1 -f $SSH_DIR/identity -P ''
169 if [ $? -ne 0 ]; then
170 echo "Command \"$SSH_KEYGEN -t rsa1 -f $SSH_DIR/identity" \
171 "-P ''\" failed" 1>&2
172 return 1
173 fi
174 else
175 echo "ssh1 RSA key is present"
176 fi
177
178 if [ ! -f $SSH_DIR/id_dsa ] || [ ! -f $SSH_DIR/id_dsa.pub ]; then
179 echo "Generating ssh2 DSA keys - please wait..."
180 rm -f $SSH_DIR/id_dsa $SSH_DIR/id_dsa.pub
181 $SSH_KEYGEN -t dsa -f $SSH_DIR/id_dsa -P ''
182 if test $? -ne 0; then
183 echo "Command \"$SSH_KEYGEN -t dsa -f $SSH_DIR/id_dsa" \
184 "-P ''\" failed" 1>&2
185 return 1
186 fi
187 else
188 echo "ssh2 DSA key is present"
189 fi
190
191 SSH1_RSA_KEY=`cat $SSH_DIR/identity.pub`
192 SSH2_DSA_KEY=`cat $SSH_DIR/id_dsa.pub`
193
194 for IP in $*; do
195 echo "You will now be asked for password for $IP"
196 ssh -oStrictHostKeyChecking=no $IP "mkdir -p ~/.ssh; chmod 700 ~/.ssh; \
197 echo \"$SSH1_RSA_KEY\" >> ~/.ssh/authorized_keys; \
198 echo \"$SSH2_DSA_KEY\" >> ~/.ssh/authorized_keys2; \
199 chmod 600 ~/.ssh/authorized_keys ~/.ssh/authorized_keys2"
200 if test $? -eq 0; then
201 echo "Keys were put successfully"
202 else
203 echo "Error putting keys to $IP" 1>&2
204 fi
205 done
206
207 for IP in $*; do
208 for ver in 1 2; do
209 echo -n "Checking $IP connectivity by ssh$ver... "
210 ssh -q -oProtocol=${ver} -oBatchMode=yes \
211 -oStrictHostKeyChecking=no $IP /bin/true
212 evaluate
213 done
214 done
215 }
216
217 git_hook_pre_commit() {
218 local USERNAME USERMAIL
219 USERNAME=$(git config --list | grep ^user.name)
220 USERNAME=${USERNAME#user.name=}
221
222 USERMAIL=$(git config --list | grep ^user.email)
223 USERMAIL=${USERMAIL#user.email=}
224
225 if [[ ! "$USERMAIL" =~ "@ipfire\.org$" ]]; then
226 echo "User email is not from ipfire.org. Can't commit."
227 return 1
228 fi
229
230 COUNTER=0
231 for i in $USERNAME; do
232 COUNTER=$[ $COUNTER + 1 ]
233 done
234
235 if [ ! "$COUNTER" -ge 2 ]; then
236 echo "Setup user name as your real name. Can't commit."
237 return 1
238 fi
239 }
240
241 # Run hooks
242 if [ "$(basename $0)" = "pre-commit" ]; then
243 git_hook_pre_commit
244 exit $?
245 fi