git-commit: finishing touches.
[git/git.git] / git-commit.sh
CommitLineData
a3e870f2 1#!/bin/sh
5fec3ef1
JH
2#
3# Copyright (c) 2005 Linus Torvalds
130fcca6
JH
4# Copyright (c) 2006 Junio C Hamano
5
6USAGE='[-a] [-i] [-s] [-v | --no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-e] [--author <author>] [<path>...]'
5fec3ef1 7
130fcca6 8SUBDIRECTORY_OK=Yes
ae2b0f15 9. git-sh-setup
b33e9666 10
130fcca6
JH
11git-rev-parse --verify HEAD >/dev/null 2>&1 ||
12initial_commit=t
13
14refuse_partial () {
15 echo >&2 "$1"
130fcca6
JH
16 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
17 exit 1
18}
19
5a798fb5
JH
20SAVE_INDEX="$GIT_DIR/save-index$$"
21save_index () {
22 cp "$GIT_DIR/index" "$SAVE_INDEX"
23}
24
25run_status () {
26 (
27 cd "$TOP"
28 if test '' != "$TMP_INDEX"
29 then
30 GIT_INDEX_FILE="$TMP_INDEX" git-status
31 else
32 git-status
33 fi
34 )
35}
36
130fcca6
JH
37all=
38also=
5a798fb5 39only=
130fcca6
JH
40logfile=
41use_commit=
42no_edit=
43log_given=
44log_message=
45verify=t
46signoff=
47force_author=
0c091296 48while case "$#" in 0) break;; esac
5fec3ef1 49do
0c091296 50 case "$1" in
130fcca6
JH
51 -F|--F|-f|--f|--fi|--fil|--file)
52 case "$#" in 1) usage ;; esac
53 shift
54 no_edit=t
55 log_given=t$log_given
56 logfile="$1"
57 shift
58 ;;
59 -F*|-f*)
60 no_edit=t
61 log_given=t$log_given
62 logfile=`expr "$1" : '-[Ff]\(.*\)'`
63 shift
64 ;;
65 --F=*|--f=*|--fi=*|--fil=*|--file=*)
66 no_edit=t
67 log_given=t$log_given
68 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
69 shift
70 ;;
0c091296 71 -a|--a|--al|--all)
130fcca6
JH
72 all=t
73 shift
74 ;;
75 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
76 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
77 shift
78 ;;
79 --au|--aut|--auth|--autho|--author)
80 case "$#" in 1) usage ;; esac
81 shift
82 force_author="$1"
83 shift
84 ;;
85 -e|--e|--ed|--edi|--edit)
86 no_edit=
87 shift
88 ;;
89 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
90 also=t
91 shift
92 ;;
5a798fb5
JH
93 -o|--o|--on|--onl|--only)
94 only=t
95 shift
96 ;;
0c091296 97 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
130fcca6
JH
98 case "$#" in 1) usage ;; esac
99 shift
100 log_given=t$log_given
101 log_message="$1"
102 no_edit=t
103 shift
104 ;;
105 -m*)
106 log_given=t$log_given
107 log_message=`expr "$1" : '-m\(.*\)'`
108 no_edit=t
109 shift
110 ;;
111 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
112 log_given=t$log_given
113 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
114 no_edit=t
115 shift
116 ;;
117 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
118 verify=
119 shift
120 ;;
121 -c)
122 case "$#" in 1) usage ;; esac
123 shift
124 log_given=t$log_given
125 use_commit="$1"
126 no_edit=
127 shift
128 ;;
129 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
0c091296
JH
130 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
131 --reedit-messag=*|--reedit-message=*)
130fcca6
JH
132 log_given=t$log_given
133 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
134 no_edit=
135 shift
136 ;;
137 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
0c091296 138 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
130fcca6
JH
139 case "$#" in 1) usage ;; esac
140 shift
141 log_given=t$log_given
142 use_commit="$1"
143 no_edit=
144 shift
145 ;;
146 -C)
147 case "$#" in 1) usage ;; esac
148 shift
149 log_given=t$log_given
150 use_commit="$1"
151 no_edit=t
152 shift
153 ;;
154 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
0c091296
JH
155 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
156 --reuse-message=*)
130fcca6
JH
157 log_given=t$log_given
158 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
159 no_edit=t
160 shift
161 ;;
162 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
0c091296 163 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
130fcca6
JH
164 case "$#" in 1) usage ;; esac
165 shift
166 log_given=t$log_given
167 use_commit="$1"
168 no_edit=t
169 shift
170 ;;
0cfe1d30 171 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
130fcca6
JH
172 signoff=t
173 shift
174 ;;
0cfe1d30 175 -v|--v|--ve|--ver|--veri|--verif|--verify)
130fcca6 176 verify=t
5a798fb5 177 shift
130fcca6 178 ;;
0c091296 179 --)
130fcca6
JH
180 shift
181 break
182 ;;
0c091296 183 -*)
130fcca6
JH
184 usage
185 ;;
0c091296 186 *)
130fcca6
JH
187 break
188 ;;
0c091296 189 esac
5fec3ef1
JH
190done
191
0c091296
JH
192case "$log_given" in
193tt*)
194 die "Only one of -c/-C/-F/-m can be used." ;;
195esac
196
5a798fb5
JH
197case "$#,$also$only" in
198*,tt)
199 die "Only one of --include/--only can be used." ;;
2000,t)
201 die "No paths with --include/--only does not make sense." ;;
2020,)
203 ;;
204*,)
205 echo >&2 "assuming --include paths..."
206 also=t
207 # Later when switch the defaults, we will replace them with these:
208 # echo >&2 "assuming --only paths..."
209 # also=
210 ;;
211esac
212unset only
213
130fcca6 214TOP=`git-rev-parse --show-cdup`
5a798fb5
JH
215if test -z "$TOP"
216then
217 TOP=./
218fi
130fcca6
JH
219
220case "$all,$also" in
221t,t)
222 die "Cannot use -a and -i at the same time." ;;
223t,)
5a798fb5
JH
224 case "$#" in
225 0) ;;
226 *) die "Paths with -a does not make sense." ;;
227 esac
228
229 save_index &&
130fcca6 230 (
5a798fb5 231 cd "$TOP"
130fcca6
JH
232 git-diff-files --name-only -z |
233 git-update-index --remove -z --stdin
234 )
6a0049c0 235 ;;
130fcca6
JH
236,t)
237 case "$#" in
238 0) die "No paths with -i does not make sense." ;;
239 esac
5a798fb5
JH
240
241 save_index &&
130fcca6 242 git-diff-files --name-only -z -- "$@" |
5a798fb5 243 (cd "$TOP" && git-update-index --remove -z --stdin)
22cff6a5 244 ;;
130fcca6
JH
245,)
246 case "$#" in
247 0)
248 ;; # commit as-is
249 *)
250 if test -f "$GIT_DIR/MERGE_HEAD"
251 then
252 refuse_partial "Cannot do a partial commit during a merge."
253 fi
254 TMP_INDEX="$GIT_DIR/tmp-index$$"
255 if test -z "$initial_commit"
256 then
257 # make sure index is clean at the specified paths, or
258 # they are additions.
259 dirty_in_index=`git-diff-index --cached --name-status \
5a798fb5 260 --diff-filter=DMTU HEAD -- "$@"`
130fcca6 261 test -z "$dirty_in_index" ||
5a798fb5 262 refuse_partial "Different in index and the last commit:
130fcca6
JH
263$dirty_in_index"
264 fi
265 commit_only=`git-ls-files -- "$@"` ;;
266 esac
267 ;;
268esac
269
215a7ad1 270git-update-index -q --refresh || exit 1
0c091296 271
130fcca6 272trap '
5a798fb5
JH
273 test -z "$TMP_INDEX" || {
274 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
275 }
130fcca6
JH
276 test -f "$SAVE_INDEX" && mv -f "$SAVE_INDEX" "$GIT_DIR/index"
277' 0
278
279if test "$TMP_INDEX"
280then
281 if test -z "$initial_commit"
89e2c5f1 282 then
130fcca6
JH
283 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree HEAD
284 else
285 rm -f "$TMP_INDEX"
286 fi || exit
287 echo "$commit_only" |
288 GIT_INDEX_FILE="$TMP_INDEX" git-update-index --add --remove --stdin &&
5a798fb5 289 save_index &&
130fcca6
JH
290 echo "$commit_only" |
291 git-update-index --remove --stdin ||
292 exit
130fcca6
JH
293fi
294
295if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
296then
297 if test "$TMP_INDEX"
298 then
299 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
300 else
301 "$GIT_DIR"/hooks/pre-commit
302 fi || exit
303fi
0cfe1d30 304
aba2da13
JH
305if test "$log_message" != ''
306then
307 echo "$log_message"
308elif test "$logfile" != ""
309then
310 if test "$logfile" = -
311 then
312 test -t 0 &&
313 echo >&2 "(reading log message from standard input)"
314 cat
315 else
316 cat <"$logfile"
317 fi
318elif test "$use_commit" != ""
319then
320 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
e5215804
JH
321elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
322then
323 cat "$GIT_DIR/MERGE_MSG"
f8e2c54c 324fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
325
326case "$signoff" in
327t)
f6413391
JH
328 {
329 echo
330 git-var GIT_COMMITTER_IDENT | sed -e '
331 s/>.*/>/
332 s/^/Signed-off-by: /
333 '
f8e2c54c 334 } >>"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
335 ;;
336esac
337
338if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
339 echo "#"
b6ae5409 340 echo "# It looks like you may be committing a MERGE."
2d569933
JH
341 echo "# If this is not correct, please remove the file"
342 echo "# $GIT_DIR/MERGE_HEAD"
343 echo "# and try again"
344 echo "#"
f8e2c54c 345fi >>"$GIT_DIR"/COMMIT_EDITMSG
aba2da13 346
130fcca6 347# Author
5a798fb5
JH
348if test '' != "$force_author"
349then
350 GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
351 GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
352 test '' != "$GIT_AUTHOR_NAME" &&
353 test '' != "$GIT_AUTHOR_EMAIL" ||
354 die "malformatted --author parameter"
355 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
356elif test '' != "$use_commit"
130fcca6
JH
357then
358 pick_author_script='
359 /^author /{
360 s/'\''/'\''\\'\'\''/g
361 h
362 s/^author \([^<]*\) <[^>]*> .*$/\1/
363 s/'\''/'\''\'\'\''/g
364 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
365
366 g
367 s/^author [^<]* <\([^>]*\)> .*$/\1/
368 s/'\''/'\''\'\'\''/g
369 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
370
371 g
372 s/^author [^<]* <[^>]*> \(.*\)$/\1/
373 s/'\''/'\''\'\'\''/g
374 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
375
376 q
377 }
378 '
379 set_author_env=`git-cat-file commit "$use_commit" |
380 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
381 eval "$set_author_env"
382 export GIT_AUTHOR_NAME
383 export GIT_AUTHOR_EMAIL
384 export GIT_AUTHOR_DATE
130fcca6
JH
385fi
386
96069cf0 387PARENTS="-p HEAD"
130fcca6 388if test -z "$initial_commit"
8098a178 389then
5fec3ef1 390 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
91063bbc 391 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
aba2da13 392 fi
8098a178
JH
393else
394 if [ -z "$(git-ls-files)" ]; then
5a798fb5 395 echo >&2 Nothing to commit
8098a178
JH
396 exit 1
397 fi
398 PARENTS=""
96069cf0 399fi
130fcca6 400
5a798fb5
JH
401
402run_status >>"$GIT_DIR"/COMMIT_EDITMSG
df34297a 403if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
a3e870f2 404then
f8e2c54c 405 rm -f "$GIT_DIR/COMMIT_EDITMSG"
5a798fb5 406 run_status
a3e870f2
LT
407 exit 1
408fi
0c091296 409case "$no_edit" in
5fec3ef1 410'')
7334f06c
JH
411 case "${VISUAL:-$EDITOR},$TERM" in
412 ,dumb)
413 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
414 echo >&2 "Please supply the commit log message using either"
415 echo >&2 "-m or -F option. A boilerplate log message has"
416 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
130fcca6
JH
417 exit 1
418 ;;
7334f06c 419 esac
f8e2c54c 420 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
5fec3ef1
JH
421 ;;
422esac
89e2c5f1
JH
423
424case "$verify" in
425t)
426 if test -x "$GIT_DIR"/hooks/commit-msg
427 then
f8e2c54c 428 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
89e2c5f1
JH
429 fi
430esac
431
f8e2c54c
SB
432grep -v '^#' < "$GIT_DIR"/COMMIT_EDITMSG |
433git-stripspace > "$GIT_DIR"/COMMIT_MSG
434
435if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
436 git-stripspace |
437 wc -l` &&
438 test 0 -lt $cnt
0c091296 439then
130fcca6
JH
440 if test -z "$TMP_INDEX"
441 then
442 tree=$(git-write-tree)
443 else
444 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
445 rm -f "$TMP_INDEX"
446 fi &&
f8e2c54c 447 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
bf7960eb 448 git-update-ref HEAD $commit $current &&
5fec3ef1 449 rm -f -- "$GIT_DIR/MERGE_HEAD"
0c091296
JH
450else
451 echo >&2 "* no commit message? aborting commit."
452 false
453fi
170241b7 454ret="$?"
f8e2c54c 455rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
8389b52b 456git-rerere
89e2c5f1
JH
457
458if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
459then
460 "$GIT_DIR"/hooks/post-commit
461fi
130fcca6
JH
462if test 0 -eq "$ret"
463then
464 rm -f "$SAVE_INDEX"
465fi
170241b7 466exit "$ret"