2020-05-22 22:40:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2024-01-03 11:19:35 +00:00
|
|
|
# Copyright (C) 2020-2024 Free Software Foundation, Inc.
|
2020-05-22 22:40:35 +00:00
|
|
|
#
|
|
|
|
# This file is part of GCC.
|
|
|
|
#
|
|
|
|
# GCC is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# GCC is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with GCC; see the file COPYING. If not, write to
|
|
|
|
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
COMMIT_MSG_FILE=$1
|
|
|
|
COMMIT_SOURCE=$2
|
|
|
|
SHA1=$3
|
|
|
|
|
2020-06-02 10:18:21 +00:00
|
|
|
# We might be on a branch before the file was added.
|
|
|
|
if ! [ -x contrib/mklog.py ]; then exit 0; fi
|
|
|
|
|
2020-05-22 22:40:35 +00:00
|
|
|
# Can't do anything if $COMMIT_MSG_FILE isn't a file.
|
|
|
|
if ! [ -f "$COMMIT_MSG_FILE" ]; then exit 0; fi
|
|
|
|
|
|
|
|
# Don't do anything unless requested to.
|
|
|
|
if [ -z "$GCC_FORCE_MKLOG" ]; then exit 0; fi
|
|
|
|
|
2023-07-06 20:40:57 +00:00
|
|
|
if [ -z "$COMMIT_SOURCE" ] || [ "$COMMIT_SOURCE" = template ]; then
|
2020-05-22 22:40:35 +00:00
|
|
|
# No source or "template" means new commit.
|
|
|
|
cmd="diff --cached"
|
|
|
|
|
2023-07-06 20:40:57 +00:00
|
|
|
elif [ "$COMMIT_SOURCE" = message ]; then
|
2020-05-22 22:40:35 +00:00
|
|
|
# "message" means -m; assume a new commit if there are any changes staged.
|
|
|
|
if ! git diff --cached --quiet; then
|
|
|
|
cmd="diff --cached"
|
|
|
|
else
|
|
|
|
cmd="diff --cached HEAD^"
|
|
|
|
fi
|
|
|
|
|
2023-07-06 20:40:57 +00:00
|
|
|
elif [ "$COMMIT_SOURCE" = commit ]; then
|
2020-05-22 22:40:35 +00:00
|
|
|
# The message of an existing commit. If it's HEAD, assume --amend;
|
|
|
|
# otherwise, assume a new commit with -C.
|
2023-07-06 20:40:57 +00:00
|
|
|
if [ "$SHA1" = HEAD ]; then
|
2020-05-22 22:40:35 +00:00
|
|
|
cmd="diff --cached HEAD^"
|
2020-06-11 19:22:17 +00:00
|
|
|
if [ "$(git config gcc-config.mklog-hook-type)" = "smart-amend" ]; then
|
|
|
|
# Check if the existing message still describes the staged changes.
|
|
|
|
f=$(mktemp /tmp/git-commit.XXXXXX) || exit 1
|
2023-07-06 20:40:57 +00:00
|
|
|
git log -1 --pretty=email HEAD > "$f"
|
|
|
|
printf '\n---\n\n' >> "$f"
|
|
|
|
git $cmd >> "$f"
|
2020-06-11 19:22:17 +00:00
|
|
|
if contrib/gcc-changelog/git_email.py "$f" >/dev/null 2>&1; then
|
|
|
|
# Existing commit message is still OK for amended commit.
|
2023-07-06 20:40:57 +00:00
|
|
|
rm "$f"
|
2020-06-11 19:22:17 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
2023-07-06 20:40:57 +00:00
|
|
|
rm "$f"
|
2020-06-11 19:22:17 +00:00
|
|
|
fi
|
2020-05-22 22:40:35 +00:00
|
|
|
else
|
|
|
|
cmd="diff --cached"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Do nothing for merge or squash.
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-05-26 07:08:31 +00:00
|
|
|
# Save diff to a file if requested.
|
2020-06-11 08:02:26 +00:00
|
|
|
DIFF_FILE=$(git config gcc-config.diff-file)
|
2023-07-06 20:40:57 +00:00
|
|
|
if [ -n "$DIFF_FILE" ]; then
|
2020-06-11 08:02:26 +00:00
|
|
|
tee="tee $DIFF_FILE"
|
2020-06-11 07:57:58 +00:00
|
|
|
else
|
|
|
|
tee="cat"
|
2020-05-26 07:08:31 +00:00
|
|
|
fi
|
|
|
|
|
2022-07-22 09:26:08 +00:00
|
|
|
git $cmd | $tee | git gcc-mklog -c "$COMMIT_MSG_FILE"
|