prepare-commit-msg 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/sh
  2. # Copyright (C) 2020 Free Software Foundation, Inc.
  3. #
  4. # This file is part of GCC.
  5. #
  6. # GCC is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # GCC is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with GCC; see the file COPYING. If not, write to
  18. # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  19. # Boston, MA 02110-1301, USA.
  20. COMMIT_MSG_FILE=$1
  21. COMMIT_SOURCE=$2
  22. SHA1=$3
  23. # We might be on a branch before the file was added.
  24. if ! [ -x contrib/mklog.py ]; then exit 0; fi
  25. # Can't do anything if $COMMIT_MSG_FILE isn't a file.
  26. if ! [ -f "$COMMIT_MSG_FILE" ]; then exit 0; fi
  27. # Don't do anything unless requested to.
  28. if [ -z "$GCC_FORCE_MKLOG" ]; then exit 0; fi
  29. if [ -z "$COMMIT_SOURCE" ] || [ $COMMIT_SOURCE = template ]; then
  30. # No source or "template" means new commit.
  31. cmd="diff --cached"
  32. elif [ $COMMIT_SOURCE = message ]; then
  33. # "message" means -m; assume a new commit if there are any changes staged.
  34. if ! git diff --cached --quiet; then
  35. cmd="diff --cached"
  36. else
  37. cmd="diff --cached HEAD^"
  38. fi
  39. elif [ $COMMIT_SOURCE = commit ]; then
  40. # The message of an existing commit. If it's HEAD, assume --amend;
  41. # otherwise, assume a new commit with -C.
  42. if [ $SHA1 = HEAD ]; then
  43. cmd="diff --cached HEAD^"
  44. if [ "$(git config gcc-config.mklog-hook-type)" = "smart-amend" ]; then
  45. # Check if the existing message still describes the staged changes.
  46. f=$(mktemp /tmp/git-commit.XXXXXX) || exit 1
  47. git log -1 --pretty=email HEAD > $f
  48. printf '\n---\n\n' >> $f
  49. git $cmd >> $f
  50. if contrib/gcc-changelog/git_email.py "$f" >/dev/null 2>&1; then
  51. # Existing commit message is still OK for amended commit.
  52. rm $f
  53. exit 0
  54. fi
  55. rm $f
  56. fi
  57. else
  58. cmd="diff --cached"
  59. fi
  60. else
  61. # Do nothing for merge or squash.
  62. exit 0
  63. fi
  64. # Save diff to a file if requested.
  65. DIFF_FILE=$(git config gcc-config.diff-file)
  66. if ! [ -z "$DIFF_FILE" ]; then
  67. tee="tee $DIFF_FILE"
  68. else
  69. tee="cat"
  70. fi
  71. git $cmd | $tee | git gcc-mklog $GCC_MKLOG_ARGS -c "$COMMIT_MSG_FILE"