commit-msg.hook 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. # From Gerrit Code Review 2.2.1
  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. CHANGE_ID_AFTER="Bug|Issue"
  21. MSG="$1"
  22. # Check for, and add if missing, a unique Change-Id
  23. #
  24. add_ChangeId() {
  25. clean_message=`sed -e '
  26. /^diff --git a\/.*/{
  27. s///
  28. q
  29. }
  30. /^Signed-off-by:/d
  31. /^#/d
  32. ' "$MSG" | git stripspace`
  33. if test -z "$clean_message"
  34. then
  35. return
  36. fi
  37. if grep -i '^Change-Id:' "$MSG" >/dev/null
  38. then
  39. return
  40. fi
  41. id=`_gen_ChangeId`
  42. perl -e '
  43. $MSG = shift;
  44. $id = shift;
  45. $CHANGE_ID_AFTER = shift;
  46. undef $/;
  47. open(I, $MSG); $_ = <I>; close I;
  48. s|^diff --git a/.*||ms;
  49. s|^#.*$||mg;
  50. exit unless $_;
  51. @message = split /\n/;
  52. $haveFooter = 0;
  53. $startFooter = @message;
  54. for($line = @message - 1; $line >= 0; $line--) {
  55. $_ = $message[$line];
  56. if (/^[a-zA-Z0-9-]+:/ && !m,^[a-z0-9-]+://,) {
  57. $haveFooter++;
  58. next;
  59. }
  60. next if /^[ []/;
  61. $startFooter = $line if ($haveFooter && /^\r?$/);
  62. last;
  63. }
  64. @footer = @message[$startFooter+1..@message];
  65. @message = @message[0..$startFooter];
  66. push(@footer, "") unless @footer;
  67. for ($line = 0; $line < @footer; $line++) {
  68. $_ = $footer[$line];
  69. next if /^($CHANGE_ID_AFTER):/i;
  70. last;
  71. }
  72. splice(@footer, $line, 0, "Change-Id: I$id");
  73. $_ = join("\n", @message, @footer);
  74. open(O, ">$MSG"); print O; close O;
  75. ' "$MSG" "$id" "$CHANGE_ID_AFTER"
  76. }
  77. _gen_ChangeIdInput() {
  78. echo "tree `git write-tree`"
  79. if parent=`git rev-parse HEAD^0 2>/dev/null`
  80. then
  81. echo "parent $parent"
  82. fi
  83. echo "author `git var GIT_AUTHOR_IDENT`"
  84. echo "committer `git var GIT_COMMITTER_IDENT`"
  85. echo
  86. printf '%s' "$clean_message"
  87. }
  88. _gen_ChangeId() {
  89. _gen_ChangeIdInput |
  90. git hash-object -t commit --stdin
  91. }
  92. add_ChangeId