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