summaryrefslogtreecommitdiffstats
path: root/git_hooks
diff options
context:
space:
mode:
authorSylvestre Ledru <sylvestre.ledru@scilab-enterprises.com>2012-12-06 12:18:15 +0100
committerSylvestre Ledru <sylvestre.ledru@scilab-enterprises.com>2012-12-06 12:18:32 +0100
commit683149ec820c69eb7e901a68829f0a715eb5b361 (patch)
treec426198be514a7017afebda566b4dcbdc4cb9348 /git_hooks
parentea90846445802b986457f4f298e19a6cddb790e6 (diff)
downloadscilab-683149ec820c69eb7e901a68829f0a715eb5b361.zip
scilab-683149ec820c69eb7e901a68829f0a715eb5b361.tar.gz
Update the commit-msg hooks to the one from our gerrit instance
To reproduce: wget http://codereview.scilab.org//tools/hooks/commit-msg Change-Id: I9cd4381cd0e26e04f124962cfeb46c0400344d1b
Diffstat (limited to 'git_hooks')
-rwxr-xr-xgit_hooks/commit-msg156
1 files changed, 113 insertions, 43 deletions
diff --git a/git_hooks/commit-msg b/git_hooks/commit-msg
index 0c50f69..e2c3df2 100755
--- a/git_hooks/commit-msg
+++ b/git_hooks/commit-msg
@@ -1,5 +1,5 @@
1#!/bin/sh 1#!/bin/sh
2# From Gerrit Code Review 2.1.5 2# From Gerrit Code Review 2.4.2
3# 3#
4# Part of Gerrit Code Review (http://code.google.com/p/gerrit/) 4# Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
5# 5#
@@ -24,74 +24,144 @@ MSG="$1"
24# Check for, and add if missing, a unique Change-Id 24# Check for, and add if missing, a unique Change-Id
25# 25#
26add_ChangeId() { 26add_ChangeId() {
27 clean_message=$(sed -e ' 27 clean_message=`sed -e '
28 /^diff --git a\/.*/{ 28 /^diff --git a\/.*/{
29 s/// 29 s///
30 q 30 q
31 } 31 }
32 /^Signed-off-by:/d 32 /^Signed-off-by:/d
33 /^#/d 33 /^#/d
34 ' "$MSG" | git stripspace) 34 ' "$MSG" | git stripspace`
35 if test -z "$clean_message" 35 if test -z "$clean_message"
36 then 36 then
37 return 37 return
38 fi 38 fi
39 39
40 # Does Change-Id: already exist? if so, exit (no change).
40 if grep -i '^Change-Id:' "$MSG" >/dev/null 41 if grep -i '^Change-Id:' "$MSG" >/dev/null
41 then 42 then
42 return 43 return
43 fi 44 fi
44 45
45 id=$(_gen_ChangeId) 46 id=`_gen_ChangeId`
46 perl -e ' 47 T="$MSG.tmp.$$"
47 $MSG = shift; 48 AWK=awk
48 $id = shift; 49 if [ -x /usr/xpg4/bin/awk ]; then
49 $CHANGE_ID_AFTER = shift; 50 # Solaris AWK is just too broken
50 51 AWK=/usr/xpg4/bin/awk
51 undef $/; 52 fi
52 open(I, $MSG); $_ = <I>; close I; 53
53 s|^diff --git a/.*||ms; 54 # How this works:
54 s|^#.*$||mg; 55 # - parse the commit message as (textLine+ blankLine*)*
55 exit unless $_; 56 # - assume textLine+ to be a footer until proven otherwise
56 57 # - exception: the first block is not footer (as it is the title)
57 @message = split /\n/; 58 # - read textLine+ into a variable
58 $haveFooter = 0; 59 # - then count blankLines
59 $startFooter = @message; 60 # - once the next textLine appears, print textLine+ blankLine* as these
60 for($line = @message - 1; $line >= 0; $line--) { 61 # aren't footer
61 $_ = $message[$line]; 62 # - in END, the last textLine+ block is available for footer parsing
62 63 $AWK '
63 if (/^[a-zA-Z0-9-]+:/ && !m,^[a-z0-9-]+://,) { 64 BEGIN {
64 $haveFooter++; 65 # while we start with the assumption that textLine+
65 next; 66 # is a footer, the first block is not.
66 } 67 isFooter = 0
67 next if /^[ []/; 68 footerComment = 0
68 $startFooter = $line if ($haveFooter && /^\r?$/); 69 blankLines = 0
69 last; 70 }
71
72 # Skip lines starting with "#" without any spaces before it.
73 /^#/ { next }
74
75 # Skip the line starting with the diff command and everything after it,
76 # up to the end of the file, assuming it is only patch data.
77 # If more than one line before the diff was empty, strip all but one.
78 /^diff --git a/ {
79 blankLines = 0
80 while (getline) { }
81 next
82 }
83
84 # Count blank lines outside footer comments
85 /^$/ && (footerComment == 0) {
86 blankLines++
87 next
88 }
89
90 # Catch footer comment
91 /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
92 footerComment = 1
93 }
94
95 /]$/ && (footerComment == 1) {
96 footerComment = 2
97 }
98
99 # We have a non-blank line after blank lines. Handle this.
100 (blankLines > 0) {
101 print lines
102 for (i = 0; i < blankLines; i++) {
103 print ""
70 } 104 }
71 105
72 @footer = @message[$startFooter+1..@message]; 106 lines = ""
73 @message = @message[0..$startFooter]; 107 blankLines = 0
74 push(@footer, "") unless @footer; 108 isFooter = 1
109 footerComment = 0
110 }
111
112 # Detect that the current block is not the footer
113 (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
114 isFooter = 0
115 }
75 116
76 for ($line = 0; $line < @footer; $line++) { 117 {
77 $_ = $footer[$line]; 118 # We need this information about the current last comment line
78 next if /^($CHANGE_ID_AFTER):/i; 119 if (footerComment == 2) {
79 last; 120 footerComment = 0
80 } 121 }
81 splice(@footer, $line, 0, "Change-Id: I$id"); 122 if (lines != "") {
123 lines = lines "\n";
124 }
125 lines = lines $0
126 }
82 127
83 $_ = join("\n", @message, @footer); 128 # Footer handling:
84 open(O, ">$MSG"); print O; close O; 129 # If the last block is considered a footer, splice in the Change-Id at the
85 ' "$MSG" "$id" "$CHANGE_ID_AFTER" 130 # right place.
131 # Look for the right place to inject Change-Id by considering
132 # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
133 # then Change-Id, then everything else (eg. Signed-off-by:).
134 #
135 # Otherwise just print the last block, a new line and the Change-Id as a
136 # block of its own.
137 END {
138 unprinted = 1
139 if (isFooter == 0) {
140 print lines "\n"
141 lines = ""
142 }
143 changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
144 numlines = split(lines, footer, "\n")
145 for (line = 1; line <= numlines; line++) {
146 if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
147 unprinted = 0
148 print "Change-Id: I'"$id"'"
149 }
150 print footer[line]
151 }
152 if (unprinted) {
153 print "Change-Id: I'"$id"'"
154 }
155 }' "$MSG" > $T && mv $T "$MSG" || rm -f $T
86} 156}
87_gen_ChangeIdInput() { 157_gen_ChangeIdInput() {
88 echo "tree $(git write-tree)" 158 echo "tree `git write-tree`"
89 if parent=$(git rev-parse HEAD^0 2>/dev/null) 159 if parent=`git rev-parse "HEAD^0" 2>/dev/null`
90 then 160 then
91 echo "parent $parent" 161 echo "parent $parent"
92 fi 162 fi
93 echo "author $(git var GIT_AUTHOR_IDENT)" 163 echo "author `git var GIT_AUTHOR_IDENT`"
94 echo "committer $(git var GIT_COMMITTER_IDENT)" 164 echo "committer `git var GIT_COMMITTER_IDENT`"
95 echo 165 echo
96 printf '%s' "$clean_message" 166 printf '%s' "$clean_message"
97} 167}