Add commit hook and make the verification customizable.
[git/dkf.git] / git-commit-script
blob5c5f564a28e779de333ea96c35c95993f7535869
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
6 . git-sh-setup-script || die "Not a git archive"
8 usage () {
9 die 'git commit [-a] [-v | --no-verify] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'
12 all= logfile= use_commit= no_edit= log_given= log_message= verify=t signoff=
13 while case "$#" in 0) break;; esac
15 case "$1" in
16 -a|--a|--al|--all)
17 all=t
18 shift ;;
19 -F=*|--f=*|--fi=*|--fil=*|--file=*)
20 log_given=t$log_given
21 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
22 no_edit=t
23 shift ;;
24 -F|--f|--fi|--fil|--file)
25 case "$#" in 1) usage ;; esac; shift
26 log_given=t$log_given
27 logfile="$1"
28 no_edit=t
29 shift ;;
30 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
31 log_given=t$log_given
32 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
33 no_edit=t
34 shift ;;
35 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
36 case "$#" in 1) usage ;; esac; shift
37 log_given=t$log_given
38 log_message="$1"
39 no_edit=t
40 shift ;;
41 -c=*|--ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
42 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
43 --reedit-messag=*|--reedit-message=*)
44 log_given=t$log_given
45 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
46 shift ;;
47 -c|--ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
48 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
49 case "$#" in 1) usage ;; esac; shift
50 log_given=t$log_given
51 use_commit="$1"
52 shift ;;
53 -C=*|--reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
54 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
55 --reuse-message=*)
56 log_given=t$log_given
57 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
58 no_edit=t
59 shift ;;
60 -C|--reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
61 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
62 case "$#" in 1) usage ;; esac; shift
63 log_given=t$log_given
64 use_commit="$1"
65 no_edit=t
66 shift ;;
67 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
68 signoff=t
69 shift ;;
70 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
71 verify=
72 shift ;;
73 -v|--v|--ve|--ver|--veri|--verif|--verify)
74 verify=t
75 shift ;;
76 --)
77 shift
78 break ;;
79 -*)
80 usage ;;
82 break ;;
83 esac
84 done
86 case "$log_given" in
87 tt*)
88 die "Only one of -c/-C/-F/-m can be used." ;;
89 esac
91 case "$all,$#" in
92 t,*)
93 git-diff-files --name-only -z |
94 xargs -0 git-update-cache -q --remove --
96 ,0)
99 git-diff-files --name-only -z "$@" |
100 xargs -0 git-update-cache -q --remove --
102 esac || exit 1
103 git-update-cache -q --refresh || exit 1
105 case "$verify" in
107 if test -x "$GIT_DIR"/hooks/pre-commit
108 then
109 "$GIT_DIR"/hooks/pre-commit || exit
111 esac
113 PARENTS="-p HEAD"
114 if [ ! -r "$GIT_DIR/HEAD" ]; then
115 if [ -z "$(git-ls-files)" ]; then
116 echo Nothing to commit 1>&2
117 exit 1
120 echo "#"
121 echo "# Initial commit"
122 case "$no_edit" in
123 t) echo "# (ignoring your commit message for initial commit)"
124 no_edit=
125 esac
126 echo "#"
127 git-ls-files | sed 's/^/# New file: /'
128 echo "#"
129 } >.editmsg
130 PARENTS=""
131 no_edit=
132 else
133 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
135 echo "#"
136 echo "# It looks like your may be committing a MERGE."
137 echo "# If this is not correct, please remove the file"
138 echo "# $GIT_DIR/MERGE_HEAD"
139 echo "# and try again"
140 case "$no_edit" in
141 t) echo "# (ignoring your commit message for merge commit)"
142 no_edit=
143 esac
144 echo "#"
146 git-stripspace >.editmsg
147 PARENTS="-p HEAD -p MERGE_HEAD"
148 elif test "$log_message" != ''
149 then
150 echo "$log_message" |
151 git-stripspace >.editmsg
152 elif test "$logfile" != ""
153 then
154 if test "$logfile" = -
155 then
156 test -t 0 &&
157 echo >&2 "(reading log message from standard input)"
159 else
160 cat <"$logfile"
161 fi |
162 git-stripspace >.editmsg
163 elif test "$use_commit" != ""
164 then
165 pick_author_script='
166 /^author /{
168 s/^author \([^<]*\) <[^>]*> .*$/\1/
169 s/'\''/'\''\'\'\''/g
170 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
173 s/^author [^<]* <\([^>]*\)> .*$/\1/
174 s/'\''/'\''\'\'\''/g
175 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
178 s/^author [^<]* <[^>]*> \(.*\)$/\1/
179 s/'\''/'\''\'\'\''/g
180 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
185 set_author_env=`git-cat-file commit "$use_commit" |
186 sed -ne "$pick_author_script"`
187 eval "$set_author_env"
188 export GIT_AUTHOR_NAME
189 export GIT_AUTHOR_EMAIL
190 export GIT_AUTHOR_DATE
191 git-cat-file commit "$use_commit" |
192 sed -e '1,/^$/d' |
193 git-stripspace >.editmsg
196 case "$signoff" in
198 git-var GIT_COMMITTER_IDENT | sed -e '
199 s/>.*/>/
200 s/^/Signed-off-by: /' >>.editmsg ;;
201 esac
202 git-status-script >>.editmsg
204 if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]
205 then
206 cat .editmsg
207 rm .editmsg
208 exit 1
210 case "$no_edit" in
212 ${VISUAL:-${EDITOR:-vi}} .editmsg
214 esac
216 case "$verify" in
218 if test -x "$GIT_DIR"/hooks/commit-msg
219 then
220 "$GIT_DIR"/hooks/commit-msg .editmsg || exit
222 esac
224 grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
225 grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk
226 if test -s .cmitchk
227 then
228 tree=$(git-write-tree) &&
229 commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
230 echo $commit > "$GIT_DIR/HEAD" &&
231 rm -f -- "$GIT_DIR/MERGE_HEAD"
232 else
233 echo >&2 "* no commit message? aborting commit."
234 false
236 ret="$?"
237 rm -f .cmitmsg .editmsg .cmitchk
239 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
240 then
241 "$GIT_DIR"/hooks/post-commit
243 exit "$ret"