]> git.ipfire.org Git - thirdparty/git.git/blame - git-commit.sh
make commit message a little more consistent and conforting
[thirdparty/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
443f8338 6USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
130fcca6 7SUBDIRECTORY_OK=Yes
ae2b0f15 8. git-sh-setup
b33e9666 9
cf7bb589
JH
10git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
12
13case "$0" in
14*status)
15 status_only=t
16 unmerged_ok_if_status=--unmerged ;;
17*commit)
18 status_only=
19 unmerged_ok_if_status= ;;
20esac
130fcca6
JH
21
22refuse_partial () {
23 echo >&2 "$1"
130fcca6
JH
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
26}
27
cf7bb589
JH
28THIS_INDEX="$GIT_DIR/index"
29NEXT_INDEX="$GIT_DIR/next-index$$"
30rm -f "$NEXT_INDEX"
5a798fb5 31save_index () {
cc7d5bcf 32 cp -p "$THIS_INDEX" "$NEXT_INDEX"
cf7bb589
JH
33}
34
5a798fb5 35run_status () {
cf7bb589
JH
36 # If TMP_INDEX is defined, that means we are doing
37 # "--only" partial commit, and that index file is used
38 # to build the tree for the commit. Otherwise, if
39 # NEXT_INDEX exists, that is the index file used to
40 # make the commit. Otherwise we are using as-is commit
41 # so the regular index file is what we use to compare.
42 if test '' != "$TMP_INDEX"
43 then
3de63c3f
MW
44 GIT_INDEX_FILE="$TMP_INDEX"
45 export GIT_INDEX_FILE
cf7bb589
JH
46 elif test -f "$NEXT_INDEX"
47 then
3de63c3f
MW
48 GIT_INDEX_FILE="$NEXT_INDEX"
49 export GIT_INDEX_FILE
cf7bb589
JH
50 fi
51
3de63c3f
MW
52 case "$status_only" in
53 t) color= ;;
54 *) color=--nocolor ;;
55 esac
56 git-runstatus ${color} \
57 ${verbose:+--verbose} \
58 ${amend:+--amend} \
2074cb0a 59 ${untracked_files:+--untracked}
5a798fb5
JH
60}
61
cf7bb589
JH
62trap '
63 test -z "$TMP_INDEX" || {
64 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
65 }
66 rm -f "$NEXT_INDEX"
67' 0
68
69################################################################
70# Command line argument parsing and sanity checking
71
130fcca6
JH
72all=
73also=
5a798fb5 74only=
130fcca6
JH
75logfile=
76use_commit=
b4019f04 77amend=
cda8ab59 78edit_flag=
130fcca6
JH
79no_edit=
80log_given=
81log_message=
82verify=t
ebd124c6 83quiet=
cf7bb589 84verbose=
130fcca6
JH
85signoff=
86force_author=
bba319b5 87only_include_assumed=
443f8338 88untracked_files=
0c091296 89while case "$#" in 0) break;; esac
5fec3ef1 90do
3de63c3f
MW
91 case "$1" in
92 -F|--F|-f|--f|--fi|--fil|--file)
93 case "$#" in 1) usage ;; esac
94 shift
95 no_edit=t
96 log_given=t$log_given
97 logfile="$1"
98 shift
99 ;;
100 -F*|-f*)
101 no_edit=t
102 log_given=t$log_given
103 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
104 shift
105 ;;
106 --F=*|--f=*|--fi=*|--fil=*|--file=*)
107 no_edit=t
108 log_given=t$log_given
109 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
110 shift
111 ;;
112 -a|--a|--al|--all)
113 all=t
114 shift
115 ;;
116 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
117 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
118 shift
119 ;;
120 --au|--aut|--auth|--autho|--author)
121 case "$#" in 1) usage ;; esac
122 shift
123 force_author="$1"
124 shift
125 ;;
126 -e|--e|--ed|--edi|--edit)
127 edit_flag=t
128 shift
129 ;;
130 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
131 also=t
132 shift
133 ;;
134 -o|--o|--on|--onl|--only)
135 only=t
136 shift
137 ;;
138 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
139 case "$#" in 1) usage ;; esac
140 shift
141 log_given=m$log_given
142 if test "$log_message" = ''
143 then
144 log_message="$1"
145 else
146 log_message="$log_message
6891281c
SP
147
148$1"
3de63c3f
MW
149 fi
150 no_edit=t
151 shift
152 ;;
153 -m*)
154 log_given=m$log_given
155 if test "$log_message" = ''
156 then
157 log_message=`expr "z$1" : 'z-m\(.*\)'`
158 else
159 log_message="$log_message
6891281c 160
8096fae7 161`expr "z$1" : 'z-m\(.*\)'`"
3de63c3f
MW
162 fi
163 no_edit=t
164 shift
165 ;;
166 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
167 log_given=m$log_given
168 if test "$log_message" = ''
169 then
170 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
171 else
172 log_message="$log_message
6891281c 173
8096fae7 174`expr "z$1" : 'zq-[^=]*=\(.*\)'`"
3de63c3f
MW
175 fi
176 no_edit=t
177 shift
178 ;;
179 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
180 --no-verify)
181 verify=
182 shift
183 ;;
184 --a|--am|--ame|--amen|--amend)
185 amend=t
186 log_given=t$log_given
187 use_commit=HEAD
188 shift
189 ;;
190 -c)
191 case "$#" in 1) usage ;; esac
192 shift
193 log_given=t$log_given
194 use_commit="$1"
195 no_edit=
196 shift
197 ;;
198 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
199 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
200 --reedit-messag=*|--reedit-message=*)
201 log_given=t$log_given
202 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
203 no_edit=
204 shift
205 ;;
206 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
207 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
208 --reedit-message)
209 case "$#" in 1) usage ;; esac
210 shift
211 log_given=t$log_given
212 use_commit="$1"
213 no_edit=
214 shift
215 ;;
216 -C)
217 case "$#" in 1) usage ;; esac
218 shift
219 log_given=t$log_given
220 use_commit="$1"
221 no_edit=t
222 shift
223 ;;
224 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
225 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
226 --reuse-message=*)
227 log_given=t$log_given
228 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
229 no_edit=t
230 shift
231 ;;
232 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
233 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
234 case "$#" in 1) usage ;; esac
235 shift
236 log_given=t$log_given
237 use_commit="$1"
238 no_edit=t
239 shift
240 ;;
241 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
242 signoff=t
243 shift
244 ;;
ebd124c6
NP
245 -q|--q|--qu|--qui|--quie|--quiet)
246 quiet=t
247 shift
248 ;;
3de63c3f
MW
249 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
250 verbose=t
251 shift
252 ;;
253 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
254 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
255 --untracked-file|--untracked-files)
256 untracked_files=t
257 shift
258 ;;
259 --)
260 shift
261 break
262 ;;
263 -*)
264 usage
265 ;;
266 *)
267 break
268 ;;
269 esac
5fec3ef1 270done
cda8ab59 271case "$edit_flag" in t) no_edit= ;; esac
5fec3ef1 272
cf7bb589
JH
273################################################################
274# Sanity check options
275
b4019f04
JH
276case "$amend,$initial_commit" in
277t,t)
3de63c3f 278 die "You do not have anything to amend." ;;
b4019f04 279t,)
3de63c3f
MW
280 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
281 die "You are in the middle of a merge -- cannot amend."
282 fi ;;
b4019f04
JH
283esac
284
0c091296
JH
285case "$log_given" in
286tt*)
3de63c3f 287 die "Only one of -c/-C/-F can be used." ;;
6891281c 288*tm*|*mt*)
3de63c3f 289 die "Option -m cannot be combined with -c/-C/-F." ;;
0c091296
JH
290esac
291
6a74642c
JH
292case "$#,$also,$only,$amend" in
293*,t,t,*)
3de63c3f 294 die "Only one of --include/--only can be used." ;;
6a74642c 2950,t,,* | 0,,t,)
3de63c3f 296 die "No paths with --include/--only does not make sense." ;;
6a74642c 2970,,t,t)
3de63c3f 298 only_include_assumed="# Clever... amending the last one with dirty index." ;;
6a74642c 2990,,,*)
3de63c3f 300 ;;
6a74642c 301*,,,*)
3de63c3f
MW
302 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
303 also=
304 ;;
5a798fb5
JH
305esac
306unset only
cf7bb589
JH
307case "$all,$also,$#" in
308t,t,*)
309 die "Cannot use -a and -i at the same time." ;;
310t,,[1-9]*)
311 die "Paths with -a does not make sense." ;;
312,t,0)
313 die "No paths with -i does not make sense." ;;
314esac
315
316################################################################
317# Prepare index to have a tree to be committed
5a798fb5 318
130fcca6 319TOP=`git-rev-parse --show-cdup`
5a798fb5
JH
320if test -z "$TOP"
321then
322 TOP=./
323fi
130fcca6
JH
324
325case "$all,$also" in
130fcca6 326t,)
5a798fb5 327 save_index &&
130fcca6 328 (
5a798fb5 329 cd "$TOP"
cf7bb589
JH
330 GIT_INDEX_FILE="$NEXT_INDEX"
331 export GIT_INDEX_FILE
130fcca6
JH
332 git-diff-files --name-only -z |
333 git-update-index --remove -z --stdin
334 )
6a0049c0 335 ;;
130fcca6 336,t)
5a798fb5 337 save_index &&
bba319b5
JH
338 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
339
130fcca6 340 git-diff-files --name-only -z -- "$@" |
cf7bb589
JH
341 (
342 cd "$TOP"
343 GIT_INDEX_FILE="$NEXT_INDEX"
344 export GIT_INDEX_FILE
345 git-update-index --remove -z --stdin
346 )
22cff6a5 347 ;;
130fcca6
JH
348,)
349 case "$#" in
350 0)
3de63c3f 351 ;; # commit as-is
130fcca6 352 *)
3de63c3f
MW
353 if test -f "$GIT_DIR/MERGE_HEAD"
354 then
355 refuse_partial "Cannot do a partial commit during a merge."
356 fi
357 TMP_INDEX="$GIT_DIR/tmp-index$$"
3de63c3f 358 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
cf7bb589 359
158d0577 360 # Build a temporary index and update the real index
3de63c3f
MW
361 # the same way.
362 if test -z "$initial_commit"
363 then
364 cp "$THIS_INDEX" "$TMP_INDEX"
365 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
366 else
367 rm -f "$TMP_INDEX"
368 fi || exit
cf7bb589 369
3de63c3f
MW
370 echo "$commit_only" |
371 GIT_INDEX_FILE="$TMP_INDEX" \
372 git-update-index --add --remove --stdin &&
cf7bb589 373
3de63c3f
MW
374 save_index &&
375 echo "$commit_only" |
376 (
377 GIT_INDEX_FILE="$NEXT_INDEX"
378 export GIT_INDEX_FILE
379 git-update-index --remove --stdin
380 ) || exit
381 ;;
130fcca6
JH
382 esac
383 ;;
384esac
385
cf7bb589
JH
386################################################################
387# If we do as-is commit, the index file will be THIS_INDEX,
388# otherwise NEXT_INDEX after we make this commit. We leave
389# the index as is if we abort.
130fcca6 390
cf7bb589 391if test -f "$NEXT_INDEX"
130fcca6 392then
cf7bb589
JH
393 USE_INDEX="$NEXT_INDEX"
394else
395 USE_INDEX="$THIS_INDEX"
130fcca6
JH
396fi
397
cf7bb589 398GIT_INDEX_FILE="$USE_INDEX" \
3de63c3f 399 git-update-index -q $unmerged_ok_if_status --refresh || exit
cf7bb589
JH
400
401################################################################
402# If the request is status, just show it and exit.
403
404case "$0" in
405*status)
406 run_status
407 exit $?
408esac
409
410################################################################
411# Grab commit message, write out tree and make commit.
412
130fcca6
JH
413if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
414then
415 if test "$TMP_INDEX"
416 then
417 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
418 else
cf7bb589 419 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
130fcca6
JH
420 fi || exit
421fi
0cfe1d30 422
aba2da13
JH
423if test "$log_message" != ''
424then
425 echo "$log_message"
426elif test "$logfile" != ""
427then
428 if test "$logfile" = -
429 then
430 test -t 0 &&
431 echo >&2 "(reading log message from standard input)"
432 cat
433 else
434 cat <"$logfile"
435 fi
436elif test "$use_commit" != ""
437then
438 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
a9cb3c6e 439elif test -f "$GIT_DIR/MERGE_MSG"
e5215804
JH
440then
441 cat "$GIT_DIR/MERGE_MSG"
7d0c6887
JH
442elif test -f "$GIT_DIR/SQUASH_MSG"
443then
444 cat "$GIT_DIR/SQUASH_MSG"
f8e2c54c 445fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
446
447case "$signoff" in
448t)
f6413391
JH
449 {
450 echo
451 git-var GIT_COMMITTER_IDENT | sed -e '
452 s/>.*/>/
453 s/^/Signed-off-by: /
454 '
f8e2c54c 455 } >>"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
456 ;;
457esac
458
475443c8 459if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
2d569933 460 echo "#"
b6ae5409 461 echo "# It looks like you may be committing a MERGE."
2d569933
JH
462 echo "# If this is not correct, please remove the file"
463 echo "# $GIT_DIR/MERGE_HEAD"
464 echo "# and try again"
465 echo "#"
f8e2c54c 466fi >>"$GIT_DIR"/COMMIT_EDITMSG
aba2da13 467
130fcca6 468# Author
5a798fb5
JH
469if test '' != "$force_author"
470then
f327dbce
MW
471 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
472 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
5a798fb5
JH
473 test '' != "$GIT_AUTHOR_NAME" &&
474 test '' != "$GIT_AUTHOR_EMAIL" ||
82e5a82f 475 die "malformed --author parameter"
5a798fb5
JH
476 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
477elif test '' != "$use_commit"
130fcca6
JH
478then
479 pick_author_script='
480 /^author /{
481 s/'\''/'\''\\'\'\''/g
482 h
483 s/^author \([^<]*\) <[^>]*> .*$/\1/
484 s/'\''/'\''\'\'\''/g
485 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
486
487 g
488 s/^author [^<]* <\([^>]*\)> .*$/\1/
489 s/'\''/'\''\'\'\''/g
490 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
491
492 g
493 s/^author [^<]* <[^>]*> \(.*\)$/\1/
494 s/'\''/'\''\'\'\''/g
495 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
496
497 q
498 }
499 '
500 set_author_env=`git-cat-file commit "$use_commit" |
501 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
502 eval "$set_author_env"
503 export GIT_AUTHOR_NAME
504 export GIT_AUTHOR_EMAIL
505 export GIT_AUTHOR_DATE
130fcca6
JH
506fi
507
96069cf0 508PARENTS="-p HEAD"
130fcca6 509if test -z "$initial_commit"
8098a178 510then
a3a733e6 511 rloga='commit'
5fec3ef1 512 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
a3a733e6 513 rloga='commit (merge)'
91063bbc 514 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
b4019f04 515 elif test -n "$amend"; then
a3a733e6 516 rloga='commit (amend)'
b4019f04
JH
517 PARENTS=$(git-cat-file commit HEAD |
518 sed -n -e '/^$/q' -e 's/^parent /-p /p')
aba2da13 519 fi
cede7526 520 current="$(git-rev-parse --verify HEAD)"
8098a178
JH
521else
522 if [ -z "$(git-ls-files)" ]; then
5a798fb5 523 echo >&2 Nothing to commit
8098a178
JH
524 exit 1
525 fi
526 PARENTS=""
a3a733e6 527 rloga='commit (initial)'
cede7526 528 current=''
96069cf0 529fi
130fcca6 530
475443c8
JH
531if test -z "$no_edit"
532then
533 {
88a15314
MW
534 echo ""
535 echo "# Please enter the commit message for your changes."
536 echo "# (Comment lines starting with '#' will not be included)"
475443c8
JH
537 test -z "$only_include_assumed" || echo "$only_include_assumed"
538 run_status
539 } >>"$GIT_DIR"/COMMIT_EDITMSG
540else
541 # we need to check if there is anything to commit
542 run_status >/dev/null
543fi
8588452c 544if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
a3e870f2 545then
7d0c6887 546 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
5a798fb5 547 run_status
a3e870f2
LT
548 exit 1
549fi
475443c8 550
0c091296 551case "$no_edit" in
5fec3ef1 552'')
7334f06c
JH
553 case "${VISUAL:-$EDITOR},$TERM" in
554 ,dumb)
555 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
556 echo >&2 "Please supply the commit log message using either"
557 echo >&2 "-m or -F option. A boilerplate log message has"
558 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
130fcca6
JH
559 exit 1
560 ;;
7334f06c 561 esac
ec4e69c0
SE
562 git-var GIT_AUTHOR_IDENT > /dev/null || die
563 git-var GIT_COMMITTER_IDENT > /dev/null || die
f8e2c54c 564 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
5fec3ef1
JH
565 ;;
566esac
89e2c5f1
JH
567
568case "$verify" in
569t)
570 if test -x "$GIT_DIR"/hooks/commit-msg
571 then
f8e2c54c 572 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
89e2c5f1
JH
573 fi
574esac
575
29f4ad86
YD
576if test -z "$no_edit"
577then
578 sed -e '
579 /^diff --git a\/.*/{
580 s///
581 q
582 }
583 /^#/d
584 ' "$GIT_DIR"/COMMIT_EDITMSG
585else
586 cat "$GIT_DIR"/COMMIT_EDITMSG
587fi |
cf7bb589 588git-stripspace >"$GIT_DIR"/COMMIT_MSG
f8e2c54c
SB
589
590if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
591 git-stripspace |
592 wc -l` &&
593 test 0 -lt $cnt
0c091296 594then
130fcca6
JH
595 if test -z "$TMP_INDEX"
596 then
cf7bb589 597 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
130fcca6
JH
598 else
599 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
600 rm -f "$TMP_INDEX"
601 fi &&
f8e2c54c 602 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
67644a4d 603 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
cede7526 604 git-update-ref -m "$rloga: $rlogm" HEAD $commit "$current" &&
a9cb3c6e 605 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
cf7bb589
JH
606 if test -f "$NEXT_INDEX"
607 then
608 mv "$NEXT_INDEX" "$THIS_INDEX"
609 else
610 : ;# happy
611 fi
0c091296
JH
612else
613 echo >&2 "* no commit message? aborting commit."
614 false
615fi
170241b7 616ret="$?"
7d0c6887 617rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
1536dd9c
JH
618if test -d "$GIT_DIR/rr-cache"
619then
620 git-rerere
621fi
89e2c5f1 622
ebd124c6 623if test "$ret" = 0
89e2c5f1 624then
ebd124c6
NP
625 if test -x "$GIT_DIR"/hooks/post-commit
626 then
627 "$GIT_DIR"/hooks/post-commit
628 fi
629 if test -z "$quiet"
630 then
631 echo "Created${initial_commit:+ initial} commit $commit"
632 git-diff-tree --shortstat --summary --root --no-commit-id HEAD
633 fi
89e2c5f1 634fi
61f5cb7f 635
170241b7 636exit "$ret"