summaryrefslogtreecommitdiffstats
path: root/git_hooks
diff options
context:
space:
mode:
authorSylvestre Ledru <sylvestre.ledru@scilab.org>2011-06-12 01:43:18 +0200
committerSylvestre Ledru <sylvestre.ledru@scilab.org>2011-06-20 11:03:18 +0200
commit0acc3b3a5f496096af5041dd5d45f1b5de14aaed (patch)
tree819be0fa373d252109934f36b718f1286f293ec4 /git_hooks
parent7440f8b4a37cc38d9beaa19c977d6348e3b103a7 (diff)
downloadscilab-0acc3b3a5f496096af5041dd5d45f1b5de14aaed.zip
scilab-0acc3b3a5f496096af5041dd5d45f1b5de14aaed.tar.gz
Add some Git hooks to ease dev.
Enabled by: mv .git/hooks /tmp/ cd .git/ ln -s ../git_hooks/ hooks Change-Id: I35afa478cb9baef5114e16251811e714f255e00e
Diffstat (limited to 'git_hooks')
-rwxr-xr-xgit_hooks/commit-msg104
-rwxr-xr-xgit_hooks/pre-commit96
-rwxr-xr-xgit_hooks/prepare-commit-msg21
3 files changed, 221 insertions, 0 deletions
diff --git a/git_hooks/commit-msg b/git_hooks/commit-msg
new file mode 100755
index 0000000..0c50f69
--- /dev/null
+++ b/git_hooks/commit-msg
@@ -0,0 +1,104 @@
1#!/bin/sh
2# From Gerrit Code Review 2.1.5
3#
4# Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
5#
6# Copyright (C) 2009 The Android Open Source Project
7#
8# Licensed under the Apache License, Version 2.0 (the "License");
9# you may not use this file except in compliance with the License.
10# You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS,
16# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17# See the License for the specific language governing permissions and
18# limitations under the License.
19#
20
21CHANGE_ID_AFTER="Bug|Issue"
22MSG="$1"
23
24# Check for, and add if missing, a unique Change-Id
25#
26add_ChangeId() {
27 clean_message=$(sed -e '
28 /^diff --git a\/.*/{
29 s///
30 q
31 }
32 /^Signed-off-by:/d
33 /^#/d
34 ' "$MSG" | git stripspace)
35 if test -z "$clean_message"
36 then
37 return
38 fi
39
40 if grep -i '^Change-Id:' "$MSG" >/dev/null
41 then
42 return
43 fi
44
45 id=$(_gen_ChangeId)
46 perl -e '
47 $MSG = shift;
48 $id = shift;
49 $CHANGE_ID_AFTER = shift;
50
51 undef $/;
52 open(I, $MSG); $_ = <I>; close I;
53 s|^diff --git a/.*||ms;
54 s|^#.*$||mg;
55 exit unless $_;
56
57 @message = split /\n/;
58 $haveFooter = 0;
59 $startFooter = @message;
60 for($line = @message - 1; $line >= 0; $line--) {
61 $_ = $message[$line];
62
63 if (/^[a-zA-Z0-9-]+:/ && !m,^[a-z0-9-]+://,) {
64 $haveFooter++;
65 next;
66 }
67 next if /^[ []/;
68 $startFooter = $line if ($haveFooter && /^\r?$/);
69 last;
70 }
71
72 @footer = @message[$startFooter+1..@message];
73 @message = @message[0..$startFooter];
74 push(@footer, "") unless @footer;
75
76 for ($line = 0; $line < @footer; $line++) {
77 $_ = $footer[$line];
78 next if /^($CHANGE_ID_AFTER):/i;
79 last;
80 }
81 splice(@footer, $line, 0, "Change-Id: I$id");
82
83 $_ = join("\n", @message, @footer);
84 open(O, ">$MSG"); print O; close O;
85 ' "$MSG" "$id" "$CHANGE_ID_AFTER"
86}
87_gen_ChangeIdInput() {
88 echo "tree $(git write-tree)"
89 if parent=$(git rev-parse HEAD^0 2>/dev/null)
90 then
91 echo "parent $parent"
92 fi
93 echo "author $(git var GIT_AUTHOR_IDENT)"
94 echo "committer $(git var GIT_COMMITTER_IDENT)"
95 echo
96 printf '%s' "$clean_message"
97}
98_gen_ChangeId() {
99 _gen_ChangeIdInput |
100 git hash-object -t commit --stdin
101}
102
103
104add_ChangeId
diff --git a/git_hooks/pre-commit b/git_hooks/pre-commit
new file mode 100755
index 0000000..f7f0c99
--- /dev/null
+++ b/git_hooks/pre-commit
@@ -0,0 +1,96 @@
1#!/bin/sh
2#
3# Hook used to indent all xcos files before commiting
4#
5
6XMLLINT="$(git config hooks.xmllint)"
7if test ! -x "$XMLLINT"
8then
9 echo "Unable to find xmllint executable on the configuration."
10 echo
11 echo "Please configure it with :"
12 echo " git config hooks.xmllint C:/path/to/xmllint"
13 echo
14fi
15
16INDENT="$(git config hooks.indent)"
17if test ! -x "$INDENT"
18then
19 echo "Unable to find indent executable on the configuration."
20 echo
21 echo "Please configure it with :"
22 echo " git config hooks.indent C:/path/to/indent"
23 echo
24fi
25
26# indent / format file by type
27indent() {
28 # getting against as the current commit
29 if git rev-parse --verify HEAD >/dev/null 2>&1
30 then
31 local against=HEAD
32 else
33 # Initial commit: diff against an empty tree object
34 local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
35 fi
36
37 # loop on modified files
38 git diff --cached --name-only $against |while read file;
39 do
40 local ext=$(expr "$file" : ".*\(\..*\)")
41 case $ext in
42 .xcos)
43 __indent_Xml;
44 ;;
45 .xml)
46 __indent_Xml;
47 ;;
48 .h)
49 __indent_C;
50 ;;
51 .c)
52 __indent_C;
53 ;;
54 .hxx)
55 __indent_C;
56 ;;
57 .cpp)
58 __indent_C;
59 ;;
60 esac
61 done
62}
63# Indent the file with xmllint if this is an xcos file
64__indent_Xml() {
65
66 if test ! -x "$XMLLINT"
67 then
68 return;
69 fi
70 if test ! -f $file
71 then
72 return;
73 fi
74
75 echo "Formatting " $file
76 "$XMLLINT" --format -o "$file" "$file"
77 git add "$file"
78}
79# Indent the file with `indent' if this is a C/CPP file
80__indent_C() {
81
82 if test ! -x "$INDENT"
83 then
84 return;
85 fi
86 if test ! -f $file
87 then
88 return;
89 fi
90
91 echo "Indenting " $file
92 "$INDENT" -npro --braces-after-if-line -i4 -ts4 -sob -ss -ncs -sc --no-space-after-parentheses -cp1 --no-tabs -bap -bad -npcs --dont-break-function-decl-args --dont-break-procedure-type -bli0 -l150 -il0 "$file"
93 git add "$file"
94}
95
96indent
diff --git a/git_hooks/prepare-commit-msg b/git_hooks/prepare-commit-msg
new file mode 100755
index 0000000..0bfbee9
--- /dev/null
+++ b/git_hooks/prepare-commit-msg
@@ -0,0 +1,21 @@
1#!/bin/sh
2#
3# Hook used to produce a commit message based on CHANGES
4#
5
6FILE="$1"
7SOURCE="$2"
8COMMIT="$3"
9
10case $SOURCE in
11'')
12 TMP=$(mktemp) && \
13 cat $FILE >$TMP && \
14 git diff --cached scilab/CHANGES_* |sed -n '/^+/p' |sed -n '/^+++/!p' |sed 's/^.\{1\}//g' >$FILE && \
15 cat $TMP >>$FILE && \
16 rm $TMP
17 ;;
18*)
19 ;;
20esac
21