git.el: Take into account the core.excludesfile config option.
[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 | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]'
7 SUBDIRECTORY_OK=Yes
8 . git-sh-setup
9 require_work_tree
10
11 git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
12
13 case "$0" in
14 *status)
15         status_only=t
16         ;;
17 *commit)
18         status_only=
19         ;;
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 -p "$THIS_INDEX" "$NEXT_INDEX"
33 }
34
35 run_status () {
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"
43         then
44                 GIT_INDEX_FILE="$TMP_INDEX"
45                 export GIT_INDEX_FILE
46         elif test -f "$NEXT_INDEX"
47         then
48                 GIT_INDEX_FILE="$NEXT_INDEX"
49                 export GIT_INDEX_FILE
50         fi
51
52         case "$status_only" in
53         t) color= ;;
54         *) color=--nocolor ;;
55         esac
56         git runstatus ${color} \
57                 ${verbose:+--verbose} \
58                 ${amend:+--amend} \
59                 ${untracked_files:+--untracked}
60 }
61
62 trap '
63         test -z "$TMP_INDEX" || {
64                 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
65         }
66         rm -f "$NEXT_INDEX"
67 ' 0
68
69 ################################################################
70 # Command line argument parsing and sanity checking
71
72 all=
73 also=
74 interactive=
75 only=
76 logfile=
77 use_commit=
78 amend=
79 edit_flag=
80 no_edit=
81 log_given=
82 log_message=
83 verify=t
84 quiet=
85 verbose=
86 signoff=
87 force_author=
88 only_include_assumed=
89 untracked_files=
90 templatefile="`git config commit.template`"
91 while case "$#" in 0) break;; esac
92 do
93         case "$1" in
94         -F|--F|-f|--f|--fi|--fil|--file)
95                 case "$#" in 1) usage ;; esac
96                 shift
97                 no_edit=t
98                 log_given=t$log_given
99                 logfile="$1"
100                 shift
101                 ;;
102         -F*|-f*)
103                 no_edit=t
104                 log_given=t$log_given
105                 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
106                 shift
107                 ;;
108         --F=*|--f=*|--fi=*|--fil=*|--file=*)
109                 no_edit=t
110                 log_given=t$log_given
111                 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
112                 shift
113                 ;;
114         -a|--a|--al|--all)
115                 all=t
116                 shift
117                 ;;
118         --au=*|--aut=*|--auth=*|--autho=*|--author=*)
119                 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
120                 shift
121                 ;;
122         --au|--aut|--auth|--autho|--author)
123                 case "$#" in 1) usage ;; esac
124                 shift
125                 force_author="$1"
126                 shift
127                 ;;
128         -e|--e|--ed|--edi|--edit)
129                 edit_flag=t
130                 shift
131                 ;;
132         -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
133                 also=t
134                 shift
135                 ;;
136         --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
137         --interactiv|--interactive)
138                 interactive=t
139                 shift
140                 ;;
141         -o|--o|--on|--onl|--only)
142                 only=t
143                 shift
144                 ;;
145         -m|--m|--me|--mes|--mess|--messa|--messag|--message)
146                 case "$#" in 1) usage ;; esac
147                 shift
148                 log_given=m$log_given
149                 if test "$log_message" = ''
150                 then
151                     log_message="$1"
152                 else
153                     log_message="$log_message
154
155 $1"
156                 fi
157                 no_edit=t
158                 shift
159                 ;;
160         -m*)
161                 log_given=m$log_given
162                 if test "$log_message" = ''
163                 then
164                     log_message=`expr "z$1" : 'z-m\(.*\)'`
165                 else
166                     log_message="$log_message
167
168 `expr "z$1" : 'z-m\(.*\)'`"
169                 fi
170                 no_edit=t
171                 shift
172                 ;;
173         --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
174                 log_given=m$log_given
175                 if test "$log_message" = ''
176                 then
177                     log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
178                 else
179                     log_message="$log_message
180
181 `expr "z$1" : 'zq-[^=]*=\(.*\)'`"
182                 fi
183                 no_edit=t
184                 shift
185                 ;;
186         -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
187         --no-verify)
188                 verify=
189                 shift
190                 ;;
191         --a|--am|--ame|--amen|--amend)
192                 amend=t
193                 log_given=t$log_given
194                 use_commit=HEAD
195                 shift
196                 ;;
197         -c)
198                 case "$#" in 1) usage ;; esac
199                 shift
200                 log_given=t$log_given
201                 use_commit="$1"
202                 no_edit=
203                 shift
204                 ;;
205         --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
206         --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
207         --reedit-messag=*|--reedit-message=*)
208                 log_given=t$log_given
209                 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
210                 no_edit=
211                 shift
212                 ;;
213         --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
214         --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
215         --reedit-message)
216                 case "$#" in 1) usage ;; esac
217                 shift
218                 log_given=t$log_given
219                 use_commit="$1"
220                 no_edit=
221                 shift
222                 ;;
223         -C)
224                 case "$#" in 1) usage ;; esac
225                 shift
226                 log_given=t$log_given
227                 use_commit="$1"
228                 no_edit=t
229                 shift
230                 ;;
231         --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
232         --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
233         --reuse-message=*)
234                 log_given=t$log_given
235                 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
236                 no_edit=t
237                 shift
238                 ;;
239         --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
240         --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
241                 case "$#" in 1) usage ;; esac
242                 shift
243                 log_given=t$log_given
244                 use_commit="$1"
245                 no_edit=t
246                 shift
247                 ;;
248         -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
249                 signoff=t
250                 shift
251                 ;;
252         -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
253                 case "$#" in 1) usage ;; esac
254                 shift
255                 templatefile="$1"
256                 no_edit=
257                 shift
258                 ;;
259         -q|--q|--qu|--qui|--quie|--quiet)
260                 quiet=t
261                 shift
262                 ;;
263         -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
264                 verbose=t
265                 shift
266                 ;;
267         -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
268         --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
269         --untracked-file|--untracked-files)
270                 untracked_files=t
271                 shift
272                 ;;
273         --)
274                 shift
275                 break
276                 ;;
277         -*)
278                 usage
279                 ;;
280         *)
281                 break
282                 ;;
283         esac
284 done
285 case "$edit_flag" in t) no_edit= ;; esac
286
287 ################################################################
288 # Sanity check options
289
290 case "$amend,$initial_commit" in
291 t,t)
292         die "You do not have anything to amend." ;;
293 t,)
294         if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
295                 die "You are in the middle of a merge -- cannot amend."
296         fi ;;
297 esac
298
299 case "$log_given" in
300 tt*)
301         die "Only one of -c/-C/-F/--amend can be used." ;;
302 *tm*|*mt*)
303         die "Option -m cannot be combined with -c/-C/-F/--amend." ;;
304 esac
305
306 case "$#,$also,$only,$amend" in
307 *,t,t,*)
308         die "Only one of --include/--only can be used." ;;
309 0,t,,* | 0,,t,)
310         die "No paths with --include/--only does not make sense." ;;
311 0,,t,t)
312         only_include_assumed="# Clever... amending the last one with dirty index." ;;
313 0,,,*)
314         ;;
315 *,,,*)
316         only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
317         also=
318         ;;
319 esac
320 unset only
321 case "$all,$interactive,$also,$#" in
322 *t,*t,*)
323         die "Cannot use -a, --interactive or -i at the same time." ;;
324 t,,[1-9]*)
325         die "Paths with -a does not make sense." ;;
326 ,t,[1-9]*)
327         die "Paths with --interactive does not make sense." ;;
328 ,,t,0)
329         die "No paths with -i does not make sense." ;;
330 esac
331
332 if test ! -z "$templatefile" -a -z "$log_given"
333 then
334         if test ! -f "$templatefile"
335         then
336                 die "Commit template file does not exist."
337         fi
338 fi
339
340 ################################################################
341 # Prepare index to have a tree to be committed
342
343 case "$all,$also" in
344 t,)
345         if test ! -f "$THIS_INDEX"
346         then
347                 die 'nothing to commit (use "git add file1 file2" to include for commit)'
348         fi
349         save_index &&
350         (
351                 cd_to_toplevel &&
352                 GIT_INDEX_FILE="$NEXT_INDEX" &&
353                 export GIT_INDEX_FILE &&
354                 git diff-files --name-only -z |
355                 git update-index --remove -z --stdin
356         ) || exit
357         ;;
358 ,t)
359         save_index &&
360         git ls-files --error-unmatch -- "$@" >/dev/null || exit
361
362         git diff-files --name-only -z -- "$@"  |
363         (
364                 cd_to_toplevel &&
365                 GIT_INDEX_FILE="$NEXT_INDEX" &&
366                 export GIT_INDEX_FILE &&
367                 git update-index --remove -z --stdin
368         ) || exit
369         ;;
370 ,)
371         if test "$interactive" = t; then
372                 git add --interactive || exit
373         fi
374         case "$#" in
375         0)
376                 ;; # commit as-is
377         *)
378                 if test -f "$GIT_DIR/MERGE_HEAD"
379                 then
380                         refuse_partial "Cannot do a partial commit during a merge."
381                 fi
382                 TMP_INDEX="$GIT_DIR/tmp-index$$"
383                 commit_only=`git ls-files --error-unmatch -- "$@"` || exit
384
385                 # Build a temporary index and update the real index
386                 # the same way.
387                 if test -z "$initial_commit"
388                 then
389                         GIT_INDEX_FILE="$THIS_INDEX" \
390                         git read-tree --index-output="$TMP_INDEX" -i -m HEAD
391                 else
392                         rm -f "$TMP_INDEX"
393                 fi || exit
394
395                 printf '%s\n' "$commit_only" |
396                 GIT_INDEX_FILE="$TMP_INDEX" \
397                 git update-index --add --remove --stdin &&
398
399                 save_index &&
400                 printf '%s\n' "$commit_only" |
401                 (
402                         GIT_INDEX_FILE="$NEXT_INDEX"
403                         export GIT_INDEX_FILE
404                         git update-index --remove --stdin
405                 ) || exit
406                 ;;
407         esac
408         ;;
409 esac
410
411 ################################################################
412 # If we do as-is commit, the index file will be THIS_INDEX,
413 # otherwise NEXT_INDEX after we make this commit.  We leave
414 # the index as is if we abort.
415
416 if test -f "$NEXT_INDEX"
417 then
418         USE_INDEX="$NEXT_INDEX"
419 else
420         USE_INDEX="$THIS_INDEX"
421 fi
422
423 case "$status_only" in
424 t)
425         # This will silently fail in a read-only repository, which is
426         # what we want.
427         GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
428         run_status
429         exit $?
430         ;;
431 '')
432         GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
433         ;;
434 esac
435
436 ################################################################
437 # Grab commit message, write out tree and make commit.
438
439 if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
440 then
441         if test "$TMP_INDEX"
442         then
443                 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
444         else
445                 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
446         fi || exit
447 fi
448
449 if test "$log_message" != ''
450 then
451         printf '%s\n' "$log_message"
452 elif test "$logfile" != ""
453 then
454         if test "$logfile" = -
455         then
456                 test -t 0 &&
457                 echo >&2 "(reading log message from standard input)"
458                 cat
459         else
460                 cat <"$logfile"
461         fi
462 elif test "$use_commit" != ""
463 then
464         encoding=$(git config i18n.commitencoding || echo UTF-8)
465         git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
466         sed -e '1,/^$/d' -e 's/^    //'
467 elif test -f "$GIT_DIR/MERGE_MSG"
468 then
469         cat "$GIT_DIR/MERGE_MSG"
470 elif test -f "$GIT_DIR/SQUASH_MSG"
471 then
472         cat "$GIT_DIR/SQUASH_MSG"
473 elif test "$templatefile" != ""
474 then
475         cat "$templatefile"
476 fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
477
478 case "$signoff" in
479 t)
480         sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
481                 s/>.*/>/
482                 s/^/Signed-off-by: /
483                 ')
484         blank_before_signoff=
485         tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
486         grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
487 '
488         tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
489         grep "$sign"$ >/dev/null ||
490         printf '%s%s\n' "$blank_before_signoff" "$sign" \
491                 >>"$GIT_DIR"/COMMIT_EDITMSG
492         ;;
493 esac
494
495 if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
496         echo "#"
497         echo "# It looks like you may be committing a MERGE."
498         echo "# If this is not correct, please remove the file"
499         printf '%s\n' "#        $GIT_DIR/MERGE_HEAD"
500         echo "# and try again"
501         echo "#"
502 fi >>"$GIT_DIR"/COMMIT_EDITMSG
503
504 # Author
505 if test '' != "$use_commit"
506 then
507         eval "$(get_author_ident_from_commit "$use_commit")"
508         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
509 fi
510 if test '' != "$force_author"
511 then
512         GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
513         GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
514         test '' != "$GIT_AUTHOR_NAME" &&
515         test '' != "$GIT_AUTHOR_EMAIL" ||
516         die "malformed --author parameter"
517         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
518 fi
519
520 PARENTS="-p HEAD"
521 if test -z "$initial_commit"
522 then
523         rloga='commit'
524         if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
525                 rloga='commit (merge)'
526                 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
527         elif test -n "$amend"; then
528                 rloga='commit (amend)'
529                 PARENTS=$(git cat-file commit HEAD |
530                         sed -n -e '/^$/q' -e 's/^parent /-p /p')
531         fi
532         current="$(git rev-parse --verify HEAD)"
533 else
534         if [ -z "$(git ls-files)" ]; then
535                 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
536                 exit 1
537         fi
538         PARENTS=""
539         rloga='commit (initial)'
540         current=''
541 fi
542 set_reflog_action "$rloga"
543
544 if test -z "$no_edit"
545 then
546         {
547                 echo ""
548                 echo "# Please enter the commit message for your changes."
549                 echo "# (Comment lines starting with '#' will not be included)"
550                 test -z "$only_include_assumed" || echo "$only_include_assumed"
551                 run_status
552         } >>"$GIT_DIR"/COMMIT_EDITMSG
553 else
554         # we need to check if there is anything to commit
555         run_status >/dev/null
556 fi
557 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
558 then
559         rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
560         run_status
561         exit 1
562 fi
563
564 case "$no_edit" in
565 '')
566         git-var GIT_AUTHOR_IDENT > /dev/null  || die
567         git-var GIT_COMMITTER_IDENT > /dev/null  || die
568         git_editor "$GIT_DIR/COMMIT_EDITMSG"
569         ;;
570 esac
571
572 case "$verify" in
573 t)
574         if test -x "$GIT_DIR"/hooks/commit-msg
575         then
576                 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
577         fi
578 esac
579
580 if test -z "$no_edit"
581 then
582     sed -e '
583         /^diff --git a\/.*/{
584             s///
585             q
586         }
587         /^#/d
588     ' "$GIT_DIR"/COMMIT_EDITMSG
589 else
590     cat "$GIT_DIR"/COMMIT_EDITMSG
591 fi |
592 git stripspace >"$GIT_DIR"/COMMIT_MSG
593
594 # Test whether the commit message has any content we didn't supply.
595 have_commitmsg=
596 grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
597         git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
598
599 # Is the commit message totally empty?
600 if test -s "$GIT_DIR"/COMMIT_BAREMSG
601 then
602         if test "$templatefile" != ""
603         then
604                 # Test whether this is just the unaltered template.
605                 if cnt=`sed -e '/^#/d' < "$templatefile" |
606                         git stripspace |
607                         diff "$GIT_DIR"/COMMIT_BAREMSG - |
608                         wc -l` &&
609                    test 0 -lt $cnt
610                 then
611                         have_commitmsg=t
612                 fi
613         else
614                 # No template, so the content in the commit message must
615                 # have come from the user.
616                 have_commitmsg=t
617         fi
618 fi
619
620 rm -f "$GIT_DIR"/COMMIT_BAREMSG
621
622 if test "$have_commitmsg" = "t"
623 then
624         if test -z "$TMP_INDEX"
625         then
626                 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
627         else
628                 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
629                 rm -f "$TMP_INDEX"
630         fi &&
631         commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
632         rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
633         git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
634         rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
635         if test -f "$NEXT_INDEX"
636         then
637                 mv "$NEXT_INDEX" "$THIS_INDEX"
638         else
639                 : ;# happy
640         fi
641 else
642         echo >&2 "* no commit message?  aborting commit."
643         false
644 fi
645 ret="$?"
646 rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
647
648 cd_to_toplevel
649
650 git rerere
651
652 if test "$ret" = 0
653 then
654         if test -x "$GIT_DIR"/hooks/post-commit
655         then
656                 "$GIT_DIR"/hooks/post-commit
657         fi
658         if test -z "$quiet"
659         then
660                 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
661                        --summary --root HEAD --`
662                 echo "Created${initial_commit:+ initial} commit $commit"
663         fi
664 fi
665
666 exit "$ret"