Merge branch 'jc/commit'
[git.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>] [-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         # If TMP_INDEX is defined, that means we are doing
68         # "--only" partial commit, and that index file is used
69         # to build the tree for the commit.  Otherwise, if
70         # NEXT_INDEX exists, that is the index file used to
71         # make the commit.  Otherwise we are using as-is commit
72         # so the regular index file is what we use to compare.
73         if test '' != "$TMP_INDEX"
74         then
75             GIT_INDEX_FILE="$TMP_INDEX"
76             export GIT_INDEX_FILE
77         elif test -f "$NEXT_INDEX"
78         then
79             GIT_INDEX_FILE="$NEXT_INDEX"
80             export GIT_INDEX_FILE
81         fi
82
83         case "$branch" in
84         refs/heads/master) ;;
85         *)  echo "# On branch $branch" ;;
86         esac
87
88         if test -z "$initial_commit"
89         then
90             git-diff-index -M --cached --name-status \
91                 --diff-filter=MDTCRA HEAD |
92             sed -e '
93                     s/\\/\\\\/g
94                     s/ /\\ /g
95             ' |
96             report "Updated but not checked in" "will commit"
97             committable="$?"
98         else
99             echo '#
100 # Initial commit
101 #'
102             git-ls-files |
103             sed -e '
104                     s/\\/\\\\/g
105                     s/ /\\ /g
106                     s/^/A /
107             ' |
108             report "Updated but not checked in" "will commit"
109
110             committable="$?"
111         fi
112
113         git-diff-files  --name-status |
114         sed -e '
115                 s/\\/\\\\/g
116                 s/ /\\ /g
117         ' |
118         report "Changed but not updated" \
119             "use git-update-index to mark for commit"
120
121         if test -f "$GIT_DIR/info/exclude"
122         then
123             git-ls-files -z --others --directory \
124                 --exclude-from="$GIT_DIR/info/exclude" \
125                 --exclude-per-directory=.gitignore
126         else
127             git-ls-files -z --others --directory \
128                 --exclude-per-directory=.gitignore
129         fi |
130         perl -e '$/ = "\0";
131             my $shown = 0;
132             while (<>) {
133                 chomp;
134                 s|\\|\\\\|g;
135                 s|\t|\\t|g;
136                 s|\n|\\n|g;
137                 s/^/#   /;
138                 if (!$shown) {
139                     print "#\n# Untracked files:\n";
140                     print "#   (use \"git add\" to add to commit)\n";
141                     print "#\n";
142                     $shown = 1;
143                 }
144                 print "$_\n";
145             }
146         '
147
148         if test -n "$verbose"
149         then
150             git-diff-index --cached -M -p --diff-filter=MDTCRA HEAD
151         fi
152         case "$committable" in
153         0)
154             echo "nothing to commit"
155             exit 1
156         esac
157         exit 0
158     )
159 }
160
161 trap '
162         test -z "$TMP_INDEX" || {
163                 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
164         }
165         rm -f "$NEXT_INDEX"
166 ' 0
167
168 ################################################################
169 # Command line argument parsing and sanity checking
170
171 all=
172 also=
173 only=
174 logfile=
175 use_commit=
176 no_edit=
177 log_given=
178 log_message=
179 verify=t
180 verbose=
181 signoff=
182 force_author=
183 while case "$#" in 0) break;; esac
184 do
185   case "$1" in
186   -F|--F|-f|--f|--fi|--fil|--file)
187       case "$#" in 1) usage ;; esac
188       shift
189       no_edit=t
190       log_given=t$log_given
191       logfile="$1"
192       shift
193       ;;
194   -F*|-f*)
195       no_edit=t
196       log_given=t$log_given
197       logfile=`expr "$1" : '-[Ff]\(.*\)'`
198       shift
199       ;;
200   --F=*|--f=*|--fi=*|--fil=*|--file=*)
201       no_edit=t
202       log_given=t$log_given
203       logfile=`expr "$1" : '-[^=]*=\(.*\)'`
204       shift
205       ;;
206   -a|--a|--al|--all)
207       all=t
208       shift
209       ;;
210   --au=*|--aut=*|--auth=*|--autho=*|--author=*)
211       force_author=`expr "$1" : '-[^=]*=\(.*\)'`
212       shift
213       ;;
214   --au|--aut|--auth|--autho|--author)
215       case "$#" in 1) usage ;; esac
216       shift
217       force_author="$1"
218       shift
219       ;;
220   -e|--e|--ed|--edi|--edit)
221       no_edit=
222       shift
223       ;;
224   -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
225       also=t
226       shift
227       ;;
228   -o|--o|--on|--onl|--only)
229       only=t
230       shift
231       ;;
232   -m|--m|--me|--mes|--mess|--messa|--messag|--message)
233       case "$#" in 1) usage ;; esac
234       shift
235       log_given=t$log_given
236       log_message="$1"
237       no_edit=t
238       shift
239       ;;
240   -m*)
241       log_given=t$log_given
242       log_message=`expr "$1" : '-m\(.*\)'`
243       no_edit=t
244       shift
245       ;;
246   --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
247       log_given=t$log_given
248       log_message=`expr "$1" : '-[^=]*=\(.*\)'`
249       no_edit=t
250       shift
251       ;;
252   -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
253       verify=
254       shift
255       ;;
256   -c)
257       case "$#" in 1) usage ;; esac
258       shift
259       log_given=t$log_given
260       use_commit="$1"
261       no_edit=
262       shift
263       ;;
264   --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
265   --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
266   --reedit-messag=*|--reedit-message=*)
267       log_given=t$log_given
268       use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
269       no_edit=
270       shift
271       ;;
272   --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
273   --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
274       case "$#" in 1) usage ;; esac
275       shift
276       log_given=t$log_given
277       use_commit="$1"
278       no_edit=
279       shift
280       ;;
281   -C)
282       case "$#" in 1) usage ;; esac
283       shift
284       log_given=t$log_given
285       use_commit="$1"
286       no_edit=t
287       shift
288       ;;
289   --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
290   --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
291   --reuse-message=*)
292       log_given=t$log_given
293       use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
294       no_edit=t
295       shift
296       ;;
297   --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
298   --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
299       case "$#" in 1) usage ;; esac
300       shift
301       log_given=t$log_given
302       use_commit="$1"
303       no_edit=t
304       shift
305       ;;
306   -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
307       signoff=t
308       shift
309       ;;
310   -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
311       verbose=t
312       shift
313       ;;
314   --)
315       shift
316       break
317       ;;
318   -*)
319       usage
320       ;;
321   *)
322       break
323       ;;
324   esac
325 done
326
327 ################################################################
328 # Sanity check options
329
330 case "$log_given" in
331 tt*)
332   die "Only one of -c/-C/-F/-m can be used." ;;
333 esac
334
335 case "$#,$also$only" in
336 *,tt)
337   die "Only one of --include/--only can be used." ;;
338 0,t)
339   die "No paths with --include/--only does not make sense." ;;
340 0,)
341   ;;
342 *,)
343   echo >&2 "assuming --only paths..."
344   also=
345
346   # If we are going to launch an editor, the message won't be
347   # shown without this...
348   test -z "$log_given$status_only" && sleep 1
349   ;;
350 esac
351 unset only
352 case "$all,$also,$#" in
353 t,t,*)
354         die "Cannot use -a and -i at the same time." ;;
355 t,,[1-9]*)
356         die "Paths with -a does not make sense." ;;
357 ,t,0)
358         die "No paths with -i does not make sense." ;;
359 esac
360
361 ################################################################
362 # Prepare index to have a tree to be committed
363
364 TOP=`git-rev-parse --show-cdup`
365 if test -z "$TOP"
366 then
367         TOP=./
368 fi
369
370 case "$all,$also" in
371 t,)
372         save_index &&
373         (
374                 cd "$TOP"
375                 GIT_INDEX_FILE="$NEXT_INDEX"
376                 export GIT_INDEX_FILE
377                 git-diff-files --name-only -z |
378                 git-update-index --remove -z --stdin
379         )
380         ;;
381 ,t)
382         save_index &&
383         git-diff-files --name-only -z -- "$@"  |
384         (
385                 cd "$TOP"
386                 GIT_INDEX_FILE="$NEXT_INDEX"
387                 export GIT_INDEX_FILE
388                 git-update-index --remove -z --stdin
389         )
390         ;;
391 ,)
392         case "$#" in
393         0)
394             ;; # commit as-is
395         *)
396             if test -f "$GIT_DIR/MERGE_HEAD"
397             then
398                 refuse_partial "Cannot do a partial commit during a merge."
399             fi
400             TMP_INDEX="$GIT_DIR/tmp-index$$"
401             if test -z "$initial_commit"
402             then
403                 # make sure index is clean at the specified paths, or
404                 # they are additions.
405                 dirty_in_index=`git-diff-index --cached --name-status \
406                         --diff-filter=DMTU HEAD -- "$@"`
407                 test -z "$dirty_in_index" ||
408                 refuse_partial "Different in index and the last commit:
409 $dirty_in_index"
410             fi
411             commit_only=`git-ls-files -- "$@"`
412
413             # Build the temporary index and update the real index
414             # the same way.
415             if test -z "$initial_commit"
416             then
417                 cp "$THIS_INDEX" "$TMP_INDEX"
418                 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
419             else
420                     rm -f "$TMP_INDEX"
421             fi || exit
422
423             echo "$commit_only" |
424             GIT_INDEX_FILE="$TMP_INDEX" \
425             git-update-index --add --remove --stdin &&
426
427             save_index &&
428             echo "$commit_only" |
429             (
430                 GIT_INDEX_FILE="$NEXT_INDEX"
431                 export GIT_INDEX_FILE
432                 git-update-index --remove --stdin
433             ) || exit
434             ;;
435         esac
436         ;;
437 esac
438
439 ################################################################
440 # If we do as-is commit, the index file will be THIS_INDEX,
441 # otherwise NEXT_INDEX after we make this commit.  We leave
442 # the index as is if we abort.
443
444 if test -f "$NEXT_INDEX"
445 then
446         USE_INDEX="$NEXT_INDEX"
447 else
448         USE_INDEX="$THIS_INDEX"
449 fi
450
451 GIT_INDEX_FILE="$USE_INDEX" \
452     git-update-index -q $unmerged_ok_if_status --refresh || exit
453
454 ################################################################
455 # If the request is status, just show it and exit.
456
457 case "$0" in
458 *status)
459         run_status
460         exit $?
461 esac
462
463 ################################################################
464 # Grab commit message, write out tree and make commit.
465
466 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
467 then
468         if test "$TMP_INDEX"
469         then
470                 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
471         else
472                 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
473         fi || exit
474 fi
475
476 if test "$log_message" != ''
477 then
478         echo "$log_message"
479 elif test "$logfile" != ""
480 then
481         if test "$logfile" = -
482         then
483                 test -t 0 &&
484                 echo >&2 "(reading log message from standard input)"
485                 cat
486         else
487                 cat <"$logfile"
488         fi
489 elif test "$use_commit" != ""
490 then
491         git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
492 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
493 then
494         cat "$GIT_DIR/MERGE_MSG"
495 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
496
497 case "$signoff" in
498 t)
499         {
500                 echo
501                 git-var GIT_COMMITTER_IDENT | sed -e '
502                         s/>.*/>/
503                         s/^/Signed-off-by: /
504                 '
505         } >>"$GIT_DIR"/COMMIT_EDITMSG
506         ;;
507 esac
508
509 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
510         echo "#"
511         echo "# It looks like you may be committing a MERGE."
512         echo "# If this is not correct, please remove the file"
513         echo "# $GIT_DIR/MERGE_HEAD"
514         echo "# and try again"
515         echo "#"
516 fi >>"$GIT_DIR"/COMMIT_EDITMSG
517
518 # Author
519 if test '' != "$force_author"
520 then
521         GIT_AUTHOR_NAME=`expr "$force_author" : '\(.*[^ ]\) *<.*'` &&
522         GIT_AUTHOR_EMAIL=`expr "$force_author" : '.*\(<.*\)'` &&
523         test '' != "$GIT_AUTHOR_NAME" &&
524         test '' != "$GIT_AUTHOR_EMAIL" ||
525         die "malformatted --author parameter"
526         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
527 elif test '' != "$use_commit"
528 then
529         pick_author_script='
530         /^author /{
531                 s/'\''/'\''\\'\'\''/g
532                 h
533                 s/^author \([^<]*\) <[^>]*> .*$/\1/
534                 s/'\''/'\''\'\'\''/g
535                 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
536
537                 g
538                 s/^author [^<]* <\([^>]*\)> .*$/\1/
539                 s/'\''/'\''\'\'\''/g
540                 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
541
542                 g
543                 s/^author [^<]* <[^>]*> \(.*\)$/\1/
544                 s/'\''/'\''\'\'\''/g
545                 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
546
547                 q
548         }
549         '
550         set_author_env=`git-cat-file commit "$use_commit" |
551         LANG=C LC_ALL=C sed -ne "$pick_author_script"`
552         eval "$set_author_env"
553         export GIT_AUTHOR_NAME
554         export GIT_AUTHOR_EMAIL
555         export GIT_AUTHOR_DATE
556 fi
557
558 PARENTS="-p HEAD"
559 if test -z "$initial_commit"
560 then
561         if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
562                 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
563         fi
564 else
565         if [ -z "$(git-ls-files)" ]; then
566                 echo >&2 Nothing to commit
567                 exit 1
568         fi
569         PARENTS=""
570 fi
571
572 run_status >>"$GIT_DIR"/COMMIT_EDITMSG
573 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
574 then
575         rm -f "$GIT_DIR/COMMIT_EDITMSG"
576         run_status
577         exit 1
578 fi
579 case "$no_edit" in
580 '')
581         case "${VISUAL:-$EDITOR},$TERM" in
582         ,dumb)
583                 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
584                 echo >&2 "Please supply the commit log message using either"
585                 echo >&2 "-m or -F option.  A boilerplate log message has"
586                 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
587                 exit 1
588                 ;;
589         esac
590         ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
591         ;;
592 esac
593
594 case "$verify" in
595 t)
596         if test -x "$GIT_DIR"/hooks/commit-msg
597         then
598                 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
599         fi
600 esac
601
602 sed -e '
603     /^diff --git a\/.*/{
604         s///
605         q
606     }
607     /^#/d
608 ' "$GIT_DIR"/COMMIT_EDITMSG |
609 git-stripspace >"$GIT_DIR"/COMMIT_MSG
610
611 if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
612         git-stripspace |
613         wc -l` &&
614    test 0 -lt $cnt
615 then
616         if test -z "$TMP_INDEX"
617         then
618                 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
619         else
620                 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
621                 rm -f "$TMP_INDEX"
622         fi &&
623         commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
624         git-update-ref HEAD $commit $current &&
625         rm -f -- "$GIT_DIR/MERGE_HEAD" &&
626         if test -f "$NEXT_INDEX"
627         then
628                 mv "$NEXT_INDEX" "$THIS_INDEX"
629         else
630                 : ;# happy
631         fi
632 else
633         echo >&2 "* no commit message?  aborting commit."
634         false
635 fi
636 ret="$?"
637 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
638 if test -d "$GIT_DIR/rr-cache"
639 then
640         git-rerere
641 fi
642
643 if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
644 then
645         "$GIT_DIR"/hooks/post-commit
646 fi
647 exit "$ret"