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