GoogleGit

blob: d8f009b63863a9caf92605a40149ebeb7dbf9eac [file] [log] [blame]
  1. #!/bin/sh
  2. #
  3. # Part of Gerrit Code Review (http://code.google.com/p/gerrit/)
  4. #
  5. # Copyright (C) 2009 The Android Open Source Project
  6. #
  7. # Licensed under the Apache License, Version 2.0 (the "License");
  8. # you may not use this file except in compliance with the License.
  9. # You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. unset GREP_OPTIONS
  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 .*/{
  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 test "false" = "`git config --bool --get gerrit.createChangeId`"
  38. then
  39. return
  40. fi
  41. # Does Change-Id: already exist? if so, exit (no change).
  42. if grep -i '^Change-Id:' "$MSG" >/dev/null
  43. then
  44. return
  45. fi
  46. id=`_gen_ChangeId`
  47. T="$MSG.tmp.$$"
  48. AWK=awk
  49. if [ -x /usr/xpg4/bin/awk ]; then
  50. # Solaris AWK is just too broken
  51. AWK=/usr/xpg4/bin/awk
  52. fi
  53. # How this works:
  54. # - parse the commit message as (textLine+ blankLine*)*
  55. # - assume textLine+ to be a footer until proven otherwise
  56. # - exception: the first block is not footer (as it is the title)
  57. # - read textLine+ into a variable
  58. # - then count blankLines
  59. # - once the next textLine appears, print textLine+ blankLine* as these
  60. # aren't footer
  61. # - in END, the last textLine+ block is available for footer parsing
  62. $AWK '
  63. BEGIN {
  64. # while we start with the assumption that textLine+
  65. # is a footer, the first block is not.
  66. isFooter = 0
  67. footerComment = 0
  68. blankLines = 0
  69. }
  70. # Skip lines starting with "#" without any spaces before it.
  71. /^#/ { next }
  72. # Skip the line starting with the diff command and everything after it,
  73. # up to the end of the file, assuming it is only patch data.
  74. # If more than one line before the diff was empty, strip all but one.
  75. /^diff --git / {
  76. blankLines = 0
  77. while (getline) { }
  78. next
  79. }
  80. # Count blank lines outside footer comments
  81. /^$/ && (footerComment == 0) {
  82. blankLines++
  83. next
  84. }
  85. # Catch footer comment
  86. /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) {
  87. footerComment = 1
  88. }
  89. /]$/ && (footerComment == 1) {
  90. footerComment = 2
  91. }
  92. # We have a non-blank line after blank lines. Handle this.
  93. (blankLines > 0) {
  94. print lines
  95. for (i = 0; i < blankLines; i++) {
  96. print ""
  97. }
  98. lines = ""
  99. blankLines = 0
  100. isFooter = 1
  101. footerComment = 0
  102. }
  103. # Detect that the current block is not the footer
  104. (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) {
  105. isFooter = 0
  106. }
  107. {
  108. # We need this information about the current last comment line
  109. if (footerComment == 2) {
  110. footerComment = 0
  111. }
  112. if (lines != "") {
  113. lines = lines "\n";
  114. }
  115. lines = lines $0
  116. }
  117. # Footer handling:
  118. # If the last block is considered a footer, splice in the Change-Id at the
  119. # right place.
  120. # Look for the right place to inject Change-Id by considering
  121. # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first,
  122. # then Change-Id, then everything else (eg. Signed-off-by:).
  123. #
  124. # Otherwise just print the last block, a new line and the Change-Id as a
  125. # block of its own.
  126. END {
  127. unprinted = 1
  128. if (isFooter == 0) {
  129. print lines "\n"
  130. lines = ""
  131. }
  132. changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):"
  133. numlines = split(lines, footer, "\n")
  134. for (line = 1; line <= numlines; line++) {
  135. if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) {
  136. unprinted = 0
  137. print "Change-Id: I'"$id"'"
  138. }
  139. print footer[line]
  140. }
  141. if (unprinted) {
  142. print "Change-Id: I'"$id"'"
  143. }
  144. }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T"
  145. }
  146. _gen_ChangeIdInput() {
  147. echo "tree `git write-tree`"
  148. if parent=`git rev-parse "HEAD^0" 2>/dev/null`
  149. then
  150. echo "parent $parent"
  151. fi
  152. echo "author `git var GIT_AUTHOR_IDENT`"
  153. echo "committer `git var GIT_COMMITTER_IDENT`"
  154. echo
  155. printf '%s' "$clean_message"
  156. }
  157. _gen_ChangeId() {
  158. _gen_ChangeIdInput |
  159. git hash-object -t commit --stdin
  160. }
  161. add_ChangeId