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