]> git.ipfire.org Git - thirdparty/git.git/blame - git-commit.sh
Merge branch 'maint'
[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
JH
10git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
11branch=$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD)
12
13case "$0" in
14*status)
15 status_only=t
16 unmerged_ok_if_status=--unmerged ;;
17*commit)
18 status_only=
19 unmerged_ok_if_status= ;;
20esac
130fcca6
JH
21
22refuse_partial () {
23 echo >&2 "$1"
130fcca6
JH
24 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
26}
27
cf7bb589
JH
28THIS_INDEX="$GIT_DIR/index"
29NEXT_INDEX="$GIT_DIR/next-index$$"
30rm -f "$NEXT_INDEX"
5a798fb5 31save_index () {
cc7d5bcf 32 cp -p "$THIS_INDEX" "$NEXT_INDEX"
cf7bb589
JH
33}
34
5a798fb5 35run_status () {
cf7bb589
JH
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
3de63c3f
MW
44 GIT_INDEX_FILE="$TMP_INDEX"
45 export GIT_INDEX_FILE
cf7bb589
JH
46 elif test -f "$NEXT_INDEX"
47 then
3de63c3f
MW
48 GIT_INDEX_FILE="$NEXT_INDEX"
49 export GIT_INDEX_FILE
cf7bb589
JH
50 fi
51
3de63c3f
MW
52 case "$status_only" in
53 t) color= ;;
54 *) color=--nocolor ;;
55 esac
56 git-runstatus ${color} \
57 ${verbose:+--verbose} \
58 ${amend:+--amend} \
2074cb0a 59 ${untracked_files:+--untracked}
5a798fb5
JH
60}
61
cf7bb589
JH
62trap '
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
130fcca6
JH
72all=
73also=
5a798fb5 74only=
130fcca6
JH
75logfile=
76use_commit=
b4019f04 77amend=
cda8ab59 78edit_flag=
130fcca6
JH
79no_edit=
80log_given=
81log_message=
82verify=t
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 ;;
244 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
245 verbose=t
246 shift
247 ;;
248 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
249 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
250 --untracked-file|--untracked-files)
251 untracked_files=t
252 shift
253 ;;
254 --)
255 shift
256 break
257 ;;
258 -*)
259 usage
260 ;;
261 *)
262 break
263 ;;
264 esac
5fec3ef1 265done
cda8ab59 266case "$edit_flag" in t) no_edit= ;; esac
5fec3ef1 267
cf7bb589
JH
268################################################################
269# Sanity check options
270
b4019f04
JH
271case "$amend,$initial_commit" in
272t,t)
3de63c3f 273 die "You do not have anything to amend." ;;
b4019f04 274t,)
3de63c3f
MW
275 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
276 die "You are in the middle of a merge -- cannot amend."
277 fi ;;
b4019f04
JH
278esac
279
0c091296
JH
280case "$log_given" in
281tt*)
3de63c3f 282 die "Only one of -c/-C/-F can be used." ;;
6891281c 283*tm*|*mt*)
3de63c3f 284 die "Option -m cannot be combined with -c/-C/-F." ;;
0c091296
JH
285esac
286
6a74642c
JH
287case "$#,$also,$only,$amend" in
288*,t,t,*)
3de63c3f 289 die "Only one of --include/--only can be used." ;;
6a74642c 2900,t,,* | 0,,t,)
3de63c3f 291 die "No paths with --include/--only does not make sense." ;;
6a74642c 2920,,t,t)
3de63c3f 293 only_include_assumed="# Clever... amending the last one with dirty index." ;;
6a74642c 2940,,,*)
3de63c3f 295 ;;
6a74642c 296*,,,*)
3de63c3f
MW
297 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
298 also=
299 ;;
5a798fb5
JH
300esac
301unset only
cf7bb589
JH
302case "$all,$also,$#" in
303t,t,*)
304 die "Cannot use -a and -i at the same time." ;;
305t,,[1-9]*)
306 die "Paths with -a does not make sense." ;;
307,t,0)
308 die "No paths with -i does not make sense." ;;
309esac
310
311################################################################
312# Prepare index to have a tree to be committed
5a798fb5 313
130fcca6 314TOP=`git-rev-parse --show-cdup`
5a798fb5
JH
315if test -z "$TOP"
316then
317 TOP=./
318fi
130fcca6
JH
319
320case "$all,$also" in
130fcca6 321t,)
5a798fb5 322 save_index &&
130fcca6 323 (
5a798fb5 324 cd "$TOP"
cf7bb589
JH
325 GIT_INDEX_FILE="$NEXT_INDEX"
326 export GIT_INDEX_FILE
130fcca6
JH
327 git-diff-files --name-only -z |
328 git-update-index --remove -z --stdin
329 )
6a0049c0 330 ;;
130fcca6 331,t)
5a798fb5 332 save_index &&
bba319b5
JH
333 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
334
130fcca6 335 git-diff-files --name-only -z -- "$@" |
cf7bb589
JH
336 (
337 cd "$TOP"
338 GIT_INDEX_FILE="$NEXT_INDEX"
339 export GIT_INDEX_FILE
340 git-update-index --remove -z --stdin
341 )
22cff6a5 342 ;;
130fcca6
JH
343,)
344 case "$#" in
345 0)
3de63c3f 346 ;; # commit as-is
130fcca6 347 *)
3de63c3f
MW
348 if test -f "$GIT_DIR/MERGE_HEAD"
349 then
350 refuse_partial "Cannot do a partial commit during a merge."
351 fi
352 TMP_INDEX="$GIT_DIR/tmp-index$$"
353 if test -z "$initial_commit"
354 then
355 # make sure index is clean at the specified paths, or
356 # they are additions.
357 dirty_in_index=`git-diff-index --cached --name-status \
358 --diff-filter=DMTU HEAD -- "$@"`
359 test -z "$dirty_in_index" ||
360 refuse_partial "Different in index and the last commit:
130fcca6 361$dirty_in_index"
3de63c3f
MW
362 fi
363 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
cf7bb589 364
3de63c3f
MW
365 # Build the temporary index and update the real index
366 # the same way.
367 if test -z "$initial_commit"
368 then
369 cp "$THIS_INDEX" "$TMP_INDEX"
370 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
371 else
372 rm -f "$TMP_INDEX"
373 fi || exit
cf7bb589 374
3de63c3f
MW
375 echo "$commit_only" |
376 GIT_INDEX_FILE="$TMP_INDEX" \
377 git-update-index --add --remove --stdin &&
cf7bb589 378
3de63c3f
MW
379 save_index &&
380 echo "$commit_only" |
381 (
382 GIT_INDEX_FILE="$NEXT_INDEX"
383 export GIT_INDEX_FILE
384 git-update-index --remove --stdin
385 ) || exit
386 ;;
130fcca6
JH
387 esac
388 ;;
389esac
390
cf7bb589
JH
391################################################################
392# If we do as-is commit, the index file will be THIS_INDEX,
393# otherwise NEXT_INDEX after we make this commit. We leave
394# the index as is if we abort.
130fcca6 395
cf7bb589 396if test -f "$NEXT_INDEX"
130fcca6 397then
cf7bb589
JH
398 USE_INDEX="$NEXT_INDEX"
399else
400 USE_INDEX="$THIS_INDEX"
130fcca6
JH
401fi
402
cf7bb589 403GIT_INDEX_FILE="$USE_INDEX" \
3de63c3f 404 git-update-index -q $unmerged_ok_if_status --refresh || exit
cf7bb589
JH
405
406################################################################
407# If the request is status, just show it and exit.
408
409case "$0" in
410*status)
411 run_status
412 exit $?
413esac
414
415################################################################
416# Grab commit message, write out tree and make commit.
417
130fcca6
JH
418if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
419then
420 if test "$TMP_INDEX"
421 then
422 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
423 else
cf7bb589 424 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
130fcca6
JH
425 fi || exit
426fi
0cfe1d30 427
aba2da13
JH
428if test "$log_message" != ''
429then
430 echo "$log_message"
431elif test "$logfile" != ""
432then
433 if test "$logfile" = -
434 then
435 test -t 0 &&
436 echo >&2 "(reading log message from standard input)"
437 cat
438 else
439 cat <"$logfile"
440 fi
441elif test "$use_commit" != ""
442then
443 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
e5215804
JH
444elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
445then
446 cat "$GIT_DIR/MERGE_MSG"
7d0c6887
JH
447elif test -f "$GIT_DIR/SQUASH_MSG"
448then
449 cat "$GIT_DIR/SQUASH_MSG"
f8e2c54c 450fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
451
452case "$signoff" in
453t)
f6413391
JH
454 {
455 echo
456 git-var GIT_COMMITTER_IDENT | sed -e '
457 s/>.*/>/
458 s/^/Signed-off-by: /
459 '
f8e2c54c 460 } >>"$GIT_DIR"/COMMIT_EDITMSG
2d569933
JH
461 ;;
462esac
463
475443c8 464if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
2d569933 465 echo "#"
b6ae5409 466 echo "# It looks like you may be committing a MERGE."
2d569933
JH
467 echo "# If this is not correct, please remove the file"
468 echo "# $GIT_DIR/MERGE_HEAD"
469 echo "# and try again"
470 echo "#"
f8e2c54c 471fi >>"$GIT_DIR"/COMMIT_EDITMSG
aba2da13 472
130fcca6 473# Author
5a798fb5
JH
474if test '' != "$force_author"
475then
f327dbce
MW
476 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
477 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
5a798fb5
JH
478 test '' != "$GIT_AUTHOR_NAME" &&
479 test '' != "$GIT_AUTHOR_EMAIL" ||
82e5a82f 480 die "malformed --author parameter"
5a798fb5
JH
481 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
482elif test '' != "$use_commit"
130fcca6
JH
483then
484 pick_author_script='
485 /^author /{
486 s/'\''/'\''\\'\'\''/g
487 h
488 s/^author \([^<]*\) <[^>]*> .*$/\1/
489 s/'\''/'\''\'\'\''/g
490 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
491
492 g
493 s/^author [^<]* <\([^>]*\)> .*$/\1/
494 s/'\''/'\''\'\'\''/g
495 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
496
497 g
498 s/^author [^<]* <[^>]*> \(.*\)$/\1/
499 s/'\''/'\''\'\'\''/g
500 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
501
502 q
503 }
504 '
505 set_author_env=`git-cat-file commit "$use_commit" |
506 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
507 eval "$set_author_env"
508 export GIT_AUTHOR_NAME
509 export GIT_AUTHOR_EMAIL
510 export GIT_AUTHOR_DATE
130fcca6
JH
511fi
512
96069cf0 513PARENTS="-p HEAD"
130fcca6 514if test -z "$initial_commit"
8098a178 515then
a3a733e6 516 rloga='commit'
5fec3ef1 517 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
a3a733e6 518 rloga='commit (merge)'
91063bbc 519 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
b4019f04 520 elif test -n "$amend"; then
a3a733e6 521 rloga='commit (amend)'
b4019f04
JH
522 PARENTS=$(git-cat-file commit HEAD |
523 sed -n -e '/^$/q' -e 's/^parent /-p /p')
aba2da13 524 fi
b8310152 525 current=$(git-rev-parse --verify HEAD)
8098a178
JH
526else
527 if [ -z "$(git-ls-files)" ]; then
5a798fb5 528 echo >&2 Nothing to commit
8098a178
JH
529 exit 1
530 fi
531 PARENTS=""
b8310152 532 current=
a3a733e6 533 rloga='commit (initial)'
96069cf0 534fi
130fcca6 535
475443c8
JH
536if test -z "$no_edit"
537then
538 {
88a15314
MW
539 echo ""
540 echo "# Please enter the commit message for your changes."
541 echo "# (Comment lines starting with '#' will not be included)"
475443c8
JH
542 test -z "$only_include_assumed" || echo "$only_include_assumed"
543 run_status
544 } >>"$GIT_DIR"/COMMIT_EDITMSG
545else
546 # we need to check if there is anything to commit
547 run_status >/dev/null
548fi
8588452c 549if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
a3e870f2 550then
7d0c6887 551 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
5a798fb5 552 run_status
a3e870f2
LT
553 exit 1
554fi
475443c8 555
0c091296 556case "$no_edit" in
5fec3ef1 557'')
7334f06c
JH
558 case "${VISUAL:-$EDITOR},$TERM" in
559 ,dumb)
560 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
561 echo >&2 "Please supply the commit log message using either"
562 echo >&2 "-m or -F option. A boilerplate log message has"
563 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
130fcca6
JH
564 exit 1
565 ;;
7334f06c 566 esac
ec4e69c0
SE
567 git-var GIT_AUTHOR_IDENT > /dev/null || die
568 git-var GIT_COMMITTER_IDENT > /dev/null || die
f8e2c54c 569 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
5fec3ef1
JH
570 ;;
571esac
89e2c5f1
JH
572
573case "$verify" in
574t)
575 if test -x "$GIT_DIR"/hooks/commit-msg
576 then
f8e2c54c 577 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
89e2c5f1
JH
578 fi
579esac
580
29f4ad86
YD
581if test -z "$no_edit"
582then
583 sed -e '
584 /^diff --git a\/.*/{
585 s///
586 q
587 }
588 /^#/d
589 ' "$GIT_DIR"/COMMIT_EDITMSG
590else
591 cat "$GIT_DIR"/COMMIT_EDITMSG
592fi |
cf7bb589 593git-stripspace >"$GIT_DIR"/COMMIT_MSG
f8e2c54c
SB
594
595if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
596 git-stripspace |
597 wc -l` &&
598 test 0 -lt $cnt
0c091296 599then
130fcca6
JH
600 if test -z "$TMP_INDEX"
601 then
cf7bb589 602 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
130fcca6
JH
603 else
604 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
605 rm -f "$TMP_INDEX"
606 fi &&
f8e2c54c 607 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
67644a4d 608 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
a3a733e6 609 git-update-ref -m "$rloga: $rlogm" HEAD $commit $current &&
cf7bb589
JH
610 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
611 if test -f "$NEXT_INDEX"
612 then
613 mv "$NEXT_INDEX" "$THIS_INDEX"
614 else
615 : ;# happy
616 fi
0c091296
JH
617else
618 echo >&2 "* no commit message? aborting commit."
619 false
620fi
170241b7 621ret="$?"
7d0c6887 622rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
1536dd9c
JH
623if test -d "$GIT_DIR/rr-cache"
624then
625 git-rerere
626fi
89e2c5f1
JH
627
628if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
629then
630 "$GIT_DIR"/hooks/post-commit
631fi
170241b7 632exit "$ret"