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"
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"
44 GIT_INDEX_FILE="$TMP_INDEX"
46 elif test -f "$NEXT_INDEX"
48 GIT_INDEX_FILE="$NEXT_INDEX"
52 case "$status_only" in
56 git-runstatus ${color} \
57 ${verbose:+--verbose} \
59 ${untracked_files:+--untracked}
63 test -z "$TMP_INDEX" || {
64 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
69 ################################################################
70 # Command line argument parsing and sanity checking
88 while case "$#" in 0) break;; esac
91 -F|--F|-f|--f|--fi|--fil|--file)
92 case "$#" in 1) usage ;; esac
101 log_given=t$log_given
102 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
105 --F=*|--f=*|--fi=*|--fil=*|--file=*)
107 log_given=t$log_given
108 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
115 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
116 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
119 --au|--aut|--auth|--autho|--author)
120 case "$#" in 1) usage ;; esac
125 -e|--e|--ed|--edi|--edit)
129 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
133 -o|--o|--on|--onl|--only)
137 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
138 case "$#" in 1) usage ;; esac
140 log_given=m$log_given
141 if test "$log_message" = ''
145 log_message="$log_message
153 log_given=m$log_given
154 if test "$log_message" = ''
156 log_message=`expr "z$1" : 'z-m\(.*\)'`
158 log_message="$log_message
160 `expr "z$1" : 'z-m\(.*\)'`"
165 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
166 log_given=m$log_given
167 if test "$log_message" = ''
169 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
171 log_message="$log_message
173 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
178 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
183 --a|--am|--ame|--amen|--amend)
185 log_given=t$log_given
190 case "$#" in 1) usage ;; esac
192 log_given=t$log_given
197 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
198 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
199 --reedit-messag=*|--reedit-message=*)
200 log_given=t$log_given
201 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
205 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
206 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
208 case "$#" in 1) usage ;; esac
210 log_given=t$log_given
216 case "$#" in 1) usage ;; esac
218 log_given=t$log_given
223 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
224 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
226 log_given=t$log_given
227 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
231 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
232 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
233 case "$#" in 1) usage ;; esac
235 log_given=t$log_given
240 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
244 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
248 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
249 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
250 --untracked-file|--untracked-files)
266 case "$edit_flag" in t) no_edit= ;; esac
268 ################################################################
269 # Sanity check options
271 case "$amend,$initial_commit" in
273 die "You do not have anything to amend." ;;
275 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
276 die "You are in the middle of a merge -- cannot amend."
282 die "Only one of -c/-C/-F can be used." ;;
284 die "Option -m cannot be combined with -c/-C/-F." ;;
287 case "$#,$also,$only,$amend" in
289 die "Only one of --include/--only can be used." ;;
291 die "No paths with --include/--only does not make sense." ;;
293 only_include_assumed="# Clever... amending the last one with dirty index." ;;
297 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
302 case "$all,$also,$#" in
304 die "Cannot use -a and -i at the same time." ;;
306 die "Paths with -a does not make sense." ;;
308 die "No paths with -i does not make sense." ;;
311 ################################################################
312 # Prepare index to have a tree to be committed
314 TOP=`git-rev-parse --show-cdup`
325 GIT_INDEX_FILE="$NEXT_INDEX"
326 export GIT_INDEX_FILE
327 git-diff-files --name-only -z |
328 git-update-index --remove -z --stdin
333 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
335 git-diff-files --name-only -z -- "$@" |
338 GIT_INDEX_FILE="$NEXT_INDEX"
339 export GIT_INDEX_FILE
340 git-update-index --remove -z --stdin
348 if test -f "$GIT_DIR/MERGE_HEAD"
350 refuse_partial "Cannot do a partial commit during a merge."
352 TMP_INDEX="$GIT_DIR/tmp-index$$"
353 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
355 # Build a temporary index and update the real index
357 if test -z "$initial_commit"
359 cp "$THIS_INDEX" "$TMP_INDEX"
360 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
365 echo "$commit_only" |
366 GIT_INDEX_FILE="$TMP_INDEX" \
367 git-update-index --add --remove --stdin &&
370 echo "$commit_only" |
372 GIT_INDEX_FILE="$NEXT_INDEX"
373 export GIT_INDEX_FILE
374 git-update-index --remove --stdin
381 ################################################################
382 # If we do as-is commit, the index file will be THIS_INDEX,
383 # otherwise NEXT_INDEX after we make this commit. We leave
384 # the index as is if we abort.
386 if test -f "$NEXT_INDEX"
388 USE_INDEX="$NEXT_INDEX"
390 USE_INDEX="$THIS_INDEX"
393 GIT_INDEX_FILE="$USE_INDEX" \
394 git-update-index -q $unmerged_ok_if_status --refresh || exit
396 ################################################################
397 # If the request is status, just show it and exit.
405 ################################################################
406 # Grab commit message, write out tree and make commit.
408 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
412 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
414 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
418 if test "$log_message" != ''
421 elif test "$logfile" != ""
423 if test "$logfile" = -
426 echo >&2 "(reading log message from standard input)"
431 elif test "$use_commit" != ""
433 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
434 elif test -f "$GIT_DIR/MERGE_MSG"
436 cat "$GIT_DIR/MERGE_MSG"
437 elif test -f "$GIT_DIR/SQUASH_MSG"
439 cat "$GIT_DIR/SQUASH_MSG"
440 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
446 git-var GIT_COMMITTER_IDENT | sed -e '
450 } >>"$GIT_DIR"/COMMIT_EDITMSG
454 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
456 echo "# It looks like you may be committing a MERGE."
457 echo "# If this is not correct, please remove the file"
458 echo "# $GIT_DIR/MERGE_HEAD"
459 echo "# and try again"
461 fi >>"$GIT_DIR"/COMMIT_EDITMSG
464 if test '' != "$force_author"
466 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
467 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
468 test '' != "$GIT_AUTHOR_NAME" &&
469 test '' != "$GIT_AUTHOR_EMAIL" ||
470 die "malformed --author parameter"
471 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
472 elif test '' != "$use_commit"
476 s/'\''/'\''\\'\'\''/g
478 s/^author \([^<]*\) <[^>]*> .*$/\1/
480 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
483 s/^author [^<]* <\([^>]*\)> .*$/\1/
485 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
488 s/^author [^<]* <[^>]*> \(.*\)$/\1/
490 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
495 set_author_env=`git-cat-file commit "$use_commit" |
496 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
497 eval "$set_author_env"
498 export GIT_AUTHOR_NAME
499 export GIT_AUTHOR_EMAIL
500 export GIT_AUTHOR_DATE
504 if test -z "$initial_commit"
507 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
508 rloga='commit (merge)'
509 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
510 elif test -n "$amend"; then
511 rloga='commit (amend)'
512 PARENTS=$(git-cat-file commit HEAD |
513 sed -n -e '/^$/q' -e 's/^parent /-p /p')
515 current="$(git-rev-parse --verify HEAD)"
517 if [ -z "$(git-ls-files)" ]; then
518 echo >&2 Nothing to commit
522 rloga='commit (initial)'
526 if test -z "$no_edit"
530 echo "# Please enter the commit message for your changes."
531 echo "# (Comment lines starting with '#' will not be included)"
532 test -z "$only_include_assumed" || echo "$only_include_assumed"
534 } >>"$GIT_DIR"/COMMIT_EDITMSG
536 # we need to check if there is anything to commit
537 run_status >/dev/null
539 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
541 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
548 case "${VISUAL:-$EDITOR},$TERM" in
550 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
551 echo >&2 "Please supply the commit log message using either"
552 echo >&2 "-m or -F option. A boilerplate log message has"
553 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
557 git-var GIT_AUTHOR_IDENT > /dev/null || die
558 git-var GIT_COMMITTER_IDENT > /dev/null || die
559 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
565 if test -x "$GIT_DIR"/hooks/commit-msg
567 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
571 if test -z "$no_edit"
579 ' "$GIT_DIR"/COMMIT_EDITMSG
581 cat "$GIT_DIR"/COMMIT_EDITMSG
583 git-stripspace >"$GIT_DIR"/COMMIT_MSG
585 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
590 if test -z "$TMP_INDEX"
592 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
594 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
597 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
598 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
599 git-update-ref -m "$rloga: $rlogm" HEAD $commit "$current" &&
600 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
601 if test -f "$NEXT_INDEX"
603 mv "$NEXT_INDEX" "$THIS_INDEX"
608 echo >&2 "* no commit message? aborting commit."
612 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
613 if test -d "$GIT_DIR/rr-cache"
618 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
620 "$GIT_DIR"/hooks/post-commit