Insert Change-Id at start of trailers

In older versions of Gerrit the Change-Id field was inserted at the
start of the trailers. Commit 68296f71804feab2e0ae18ae33f834a8a41621e4
simplified the trailers code by using git trailers instead of custom
code but now inserts Change-Id at the end of the trailers section.

A consequence of this is that folks who sign-off their commits using
`git commit -s` now has the sign-off appear first followed by
Change-Id. If the user then runs `git commit -s --amend` to update
the change because the Sign-off-by line is not last, git inserts
a 2nd duplicate Signed-off-by line.

This patch simply restores the previous behaviour of the Gerrit
commit-msg hook where Change-Id would be inserted before the
Sign-off-by line to avoid this issue.

Bug: Issue 12546
Change-Id: Ice8ffd5c74c9a1023ca38d060a1ed844330b422a
Signed-off-by: Thanh Ha <zxiiro@gmail.com>
diff --git a/resources/com/google/gerrit/server/commit-msg_test.sh b/resources/com/google/gerrit/server/commit-msg_test.sh
index d797be3..c016a89 100755
--- a/resources/com/google/gerrit/server/commit-msg_test.sh
+++ b/resources/com/google/gerrit/server/commit-msg_test.sh
@@ -63,9 +63,9 @@
 @@ -38,6 +38,7 @@
  context
  line
- 
+
 +hello, world
- 
+
  context
  line
 EOF
@@ -111,7 +111,7 @@
 }
 
 # Change-Id goes after existing trailers.
-function test_at_end {
+function test_at_start {
   cat << EOF > input
 bla bla
 
@@ -119,16 +119,16 @@
 EOF
 
   ${hook} input || fail "failed hook execution"
-  result=$(tail -1 input | grep ^Change-Id)
+  result=$(git interpret-trailers --parse input | head -1 | grep ^Change-Id)
   if [[ -z "${result}" ]] ; then
     echo "after: "
     cat input
 
-    fail "did not find Change-Id at end"
+    fail "did not find Change-Id at start"
   fi
 }
 
-function test_dash_at_end {
+function test_dash_at_start {
   if [[ ! -x /bin/dash ]] ; then
     echo "/bin/dash not installed; skipping dash test."
     return
@@ -142,12 +142,12 @@
 
   /bin/dash ${hook} input || fail "failed hook execution"
 
-  result=$(tail -1 input | grep ^Change-Id)
+  result=$(git interpret-trailers --parse input | head -1 | grep ^Change-Id)
   if [[ -z "${result}" ]] ; then
     echo "after: "
     cat input
 
-    fail "did not find Change-Id at end"
+    fail "did not find Change-Id at start"
   fi
 }
 
diff --git a/resources/com/google/gerrit/server/tools/root/hooks/commit-msg b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
index 2901232..2b1a2fc 100755
--- a/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
+++ b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg
@@ -50,7 +50,7 @@
 
 # Avoid the --in-place option which only appeared in Git 2.8
 # Avoid the --if-exists option which only appeared in Git 2.15
-if ! git -c trailer.ifexists=doNothing interpret-trailers \
+if ! git -c trailer.ifexists=doNothing interpret-trailers --where start \
       --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
   echo "cannot insert change-id line in $1"
   exit 1