commit-msg: Don't create message with only Change-Id

If a user aborts a commit, the commit-msg hook is still called,
but with an empty file.  We need to leave the empty file alone.

Change-Id: I13766135dac267823cb08ab76f67d2000ba2d1ce
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/hooks/commit-msg b/hooks/commit-msg
index 5b1fe3a..fd76c07 100755
--- a/hooks/commit-msg
+++ b/hooks/commit-msg
@@ -1,5 +1,5 @@
 #!/bin/sh
-# From Gerrit Code Review v2.0.18-100-g98fc90a
+# From Gerrit Code Review v2.0.19.1-4-g21d307b
 #
 # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
 #
@@ -23,21 +23,34 @@
 # Check for, and add if missing, a unique Change-Id
 #
 add_ChangeId() {
-	if grep '^Change-Id: ' "$MSG" >/dev/null
+	clean_message=$(sed -e '
+		/^diff --git a\/.*/{
+			s///
+			q
+		}
+		/^Signed-off-by:/d
+		/^#/d
+	' "$MSG" | git stripspace)
+	if test -z "$clean_message"
+	then
+		return
+	fi
+
+	if grep -i '^Change-Id:' "$MSG" >/dev/null
 	then
 		return
 	fi
 
 	id=$(_gen_ChangeId)
-	out="$MSG.new"
-	ftt="$MSG.footers"
+	out="$MSG.OUT"
+	ftt="$MSG.FTT"
 	sed -e  '2,${
 			/^[A-Za-z][A-Za-z0-9-]*: /,$d
 		}' <"$MSG" >"$out"
 	sed -ne '2,${
 			/^[A-Za-z][A-Za-z0-9-]*: /,$p
 		}' <"$MSG" >"$ftt"
-	if ! [ -s "$ftt" ]
+	if ! test -s "$ftt"
 	then
 		echo >>"$out"
 	fi
@@ -55,7 +68,7 @@
 	echo "author $(git var GIT_AUTHOR_IDENT)"
 	echo "committer $(git var GIT_COMMITTER_IDENT)"
 	echo
-	cat "$MSG"
+	printf '%s' "$clean_message"
 }
 _gen_ChangeId() {
 	_gen_ChangeIdInput |