Git.pm: Support for perl/ being built by a different compiler
[git/repo.git] / git-commit.sh
blob22c4ce86c3cc5b35fab27bbe9e75dbe334f58534
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 -p "$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 "z$1" : 'z-[Ff]\(.*\)'`
227 shift
229 --F=*|--f=*|--fi=*|--fil=*|--file=*)
230 no_edit=t
231 log_given=t$log_given
232 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
233 shift
235 -a|--a|--al|--all)
236 all=t
237 shift
239 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
240 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
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 "z$1" : 'z-m\(.*\)'`
281 else
282 log_message="$log_message
284 `expr "z$1" : 'z-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 "z$1" : 'z-[^=]*=\(.*\)'`
294 else
295 log_message="$log_message
297 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
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 "z$1" : 'z-[^=]*=\(.*\)'`
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 "z$1" : 'z-[^=]*=\(.*\)'`
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 elif test -f "$GIT_DIR/SQUASH_MSG"
570 then
571 cat "$GIT_DIR/SQUASH_MSG"
572 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
574 case "$signoff" in
577 echo
578 git-var GIT_COMMITTER_IDENT | sed -e '
579 s/>.*/>/
580 s/^/Signed-off-by: /
582 } >>"$GIT_DIR"/COMMIT_EDITMSG
584 esac
586 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
587 echo "#"
588 echo "# It looks like you may be committing a MERGE."
589 echo "# If this is not correct, please remove the file"
590 echo "# $GIT_DIR/MERGE_HEAD"
591 echo "# and try again"
592 echo "#"
593 fi >>"$GIT_DIR"/COMMIT_EDITMSG
595 # Author
596 if test '' != "$force_author"
597 then
598 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
599 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
600 test '' != "$GIT_AUTHOR_NAME" &&
601 test '' != "$GIT_AUTHOR_EMAIL" ||
602 die "malformatted --author parameter"
603 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
604 elif test '' != "$use_commit"
605 then
606 pick_author_script='
607 /^author /{
608 s/'\''/'\''\\'\'\''/g
610 s/^author \([^<]*\) <[^>]*> .*$/\1/
611 s/'\''/'\''\'\'\''/g
612 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
615 s/^author [^<]* <\([^>]*\)> .*$/\1/
616 s/'\''/'\''\'\'\''/g
617 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
620 s/^author [^<]* <[^>]*> \(.*\)$/\1/
621 s/'\''/'\''\'\'\''/g
622 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
627 set_author_env=`git-cat-file commit "$use_commit" |
628 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
629 eval "$set_author_env"
630 export GIT_AUTHOR_NAME
631 export GIT_AUTHOR_EMAIL
632 export GIT_AUTHOR_DATE
635 PARENTS="-p HEAD"
636 if test -z "$initial_commit"
637 then
638 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
639 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
640 elif test -n "$amend"; then
641 PARENTS=$(git-cat-file commit HEAD |
642 sed -n -e '/^$/q' -e 's/^parent /-p /p')
644 current=$(git-rev-parse --verify HEAD)
645 else
646 if [ -z "$(git-ls-files)" ]; then
647 echo >&2 Nothing to commit
648 exit 1
650 PARENTS=""
651 current=
654 if test -z "$no_edit"
655 then
657 echo ""
658 echo "# Please enter the commit message for your changes."
659 echo "# (Comment lines starting with '#' will not be included)"
660 test -z "$only_include_assumed" || echo "$only_include_assumed"
661 run_status
662 } >>"$GIT_DIR"/COMMIT_EDITMSG
663 else
664 # we need to check if there is anything to commit
665 run_status >/dev/null
667 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
668 then
669 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
670 run_status
671 exit 1
674 case "$no_edit" in
676 case "${VISUAL:-$EDITOR},$TERM" in
677 ,dumb)
678 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
679 echo >&2 "Please supply the commit log message using either"
680 echo >&2 "-m or -F option. A boilerplate log message has"
681 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
682 exit 1
684 esac
685 git-var GIT_AUTHOR_IDENT > /dev/null || die
686 git-var GIT_COMMITTER_IDENT > /dev/null || die
687 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
689 esac
691 case "$verify" in
693 if test -x "$GIT_DIR"/hooks/commit-msg
694 then
695 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
697 esac
699 if test -z "$no_edit"
700 then
701 sed -e '
702 /^diff --git a\/.*/{
703 s///
706 /^#/d
707 ' "$GIT_DIR"/COMMIT_EDITMSG
708 else
709 cat "$GIT_DIR"/COMMIT_EDITMSG
710 fi |
711 git-stripspace >"$GIT_DIR"/COMMIT_MSG
713 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
714 git-stripspace |
715 wc -l` &&
716 test 0 -lt $cnt
717 then
718 if test -z "$TMP_INDEX"
719 then
720 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
721 else
722 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
723 rm -f "$TMP_INDEX"
724 fi &&
725 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
726 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
727 git-update-ref -m "commit: $rlogm" HEAD $commit $current &&
728 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
729 if test -f "$NEXT_INDEX"
730 then
731 mv "$NEXT_INDEX" "$THIS_INDEX"
732 else
733 : ;# happy
735 else
736 echo >&2 "* no commit message? aborting commit."
737 false
739 ret="$?"
740 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
741 if test -d "$GIT_DIR/rr-cache"
742 then
743 git-rerere
746 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
747 then
748 "$GIT_DIR"/hooks/post-commit
750 exit "$ret"