git-commit: allow -e option anywhere on command line
[git/peff.git] / git-commit.sh
blobe74fe640b8acb87bef2cc118e87d8779c79f8786
1 #!/bin/sh
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>...]'
7 SUBDIRECTORY_OK=Yes
8 . git-sh-setup
10 git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11 branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
13 case "$0" in
14 *status)
15 status_only=t
16 unmerged_ok_if_status=--unmerged ;;
17 *commit)
18 status_only=
19 unmerged_ok_if_status= ;;
20 esac
22 refuse_partial () {
23 echo >&2 "$1"
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
28 THIS_INDEX="$GIT_DIR/index"
29 NEXT_INDEX="$GIT_DIR/next-index$$"
30 rm -f "$NEXT_INDEX"
31 save_index () {
32 cp "$THIS_INDEX" "$NEXT_INDEX"
35 report () {
36 header="#
37 # $1:
38 # ($2)
41 trailer=""
42 while read status name newname
44 printf '%s' "$header"
45 header=""
46 trailer="#
48 case "$status" in
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";;
56 esac
57 done
58 printf '%s' "$trailer"
59 [ "$header" ]
62 run_status () {
64 # We always show status for the whole tree.
65 cd "$TOP"
67 IS_INITIAL="$initial_commit"
68 REFERENCE=HEAD
69 case "$amend" in
71 # If we are amending the initial commit, there
72 # is no HEAD^1.
73 if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1
74 then
75 REFERENCE="HEAD^1"
76 IS_INITIAL=
77 else
78 IS_INITIAL=t
81 esac
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"
90 then
91 GIT_INDEX_FILE="$TMP_INDEX"
92 export GIT_INDEX_FILE
93 elif test -f "$NEXT_INDEX"
94 then
95 GIT_INDEX_FILE="$NEXT_INDEX"
96 export GIT_INDEX_FILE
99 case "$branch" in
100 refs/heads/master) ;;
101 *) echo "# On branch $branch" ;;
102 esac
104 if test -z "$IS_INITIAL"
105 then
106 git-diff-index -M --cached --name-status \
107 --diff-filter=MDTCRA $REFERENCE |
108 sed -e '
109 s/\\/\\\\/g
110 s/ /\\ /g
112 report "Updated but not checked in" "will commit"
113 committable="$?"
114 else
115 echo '#
116 # Initial commit
118 git-ls-files |
119 sed -e '
120 s/\\/\\\\/g
121 s/ /\\ /g
122 s/^/A /
124 report "Updated but not checked in" "will commit"
126 committable="$?"
129 git-diff-files --name-status |
130 sed -e '
131 s/\\/\\\\/g
132 s/ /\\ /g
134 report "Changed but not updated" \
135 "use git-update-index to mark for commit"
137 option=""
138 if test -z "$untracked_files"; then
139 option="--directory --no-empty-directory"
141 if test -f "$GIT_DIR/info/exclude"
142 then
143 git-ls-files -z --others $option \
144 --exclude-from="$GIT_DIR/info/exclude" \
145 --exclude-per-directory=.gitignore
146 else
147 git-ls-files -z --others $option \
148 --exclude-per-directory=.gitignore
149 fi |
150 perl -e '$/ = "\0";
151 my $shown = 0;
152 while (<>) {
153 chomp;
154 s|\\|\\\\|g;
155 s|\t|\\t|g;
156 s|\n|\\n|g;
157 s/^/# /;
158 if (!$shown) {
159 print "#\n# Untracked files:\n";
160 print "# (use \"git add\" to add to commit)\n";
161 print "#\n";
162 $shown = 1;
164 print "$_\n";
168 if test -n "$verbose" -a -z "$IS_INITIAL"
169 then
170 git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
172 case "$committable" in
174 case "$amend" in
176 echo "# No changes" ;;
178 echo "nothing to commit" ;;
179 esac
180 exit 1 ;;
181 esac
182 exit 0
186 trap '
187 test -z "$TMP_INDEX" || {
188 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
190 rm -f "$NEXT_INDEX"
193 ################################################################
194 # Command line argument parsing and sanity checking
196 all=
197 also=
198 only=
199 logfile=
200 use_commit=
201 amend=
202 edit_flag=
203 no_edit=
204 log_given=
205 log_message=
206 verify=t
207 verbose=
208 signoff=
209 force_author=
210 only_include_assumed=
211 untracked_files=
212 while case "$#" in 0) break;; esac
214 case "$1" in
215 -F|--F|-f|--f|--fi|--fil|--file)
216 case "$#" in 1) usage ;; esac
217 shift
218 no_edit=t
219 log_given=t$log_given
220 logfile="$1"
221 shift
223 -F*|-f*)
224 no_edit=t
225 log_given=t$log_given
226 logfile=`expr "$1" : '-[Ff]\(.*\)'`
227 shift
229 --F=*|--f=*|--fi=*|--fil=*|--file=*)
230 no_edit=t
231 log_given=t$log_given
232 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
233 shift
235 -a|--a|--al|--all)
236 all=t
237 shift
239 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
240 force_author=`expr "$1" : '-[^=]*=\(.*\)'`
241 shift
243 --au|--aut|--auth|--autho|--author)
244 case "$#" in 1) usage ;; esac
245 shift
246 force_author="$1"
247 shift
249 -e|--e|--ed|--edi|--edit)
250 edit_flag=t
251 shift
253 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
254 also=t
255 shift
257 -o|--o|--on|--onl|--only)
258 only=t
259 shift
261 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
262 case "$#" in 1) usage ;; esac
263 shift
264 log_given=m$log_given
265 if test "$log_message" = ''
266 then
267 log_message="$1"
268 else
269 log_message="$log_message
273 no_edit=t
274 shift
276 -m*)
277 log_given=m$log_given
278 if test "$log_message" = ''
279 then
280 log_message=`expr "$1" : '-m\(.*\)'`
281 else
282 log_message="$log_message
284 `expr "$1" : '-m\(.*\)'`"
286 no_edit=t
287 shift
289 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
290 log_given=m$log_given
291 if test "$log_message" = ''
292 then
293 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
294 else
295 log_message="$log_message
297 `expr "$1" : '-[^=]*=\(.*\)'`"
299 no_edit=t
300 shift
302 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
303 verify=
304 shift
306 --a|--am|--ame|--amen|--amend)
307 amend=t
308 log_given=t$log_given
309 use_commit=HEAD
310 shift
313 case "$#" in 1) usage ;; esac
314 shift
315 log_given=t$log_given
316 use_commit="$1"
317 no_edit=
318 shift
320 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
321 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
322 --reedit-messag=*|--reedit-message=*)
323 log_given=t$log_given
324 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
325 no_edit=
326 shift
328 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
329 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
330 case "$#" in 1) usage ;; esac
331 shift
332 log_given=t$log_given
333 use_commit="$1"
334 no_edit=
335 shift
338 case "$#" in 1) usage ;; esac
339 shift
340 log_given=t$log_given
341 use_commit="$1"
342 no_edit=t
343 shift
345 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
346 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
347 --reuse-message=*)
348 log_given=t$log_given
349 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
350 no_edit=t
351 shift
353 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
354 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
355 case "$#" in 1) usage ;; esac
356 shift
357 log_given=t$log_given
358 use_commit="$1"
359 no_edit=t
360 shift
362 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
363 signoff=t
364 shift
366 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
367 verbose=t
368 shift
370 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
371 --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
372 --untracked-files)
373 untracked_files=t
374 shift
377 shift
378 break
381 usage
384 break
386 esac
387 done
388 case "$edit_flag" in t) no_edit= ;; esac
390 ################################################################
391 # Sanity check options
393 case "$amend,$initial_commit" in
394 t,t)
395 die "You do not have anything to amend." ;;
397 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
398 die "You are in the middle of a merge -- cannot amend."
399 fi ;;
400 esac
402 case "$log_given" in
403 tt*)
404 die "Only one of -c/-C/-F can be used." ;;
405 *tm*|*mt*)
406 die "Option -m cannot be combined with -c/-C/-F." ;;
407 esac
409 case "$#,$also,$only,$amend" in
410 *,t,t,*)
411 die "Only one of --include/--only can be used." ;;
412 0,t,,* | 0,,t,)
413 die "No paths with --include/--only does not make sense." ;;
414 0,,t,t)
415 only_include_assumed="# Clever... amending the last one with dirty index." ;;
416 0,,,*)
418 *,,,*)
419 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
420 also=
422 esac
423 unset only
424 case "$all,$also,$#" in
425 t,t,*)
426 die "Cannot use -a and -i at the same time." ;;
427 t,,[1-9]*)
428 die "Paths with -a does not make sense." ;;
429 ,t,0)
430 die "No paths with -i does not make sense." ;;
431 esac
433 ################################################################
434 # Prepare index to have a tree to be committed
436 TOP=`git-rev-parse --show-cdup`
437 if test -z "$TOP"
438 then
439 TOP=./
442 case "$all,$also" in
444 save_index &&
446 cd "$TOP"
447 GIT_INDEX_FILE="$NEXT_INDEX"
448 export GIT_INDEX_FILE
449 git-diff-files --name-only -z |
450 git-update-index --remove -z --stdin
454 save_index &&
455 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
457 git-diff-files --name-only -z -- "$@" |
459 cd "$TOP"
460 GIT_INDEX_FILE="$NEXT_INDEX"
461 export GIT_INDEX_FILE
462 git-update-index --remove -z --stdin
466 case "$#" in
468 ;; # commit as-is
470 if test -f "$GIT_DIR/MERGE_HEAD"
471 then
472 refuse_partial "Cannot do a partial commit during a merge."
474 TMP_INDEX="$GIT_DIR/tmp-index$$"
475 if test -z "$initial_commit"
476 then
477 # make sure index is clean at the specified paths, or
478 # they are additions.
479 dirty_in_index=`git-diff-index --cached --name-status \
480 --diff-filter=DMTU HEAD -- "$@"`
481 test -z "$dirty_in_index" ||
482 refuse_partial "Different in index and the last commit:
483 $dirty_in_index"
485 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
487 # Build the temporary index and update the real index
488 # the same way.
489 if test -z "$initial_commit"
490 then
491 cp "$THIS_INDEX" "$TMP_INDEX"
492 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
493 else
494 rm -f "$TMP_INDEX"
495 fi || exit
497 echo "$commit_only" |
498 GIT_INDEX_FILE="$TMP_INDEX" \
499 git-update-index --add --remove --stdin &&
501 save_index &&
502 echo "$commit_only" |
504 GIT_INDEX_FILE="$NEXT_INDEX"
505 export GIT_INDEX_FILE
506 git-update-index --remove --stdin
507 ) || exit
509 esac
511 esac
513 ################################################################
514 # If we do as-is commit, the index file will be THIS_INDEX,
515 # otherwise NEXT_INDEX after we make this commit. We leave
516 # the index as is if we abort.
518 if test -f "$NEXT_INDEX"
519 then
520 USE_INDEX="$NEXT_INDEX"
521 else
522 USE_INDEX="$THIS_INDEX"
525 GIT_INDEX_FILE="$USE_INDEX" \
526 git-update-index -q $unmerged_ok_if_status --refresh || exit
528 ################################################################
529 # If the request is status, just show it and exit.
531 case "$0" in
532 *status)
533 run_status
534 exit $?
535 esac
537 ################################################################
538 # Grab commit message, write out tree and make commit.
540 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
541 then
542 if test "$TMP_INDEX"
543 then
544 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
545 else
546 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
547 fi || exit
550 if test "$log_message" != ''
551 then
552 echo "$log_message"
553 elif test "$logfile" != ""
554 then
555 if test "$logfile" = -
556 then
557 test -t 0 &&
558 echo >&2 "(reading log message from standard input)"
560 else
561 cat <"$logfile"
563 elif test "$use_commit" != ""
564 then
565 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
566 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
567 then
568 cat "$GIT_DIR/MERGE_MSG"
569 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
571 case "$signoff" in
574 echo
575 git-var GIT_COMMITTER_IDENT | sed -e '
576 s/>.*/>/
577 s/^/Signed-off-by: /
579 } >>"$GIT_DIR"/COMMIT_EDITMSG
581 esac
583 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
584 echo "#"
585 echo "# It looks like you may be committing a MERGE."
586 echo "# If this is not correct, please remove the file"
587 echo "# $GIT_DIR/MERGE_HEAD"
588 echo "# and try again"
589 echo "#"
590 fi >>"$GIT_DIR"/COMMIT_EDITMSG
592 # Author
593 if test '' != "$force_author"
594 then
595 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
596 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
597 test '' != "$GIT_AUTHOR_NAME" &&
598 test '' != "$GIT_AUTHOR_EMAIL" ||
599 die "malformatted --author parameter"
600 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
601 elif test '' != "$use_commit"
602 then
603 pick_author_script='
604 /^author /{
605 s/'\''/'\''\\'\'\''/g
607 s/^author \([^<]*\) <[^>]*> .*$/\1/
608 s/'\''/'\''\'\'\''/g
609 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
612 s/^author [^<]* <\([^>]*\)> .*$/\1/
613 s/'\''/'\''\'\'\''/g
614 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
617 s/^author [^<]* <[^>]*> \(.*\)$/\1/
618 s/'\''/'\''\'\'\''/g
619 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
624 set_author_env=`git-cat-file commit "$use_commit" |
625 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
626 eval "$set_author_env"
627 export GIT_AUTHOR_NAME
628 export GIT_AUTHOR_EMAIL
629 export GIT_AUTHOR_DATE
632 PARENTS="-p HEAD"
633 if test -z "$initial_commit"
634 then
635 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
636 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
637 elif test -n "$amend"; then
638 PARENTS=$(git-cat-file commit HEAD |
639 sed -n -e '/^$/q' -e 's/^parent /-p /p')
641 current=$(git-rev-parse --verify HEAD)
642 else
643 if [ -z "$(git-ls-files)" ]; then
644 echo >&2 Nothing to commit
645 exit 1
647 PARENTS=""
648 current=
651 if test -z "$no_edit"
652 then
654 echo ""
655 echo "# Please enter the commit message for your changes."
656 echo "# (Comment lines starting with '#' will not be included)"
657 test -z "$only_include_assumed" || echo "$only_include_assumed"
658 run_status
659 } >>"$GIT_DIR"/COMMIT_EDITMSG
660 else
661 # we need to check if there is anything to commit
662 run_status >/dev/null
664 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
665 then
666 rm -f "$GIT_DIR/COMMIT_EDITMSG"
667 run_status
668 exit 1
671 case "$no_edit" in
673 case "${VISUAL:-$EDITOR},$TERM" in
674 ,dumb)
675 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
676 echo >&2 "Please supply the commit log message using either"
677 echo >&2 "-m or -F option. A boilerplate log message has"
678 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
679 exit 1
681 esac
682 git-var GIT_AUTHOR_IDENT > /dev/null || die
683 git-var GIT_COMMITTER_IDENT > /dev/null || die
684 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
686 esac
688 case "$verify" in
690 if test -x "$GIT_DIR"/hooks/commit-msg
691 then
692 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
694 esac
696 sed -e '
697 /^diff --git a\/.*/{
698 s///
701 /^#/d
702 ' "$GIT_DIR"/COMMIT_EDITMSG |
703 git-stripspace >"$GIT_DIR"/COMMIT_MSG
705 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
706 git-stripspace |
707 wc -l` &&
708 test 0 -lt $cnt
709 then
710 if test -z "$TMP_INDEX"
711 then
712 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
713 else
714 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
715 rm -f "$TMP_INDEX"
716 fi &&
717 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
718 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
719 git-update-ref -m "commit: $rlogm" HEAD $commit $current &&
720 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
721 if test -f "$NEXT_INDEX"
722 then
723 mv "$NEXT_INDEX" "$THIS_INDEX"
724 else
725 : ;# happy
727 else
728 echo >&2 "* no commit message? aborting commit."
729 false
731 ret="$?"
732 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
733 if test -d "$GIT_DIR/rr-cache"
734 then
735 git-rerere
738 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
739 then
740 "$GIT_DIR"/hooks/post-commit
742 exit "$ret"