Add support for submodules.
[git-scripts.git] / git-hook-support.sh
blob573e9fbd988841f68d449487a71afe356030ff54
2 # Tools
4 STYLECHECKER=style_checker
5 PYTHON=/usr/bin/python
7 TRACENV=/home/trac/rootenv
8 TRACURL=http://localhost:8000
10 # Path needed by some tools (style_checker)
12 PATH=/opt/gnat/bin:/opt/bin:/usr/bin:/bin:/usr/local/bin:$PATH
13 export PATH
15 # Style Check all files for a specific transaction
17 root=$(mktemp -d /tmp/git-update-hook-XXXXXX)
18 log=$root/log
19 tree=$root/diff-tree
20 frem=$root/files-removed
21 fadd=$root/files-added
22 fman=$root/manifest
24 # Set Style_Checker options
26 OWEB="-H -cP -cY -l256"
28 # The copyright pattern to check
29 CPYR=""
31 # Pathnames matching the regexp will be excluded from the test
32 EXCLUDE=""
34 SC_OPTS="-H -ign out -ign tmplt -ign sed -ign txt \
35 -lang Ada -H -cP -cY -sp -gnat05 -lang TXML $OWEB \
36 -lang XML $OWEB -lang HTML $OWEB -lang XSD $OWEB -lang CSS $OWEB"
38 # To be set with the name of the MANIFEST file if any, this script will check
39 # if an added or removed file is properly added or removed from MANIFEST.
40 MANIFEST=""
42 # Process each file
44 check_file() {
45 id=$2
46 file=`basename $4`
47 mode=$(echo $3 | cut -c1)
49 # Check if it is a file to ignore
51 if [[ -n "$EXCLUDE" && `echo "$6" | grep --regexp="$EXCLUDE"` ]]; then
52 return 0;
55 # skip deleted/renamed files
57 if [[ "$mode" = "D" || "$mode" = "R" ]]; then
58 echo $4 >> $frem
59 return 0;
62 git show $id > $root/$file
64 # If the MANIFEST keep it around
66 if [[ "$4" = "$MANIFEST" ]]; then
67 cp $root/$file $fman
70 # If a new file, record it
72 if [[ "$mode" == "A" ]]; then
73 echo $4 >> $fadd
76 (cd $root; $STYLECHECKER $SC_OPTS -n "$4" "$file" )
77 res=$?
78 rm -f $root/$file
79 return $res
82 # Process the changeset
84 check_style()
86 oldrev=$1
87 newrev=$2
89 git diff-tree -r "$oldrev" "$newrev" > $tree
91 exit_status=0
93 while read old_mode new_mode old_sha1 new_sha1 status name; do
94 # skip submodules
95 if [ "$new_mode" != 160000 ]; then
96 check_file $old_sha1 $new_sha1 $status $name 2>&1 >> $log
97 if [[ $? != 0 ]]; then
98 echo
99 cat $log >&2
100 echo -e "For details run: git diff ${old_sha1:0:7} ${new_sha1:0:7}" >&2
101 echo
102 exit_status=1
105 done < $tree
107 # Check for MANIFEST if needed
109 if [[ -n "$MANIFEST" && -f $fadd ]]; then
110 if [[ ! -f $fman ]]; then
111 echo ""
112 echo Files added, but $MANIFEST not updated, consider adding:
113 git diff-tree --name-only --diff-filter=A -r $oldrev $newrev
114 exit_status=1
115 else
116 while read fadded
118 grep --quiet "$fadded\$" $fman
119 if [[ $? = 1 ]]; then
120 echo File $fadded added but missing in $MANIFEST
121 exit_status=1
123 done < $fadd
127 if [[ -n "$MANIFEST" && -f $frem ]]; then
128 if [[ ! -f $fman ]]; then
129 echo ""
130 echo Files removed, but $MANIFEST not updated, condider removing:
131 git diff-tree --name-only --diff-filter=D -r $oldrev $newrev
132 exit_status=1
133 else
134 while read fremoved
136 grep --quiet "$fremoved\$" $fman
137 if [[ $? = 0 ]]; then
138 echo File $fremoved removed but still in $MANIFEST
139 exit_status=1
141 done < $frem
145 # --- Finished
146 return $exit_status
149 # Clean tmp directory
151 clean_temp() {
152 rm -fr $root
155 # Check log not empty
157 function log_not_empty() {
158 REV="$1"
160 RES=$(git log -1 --pretty="%s%n%b" $REV | grep "[a-zA-Z0-9]")
162 if [ "$RES" = "" ]; then
163 echo "Won't commit with an empty log message." 1>&2
164 return 1;
167 return 0;
170 # Post receive action to log message in Trac
172 function trac_post_receive_record_log() {
173 oldrev=$1
174 newrev=$2
175 ref=$3
176 MODULE="$4"
178 if expr "$oldrev" : "0*$" >/dev/null
179 then
180 git-rev-list "$newrev"
181 else
182 git-rev-list "$newrev" "^$oldrev"
183 fi | tac | while read csha ; do
184 AUTHOR="$(git-rev-list -n 1 $csha --pretty=format:%an | sed '1d')"
185 LOG="$(git-rev-list -n 1 $csha --pretty=medium | sed '1,3d;s:^ ::')"
187 $PYTHON /home/git/scripts/trac-post-commit-hook \
188 -p "${TRACENV}/$MODULE" \
189 -r "$csha" \
190 -u "$AUTHOR" \
191 -m "$LOG" \
192 -s "${TRACURL}/$MODULE" 2> $root/post_commit_err_$csha
193 done
196 # Pre update action to check for proper ticket
198 function is_merge_commit() {
199 git rev-parse --verify --quiet $1^2 > /dev/null
202 function trac_update_check_log() {
203 oldrev=$1
204 newrev=$2
205 MODULE="$3"
207 for csha in $(git-rev-list $oldrev..$newrev); do
208 is_merge_commit $csha
209 if [ $? = 0 ]; then
210 echo $csha is a merge commit, skip checks
211 else
212 LOG="$(git-rev-list -n 1 $csha --pretty=medium | sed '1,3d;s:^ ::')"
214 $PYTHON /home/git/scripts/trac-pre-commit-hook \
215 "$TRACENV/$MODULE" "$LOG" || return 1
217 done;
219 return 0;
222 # Post commit send mail
223 # Can be called with $4 = "--diff n" to skip log diff in e-mail
225 function send_mail_post_receive() {
226 oldrev=$1
227 newrev=$2
228 ref=$3
229 MODULE="$4"
231 if expr "$oldrev" : "0*$" >/dev/null
232 then
233 git-rev-list "$newrev"
234 else
235 git-rev-list --reverse "$newrev" "^$oldrev"
236 fi | while read csha;
238 git-show $csha | mail -s "[$MODULE] $ref commit $csha" $5 $6 $7 $8 $9
239 done
242 # Post commit send xmpp
244 function send_xmpp_post_commit() {
245 REPOS="$1"
246 REV="$2"
247 SUBJECT=$3
248 XMPPRC=$4
250 AUTHOR=$(git log -1 --pretty="%an")
251 LOG=$(git log -1 --pretty="%s%n%b")
253 echo $AUTHOR $LOG | sendxmpp -f $XMPPRC -s $SUBJECT $5 $6 $7