Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

  1. #!/bin/sh
  2. # From Gerrit Code Review 3.5.6
  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. set -u
  20. # avoid [[ which is not POSIX sh.
  21. if test "$#" != 1 ; then
  22. echo "$0 requires an argument."
  23. exit 1
  24. fi
  25. if test ! -f "$1" ; then
  26. echo "file does not exist: $1"
  27. exit 1
  28. fi
  29. # Do not create a change id if requested
  30. if test "false" = "$(git config --bool --get gerrit.createChangeId)" ; then
  31. exit 0
  32. fi
  33. if git rev-parse --verify HEAD >/dev/null 2>&1; then
  34. refhash="$(git rev-parse HEAD)"
  35. else
  36. refhash="$(git hash-object -t tree /dev/null)"
  37. fi
  38. random=$({ git var GIT_COMMITTER_IDENT ; echo "$refhash" ; cat "$1"; } | git hash-object --stdin)
  39. dest="$1.tmp.${random}"
  40. trap 'rm -f "${dest}"' EXIT
  41. if ! git stripspace --strip-comments < "$1" > "${dest}" ; then
  42. echo "cannot strip comments from $1"
  43. exit 1
  44. fi
  45. if test ! -s "${dest}" ; then
  46. echo "file is empty: $1"
  47. exit 1
  48. fi
  49. reviewurl="$(git config --get gerrit.reviewUrl)"
  50. if test -n "${reviewurl}" ; then
  51. if ! git interpret-trailers --parse < "$1" | grep -q '^Link:.*/id/I[0-9a-f]\{40\}$' ; then
  52. if ! git interpret-trailers \
  53. --trailer "Link: ${reviewurl%/}/id/I${random}" < "$1" > "${dest}" ; then
  54. echo "cannot insert link footer in $1"
  55. exit 1
  56. fi
  57. fi
  58. else
  59. # Avoid the --in-place option which only appeared in Git 2.8
  60. # Avoid the --if-exists option which only appeared in Git 2.15
  61. if ! git -c trailer.ifexists=doNothing interpret-trailers \
  62. --trailer "Change-Id: I${random}" < "$1" > "${dest}" ; then
  63. echo "cannot insert change-id line in $1"
  64. exit 1
  65. fi
  66. fi
  67. if ! mv "${dest}" "$1" ; then
  68. echo "cannot mv ${dest} to $1"
  69. exit 1
  70. fi