From ad6331d25f845c661013d16e2290d3916aac1a1c Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Sat, 2 Jul 2011 00:35:02 +0200 Subject: Un-perl commit-msg hook To simplify installation on mingw a bit (even though git remains a pain), drop the perl dependency the commit-msg hook introduced to the coreboot development environment. It's replaced by awk which we use elsewhere already (and is a more lightweight utility in any case) Change-Id: I67adfe1ec43c898735d4bae4819ceb53e83c303b Signed-off-by: Patrick Georgi Reviewed-on: http://review.coreboot.org/78 Tested-by: build bot (Jenkins) Reviewed-by: Peter Stuge --- util/gitconfig/commit-msg | 121 +++++++++++++++++++++++++++++++++------------- 1 file changed, 87 insertions(+), 34 deletions(-) diff --git a/util/gitconfig/commit-msg b/util/gitconfig/commit-msg index 789bad382b..548f373522 100755 --- a/util/gitconfig/commit-msg +++ b/util/gitconfig/commit-msg @@ -1,5 +1,4 @@ #!/bin/sh -# From Gerrit Code Review 2.1.7-rc2 # # Part of Gerrit Code Review (http://code.google.com/p/gerrit/) # @@ -37,56 +36,110 @@ add_ChangeId() { return fi + # Does Change-Id: already exist? if so, exit (no change). if grep -i '^Change-Id:' "$MSG" >/dev/null then return fi id=`_gen_ChangeId` - perl -e ' - $MSG = shift; - $id = shift; - $CHANGE_ID_AFTER = shift; + T="$MSG.tmp.$$" + AWK=awk + if [ -x /usr/xpg4/bin/awk ]; then + # Solaris AWK is just too broken + AWK=/usr/xpg4/bin/awk + fi + $AWK ' + # Skip lines starting with "#" without any spaces before it. + /^#/ { next } - undef $/; - open(I, $MSG); $_ = ; close I; - s|^diff --git a/.*||ms; - s|^#.*$||mg; - exit unless $_; + # Skip the line starting with the diff command and everything after it, + # up to the end of the file, assuming it is only patch data. + # If more than one line before the diff was empty, strip all but one. + /^diff --git a/ { + if (blankLines > 1) { + blankLines = 1 + } + while (getline) { } + next + } - @message = split /\n/; - $haveFooter = 0; - $startFooter = @message; - for($line = @message - 1; $line >= 0; $line--) { - $_ = $message[$line]; + # Handle comments and continuations in tags ([foo: bar] etc) + (caught == 1) && /^[ []/ { + if (lines != "") { + lines = lines "\n" + } + lines = lines $0 + next + } - if (/^[a-zA-Z0-9-]+:/ && !m,^[a-z0-9-]+://,) { - $haveFooter++; - next; + # Handle normal lines (ie. not starting with some tag like "Signed-off-by:"). + # If normal text appears after tags were "caught", handle them as normal text, too. + # Also count blank lines in blankLines. + !/^[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\// { + if ($0 == "") { + blankLines++ + next + } else { + for (i = 0; i < blankLines; i++) { + print "" } - next if /^[ []/; - $startFooter = $line if ($haveFooter && /^\r?$/); - last; + blankLines = 0 } + if (caught == 1) { + caught = 0 + print lines + lines = "" + } + print $0 + next + } - @footer = @message[$startFooter+1..@message]; - @message = @message[0..$startFooter]; - push(@footer, "") unless @footer; - - for ($line = 0; $line < @footer; $line++) { - $_ = $footer[$line]; - next if /^($CHANGE_ID_AFTER):/i; - last; + # Handle tags. They are "caught" and collected in the "lines" variable + { + caught = 1 + if (lines != "") { + lines = lines "\n"; } - splice(@footer, $line, 0, "Change-Id: I$id"); + lines = lines $0 + } - $_ = join("\n", @message, @footer); - open(O, ">$MSG"); print O; close O; - ' "$MSG" "$id" "$CHANGE_ID_AFTER" + # Tag handling: + # If last line before tags was not blank, there were no tags. + # In that case, print everything, plus a blank line, followed by Change-Id. + # Otherwise there were tags. Look for the right place to inject Change-Id, + # by considering CHANGE_ID_AFTER. Tags listed in it (case insensitive) come first, + # then Change-Id, then everything else (eg. Signed-off-by:). + END { + unprinted = 1 + if (blankLines == 0) { + if (lines == "") { + print "" + } else { + print lines "\n" + } + } else { + for (i = 0; i < blankLines; i++) { + print "" + } + 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() { 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 echo "parent $parent" fi -- cgit v1.2.3