Merge branch 'lt/tree-2' into next
[git] / git-commit.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2006 Junio C Hamano
5
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
9
10 git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11 branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
12
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
21
22 refuse_partial () {
23         echo >&2 "$1"
24         echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25         exit 1
26 }
27
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"
33 }
34
35 report () {
36   header="#
37 # $1:
38 #   ($2)
39 #
40 "
41   trailer=""
42   while read status name newname
43   do
44     printf '%s' "$header"
45     header=""
46     trailer="#
47 "
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" ]
60 }
61
62 run_status () {
63     (
64         # We always show status for the whole tree.
65         cd "$TOP"
66
67         IS_INITIAL="$initial_commit"
68         REFERENCE=HEAD
69         case "$amend" in
70         t)
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
79                 fi
80                 ;;
81         esac
82
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
97         fi
98
99         case "$branch" in
100         refs/heads/master) ;;
101         *)  echo "# On branch $branch" ;;
102         esac
103
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
111             ' |
112             report "Updated but not checked in" "will commit"
113             committable="$?"
114         else
115             echo '#
116 # Initial commit
117 #'
118             git-ls-files |
119             sed -e '
120                     s/\\/\\\\/g
121                     s/ /\\ /g
122                     s/^/A /
123             ' |
124             report "Updated but not checked in" "will commit"
125
126             committable="$?"
127         fi
128
129         git-diff-files  --name-status |
130         sed -e '
131                 s/\\/\\\\/g
132                 s/ /\\ /g
133         ' |
134         report "Changed but not updated" \
135             "use git-update-index to mark for commit"
136
137         option=""
138         if test -z "$untracked_files"; then
139             option="--directory --no-empty-directory"
140         fi
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;
163                 }
164                 print "$_\n";
165             }
166         '
167
168         if test -n "$verbose" -a -z "$IS_INITIAL"
169         then
170             git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
171         fi
172         case "$committable" in
173         0)
174                 case "$amend" in
175                 t)
176                         echo "# No changes" ;;
177                 *)
178                         echo "nothing to commit" ;;
179                 esac
180                 exit 1 ;;
181         esac
182         exit 0
183     )
184 }
185
186 trap '
187         test -z "$TMP_INDEX" || {
188                 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
189         }
190         rm -f "$NEXT_INDEX"
191 ' 0
192
193 ################################################################
194 # Command line argument parsing and sanity checking
195
196 all=
197 also=
198 only=
199 logfile=
200 use_commit=
201 amend=
202 no_edit=
203 log_given=
204 log_message=
205 verify=t
206 verbose=
207 signoff=
208 force_author=
209 only_include_assumed=
210 untracked_files=
211 while case "$#" in 0) break;; esac
212 do
213   case "$1" in
214   -F|--F|-f|--f|--fi|--fil|--file)
215       case "$#" in 1) usage ;; esac
216       shift
217       no_edit=t
218       log_given=t$log_given
219       logfile="$1"
220       shift
221       ;;
222   -F*|-f*)
223       no_edit=t
224       log_given=t$log_given
225       logfile=`expr "$1" : '-[Ff]\(.*\)'`
226       shift
227       ;;
228   --F=*|--f=*|--fi=*|--fil=*|--file=*)
229       no_edit=t
230       log_given=t$log_given
231       logfile=`expr "$1" : '-[^=]*=\(.*\)'`
232       shift
233       ;;
234   -a|--a|--al|--all)
235       all=t
236       shift
237       ;;
238   --au=*|--aut=*|--auth=*|--autho=*|--author=*)
239       force_author=`expr "$1" : '-[^=]*=\(.*\)'`
240       shift
241       ;;
242   --au|--aut|--auth|--autho|--author)
243       case "$#" in 1) usage ;; esac
244       shift
245       force_author="$1"
246       shift
247       ;;
248   -e|--e|--ed|--edi|--edit)
249       no_edit=
250       shift
251       ;;
252   -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
253       also=t
254       shift
255       ;;
256   -o|--o|--on|--onl|--only)
257       only=t
258       shift
259       ;;
260   -m|--m|--me|--mes|--mess|--messa|--messag|--message)
261       case "$#" in 1) usage ;; esac
262       shift
263       log_given=m$log_given
264       if test "$log_message" = ''
265       then
266           log_message="$1"
267       else
268           log_message="$log_message
269
270 $1"
271       fi
272       no_edit=t
273       shift
274       ;;
275   -m*)
276       log_given=m$log_given
277       if test "$log_message" = ''
278       then
279           log_message=`expr "$1" : '-m\(.*\)'`
280       else
281           log_message="$log_message
282
283 `expr "$1" : '-m\(.*\)'`"
284       fi
285       no_edit=t
286       shift
287       ;;
288   --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
289       log_given=m$log_given
290       if test "$log_message" = ''
291       then
292           log_message=`expr "$1" : '-[^=]*=\(.*\)'`
293       else
294           log_message="$log_message
295
296 `expr "$1" : '-[^=]*=\(.*\)'`"
297       fi
298       no_edit=t
299       shift
300       ;;
301   -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
302       verify=
303       shift
304       ;;
305   --a|--am|--ame|--amen|--amend)
306       amend=t
307       log_given=t$log_given
308       use_commit=HEAD
309       shift
310       ;;
311   -c)
312       case "$#" in 1) usage ;; esac
313       shift
314       log_given=t$log_given
315       use_commit="$1"
316       no_edit=
317       shift
318       ;;
319   --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
320   --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
321   --reedit-messag=*|--reedit-message=*)
322       log_given=t$log_given
323       use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
324       no_edit=
325       shift
326       ;;
327   --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
328   --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
329       case "$#" in 1) usage ;; esac
330       shift
331       log_given=t$log_given
332       use_commit="$1"
333       no_edit=
334       shift
335       ;;
336   -C)
337       case "$#" in 1) usage ;; esac
338       shift
339       log_given=t$log_given
340       use_commit="$1"
341       no_edit=t
342       shift
343       ;;
344   --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
345   --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
346   --reuse-message=*)
347       log_given=t$log_given
348       use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
349       no_edit=t
350       shift
351       ;;
352   --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
353   --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
354       case "$#" in 1) usage ;; esac
355       shift
356       log_given=t$log_given
357       use_commit="$1"
358       no_edit=t
359       shift
360       ;;
361   -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
362       signoff=t
363       shift
364       ;;
365   -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
366       verbose=t
367       shift
368       ;;
369   -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
370   --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
371   --untracked-files)
372       untracked_files=t
373       shift
374       ;;
375   --)
376       shift
377       break
378       ;;
379   -*)
380       usage
381       ;;
382   *)
383       break
384       ;;
385   esac
386 done
387
388 ################################################################
389 # Sanity check options
390
391 case "$amend,$initial_commit" in
392 t,t)
393   die "You do not have anything to amend." ;;
394 t,)
395   if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
396     die "You are in the middle of a merge -- cannot amend."
397   fi ;;
398 esac
399
400 case "$log_given" in
401 tt*)
402   die "Only one of -c/-C/-F can be used." ;;
403 *tm*|*mt*)
404   die "Option -m cannot be combined with -c/-C/-F." ;;
405 esac
406
407 case "$#,$also,$only,$amend" in
408 *,t,t,*)
409   die "Only one of --include/--only can be used." ;;
410 0,t,,* | 0,,t,)
411   die "No paths with --include/--only does not make sense." ;;
412 0,,t,t)
413   only_include_assumed="# Clever... amending the last one with dirty index." ;;
414 0,,,*)
415   ;;
416 *,,,*)
417   only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
418   also=
419   ;;
420 esac
421 unset only
422 case "$all,$also,$#" in
423 t,t,*)
424         die "Cannot use -a and -i at the same time." ;;
425 t,,[1-9]*)
426         die "Paths with -a does not make sense." ;;
427 ,t,0)
428         die "No paths with -i does not make sense." ;;
429 esac
430
431 ################################################################
432 # Prepare index to have a tree to be committed
433
434 TOP=`git-rev-parse --show-cdup`
435 if test -z "$TOP"
436 then
437         TOP=./
438 fi
439
440 case "$all,$also" in
441 t,)
442         save_index &&
443         (
444                 cd "$TOP"
445                 GIT_INDEX_FILE="$NEXT_INDEX"
446                 export GIT_INDEX_FILE
447                 git-diff-files --name-only -z |
448                 git-update-index --remove -z --stdin
449         )
450         ;;
451 ,t)
452         save_index &&
453         git-ls-files --error-unmatch -- "$@" >/dev/null || exit
454
455         git-diff-files --name-only -z -- "$@"  |
456         (
457                 cd "$TOP"
458                 GIT_INDEX_FILE="$NEXT_INDEX"
459                 export GIT_INDEX_FILE
460                 git-update-index --remove -z --stdin
461         )
462         ;;
463 ,)
464         case "$#" in
465         0)
466             ;; # commit as-is
467         *)
468             if test -f "$GIT_DIR/MERGE_HEAD"
469             then
470                 refuse_partial "Cannot do a partial commit during a merge."
471             fi
472             TMP_INDEX="$GIT_DIR/tmp-index$$"
473             if test -z "$initial_commit"
474             then
475                 # make sure index is clean at the specified paths, or
476                 # they are additions.
477                 dirty_in_index=`git-diff-index --cached --name-status \
478                         --diff-filter=DMTU HEAD -- "$@"`
479                 test -z "$dirty_in_index" ||
480                 refuse_partial "Different in index and the last commit:
481 $dirty_in_index"
482             fi
483             commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
484
485             # Build the temporary index and update the real index
486             # the same way.
487             if test -z "$initial_commit"
488             then
489                 cp "$THIS_INDEX" "$TMP_INDEX"
490                 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
491             else
492                     rm -f "$TMP_INDEX"
493             fi || exit
494
495             echo "$commit_only" |
496             GIT_INDEX_FILE="$TMP_INDEX" \
497             git-update-index --add --remove --stdin &&
498
499             save_index &&
500             echo "$commit_only" |
501             (
502                 GIT_INDEX_FILE="$NEXT_INDEX"
503                 export GIT_INDEX_FILE
504                 git-update-index --remove --stdin
505             ) || exit
506             ;;
507         esac
508         ;;
509 esac
510
511 ################################################################
512 # If we do as-is commit, the index file will be THIS_INDEX,
513 # otherwise NEXT_INDEX after we make this commit.  We leave
514 # the index as is if we abort.
515
516 if test -f "$NEXT_INDEX"
517 then
518         USE_INDEX="$NEXT_INDEX"
519 else
520         USE_INDEX="$THIS_INDEX"
521 fi
522
523 GIT_INDEX_FILE="$USE_INDEX" \
524     git-update-index -q $unmerged_ok_if_status --refresh || exit
525
526 ################################################################
527 # If the request is status, just show it and exit.
528
529 case "$0" in
530 *status)
531         run_status
532         exit $?
533 esac
534
535 ################################################################
536 # Grab commit message, write out tree and make commit.
537
538 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
539 then
540         if test "$TMP_INDEX"
541         then
542                 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
543         else
544                 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
545         fi || exit
546 fi
547
548 if test "$log_message" != ''
549 then
550         echo "$log_message"
551 elif test "$logfile" != ""
552 then
553         if test "$logfile" = -
554         then
555                 test -t 0 &&
556                 echo >&2 "(reading log message from standard input)"
557                 cat
558         else
559                 cat <"$logfile"
560         fi
561 elif test "$use_commit" != ""
562 then
563         git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
564 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
565 then
566         cat "$GIT_DIR/MERGE_MSG"
567 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
568
569 case "$signoff" in
570 t)
571         {
572                 echo
573                 git-var GIT_COMMITTER_IDENT | sed -e '
574                         s/>.*/>/
575                         s/^/Signed-off-by: /
576                 '
577         } >>"$GIT_DIR"/COMMIT_EDITMSG
578         ;;
579 esac
580
581 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
582         echo "#"
583         echo "# It looks like you may be committing a MERGE."
584         echo "# If this is not correct, please remove the file"
585         echo "# $GIT_DIR/MERGE_HEAD"
586         echo "# and try again"
587         echo "#"
588 fi >>"$GIT_DIR"/COMMIT_EDITMSG
589
590 # Author
591 if test '' != "$force_author"
592 then
593         GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
594         GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
595         test '' != "$GIT_AUTHOR_NAME" &&
596         test '' != "$GIT_AUTHOR_EMAIL" ||
597         die "malformatted --author parameter"
598         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
599 elif test '' != "$use_commit"
600 then
601         pick_author_script='
602         /^author /{
603                 s/'\''/'\''\\'\'\''/g
604                 h
605                 s/^author \([^<]*\) <[^>]*> .*$/\1/
606                 s/'\''/'\''\'\'\''/g
607                 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
608
609                 g
610                 s/^author [^<]* <\([^>]*\)> .*$/\1/
611                 s/'\''/'\''\'\'\''/g
612                 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
613
614                 g
615                 s/^author [^<]* <[^>]*> \(.*\)$/\1/
616                 s/'\''/'\''\'\'\''/g
617                 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
618
619                 q
620         }
621         '
622         set_author_env=`git-cat-file commit "$use_commit" |
623         LANG=C LC_ALL=C sed -ne "$pick_author_script"`
624         eval "$set_author_env"
625         export GIT_AUTHOR_NAME
626         export GIT_AUTHOR_EMAIL
627         export GIT_AUTHOR_DATE
628 fi
629
630 PARENTS="-p HEAD"
631 if test -z "$initial_commit"
632 then
633         if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
634                 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
635         elif test -n "$amend"; then
636                 PARENTS=$(git-cat-file commit HEAD |
637                         sed -n -e '/^$/q' -e 's/^parent /-p /p')
638         fi
639         current=$(git-rev-parse --verify HEAD)
640 else
641         if [ -z "$(git-ls-files)" ]; then
642                 echo >&2 Nothing to commit
643                 exit 1
644         fi
645         PARENTS=""
646         current=
647 fi
648
649 if test -z "$no_edit"
650 then
651         {
652                 echo ""
653                 echo "# Please enter the commit message for your changes."
654                 echo "# (Comment lines starting with '#' will not be included)"
655                 test -z "$only_include_assumed" || echo "$only_include_assumed"
656                 run_status
657         } >>"$GIT_DIR"/COMMIT_EDITMSG
658 else
659         # we need to check if there is anything to commit
660         run_status >/dev/null 
661 fi
662 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
663 then
664         rm -f "$GIT_DIR/COMMIT_EDITMSG"
665         run_status
666         exit 1
667 fi
668
669 case "$no_edit" in
670 '')
671         case "${VISUAL:-$EDITOR},$TERM" in
672         ,dumb)
673                 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
674                 echo >&2 "Please supply the commit log message using either"
675                 echo >&2 "-m or -F option.  A boilerplate log message has"
676                 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
677                 exit 1
678                 ;;
679         esac
680         git-var GIT_AUTHOR_IDENT > /dev/null  || die
681         git-var GIT_COMMITTER_IDENT > /dev/null  || die
682         ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
683         ;;
684 esac
685
686 case "$verify" in
687 t)
688         if test -x "$GIT_DIR"/hooks/commit-msg
689         then
690                 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
691         fi
692 esac
693
694 sed -e '
695     /^diff --git a\/.*/{
696         s///
697         q
698     }
699     /^#/d
700 ' "$GIT_DIR"/COMMIT_EDITMSG |
701 git-stripspace >"$GIT_DIR"/COMMIT_MSG
702
703 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
704         git-stripspace |
705         wc -l` &&
706    test 0 -lt $cnt
707 then
708         if test -z "$TMP_INDEX"
709         then
710                 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
711         else
712                 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
713                 rm -f "$TMP_INDEX"
714         fi &&
715         commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
716         rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
717         git-update-ref -m "commit: $rlogm" HEAD $commit $current &&
718         rm -f -- "$GIT_DIR/MERGE_HEAD" &&
719         if test -f "$NEXT_INDEX"
720         then
721                 mv "$NEXT_INDEX" "$THIS_INDEX"
722         else
723                 : ;# happy
724         fi
725 else
726         echo >&2 "* no commit message?  aborting commit."
727         false
728 fi
729 ret="$?"
730 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
731 if test -d "$GIT_DIR/rr-cache"
732 then
733         git-rerere
734 fi
735
736 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
737 then
738         "$GIT_DIR"/hooks/post-commit
739 fi
740 exit "$ret"