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