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>] [-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 # If TMP_INDEX is defined, that means we are doing
68 # "--only" partial commit, and that index file is used
69 # to build the tree for the commit. Otherwise, if
70 # NEXT_INDEX exists, that is the index file used to
71 # make the commit. Otherwise we are using as-is commit
72 # so the regular index file is what we use to compare.
73 if test '' != "$TMP_INDEX"
75 GIT_INDEX_FILE="$TMP_INDEX"
77 elif test -f "$NEXT_INDEX"
79 GIT_INDEX_FILE="$NEXT_INDEX"
85 *) echo "# On branch $branch" ;;
88 if test -z "$initial_commit"
90 git-diff-index -M --cached --name-status \
91 --diff-filter=MDTCRA HEAD |
96 report "Updated but not checked in" "will commit"
108 report "Updated but not checked in" "will commit"
113 git-diff-files --name-status |
118 report "Changed but not updated" \
119 "use git-update-index to mark for commit"
121 if test -f "$GIT_DIR/info/exclude"
123 git-ls-files -z --others --directory \
124 --exclude-from="$GIT_DIR/info/exclude" \
125 --exclude-per-directory=.gitignore
127 git-ls-files -z --others --directory \
128 --exclude-per-directory=.gitignore
139 print "#\n# Untracked files:\n";
140 print "# (use \"git add\" to add to commit)\n";
148 if test -n "$verbose"
150 git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD
152 case "$committable" in
154 echo "nothing to commit"
162 test -z "$TMP_INDEX" || {
163 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
168 ################################################################
169 # Command line argument parsing and sanity checking
183 only_include_assumed=
184 while case "$#" in 0) break;; esac
187 -F|--F|-f|--f|--fi|--fil|--file)
188 case "$#" in 1) usage ;; esac
191 log_given=t$log_given
197 log_given=t$log_given
198 logfile=`expr "$1" : '-[Ff]\(.*\)'`
201 --F=*|--f=*|--fi=*|--fil=*|--file=*)
203 log_given=t$log_given
204 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
211 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
212 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
215 --au|--aut|--auth|--autho|--author)
216 case "$#" in 1) usage ;; esac
221 -e|--e|--ed|--edi|--edit)
225 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
229 -o|--o|--on|--onl|--only)
233 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
234 case "$#" in 1) usage ;; esac
236 log_given=t$log_given
242 log_given=t$log_given
243 log_message=`expr "$1" : '-m\(.*\)'`
247 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
248 log_given=t$log_given
249 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
253 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
258 case "$#" in 1) usage ;; esac
260 log_given=t$log_given
265 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
266 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
267 --reedit-messag=*|--reedit-message=*)
268 log_given=t$log_given
269 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
273 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
274 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
275 case "$#" in 1) usage ;; esac
277 log_given=t$log_given
283 case "$#" in 1) usage ;; esac
285 log_given=t$log_given
290 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
291 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
293 log_given=t$log_given
294 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
298 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
299 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
300 case "$#" in 1) usage ;; esac
302 log_given=t$log_given
307 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
311 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
328 ################################################################
329 # Sanity check options
333 die "Only one of -c/-C/-F/-m can be used." ;;
336 case "$#,$also$only" in
338 die "Only one of --include/--only can be used." ;;
340 die "No paths with --include/--only does not make sense." ;;
344 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
349 case "$all,$also,$#" in
351 die "Cannot use -a and -i at the same time." ;;
353 die "Paths with -a does not make sense." ;;
355 die "No paths with -i does not make sense." ;;
358 ################################################################
359 # Prepare index to have a tree to be committed
361 TOP=`git-rev-parse --show-cdup`
372 GIT_INDEX_FILE="$NEXT_INDEX"
373 export GIT_INDEX_FILE
374 git-diff-files --name-only -z |
375 git-update-index --remove -z --stdin
380 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
382 git-diff-files --name-only -z -- "$@" |
385 GIT_INDEX_FILE="$NEXT_INDEX"
386 export GIT_INDEX_FILE
387 git-update-index --remove -z --stdin
395 if test -f "$GIT_DIR/MERGE_HEAD"
397 refuse_partial "Cannot do a partial commit during a merge."
399 TMP_INDEX="$GIT_DIR/tmp-index$$"
400 if test -z "$initial_commit"
402 # make sure index is clean at the specified paths, or
403 # they are additions.
404 dirty_in_index=`git-diff-index --cached --name-status \
405 --diff-filter=DMTU HEAD -- "$@"`
406 test -z "$dirty_in_index" ||
407 refuse_partial "Different in index and the last commit:
410 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
412 # Build the temporary index and update the real index
414 if test -z "$initial_commit"
416 cp "$THIS_INDEX" "$TMP_INDEX"
417 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
422 echo "$commit_only" |
423 GIT_INDEX_FILE="$TMP_INDEX" \
424 git-update-index --add --remove --stdin &&
427 echo "$commit_only" |
429 GIT_INDEX_FILE="$NEXT_INDEX"
430 export GIT_INDEX_FILE
431 git-update-index --remove --stdin
438 ################################################################
439 # If we do as-is commit, the index file will be THIS_INDEX,
440 # otherwise NEXT_INDEX after we make this commit. We leave
441 # the index as is if we abort.
443 if test -f "$NEXT_INDEX"
445 USE_INDEX="$NEXT_INDEX"
447 USE_INDEX="$THIS_INDEX"
450 GIT_INDEX_FILE="$USE_INDEX" \
451 git-update-index -q $unmerged_ok_if_status --refresh || exit
453 ################################################################
454 # If the request is status, just show it and exit.
462 ################################################################
463 # Grab commit message, write out tree and make commit.
465 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
469 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
471 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
475 if test "$log_message" != ''
478 elif test "$logfile" != ""
480 if test "$logfile" = -
483 echo >&2 "(reading log message from standard input)"
488 elif test "$use_commit" != ""
490 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
491 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
493 cat "$GIT_DIR/MERGE_MSG"
494 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
500 git-var GIT_COMMITTER_IDENT | sed -e '
504 } >>"$GIT_DIR"/COMMIT_EDITMSG
508 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
510 echo "# It looks like you may be committing a MERGE."
511 echo "# If this is not correct, please remove the file"
512 echo "# $GIT_DIR/MERGE_HEAD"
513 echo "# and try again"
515 fi >>"$GIT_DIR"/COMMIT_EDITMSG
518 if test '' != "$force_author"
520 GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
521 GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
522 test '' != "$GIT_AUTHOR_NAME" &&
523 test '' != "$GIT_AUTHOR_EMAIL" ||
524 die "malformatted --author parameter"
525 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
526 elif test '' != "$use_commit"
530 s/'\''/'\''\\'\'\''/g
532 s/^author \([^<]*\) <[^>]*> .*$/\1/
534 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
537 s/^author [^<]* <\([^>]*\)> .*$/\1/
539 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
542 s/^author [^<]* <[^>]*> \(.*\)$/\1/
544 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
549 set_author_env=`git-cat-file commit "$use_commit" |
550 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
551 eval "$set_author_env"
552 export GIT_AUTHOR_NAME
553 export GIT_AUTHOR_EMAIL
554 export GIT_AUTHOR_DATE
558 if test -z "$initial_commit"
560 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
561 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
564 if [ -z "$(git-ls-files)" ]; then
565 echo >&2 Nothing to commit
572 test -z "$only_include_assumed" || echo "$only_include_assumed"
574 } >>"$GIT_DIR"/COMMIT_EDITMSG
575 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
577 rm -f "$GIT_DIR/COMMIT_EDITMSG"
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 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
598 if test -x "$GIT_DIR"/hooks/commit-msg
600 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
610 ' "$GIT_DIR"/COMMIT_EDITMSG |
611 git-stripspace >"$GIT_DIR"/COMMIT_MSG
613 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
618 if test -z "$TMP_INDEX"
620 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
622 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
625 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
626 git-update-ref HEAD $commit $current &&
627 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
628 if test -f "$NEXT_INDEX"
630 mv "$NEXT_INDEX" "$THIS_INDEX"
635 echo >&2 "* no commit message? aborting commit."
639 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
640 if test -d "$GIT_DIR/rr-cache"
645 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
647 "$GIT_DIR"/hooks/post-commit