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>) [--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 "$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"
64 # We always show status for the whole tree.
67 IS_INITIAL="$initial_commit"
71 # If we are amending the initial commit, there
73 if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1
83 # If TMP_INDEX is defined, that means we are doing
84 # "--only" partial commit, and that index file is used
85 # to build the tree for the commit. Otherwise, if
86 # NEXT_INDEX exists, that is the index file used to
87 # make the commit. Otherwise we are using as-is commit
88 # so the regular index file is what we use to compare.
89 if test '' != "$TMP_INDEX"
91 GIT_INDEX_FILE="$TMP_INDEX"
93 elif test -f "$NEXT_INDEX"
95 GIT_INDEX_FILE="$NEXT_INDEX"
100 refs/heads/master) ;;
101 *) echo "# On branch $branch" ;;
104 if test -z "$IS_INITIAL"
106 git-diff-index -M --cached --name-status \
107 --diff-filter=MDTCRA $REFERENCE |
112 report "Updated but not checked in" "will commit"
124 report "Updated but not checked in" "will commit"
129 git-diff-files --name-status |
134 report "Changed but not updated" \
135 "use git-update-index to mark for commit"
137 if test -f "$GIT_DIR/info/exclude"
139 git-ls-files -z --others --directory \
140 --exclude-from="$GIT_DIR/info/exclude" \
141 --exclude-per-directory=.gitignore
143 git-ls-files -z --others --directory \
144 --exclude-per-directory=.gitignore
155 print "#\n# Untracked files:\n";
156 print "# (use \"git add\" to add to commit)\n";
164 if test -n "$verbose" -a -z "$IS_INITIAL"
166 git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
168 case "$committable" in
172 echo "# No changes" ;;
174 echo "nothing to commit" ;;
183 test -z "$TMP_INDEX" || {
184 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
189 ################################################################
190 # Command line argument parsing and sanity checking
205 only_include_assumed=
206 while case "$#" in 0) break;; esac
209 -F|--F|-f|--f|--fi|--fil|--file)
210 case "$#" in 1) usage ;; esac
213 log_given=t$log_given
219 log_given=t$log_given
220 logfile=`expr "$1" : '-[Ff]\(.*\)'`
223 --F=*|--f=*|--fi=*|--fil=*|--file=*)
225 log_given=t$log_given
226 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
233 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
234 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
237 --au|--aut|--auth|--autho|--author)
238 case "$#" in 1) usage ;; esac
243 -e|--e|--ed|--edi|--edit)
247 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
251 -o|--o|--on|--onl|--only)
255 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
256 case "$#" in 1) usage ;; esac
258 log_given=t$log_given
264 log_given=t$log_given
265 log_message=`expr "$1" : '-m\(.*\)'`
269 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
270 log_given=t$log_given
271 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
275 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
279 --a|--am|--ame|--amen|--amend)
281 log_given=t$log_given
286 case "$#" in 1) usage ;; esac
288 log_given=t$log_given
293 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
294 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
295 --reedit-messag=*|--reedit-message=*)
296 log_given=t$log_given
297 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
301 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
302 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
303 case "$#" in 1) usage ;; esac
305 log_given=t$log_given
311 case "$#" in 1) usage ;; esac
313 log_given=t$log_given
318 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
319 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
321 log_given=t$log_given
322 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
326 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
327 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
328 case "$#" in 1) usage ;; esac
330 log_given=t$log_given
335 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
339 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
356 ################################################################
357 # Sanity check options
359 case "$amend,$initial_commit" in
361 die "You do not have anything to amend." ;;
363 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
364 die "You are in the middle of a merge -- cannot amend."
370 die "Only one of -c/-C/-F/-m can be used." ;;
373 case "$#,$also,$only,$amend" in
375 die "Only one of --include/--only can be used." ;;
377 die "No paths with --include/--only does not make sense." ;;
379 only_include_assumed="# Clever... amending the last one with dirty index." ;;
383 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
388 case "$all,$also,$#" in
390 die "Cannot use -a and -i at the same time." ;;
392 die "Paths with -a does not make sense." ;;
394 die "No paths with -i does not make sense." ;;
397 ################################################################
398 # Prepare index to have a tree to be committed
400 TOP=`git-rev-parse --show-cdup`
411 GIT_INDEX_FILE="$NEXT_INDEX"
412 export GIT_INDEX_FILE
413 git-diff-files --name-only -z |
414 git-update-index --remove -z --stdin
419 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
421 git-diff-files --name-only -z -- "$@" |
424 GIT_INDEX_FILE="$NEXT_INDEX"
425 export GIT_INDEX_FILE
426 git-update-index --remove -z --stdin
434 if test -f "$GIT_DIR/MERGE_HEAD"
436 refuse_partial "Cannot do a partial commit during a merge."
438 TMP_INDEX="$GIT_DIR/tmp-index$$"
439 if test -z "$initial_commit"
441 # make sure index is clean at the specified paths, or
442 # they are additions.
443 dirty_in_index=`git-diff-index --cached --name-status \
444 --diff-filter=DMTU HEAD -- "$@"`
445 test -z "$dirty_in_index" ||
446 refuse_partial "Different in index and the last commit:
449 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
451 # Build the temporary index and update the real index
453 if test -z "$initial_commit"
455 cp "$THIS_INDEX" "$TMP_INDEX"
456 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
461 echo "$commit_only" |
462 GIT_INDEX_FILE="$TMP_INDEX" \
463 git-update-index --add --remove --stdin &&
466 echo "$commit_only" |
468 GIT_INDEX_FILE="$NEXT_INDEX"
469 export GIT_INDEX_FILE
470 git-update-index --remove --stdin
477 ################################################################
478 # If we do as-is commit, the index file will be THIS_INDEX,
479 # otherwise NEXT_INDEX after we make this commit. We leave
480 # the index as is if we abort.
482 if test -f "$NEXT_INDEX"
484 USE_INDEX="$NEXT_INDEX"
486 USE_INDEX="$THIS_INDEX"
489 GIT_INDEX_FILE="$USE_INDEX" \
490 git-update-index -q $unmerged_ok_if_status --refresh || exit
492 ################################################################
493 # If the request is status, just show it and exit.
501 ################################################################
502 # Grab commit message, write out tree and make commit.
504 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
508 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
510 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
514 if test "$log_message" != ''
517 elif test "$logfile" != ""
519 if test "$logfile" = -
522 echo >&2 "(reading log message from standard input)"
527 elif test "$use_commit" != ""
529 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
530 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
532 cat "$GIT_DIR/MERGE_MSG"
533 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
539 git-var GIT_COMMITTER_IDENT | sed -e '
543 } >>"$GIT_DIR"/COMMIT_EDITMSG
547 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
549 echo "# It looks like you may be committing a MERGE."
550 echo "# If this is not correct, please remove the file"
551 echo "# $GIT_DIR/MERGE_HEAD"
552 echo "# and try again"
554 fi >>"$GIT_DIR"/COMMIT_EDITMSG
557 if test '' != "$force_author"
559 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
560 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
561 test '' != "$GIT_AUTHOR_NAME" &&
562 test '' != "$GIT_AUTHOR_EMAIL" ||
563 die "malformatted --author parameter"
564 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
565 elif test '' != "$use_commit"
569 s/'\''/'\''\\'\'\''/g
571 s/^author \([^<]*\) <[^>]*> .*$/\1/
573 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
576 s/^author [^<]* <\([^>]*\)> .*$/\1/
578 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
581 s/^author [^<]* <[^>]*> \(.*\)$/\1/
583 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
588 set_author_env=`git-cat-file commit "$use_commit" |
589 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
590 eval "$set_author_env"
591 export GIT_AUTHOR_NAME
592 export GIT_AUTHOR_EMAIL
593 export GIT_AUTHOR_DATE
597 if test -z "$initial_commit"
599 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
600 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
601 elif test -n "$amend"; then
602 PARENTS=$(git-cat-file commit HEAD |
603 sed -n -e '/^$/q' -e 's/^parent /-p /p')
605 current=$(git-rev-parse --verify HEAD)
607 if [ -z "$(git-ls-files)" ]; then
608 echo >&2 Nothing to commit
615 if test -z "$no_edit"
618 test -z "$only_include_assumed" || echo "$only_include_assumed"
620 } >>"$GIT_DIR"/COMMIT_EDITMSG
622 # we need to check if there is anything to commit
623 run_status >/dev/null
625 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
627 rm -f "$GIT_DIR/COMMIT_EDITMSG"
634 case "${VISUAL:-$EDITOR},$TERM" in
636 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
637 echo >&2 "Please supply the commit log message using either"
638 echo >&2 "-m or -F option. A boilerplate log message has"
639 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
643 git-var GIT_AUTHOR_IDENT > /dev/null || die
644 git-var GIT_COMMITTER_IDENT > /dev/null || die
645 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
651 if test -x "$GIT_DIR"/hooks/commit-msg
653 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
663 ' "$GIT_DIR"/COMMIT_EDITMSG |
664 git-stripspace >"$GIT_DIR"/COMMIT_MSG
666 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
671 if test -z "$TMP_INDEX"
673 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
675 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
678 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
679 git-update-ref HEAD $commit $current &&
680 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
681 if test -f "$NEXT_INDEX"
683 mv "$NEXT_INDEX" "$THIS_INDEX"
688 echo >&2 "* no commit message? aborting commit."
692 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
693 if test -d "$GIT_DIR/rr-cache"
698 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
700 "$GIT_DIR"/hooks/post-commit