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} \
89 test -z "$TMP_INDEX" || {
90 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
95 ################################################################
96 # Command line argument parsing and sanity checking
112 only_include_assumed=
114 while case "$#" in 0) break;; esac
117 -F|--F|-f|--f|--fi|--fil|--file)
118 case "$#" in 1) usage ;; esac
121 log_given=t$log_given
127 log_given=t$log_given
128 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
131 --F=*|--f=*|--fi=*|--fil=*|--file=*)
133 log_given=t$log_given
134 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
141 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
142 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
145 --au|--aut|--auth|--autho|--author)
146 case "$#" in 1) usage ;; esac
151 -e|--e|--ed|--edi|--edit)
155 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
159 -o|--o|--on|--onl|--only)
163 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
164 case "$#" in 1) usage ;; esac
166 log_given=m$log_given
167 if test "$log_message" = ''
171 log_message="$log_message
179 log_given=m$log_given
180 if test "$log_message" = ''
182 log_message=`expr "z$1" : 'z-m\(.*\)'`
184 log_message="$log_message
186 `expr "z$1" : 'z-m\(.*\)'`"
191 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
192 log_given=m$log_given
193 if test "$log_message" = ''
195 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
197 log_message="$log_message
199 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
204 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
208 --a|--am|--ame|--amen|--amend)
210 log_given=t$log_given
215 case "$#" in 1) usage ;; esac
217 log_given=t$log_given
222 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
223 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
224 --reedit-messag=*|--reedit-message=*)
225 log_given=t$log_given
226 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
230 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
231 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
232 case "$#" in 1) usage ;; esac
234 log_given=t$log_given
240 case "$#" in 1) usage ;; esac
242 log_given=t$log_given
247 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
248 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
250 log_given=t$log_given
251 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
255 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
256 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
257 case "$#" in 1) usage ;; esac
259 log_given=t$log_given
264 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
268 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
272 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
273 --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
290 case "$edit_flag" in t) no_edit= ;; esac
292 ################################################################
293 # Sanity check options
295 case "$amend,$initial_commit" in
297 die "You do not have anything to amend." ;;
299 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
300 die "You are in the middle of a merge -- cannot amend."
306 die "Only one of -c/-C/-F can be used." ;;
308 die "Option -m cannot be combined with -c/-C/-F." ;;
311 case "$#,$also,$only,$amend" in
313 die "Only one of --include/--only can be used." ;;
315 die "No paths with --include/--only does not make sense." ;;
317 only_include_assumed="# Clever... amending the last one with dirty index." ;;
321 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
326 case "$all,$also,$#" in
328 die "Cannot use -a and -i at the same time." ;;
330 die "Paths with -a does not make sense." ;;
332 die "No paths with -i does not make sense." ;;
335 ################################################################
336 # Prepare index to have a tree to be committed
338 TOP=`git-rev-parse --show-cdup`
349 GIT_INDEX_FILE="$NEXT_INDEX"
350 export GIT_INDEX_FILE
351 git-diff-files --name-only -z |
352 git-update-index --remove -z --stdin
357 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
359 git-diff-files --name-only -z -- "$@" |
362 GIT_INDEX_FILE="$NEXT_INDEX"
363 export GIT_INDEX_FILE
364 git-update-index --remove -z --stdin
372 if test -f "$GIT_DIR/MERGE_HEAD"
374 refuse_partial "Cannot do a partial commit during a merge."
376 TMP_INDEX="$GIT_DIR/tmp-index$$"
377 if test -z "$initial_commit"
379 # make sure index is clean at the specified paths, or
380 # they are additions.
381 dirty_in_index=`git-diff-index --cached --name-status \
382 --diff-filter=DMTU HEAD -- "$@"`
383 test -z "$dirty_in_index" ||
384 refuse_partial "Different in index and the last commit:
387 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
389 # Build the temporary index and update the real index
391 if test -z "$initial_commit"
393 cp "$THIS_INDEX" "$TMP_INDEX"
394 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
399 echo "$commit_only" |
400 GIT_INDEX_FILE="$TMP_INDEX" \
401 git-update-index --add --remove --stdin &&
404 echo "$commit_only" |
406 GIT_INDEX_FILE="$NEXT_INDEX"
407 export GIT_INDEX_FILE
408 git-update-index --remove --stdin
415 ################################################################
416 # If we do as-is commit, the index file will be THIS_INDEX,
417 # otherwise NEXT_INDEX after we make this commit. We leave
418 # the index as is if we abort.
420 if test -f "$NEXT_INDEX"
422 USE_INDEX="$NEXT_INDEX"
424 USE_INDEX="$THIS_INDEX"
427 GIT_INDEX_FILE="$USE_INDEX" \
428 git-update-index -q $unmerged_ok_if_status --refresh || exit
430 ################################################################
431 # If the request is status, just show it and exit.
439 ################################################################
440 # Grab commit message, write out tree and make commit.
442 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
446 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
448 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
452 if test "$log_message" != ''
455 elif test "$logfile" != ""
457 if test "$logfile" = -
460 echo >&2 "(reading log message from standard input)"
465 elif test "$use_commit" != ""
467 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
468 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
470 cat "$GIT_DIR/MERGE_MSG"
471 elif test -f "$GIT_DIR/SQUASH_MSG"
473 cat "$GIT_DIR/SQUASH_MSG"
474 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
480 git-var GIT_COMMITTER_IDENT | sed -e '
484 } >>"$GIT_DIR"/COMMIT_EDITMSG
488 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
490 echo "# It looks like you may be committing a MERGE."
491 echo "# If this is not correct, please remove the file"
492 echo "# $GIT_DIR/MERGE_HEAD"
493 echo "# and try again"
495 fi >>"$GIT_DIR"/COMMIT_EDITMSG
498 if test '' != "$force_author"
500 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
501 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
502 test '' != "$GIT_AUTHOR_NAME" &&
503 test '' != "$GIT_AUTHOR_EMAIL" ||
504 die "malformed --author parameter"
505 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
506 elif test '' != "$use_commit"
510 s/'\''/'\''\\'\'\''/g
512 s/^author \([^<]*\) <[^>]*> .*$/\1/
514 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
517 s/^author [^<]* <\([^>]*\)> .*$/\1/
519 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
522 s/^author [^<]* <[^>]*> \(.*\)$/\1/
524 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
529 set_author_env=`git-cat-file commit "$use_commit" |
530 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
531 eval "$set_author_env"
532 export GIT_AUTHOR_NAME
533 export GIT_AUTHOR_EMAIL
534 export GIT_AUTHOR_DATE
538 if test -z "$initial_commit"
541 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
542 rloga='commit (merge)'
543 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
544 elif test -n "$amend"; then
545 rloga='commit (amend)'
546 PARENTS=$(git-cat-file commit HEAD |
547 sed -n -e '/^$/q' -e 's/^parent /-p /p')
549 current=$(git-rev-parse --verify HEAD)
551 if [ -z "$(git-ls-files)" ]; then
552 echo >&2 Nothing to commit
557 rloga='commit (initial)'
560 if test -z "$no_edit"
564 echo "# Please enter the commit message for your changes."
565 echo "# (Comment lines starting with '#' will not be included)"
566 test -z "$only_include_assumed" || echo "$only_include_assumed"
568 } >>"$GIT_DIR"/COMMIT_EDITMSG
570 # we need to check if there is anything to commit
571 run_status >/dev/null
573 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
575 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
582 case "${VISUAL:-$EDITOR},$TERM" in
584 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
585 echo >&2 "Please supply the commit log message using either"
586 echo >&2 "-m or -F option. A boilerplate log message has"
587 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
591 git-var GIT_AUTHOR_IDENT > /dev/null || die
592 git-var GIT_COMMITTER_IDENT > /dev/null || die
593 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
599 if test -x "$GIT_DIR"/hooks/commit-msg
601 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
605 if test -z "$no_edit"
613 ' "$GIT_DIR"/COMMIT_EDITMSG
615 cat "$GIT_DIR"/COMMIT_EDITMSG
617 git-stripspace >"$GIT_DIR"/COMMIT_MSG
619 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
624 if test -z "$TMP_INDEX"
626 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
628 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
631 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
632 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
633 git-update-ref -m "$rloga: $rlogm" HEAD $commit $current &&
634 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
635 if test -f "$NEXT_INDEX"
637 mv "$NEXT_INDEX" "$THIS_INDEX"
642 echo >&2 "* no commit message? aborting commit."
646 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
647 if test -d "$GIT_DIR/rr-cache"
652 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
654 "$GIT_DIR"/hooks/post-commit