commit-msg.gerrit 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/sh
  2. # From Gerrit Code Review 2.14.20
  3. #
  4. # Part of Gerrit Code Review (https://www.gerritcodereview.com/)
  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. unset GREP_OPTIONS
  21. CHANGE_ID_AFTER="Bug|Depends-On|Issue|Test|Feature|Fixes|Fixed"
  22. MSG="$1"
  23. # Check for, and add if missing, a unique Change-Id
  24. #
  25. add_ChangeId() {
  26. clean_message=`sed -e '
  27. /^diff --git .*/{
  28. s///
  29. q
  30. }
  31. /^Signed-off-by:/d
  32. /^#/d
  33. ' "$MSG" | git stripspace`
  34. if test -z "$clean_message"
  35. then
  36. return
  37. fi
  38. # Do not add Change-Id to temp commits
  39. if echo "$clean_message" | head -1 | grep -q '^\(fixup\|squash\)!'
  40. then
  41. return
  42. fi
  43. if test "false" = "`git config --bool --get gerrit.createChangeId`"
  44. then
  45. return
  46. fi
  47. # Does Change-Id: already exist? if so, exit (no change).
  48. if grep -i '^Change-Id:' "$MSG" >/dev/null
  49. then
  50. return
  51. fi
  52. id=`_gen_ChangeId`
  53. T="$MSG.tmp.$$"
  54. AWK=awk
  55. if [ -x /usr/xpg4/bin/awk ]; then
  56. # Solaris AWK is just too broken
  57. AWK=/usr/xpg4/bin/awk
  58. fi
  59. # Get core.commentChar from git config or use default symbol
  60. commentChar=`git config --get core.commentChar`
  61. commentChar=${commentChar:-#}
  62. # How this works:
  63. # - parse the commit message as (textLine+ blankLine*)*
  64. # - assume textLine+ to be a footer until proven otherwise
  65. # - exception: the first block is not footer (as it is the title)
  66. # - read textLine+ into a variable
  67. # - then count blankLines
  68. # - once the next textLine appears, print textLine+ blankLine* as these
  69. # aren't footer
  70. # - in END, the last textLine+ block is available for footer parsing
  71. $AWK '
  72. BEGIN {
  73. if (match(ENVIRON["OS"], "Windows")) {
  74. RS="\r?\n" # Required on recent Cygwin
  75. }
  76. # while we start with the assumption that textLine+
  77. # is a footer, the first block is not.
  78. isFooter = 0
  79. footerComment = 0
  80. blankLines = 0
  81. }
  82. # Skip lines starting with commentChar without any spaces before it.
  83. /^'"$commentChar"'/ { next }
  84. # Skip the line starting with the diff command and everything after it,
  85. # up to the end of the file, assuming it is only patch data.
  86. # If more than one line before the diff was empty, strip all but one.
  87. /^diff --git / {
  88. blankLines = 0
  89. while (getline) { }
  90. next
  91. }
  92. # Count blank lines outside footer comments
  93. /^$/ && (footerComment == 0) {
  94. blankLines++
  95. next
  96. }
  97. # Catch footer comment
  98. /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
  99. footerComment = 1
  100. }
  101. /]$/ && (footerComment == 1) {
  102. footerComment = 2
  103. }
  104. # We have a non-blank line after blank lines. Handle this.
  105. (blankLines > 0) {
  106. print lines
  107. for (i = 0; i < blankLines; i++) {
  108. print ""
  109. }
  110. lines = ""
  111. blankLines = 0
  112. isFooter = 1
  113. footerComment = 0
  114. }
  115. # Detect that the current block is not the footer
  116. (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
  117. isFooter = 0
  118. }
  119. {
  120. # We need this information about the current last comment line
  121. if (footerComment == 2) {
  122. footerComment = 0
  123. }
  124. if (lines != "") {
  125. lines = lines "\n";
  126. }
  127. lines = lines $0
  128. }
  129. # Footer handling:
  130. # If the last block is considered a footer, splice in the Change-Id at the
  131. # right place.
  132. # Look for the right place to inject Change-Id by considering
  133. # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
  134. # then Change-Id, then everything else (eg. Signed-off-by:).
  135. #
  136. # Otherwise just print the last block, a new line and the Change-Id as a
  137. # block of its own.
  138. END {
  139. unprinted = 1
  140. if (isFooter == 0) {
  141. print lines "\n"
  142. lines = ""
  143. }
  144. changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
  145. numlines = split(lines, footer, "\n")
  146. for (line = 1; line <= numlines; line++) {
  147. if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
  148. unprinted = 0
  149. print "Change-Id: I'"$id"'"
  150. }
  151. print footer[line]
  152. }
  153. if (unprinted) {
  154. print "Change-Id: I'"$id"'"
  155. }
  156. }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
  157. }
  158. _gen_ChangeIdInput() {
  159. echo "tree `git write-tree`"
  160. if parent=`git rev-parse "HEAD^0" 2>/dev/null`
  161. then
  162. echo "parent $parent"
  163. fi
  164. echo "author `git var GIT_AUTHOR_IDENT`"
  165. echo "committer `git var GIT_COMMITTER_IDENT`"
  166. echo
  167. printf '%s' "$clean_message"
  168. }
  169. _gen_ChangeId() {
  170. _gen_ChangeIdInput |
  171. git hash-object -t commit --stdin
  172. }
  173. add_ChangeId