commit-msg 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/sh
  2. # From Gerrit Code Review v2.0.18-100-g98fc90a
  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. if grep '^Change-Id: ' "$MSG" >/dev/null
  25. then
  26. return
  27. fi
  28. id=$(_gen_ChangeId)
  29. out="$MSG.new"
  30. ftt="$MSG.footers"
  31. sed -e '2,${
  32. /^[A-Za-z][A-Za-z0-9-]*: /,$d
  33. }' <"$MSG" >"$out"
  34. sed -ne '2,${
  35. /^[A-Za-z][A-Za-z0-9-]*: /,$p
  36. }' <"$MSG" >"$ftt"
  37. if ! [ -s "$ftt" ]
  38. then
  39. echo >>"$out"
  40. fi
  41. echo "Change-Id: I$id" >>"$out"
  42. cat "$ftt" >>"$out"
  43. mv -f "$out" "$MSG"
  44. rm -f "$out" "$ftt"
  45. }
  46. _gen_ChangeIdInput() {
  47. echo "tree $(git write-tree)"
  48. if parent=$(git rev-parse HEAD^0 2>/dev/null)
  49. then
  50. echo "parent $parent"
  51. fi
  52. echo "author $(git var GIT_AUTHOR_IDENT)"
  53. echo "committer $(git var GIT_COMMITTER_IDENT)"
  54. echo
  55. cat "$MSG"
  56. }
  57. _gen_ChangeId() {
  58. _gen_ChangeIdInput |
  59. git hash-object -t commit --stdin
  60. }
  61. add_ChangeId