Update commit-msg hook to version from Gerrit v2.5-rc0

Change-Id: I0d11ac0c24cd53386e996b7dd9bd37c89c789f60
This commit is contained in:
David Pursehouse 2012-10-03 17:20:06 +09:00 committed by Gustaf Lundh
parent 01f443d75a
commit 7119f94aba

View File

@ -1,5 +1,5 @@
#!/bin/sh #!/bin/sh
# From Gerrit Code Review 2.1.2-rc2-33-g7e30c72 # From Gerrit Code Review 2.5-rc0
# #
# Part of Gerrit Code Review (http://code.google.com/p/gerrit/) # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
# #
@ -24,71 +24,144 @@ MSG="$1"
# Check for, and add if missing, a unique Change-Id # Check for, and add if missing, a unique Change-Id
# #
add_ChangeId() { add_ChangeId() {
clean_message=$(sed -e ' clean_message=`sed -e '
/^diff --git a\/.*/{ /^diff --git a\/.*/{
s/// s///
q q
} }
/^Signed-off-by:/d /^Signed-off-by:/d
/^#/d /^#/d
' "$MSG" | git stripspace) ' "$MSG" | git stripspace`
if test -z "$clean_message" if test -z "$clean_message"
then then
return return
fi fi
# Does Change-Id: already exist? if so, exit (no change).
if grep -i '^Change-Id:' "$MSG" >/dev/null if grep -i '^Change-Id:' "$MSG" >/dev/null
then then
return return
fi fi
id=$(_gen_ChangeId) id=`_gen_ChangeId`
perl -e ' T="$MSG.tmp.$$"
$MSG = shift; AWK=awk
$id = shift; if [ -x /usr/xpg4/bin/awk ]; then
$CHANGE_ID_AFTER = shift; # Solaris AWK is just too broken
AWK=/usr/xpg4/bin/awk
fi
undef $/; # How this works:
open(I, $MSG); $_ = <I>; close I; # - parse the commit message as (textLine+ blankLine*)*
s|^diff --git a/.*||ms; # - assume textLine+ to be a footer until proven otherwise
s|^#.*$||mg; # - exception: the first block is not footer (as it is the title)
exit unless $_; # - read textLine+ into a variable
# - then count blankLines
@message = split /\n/; # - once the next textLine appears, print textLine+ blankLine* as these
$haveFooter = 0; # aren't footer
$startFooter = @message; # - in END, the last textLine+ block is available for footer parsing
for($line = @message - 1; $line >= 0; $line--) { $AWK '
$_ = $message[$line]; BEGIN {
# while we start with the assumption that textLine+
($haveFooter++, next) if /^[a-zA-Z0-9-]+:/; # is a footer, the first block is not.
next if /^[ []/; isFooter = 0
$startFooter = $line if ($haveFooter && /^\r?$/); footerComment = 0
last; blankLines = 0
} }
@footer = @message[$startFooter+1..@message]; # Skip lines starting with "#" without any spaces before it.
@message = @message[0..$startFooter]; /^#/ { next }
push(@footer, "") unless @footer;
for ($line = 0; $line < @footer; $line++) { # Skip the line starting with the diff command and everything after it,
$_ = $footer[$line]; # up to the end of the file, assuming it is only patch data.
next if /^($CHANGE_ID_AFTER):/i; # If more than one line before the diff was empty, strip all but one.
last; /^diff --git a/ {
blankLines = 0
while (getline) { }
next
} }
splice(@footer, $line, 0, "Change-Id: I$id");
$_ = join("\n", @message, @footer); # Count blank lines outside footer comments
open(O, ">$MSG"); print O; close O; /^$/ && (footerComment == 0) {
' "$MSG" "$id" "$CHANGE_ID_AFTER" blankLines++
next
}
# Catch footer comment
/^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
footerComment = 1
}
/]$/ && (footerComment == 1) {
footerComment = 2
}
# We have a non-blank line after blank lines. Handle this.
(blankLines > 0) {
print lines
for (i = 0; i < blankLines; i++) {
print ""
}
lines = ""
blankLines = 0
isFooter = 1
footerComment = 0
}
# Detect that the current block is not the footer
(footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
isFooter = 0
}
{
# We need this information about the current last comment line
if (footerComment == 2) {
footerComment = 0
}
if (lines != "") {
lines = lines "\n";
}
lines = lines $0
}
# Footer handling:
# If the last block is considered a footer, splice in the Change-Id at the
# right place.
# Look for the right place to inject Change-Id by considering
# CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
# then Change-Id, then everything else (eg. Signed-off-by:).
#
# Otherwise just print the last block, a new line and the Change-Id as a
# block of its own.
END {
unprinted = 1
if (isFooter == 0) {
print lines "\n"
lines = ""
}
changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
numlines = split(lines, footer, "\n")
for (line = 1; line <= numlines; line++) {
if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
unprinted = 0
print "Change-Id: I'"$id"'"
}
print footer[line]
}
if (unprinted) {
print "Change-Id: I'"$id"'"
}
}' "$MSG" > $T && mv $T "$MSG" || rm -f $T
} }
_gen_ChangeIdInput() { _gen_ChangeIdInput() {
echo "tree $(git write-tree)" echo "tree `git write-tree`"
if parent=$(git rev-parse HEAD^0 2>/dev/null) if parent=`git rev-parse "HEAD^0" 2>/dev/null`
then then
echo "parent $parent" echo "parent $parent"
fi fi
echo "author $(git var GIT_AUTHOR_IDENT)" echo "author `git var GIT_AUTHOR_IDENT`"
echo "committer $(git var GIT_COMMITTER_IDENT)" echo "committer `git var GIT_COMMITTER_IDENT`"
echo echo
printf '%s' "$clean_message" printf '%s' "$clean_message"
} }