-export HAS_ERROR=false
-export HAS_SUMMARY=false
-export HAS_LAST_BLANK=false
-export HAS_BODY=false
-export HAS_SIGNOFF=false
-case $(grep -c "^$CHANGEID:" "$ORIGINAL") in
-0) export HAS_CHANGEID=false ;;
-1) export HAS_CHANGEID=true ;;
-*) error "with multiple $CHANGEID: lines not allowed."
- export HAS_CHANGEID=true
- HAS_ERROR=true ;;
-esac
-export HAS_COMMENTS=false
-export HAS_DIFF=false
-
-export LINE=""
-export NUM=1
-
-IFS="" # don't eat whitespace, to preserve message formatting
-while read LINE; do
- WIDTH=$(($(echo $LINE | wc -c) - 1)) # -1 for end-of-line character
-
- case "$LINE" in
- $SIGNOFF*)
- # Signed-off-by: First Last <email@host.domain>
- HAS_SOB=$(echo "$LINE" | grep "^$SIGNOFF: .* .* <.*@[^.]*\..*>")
- if [ -z "$HAS_SOB" ]; then
- error "missing valid commit summary line to show" \
- "agreement with code submission requirements."
- elif ! $HAS_SUMMARY; then
- error "missing summary before $SIGNOFF:."
- elif ! $HAS_LAST_BLANK; then
- error "missing blank line before $SIGNOFF:."
- elif ! $HAS_BODY; then
- error "missing useful comments before $SIGNOFF:."
- else
- HAS_SIGNOFF=true # allow multiple signoff lines
- fi
- echo $LINE
- ! $HAS_CHANGEID && new_changeid
- ;;
- $CHANGEID*)
- # Change-Id: I762ab50568f25527176ae54e92c446cf06112097
- HAS_ID=$(echo "$LINE" | grep "^$CHANGEID: I[0-9a-fA-F]\{40\}")
- if [ -z "$HAS_ID" ]; then
- error "has invalid $CHANGEID: line for Gerrit tracking"
- elif ! $HAS_SUMMARY; then
- error "missing summary before $CHANGEID:."
- elif ! $HAS_BODY; then
- error "missing useful comments before $CHANGEID:."
-
- # $CHANGEID used to come before $SIGNOFF in old commits.
- # Allow this to continue for some time, until they are gone.
- # elif ! $HAS_SIGNOFF; then
- # error "missing $SIGNOFF: before $CHANGEID:."
-
- # $HAS_CHANGEID was already checked and set above
- #elif $HAS_CHANGEID; then
- # error "does not allow multiple $CHANGEID: lines."
- #else
- # HAS_CHANGEID=true
- fi
- echo $LINE
- ;;
- "")
- [ $HAS_SUMMARY -a ! $HAS_BODY ] && HAS_LAST_BLANK=true
- [ $HAS_BODY ] && HAS_LAST_BLANK=true
- echo $LINE
- ;;
- diff*|index*)
- # Beginning of uncommented diffstat from "commit -v". If
- # there are diff and index lines, skip the rest of the input.
- # diff --git a/build/commit-msg b/build/commit-msg
- # index 80a3442..acb4c50 100755
- DIFF=$(echo "$LINE" | grep -- "^diff --git a/")
- [ "$DIFF" ] && HAS_DIFF=true && continue
- INDEX=$(echo "$LINE" | grep -- "^index [0-9a-fA-F]\{6,\}\.\.")
- [ $HAS_DIFF -a "$INDEX" ] && break || HAS_DIFF=false
- ;;
- \#*)
- HAS_COMMENTS=true
- continue
- ;;
- *) if [ $NUM -eq 1 ]; then
- FMT="^[A-Z]\{2,5\}-[0-9]\{1,5\} [-a-z0-9]\{2,9\}: "
- HAS_JIRA_COMPONENT=$(echo "$LINE" | grep "$FMT")
- if [ -z "$HAS_JIRA_COMPONENT" ]; then
- FMT="^[A-Z]\{2,5\}-[0-9]\{1,5\} "
- HAS_JIRA=$(echo "$LINE" | grep "$FMT")
- if [ "$HAS_JIRA" ]; then
- error "has no component in summary."
- else
- error "missing JIRA ticket number."
- fi
- elif [ $WIDTH -gt $WIDTH_SUM ]; then
- error "summary longer than $WIDTH_SUM columns."
- else
- HAS_SUMMARY=true
- fi
- elif $HAS_SIGNOFF; then
- error "trailing garbage after $SIGNOFF: line."
- elif [ $WIDTH -gt $WIDTH_REG ]; then
- error "has line longer than $WIDTH_REG columns."
- elif ! $HAS_BODY && ! $HAS_LAST_BLANK; then
- error "has no blank line after summary."
- else
- HAS_BODY=true
- fi
- HAS_LAST_BLANK=false
- HAS_DIFF=false
- echo $LINE
- ;;
- esac
-
- NUM=$((NUM + 1))
-done < "$ORIGINAL" > "$REVISED"
-
-[ $NUM -eq 1 ] && exit 1 # empty file
-
-LINE=""
-if ! $HAS_SUMMARY; then
- error "missing commit summary line."
-elif ! $HAS_BODY; then
- error "missing commit description."
-elif ! $HAS_SIGNOFF; then
- error "missing valid $SIGNOFF: line."
-elif ! $HAS_CHANGEID; then
- error "missing valid $CHANGEID: line."
-fi
+init ${1+"$@"}
+exec 3< "$ORIGINAL" 4> "$REVISED" || exit 1
+
+while IFS= read -u3 LINE; do
+ ((NUM += 1))
+ case "$LINE" in
+ $SIGNOFF* ) do_signoff ;;
+ $CHANGEID* ) do_changeid ;;
+
+ "")
+ $IS_WRAPPING_UP && \
+ error "blank lines not allowed in signoff section"
+ HAS_LAST_BLANK=true
+ ;;
+
+ \#*)
+ continue ## ignore and suppress comments
+ ;;
+
+ "diff --git a/"* )
+ # Beginning of uncommented diffstat from "commit -v". If
+ # there are diff and index lines, skip the rest of the input:
+ # diff --git a/build/commit-msg b/build/commit-msg
+ # index 80a3442..acb4c50 100755
+ # If a "diff --git" line is not followed by an index line,
+ # do the default line processing on both lines.
+ #
+ IFS= read -u3 INDEX || break
+ ((NUM += 1))
+ ln=$(echo "$INDEX" | grep "^index [0-9a-fA-F]\{6,\}\.\.")
+ (( ${#ln} > 1 )) && break
+ LINE=${LINE}$'\n'${INDEX}
+ do_default_line
+ ;;
+
+ *)
+ if [[ "$LINE" =~ ^($INNOCUOUS): ]] ; then
+ do_innocuous
+ else
+ do_default_line
+ fi
+ ;;
+ esac
+
+ echo "$LINE" >&4
+done
+
+(( NUM <= 0 )) && die "empty commit message"
+
+unset LINE
+$HAS_SIGNOFF || error "missing valid $SIGNOFF: line."
+