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