commit-msg 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/sh
  2. # From Gerrit Code Review 3.1.3
  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. # avoid [[ which is not POSIX sh.
  20. if test "$#" != 1 ; then
  21. echo "$0 requires an argument."
  22. exit 1
  23. fi
  24. if test ! -f "$1" ; then
  25. echo "file does not exist: $1"
  26. exit 1
  27. fi
  28. # Do not create a change id if requested
  29. if test "false" = "`git config --bool --get gerrit.createChangeId`" ; then
  30. exit 0
  31. fi
  32. # $RANDOM will be undefined if not using bash, so don't use set -u
  33. random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
  34. dest="$1.tmp.${random}"
  35. trap 'rm -f "${dest}"' EXIT
  36. if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
  37. echo "cannot strip comments from $1"
  38. exit 1
  39. fi
  40. if test ! -s "${dest}" ; then
  41. echo "file is empty: $1"
  42. exit 1
  43. fi
  44. # Avoid the --in-place option which only appeared in Git 2.8
  45. # Avoid the --if-exists option which only appeared in Git 2.15
  46. if ! git -c trailer.ifexists=doNothing interpret-trailers --where start \
  47. --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
  48. echo "cannot insert change-id line in $1"
  49. exit 1
  50. fi
  51. if ! mv "${dest}" "$1" ; then
  52. echo "cannot mv ${dest} to $1"
  53. exit 1
  54. fi