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|--no-verify)
182 --a|--am|--ame|--amen|--amend)
184 log_given=t$log_given
189 case "$#" in 1) usage ;; esac
191 log_given=t$log_given
196 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
197 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
198 --reedit-messag=*|--reedit-message=*)
199 log_given=t$log_given
200 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
204 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
205 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
206 case "$#" in 1) usage ;; esac
208 log_given=t$log_given
214 case "$#" in 1) usage ;; esac
216 log_given=t$log_given
221 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
222 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
224 log_given=t$log_given
225 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
229 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
230 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
231 case "$#" in 1) usage ;; esac
233 log_given=t$log_given
238 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
242 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
246 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
247 --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
264 case "$edit_flag" in t) no_edit= ;; esac
266 ################################################################
267 # Sanity check options
269 case "$amend,$initial_commit" in
271 die "You do not have anything to amend." ;;
273 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
274 die "You are in the middle of a merge -- cannot amend."
280 die "Only one of -c/-C/-F can be used." ;;
282 die "Option -m cannot be combined with -c/-C/-F." ;;
285 case "$#,$also,$only,$amend" in
287 die "Only one of --include/--only can be used." ;;
289 die "No paths with --include/--only does not make sense." ;;
291 only_include_assumed="# Clever... amending the last one with dirty index." ;;
295 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
300 case "$all,$also,$#" in
302 die "Cannot use -a and -i at the same time." ;;
304 die "Paths with -a does not make sense." ;;
306 die "No paths with -i does not make sense." ;;
309 ################################################################
310 # Prepare index to have a tree to be committed
312 TOP=`git-rev-parse --show-cdup`
323 GIT_INDEX_FILE="$NEXT_INDEX"
324 export GIT_INDEX_FILE
325 git-diff-files --name-only -z |
326 git-update-index --remove -z --stdin
331 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
333 git-diff-files --name-only -z -- "$@" |
336 GIT_INDEX_FILE="$NEXT_INDEX"
337 export GIT_INDEX_FILE
338 git-update-index --remove -z --stdin
346 if test -f "$GIT_DIR/MERGE_HEAD"
348 refuse_partial "Cannot do a partial commit during a merge."
350 TMP_INDEX="$GIT_DIR/tmp-index$$"
351 if test -z "$initial_commit"
353 # make sure index is clean at the specified paths, or
354 # they are additions.
355 dirty_in_index=`git-diff-index --cached --name-status \
356 --diff-filter=DMTU HEAD -- "$@"`
357 test -z "$dirty_in_index" ||
358 refuse_partial "Different in index and the last commit:
361 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
363 # Build the temporary index and update the real index
365 if test -z "$initial_commit"
367 cp "$THIS_INDEX" "$TMP_INDEX"
368 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
373 echo "$commit_only" |
374 GIT_INDEX_FILE="$TMP_INDEX" \
375 git-update-index --add --remove --stdin &&
378 echo "$commit_only" |
380 GIT_INDEX_FILE="$NEXT_INDEX"
381 export GIT_INDEX_FILE
382 git-update-index --remove --stdin
389 ################################################################
390 # If we do as-is commit, the index file will be THIS_INDEX,
391 # otherwise NEXT_INDEX after we make this commit. We leave
392 # the index as is if we abort.
394 if test -f "$NEXT_INDEX"
396 USE_INDEX="$NEXT_INDEX"
398 USE_INDEX="$THIS_INDEX"
401 GIT_INDEX_FILE="$USE_INDEX" \
402 git-update-index -q $unmerged_ok_if_status --refresh || exit
404 ################################################################
405 # If the request is status, just show it and exit.
413 ################################################################
414 # Grab commit message, write out tree and make commit.
416 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
420 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
422 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
426 if test "$log_message" != ''
429 elif test "$logfile" != ""
431 if test "$logfile" = -
434 echo >&2 "(reading log message from standard input)"
439 elif test "$use_commit" != ""
441 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
442 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
444 cat "$GIT_DIR/MERGE_MSG"
445 elif test -f "$GIT_DIR/SQUASH_MSG"
447 cat "$GIT_DIR/SQUASH_MSG"
448 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
454 git-var GIT_COMMITTER_IDENT | sed -e '
458 } >>"$GIT_DIR"/COMMIT_EDITMSG
462 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
464 echo "# It looks like you may be committing a MERGE."
465 echo "# If this is not correct, please remove the file"
466 echo "# $GIT_DIR/MERGE_HEAD"
467 echo "# and try again"
469 fi >>"$GIT_DIR"/COMMIT_EDITMSG
472 if test '' != "$force_author"
474 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
475 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
476 test '' != "$GIT_AUTHOR_NAME" &&
477 test '' != "$GIT_AUTHOR_EMAIL" ||
478 die "malformed --author parameter"
479 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
480 elif test '' != "$use_commit"
484 s/'\''/'\''\\'\'\''/g
486 s/^author \([^<]*\) <[^>]*> .*$/\1/
488 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
491 s/^author [^<]* <\([^>]*\)> .*$/\1/
493 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
496 s/^author [^<]* <[^>]*> \(.*\)$/\1/
498 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
503 set_author_env=`git-cat-file commit "$use_commit" |
504 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
505 eval "$set_author_env"
506 export GIT_AUTHOR_NAME
507 export GIT_AUTHOR_EMAIL
508 export GIT_AUTHOR_DATE
512 if test -z "$initial_commit"
515 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
516 rloga='commit (merge)'
517 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
518 elif test -n "$amend"; then
519 rloga='commit (amend)'
520 PARENTS=$(git-cat-file commit HEAD |
521 sed -n -e '/^$/q' -e 's/^parent /-p /p')
523 current=$(git-rev-parse --verify HEAD)
525 if [ -z "$(git-ls-files)" ]; then
526 echo >&2 Nothing to commit
531 rloga='commit (initial)'
534 if test -z "$no_edit"
538 echo "# Please enter the commit message for your changes."
539 echo "# (Comment lines starting with '#' will not be included)"
540 test -z "$only_include_assumed" || echo "$only_include_assumed"
542 } >>"$GIT_DIR"/COMMIT_EDITMSG
544 # we need to check if there is anything to commit
545 run_status >/dev/null
547 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
549 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
556 case "${VISUAL:-$EDITOR},$TERM" in
558 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
559 echo >&2 "Please supply the commit log message using either"
560 echo >&2 "-m or -F option. A boilerplate log message has"
561 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
565 git-var GIT_AUTHOR_IDENT > /dev/null || die
566 git-var GIT_COMMITTER_IDENT > /dev/null || die
567 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
573 if test -x "$GIT_DIR"/hooks/commit-msg
575 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
579 if test -z "$no_edit"
587 ' "$GIT_DIR"/COMMIT_EDITMSG
589 cat "$GIT_DIR"/COMMIT_EDITMSG
591 git-stripspace >"$GIT_DIR"/COMMIT_MSG
593 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
598 if test -z "$TMP_INDEX"
600 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
602 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
605 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
606 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
607 git-update-ref -m "$rloga: $rlogm" HEAD $commit $current &&
608 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
609 if test -f "$NEXT_INDEX"
611 mv "$NEXT_INDEX" "$THIS_INDEX"
616 echo >&2 "* no commit message? aborting commit."
620 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
621 if test -d "$GIT_DIR/rr-cache"
626 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
628 "$GIT_DIR"/hooks/post-commit