3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
6 USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
10 git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11 branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
16 unmerged_ok_if_status=--unmerged ;;
19 unmerged_ok_if_status= ;;
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
28 THIS_INDEX="$GIT_DIR/index"
29 NEXT_INDEX="$GIT_DIR/next-index$$"
32 cp -p "$THIS_INDEX" "$NEXT_INDEX"
42 while read status name newname
49 M ) echo "# modified: $name";;
50 D*) echo "# deleted: $name";;
51 T ) echo "# typechange: $name";;
52 C*) echo "# copied: $name -> $newname";;
53 R*) echo "# renamed: $name -> $newname";;
54 A*) echo "# new file: $name";;
55 U ) echo "# unmerged: $name";;
58 printf '%s' "$trailer"
63 # If TMP_INDEX is defined, that means we are doing
64 # "--only" partial commit, and that index file is used
65 # to build the tree for the commit. Otherwise, if
66 # NEXT_INDEX exists, that is the index file used to
67 # make the commit. Otherwise we are using as-is commit
68 # so the regular index file is what we use to compare.
69 if test '' != "$TMP_INDEX"
71 GIT_INDEX_FILE="$TMP_INDEX"
73 elif test -f "$NEXT_INDEX"
75 GIT_INDEX_FILE="$NEXT_INDEX"
79 case "$status_only" in
83 git-runstatus ${color} \
84 ${verbose:+--verbose} \
86 ${untracked_files:+--untracked}
90 test -z "$TMP_INDEX" || {
91 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
96 ################################################################
97 # Command line argument parsing and sanity checking
113 only_include_assumed=
115 while case "$#" in 0) break;; esac
118 -F|--F|-f|--f|--fi|--fil|--file)
119 case "$#" in 1) usage ;; esac
122 log_given=t$log_given
128 log_given=t$log_given
129 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
132 --F=*|--f=*|--fi=*|--fil=*|--file=*)
134 log_given=t$log_given
135 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
142 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
143 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
146 --au|--aut|--auth|--autho|--author)
147 case "$#" in 1) usage ;; esac
152 -e|--e|--ed|--edi|--edit)
156 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
160 -o|--o|--on|--onl|--only)
164 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
165 case "$#" in 1) usage ;; esac
167 log_given=m$log_given
168 if test "$log_message" = ''
172 log_message="$log_message
180 log_given=m$log_given
181 if test "$log_message" = ''
183 log_message=`expr "z$1" : 'z-m\(.*\)'`
185 log_message="$log_message
187 `expr "z$1" : 'z-m\(.*\)'`"
192 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
193 log_given=m$log_given
194 if test "$log_message" = ''
196 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
198 log_message="$log_message
200 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
205 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
209 --a|--am|--ame|--amen|--amend)
211 log_given=t$log_given
216 case "$#" in 1) usage ;; esac
218 log_given=t$log_given
223 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
224 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
225 --reedit-messag=*|--reedit-message=*)
226 log_given=t$log_given
227 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
231 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
232 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
233 case "$#" in 1) usage ;; esac
235 log_given=t$log_given
241 case "$#" in 1) usage ;; esac
243 log_given=t$log_given
248 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
249 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
251 log_given=t$log_given
252 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
256 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
257 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
258 case "$#" in 1) usage ;; esac
260 log_given=t$log_given
265 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
269 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
273 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
274 --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
291 case "$edit_flag" in t) no_edit= ;; esac
293 ################################################################
294 # Sanity check options
296 case "$amend,$initial_commit" in
298 die "You do not have anything to amend." ;;
300 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
301 die "You are in the middle of a merge -- cannot amend."
307 die "Only one of -c/-C/-F can be used." ;;
309 die "Option -m cannot be combined with -c/-C/-F." ;;
312 case "$#,$also,$only,$amend" in
314 die "Only one of --include/--only can be used." ;;
316 die "No paths with --include/--only does not make sense." ;;
318 only_include_assumed="# Clever... amending the last one with dirty index." ;;
322 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
327 case "$all,$also,$#" in
329 die "Cannot use -a and -i at the same time." ;;
331 die "Paths with -a does not make sense." ;;
333 die "No paths with -i does not make sense." ;;
336 ################################################################
337 # Prepare index to have a tree to be committed
339 TOP=`git-rev-parse --show-cdup`
350 GIT_INDEX_FILE="$NEXT_INDEX"
351 export GIT_INDEX_FILE
352 git-diff-files --name-only -z |
353 git-update-index --remove -z --stdin
358 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
360 git-diff-files --name-only -z -- "$@" |
363 GIT_INDEX_FILE="$NEXT_INDEX"
364 export GIT_INDEX_FILE
365 git-update-index --remove -z --stdin
373 if test -f "$GIT_DIR/MERGE_HEAD"
375 refuse_partial "Cannot do a partial commit during a merge."
377 TMP_INDEX="$GIT_DIR/tmp-index$$"
378 if test -z "$initial_commit"
380 # make sure index is clean at the specified paths, or
381 # they are additions.
382 dirty_in_index=`git-diff-index --cached --name-status \
383 --diff-filter=DMTU HEAD -- "$@"`
384 test -z "$dirty_in_index" ||
385 refuse_partial "Different in index and the last commit:
388 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
390 # Build the temporary index and update the real index
392 if test -z "$initial_commit"
394 cp "$THIS_INDEX" "$TMP_INDEX"
395 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
400 echo "$commit_only" |
401 GIT_INDEX_FILE="$TMP_INDEX" \
402 git-update-index --add --remove --stdin &&
405 echo "$commit_only" |
407 GIT_INDEX_FILE="$NEXT_INDEX"
408 export GIT_INDEX_FILE
409 git-update-index --remove --stdin
416 ################################################################
417 # If we do as-is commit, the index file will be THIS_INDEX,
418 # otherwise NEXT_INDEX after we make this commit. We leave
419 # the index as is if we abort.
421 if test -f "$NEXT_INDEX"
423 USE_INDEX="$NEXT_INDEX"
425 USE_INDEX="$THIS_INDEX"
428 GIT_INDEX_FILE="$USE_INDEX" \
429 git-update-index -q $unmerged_ok_if_status --refresh || exit
431 ################################################################
432 # If the request is status, just show it and exit.
440 ################################################################
441 # Grab commit message, write out tree and make commit.
443 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
447 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
449 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
453 if test "$log_message" != ''
456 elif test "$logfile" != ""
458 if test "$logfile" = -
461 echo >&2 "(reading log message from standard input)"
466 elif test "$use_commit" != ""
468 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
469 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
471 cat "$GIT_DIR/MERGE_MSG"
472 elif test -f "$GIT_DIR/SQUASH_MSG"
474 cat "$GIT_DIR/SQUASH_MSG"
475 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
481 git-var GIT_COMMITTER_IDENT | sed -e '
485 } >>"$GIT_DIR"/COMMIT_EDITMSG
489 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
491 echo "# It looks like you may be committing a MERGE."
492 echo "# If this is not correct, please remove the file"
493 echo "# $GIT_DIR/MERGE_HEAD"
494 echo "# and try again"
496 fi >>"$GIT_DIR"/COMMIT_EDITMSG
499 if test '' != "$force_author"
501 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
502 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
503 test '' != "$GIT_AUTHOR_NAME" &&
504 test '' != "$GIT_AUTHOR_EMAIL" ||
505 die "malformed --author parameter"
506 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
507 elif test '' != "$use_commit"
511 s/'\''/'\''\\'\'\''/g
513 s/^author \([^<]*\) <[^>]*> .*$/\1/
515 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
518 s/^author [^<]* <\([^>]*\)> .*$/\1/
520 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
523 s/^author [^<]* <[^>]*> \(.*\)$/\1/
525 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
530 set_author_env=`git-cat-file commit "$use_commit" |
531 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
532 eval "$set_author_env"
533 export GIT_AUTHOR_NAME
534 export GIT_AUTHOR_EMAIL
535 export GIT_AUTHOR_DATE
539 if test -z "$initial_commit"
542 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
543 rloga='commit (merge)'
544 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
545 elif test -n "$amend"; then
546 rloga='commit (amend)'
547 PARENTS=$(git-cat-file commit HEAD |
548 sed -n -e '/^$/q' -e 's/^parent /-p /p')
550 current="$(git-rev-parse --verify HEAD)"
552 if [ -z "$(git-ls-files)" ]; then
553 echo >&2 Nothing to commit
557 rloga='commit (initial)'
561 if test -z "$no_edit"
565 echo "# Please enter the commit message for your changes."
566 echo "# (Comment lines starting with '#' will not be included)"
567 test -z "$only_include_assumed" || echo "$only_include_assumed"
569 } >>"$GIT_DIR"/COMMIT_EDITMSG
571 # we need to check if there is anything to commit
572 run_status >/dev/null
574 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
576 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
583 case "${VISUAL:-$EDITOR},$TERM" in
585 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
586 echo >&2 "Please supply the commit log message using either"
587 echo >&2 "-m or -F option. A boilerplate log message has"
588 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
592 git-var GIT_AUTHOR_IDENT > /dev/null || die
593 git-var GIT_COMMITTER_IDENT > /dev/null || die
594 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
600 if test -x "$GIT_DIR"/hooks/commit-msg
602 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
606 if test -z "$no_edit"
614 ' "$GIT_DIR"/COMMIT_EDITMSG
616 cat "$GIT_DIR"/COMMIT_EDITMSG
618 git-stripspace >"$GIT_DIR"/COMMIT_MSG
620 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
625 if test -z "$TMP_INDEX"
627 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
629 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
632 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
633 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
634 git-update-ref -m "$rloga: $rlogm" HEAD $commit "$current" &&
635 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
636 if test -f "$NEXT_INDEX"
638 mv "$NEXT_INDEX" "$THIS_INDEX"
643 echo >&2 "* no commit message? aborting commit."
647 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
648 if test -d "$GIT_DIR/rr-cache"
653 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
655 "$GIT_DIR"/hooks/post-commit