commit-msg 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/sh
  2. # From Gerrit Code Review v2.0.19.1-4-g21d307b
  3. #
  4. # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
  5. #
  6. # Copyright (C) 2009 The Android Open Source Project
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. MSG="$1"
  21. # Check for, and add if missing, a unique Change-Id
  22. #
  23. add_ChangeId() {
  24. clean_message=$(sed -e '
  25. /^diff --git a\/.*/{
  26. s///
  27. q
  28. }
  29. /^Signed-off-by:/d
  30. /^#/d
  31. ' "$MSG" | git stripspace)
  32. if test -z "$clean_message"
  33. then
  34. return
  35. fi
  36. if grep -i '^Change-Id:' "$MSG" >/dev/null
  37. then
  38. return
  39. fi
  40. id=$(_gen_ChangeId)
  41. out="$MSG.OUT"
  42. ftt="$MSG.FTT"
  43. sed -e '2,${
  44. /^[A-Za-z][A-Za-z0-9-]*: /,$d
  45. }' <"$MSG" >"$out"
  46. sed -ne '2,${
  47. /^[A-Za-z][A-Za-z0-9-]*: /,$p
  48. }' <"$MSG" >"$ftt"
  49. if ! test -s "$ftt"
  50. then
  51. echo >>"$out"
  52. fi
  53. echo "Change-Id: I$id" >>"$out"
  54. cat "$ftt" >>"$out"
  55. mv -f "$out" "$MSG"
  56. rm -f "$out" "$ftt"
  57. }
  58. _gen_ChangeIdInput() {
  59. echo "tree $(git write-tree)"
  60. if parent=$(git rev-parse HEAD^0 2>/dev/null)
  61. then
  62. echo "parent $parent"
  63. fi
  64. echo "author $(git var GIT_AUTHOR_IDENT)"
  65. echo "committer $(git var GIT_COMMITTER_IDENT)"
  66. echo
  67. printf '%s' "$clean_message"
  68. }
  69. _gen_ChangeId() {
  70. _gen_ChangeIdInput |
  71. git hash-object -t commit --stdin
  72. }
  73. add_ChangeId