diff options
Diffstat (limited to 'hooks')
-rwxr-xr-x | hooks/commit-msg | 153 |
1 files changed, 113 insertions, 40 deletions
diff --git a/hooks/commit-msg b/hooks/commit-msg index 712921c9..172a1781 100755 --- a/hooks/commit-msg +++ b/hooks/commit-msg | |||
@@ -1,5 +1,5 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | # From Gerrit Code Review 2.1.2-rc2-33-g7e30c72 | 2 | # From Gerrit Code Review 2.5-rc0 |
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,71 +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 | # |
26 | add_ChangeId() { | 26 | add_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 | ($haveFooter++, next) if /^[a-zA-Z0-9-]+:/; | 64 | BEGIN { |
64 | next if /^[ []/; | 65 | # while we start with the assumption that textLine+ |
65 | $startFooter = $line if ($haveFooter && /^\r?$/); | 66 | # is a footer, the first block is not. |
66 | last; | 67 | isFooter = 0 |
68 | footerComment = 0 | ||
69 | blankLines = 0 | ||
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 "" | ||
67 | } | 104 | } |
68 | 105 | ||
69 | @footer = @message[$startFooter+1..@message]; | 106 | lines = "" |
70 | @message = @message[0..$startFooter]; | 107 | blankLines = 0 |
71 | 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 | } | ||
72 | 116 | ||
73 | for ($line = 0; $line < @footer; $line++) { | 117 | { |
74 | $_ = $footer[$line]; | 118 | # We need this information about the current last comment line |
75 | next if /^($CHANGE_ID_AFTER):/i; | 119 | if (footerComment == 2) { |
76 | last; | 120 | footerComment = 0 |
121 | } | ||
122 | if (lines != "") { | ||
123 | lines = lines "\n"; | ||
77 | } | 124 | } |
78 | splice(@footer, $line, 0, "Change-Id: I$id"); | 125 | lines = lines $0 |
126 | } | ||
79 | 127 | ||
80 | $_ = join("\n", @message, @footer); | 128 | # Footer handling: |
81 | open(O, ">$MSG"); print O; close O; | 129 | # If the last block is considered a footer, splice in the Change-Id at the |
82 | ' "$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 | ||
83 | } | 156 | } |
84 | _gen_ChangeIdInput() { | 157 | _gen_ChangeIdInput() { |
85 | echo "tree $(git write-tree)" | 158 | echo "tree `git write-tree`" |
86 | if parent=$(git rev-parse HEAD^0 2>/dev/null) | 159 | if parent=`git rev-parse "HEAD^0" 2>/dev/null` |
87 | then | 160 | then |
88 | echo "parent $parent" | 161 | echo "parent $parent" |
89 | fi | 162 | fi |
90 | echo "author $(git var GIT_AUTHOR_IDENT)" | 163 | echo "author `git var GIT_AUTHOR_IDENT`" |
91 | echo "committer $(git var GIT_COMMITTER_IDENT)" | 164 | echo "committer `git var GIT_COMMITTER_IDENT`" |
92 | echo | 165 | echo |
93 | printf '%s' "$clean_message" | 166 | printf '%s' "$clean_message" |
94 | } | 167 | } |