commit-msg 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/bin/sh
  2. # From Gerrit Code Review 2.1.2-rc2-33-g7e30c72
  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. ($haveFooter++, next) if /^[a-zA-Z0-9-]+:/;
  57. next if /^[ []/;
  58. $startFooter = $line if ($haveFooter && /^\r?$/);
  59. last;
  60. }
  61. @footer = @message[$startFooter+1..@message];
  62. @message = @message[0..$startFooter];
  63. push(@footer, "") unless @footer;
  64. for ($line = 0; $line < @footer; $line++) {
  65. $_ = $footer[$line];
  66. next if /^($CHANGE_ID_AFTER):/i;
  67. last;
  68. }
  69. splice(@footer, $line, 0, "Change-Id: I$id");
  70. $_ = join("\n", @message, @footer);
  71. open(O, ">$MSG"); print O; close O;
  72. ' "$MSG" "$id" "$CHANGE_ID_AFTER"
  73. }
  74. _gen_ChangeIdInput() {
  75. echo "tree $(git write-tree)"
  76. if parent=$(git rev-parse HEAD^0 2>/dev/null)
  77. then
  78. echo "parent $parent"
  79. fi
  80. echo "author $(git var GIT_AUTHOR_IDENT)"
  81. echo "committer $(git var GIT_COMMITTER_IDENT)"
  82. echo
  83. printf '%s' "$clean_message"
  84. }
  85. _gen_ChangeId() {
  86. _gen_ChangeIdInput |
  87. git hash-object -t commit --stdin
  88. }
  89. add_ChangeId