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