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
# - once the next textLine appears, print textLine+ blankLine* as these
# aren't footer
# - in END, the last textLine+ block is available for footer parsing
$AWK '
BEGIN {
# while we start with the assumption that textLine+
# is a footer, the first block is not.
isFooter = 0
footerComment = 0
blankLines = 0
}
@message = split /\n/; # Skip lines starting with "#" without any spaces before it.
$haveFooter = 0; /^#/ { next }
$startFooter = @message;
for($line = @message - 1; $line >= 0; $line--) {
$_ = $message[$line];
($haveFooter++, next) if /^[a-zA-Z0-9-]+:/; # Skip the line starting with the diff command and everything after it,
next if /^[ []/; # up to the end of the file, assuming it is only patch data.
$startFooter = $line if ($haveFooter && /^\r?$/); # If more than one line before the diff was empty, strip all but one.
last; /^diff --git a/ {
blankLines = 0
while (getline) { }
next
}
# Count blank lines outside footer comments
/^$/ && (footerComment == 0) {
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 ""
} }
@footer = @message[$startFooter+1..@message]; lines = ""
@message = @message[0..$startFooter]; blankLines = 0
push(@footer, "") unless @footer; isFooter = 1
footerComment = 0
}
for ($line = 0; $line < @footer; $line++) { # Detect that the current block is not the footer
$_ = $footer[$line]; (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
next if /^($CHANGE_ID_AFTER):/i; isFooter = 0
last; }
{
# We need this information about the current last comment line
if (footerComment == 2) {
footerComment = 0
} }
splice(@footer, $line, 0, "Change-Id: I$id"); if (lines != "") {
lines = lines "\n";
}
lines = lines $0
}
$_ = join("\n", @message, @footer); # Footer handling:
open(O, ">$MSG"); print O; close O; # If the last block is considered a footer, splice in the Change-Id at the
' "$MSG" "$id" "$CHANGE_ID_AFTER" # 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"
} }