git-stash: make "save" the default action again.
[git/mingw/4msysgit/kblees.git] / git-commit.sh
blobf866f957c44de4cc72542f34ec1df4ead42256c3
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
6 USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [[-i | -o] <path>...]'
7 SUBDIRECTORY_OK=Yes
8 . git-sh-setup
9 require_work_tree
11 git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
13 case "$0" in
14 *status)
15 status_only=t
17 *commit)
18 status_only=
20 esac
22 refuse_partial () {
23 echo >&2 "$1"
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
28 THIS_INDEX="$GIT_DIR/index"
29 NEXT_INDEX="$GIT_DIR/next-index$$"
30 rm -f "$NEXT_INDEX"
31 save_index () {
32 cp -p "$THIS_INDEX" "$NEXT_INDEX"
35 run_status () {
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
44 GIT_INDEX_FILE="$TMP_INDEX"
45 export GIT_INDEX_FILE
46 elif test -f "$NEXT_INDEX"
47 then
48 GIT_INDEX_FILE="$NEXT_INDEX"
49 export GIT_INDEX_FILE
52 case "$status_only" in
53 t) color= ;;
54 *) color=--nocolor ;;
55 esac
56 git runstatus ${color} \
57 ${verbose:+--verbose} \
58 ${amend:+--amend} \
59 ${untracked_files:+--untracked}
62 trap '
63 test -z "$TMP_INDEX" || {
64 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
66 rm -f "$NEXT_INDEX"
67 ' 0
69 ################################################################
70 # Command line argument parsing and sanity checking
72 all=
73 also=
74 interactive=
75 only=
76 logfile=
77 use_commit=
78 amend=
79 edit_flag=
80 no_edit=
81 log_given=
82 log_message=
83 verify=t
84 quiet=
85 verbose=
86 signoff=
87 force_author=
88 only_include_assumed=
89 untracked_files=
90 while case "$#" in 0) break;; esac
92 case "$1" in
93 -F|--F|-f|--f|--fi|--fil|--file)
94 case "$#" in 1) usage ;; esac
95 shift
96 no_edit=t
97 log_given=t$log_given
98 logfile="$1"
99 shift
101 -F*|-f*)
102 no_edit=t
103 log_given=t$log_given
104 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
105 shift
107 --F=*|--f=*|--fi=*|--fil=*|--file=*)
108 no_edit=t
109 log_given=t$log_given
110 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
111 shift
113 -a|--a|--al|--all)
114 all=t
115 shift
117 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
118 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
119 shift
121 --au|--aut|--auth|--autho|--author)
122 case "$#" in 1) usage ;; esac
123 shift
124 force_author="$1"
125 shift
127 -e|--e|--ed|--edi|--edit)
128 edit_flag=t
129 shift
131 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
132 also=t
133 shift
135 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
136 --interactiv|--interactive)
137 interactive=t
138 shift
140 -o|--o|--on|--onl|--only)
141 only=t
142 shift
144 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
145 case "$#" in 1) usage ;; esac
146 shift
147 log_given=m$log_given
148 if test "$log_message" = ''
149 then
150 log_message="$1"
151 else
152 log_message="$log_message
156 no_edit=t
157 shift
159 -m*)
160 log_given=m$log_given
161 if test "$log_message" = ''
162 then
163 log_message=`expr "z$1" : 'z-m\(.*\)'`
164 else
165 log_message="$log_message
167 `expr "z$1" : 'z-m\(.*\)'`"
169 no_edit=t
170 shift
172 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
173 log_given=m$log_given
174 if test "$log_message" = ''
175 then
176 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
177 else
178 log_message="$log_message
180 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
182 no_edit=t
183 shift
185 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
186 --no-verify)
187 verify=
188 shift
190 --a|--am|--ame|--amen|--amend)
191 amend=t
192 log_given=t$log_given
193 use_commit=HEAD
194 shift
197 case "$#" in 1) usage ;; esac
198 shift
199 log_given=t$log_given
200 use_commit="$1"
201 no_edit=
202 shift
204 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
205 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
206 --reedit-messag=*|--reedit-message=*)
207 log_given=t$log_given
208 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
209 no_edit=
210 shift
212 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
213 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
214 --reedit-message)
215 case "$#" in 1) usage ;; esac
216 shift
217 log_given=t$log_given
218 use_commit="$1"
219 no_edit=
220 shift
223 case "$#" in 1) usage ;; esac
224 shift
225 log_given=t$log_given
226 use_commit="$1"
227 no_edit=t
228 shift
230 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
231 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
232 --reuse-message=*)
233 log_given=t$log_given
234 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
235 no_edit=t
236 shift
238 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
239 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
240 case "$#" in 1) usage ;; esac
241 shift
242 log_given=t$log_given
243 use_commit="$1"
244 no_edit=t
245 shift
247 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
248 signoff=t
249 shift
251 -q|--q|--qu|--qui|--quie|--quiet)
252 quiet=t
253 shift
255 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
256 verbose=t
257 shift
259 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
260 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
261 --untracked-file|--untracked-files)
262 untracked_files=t
263 shift
266 shift
267 break
270 usage
273 break
275 esac
276 done
277 case "$edit_flag" in t) no_edit= ;; esac
279 ################################################################
280 # Sanity check options
282 case "$amend,$initial_commit" in
283 t,t)
284 die "You do not have anything to amend." ;;
286 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
287 die "You are in the middle of a merge -- cannot amend."
288 fi ;;
289 esac
291 case "$log_given" in
292 tt*)
293 die "Only one of -c/-C/-F/--amend can be used." ;;
294 *tm*|*mt*)
295 die "Option -m cannot be combined with -c/-C/-F/--amend." ;;
296 esac
298 case "$#,$also,$only,$amend" in
299 *,t,t,*)
300 die "Only one of --include/--only can be used." ;;
301 0,t,,* | 0,,t,)
302 die "No paths with --include/--only does not make sense." ;;
303 0,,t,t)
304 only_include_assumed="# Clever... amending the last one with dirty index." ;;
305 0,,,*)
307 *,,,*)
308 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
309 also=
311 esac
312 unset only
313 case "$all,$interactive,$also,$#" in
314 *t,*t,*)
315 die "Cannot use -a, --interactive or -i at the same time." ;;
316 t,,[1-9]*)
317 die "Paths with -a does not make sense." ;;
318 ,t,[1-9]*)
319 die "Paths with --interactive does not make sense." ;;
320 ,,t,0)
321 die "No paths with -i does not make sense." ;;
322 esac
324 ################################################################
325 # Prepare index to have a tree to be committed
327 case "$all,$also" in
329 if test ! -f "$THIS_INDEX"
330 then
331 die 'nothing to commit (use "git add file1 file2" to include for commit)'
333 save_index &&
335 cd_to_toplevel &&
336 GIT_INDEX_FILE="$NEXT_INDEX" &&
337 export GIT_INDEX_FILE &&
338 git diff-files --name-only -z |
339 git update-index --remove -z --stdin
340 ) || exit
343 save_index &&
344 git ls-files --error-unmatch -- "$@" >/dev/null || exit
346 git diff-files --name-only -z -- "$@" |
348 cd_to_toplevel &&
349 GIT_INDEX_FILE="$NEXT_INDEX" &&
350 export GIT_INDEX_FILE &&
351 git update-index --remove -z --stdin
352 ) || exit
355 if test "$interactive" = t; then
356 git add --interactive || exit
358 case "$#" in
360 ;; # commit as-is
362 if test -f "$GIT_DIR/MERGE_HEAD"
363 then
364 refuse_partial "Cannot do a partial commit during a merge."
366 TMP_INDEX="$GIT_DIR/tmp-index$$"
367 commit_only=`git ls-files --error-unmatch -- "$@"` || exit
369 # Build a temporary index and update the real index
370 # the same way.
371 if test -z "$initial_commit"
372 then
373 GIT_INDEX_FILE="$THIS_INDEX" \
374 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
375 else
376 rm -f "$TMP_INDEX"
377 fi || exit
379 printf '%s\n' "$commit_only" |
380 GIT_INDEX_FILE="$TMP_INDEX" \
381 git update-index --add --remove --stdin &&
383 save_index &&
384 printf '%s\n' "$commit_only" |
386 GIT_INDEX_FILE="$NEXT_INDEX"
387 export GIT_INDEX_FILE
388 git update-index --remove --stdin
389 ) || exit
391 esac
393 esac
395 ################################################################
396 # If we do as-is commit, the index file will be THIS_INDEX,
397 # otherwise NEXT_INDEX after we make this commit. We leave
398 # the index as is if we abort.
400 if test -f "$NEXT_INDEX"
401 then
402 USE_INDEX="$NEXT_INDEX"
403 else
404 USE_INDEX="$THIS_INDEX"
407 case "$status_only" in
409 # This will silently fail in a read-only repository, which is
410 # what we want.
411 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
412 run_status
413 exit $?
416 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
418 esac
420 ################################################################
421 # Grab commit message, write out tree and make commit.
423 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
424 then
425 if test "$TMP_INDEX"
426 then
427 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
428 else
429 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
430 fi || exit
433 if test "$log_message" != ''
434 then
435 printf '%s\n' "$log_message"
436 elif test "$logfile" != ""
437 then
438 if test "$logfile" = -
439 then
440 test -t 0 &&
441 echo >&2 "(reading log message from standard input)"
443 else
444 cat <"$logfile"
446 elif test "$use_commit" != ""
447 then
448 encoding=$(git config i18n.commitencoding || echo UTF-8)
449 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
450 sed -e '1,/^$/d' -e 's/^ //'
451 elif test -f "$GIT_DIR/MERGE_MSG"
452 then
453 cat "$GIT_DIR/MERGE_MSG"
454 elif test -f "$GIT_DIR/SQUASH_MSG"
455 then
456 cat "$GIT_DIR/SQUASH_MSG"
457 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
459 case "$signoff" in
461 need_blank_before_signoff=
462 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
463 grep 'Signed-off-by:' >/dev/null || need_blank_before_signoff=yes
465 test -z "$need_blank_before_signoff" || echo
466 git-var GIT_COMMITTER_IDENT | sed -e '
467 s/>.*/>/
468 s/^/Signed-off-by: /
470 } >>"$GIT_DIR"/COMMIT_EDITMSG
472 esac
474 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
475 echo "#"
476 echo "# It looks like you may be committing a MERGE."
477 echo "# If this is not correct, please remove the file"
478 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
479 echo "# and try again"
480 echo "#"
481 fi >>"$GIT_DIR"/COMMIT_EDITMSG
483 # Author
484 if test '' != "$use_commit"
485 then
486 eval "$(get_author_ident_from_commit "$use_commit")"
487 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
489 if test '' != "$force_author"
490 then
491 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
492 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
493 test '' != "$GIT_AUTHOR_NAME" &&
494 test '' != "$GIT_AUTHOR_EMAIL" ||
495 die "malformed --author parameter"
496 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
499 PARENTS="-p HEAD"
500 if test -z "$initial_commit"
501 then
502 rloga='commit'
503 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
504 rloga='commit (merge)'
505 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
506 elif test -n "$amend"; then
507 rloga='commit (amend)'
508 PARENTS=$(git cat-file commit HEAD |
509 sed -n -e '/^$/q' -e 's/^parent /-p /p')
511 current="$(git rev-parse --verify HEAD)"
512 else
513 if [ -z "$(git ls-files)" ]; then
514 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
515 exit 1
517 PARENTS=""
518 rloga='commit (initial)'
519 current=''
521 set_reflog_action "$rloga"
523 if test -z "$no_edit"
524 then
526 echo ""
527 echo "# Please enter the commit message for your changes."
528 echo "# (Comment lines starting with '#' will not be included)"
529 test -z "$only_include_assumed" || echo "$only_include_assumed"
530 run_status
531 } >>"$GIT_DIR"/COMMIT_EDITMSG
532 else
533 # we need to check if there is anything to commit
534 run_status >/dev/null
536 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
537 then
538 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
539 run_status
540 exit 1
543 case "$no_edit" in
545 case "${VISUAL:-$EDITOR},$TERM" in
546 ,dumb)
547 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
548 echo >&2 "Please supply the commit log message using either"
549 echo >&2 "-m or -F option. A boilerplate log message has"
550 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
551 exit 1
553 esac
554 git-var GIT_AUTHOR_IDENT > /dev/null || die
555 git-var GIT_COMMITTER_IDENT > /dev/null || die
556 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
558 esac
560 case "$verify" in
562 if test -x "$GIT_DIR"/hooks/commit-msg
563 then
564 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
566 esac
568 if test -z "$no_edit"
569 then
570 sed -e '
571 /^diff --git a\/.*/{
572 s///
575 /^#/d
576 ' "$GIT_DIR"/COMMIT_EDITMSG
577 else
578 cat "$GIT_DIR"/COMMIT_EDITMSG
579 fi |
580 git stripspace >"$GIT_DIR"/COMMIT_MSG
582 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
583 git stripspace |
584 wc -l` &&
585 test 0 -lt $cnt
586 then
587 if test -z "$TMP_INDEX"
588 then
589 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
590 else
591 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
592 rm -f "$TMP_INDEX"
593 fi &&
594 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git commit-tree $tree $PARENTS) &&
595 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
596 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
597 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
598 if test -f "$NEXT_INDEX"
599 then
600 mv "$NEXT_INDEX" "$THIS_INDEX"
601 else
602 : ;# happy
604 else
605 echo >&2 "* no commit message? aborting commit."
606 false
608 ret="$?"
609 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
611 cd_to_toplevel
613 if test -d "$GIT_DIR/rr-cache"
614 then
615 git rerere
618 if test "$ret" = 0
619 then
620 if test -x "$GIT_DIR"/hooks/post-commit
621 then
622 "$GIT_DIR"/hooks/post-commit
624 if test -z "$quiet"
625 then
626 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
627 --summary --root HEAD --`
628 echo "Created${initial_commit:+ initial} commit $commit"
632 exit "$ret"