blob: 1d04f1ff31cf99a6dec1d52866668007ab2dae72 [file] [log] [blame]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -07001#!/bin/sh
Junio C Hamano5fec3ef2005-06-25 02:22:05 -07002#
3# Copyright (c) 2005 Linus Torvalds
Junio C Hamano130fcca2006-02-05 00:07:44 -08004# Copyright (c) 2006 Junio C Hamano
Junio C Hamano5fec3ef2005-06-25 02:22:05 -07005
Steven Grimmd1cc1302007-07-22 21:17:42 -07006USAGE='[-a | --interactive] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit> | --amend] [-u] [-e] [--author <author>] [--template <file>] [[-i | -o] <path>...]'
Junio C Hamano130fcca2006-02-05 00:07:44 -08007SUBDIRECTORY_OK=Yes
Junio C Hamanoae2b0f12005-11-24 00:12:11 -08008. git-sh-setup
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -05009require_work_tree
Linus Torvaldsb33e9662005-07-08 10:57:21 -070010
Junio C Hamano5be60072007-07-02 22:52:14 -070011git rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
Junio C Hamanocf7bb582006-02-10 00:45:59 -080012
13case "$0" in
14*status)
15 status_only=t
Junio C Hamano2b5f9a82007-02-22 00:28:49 -080016 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -080017*commit)
18 status_only=
Junio C Hamano2b5f9a82007-02-22 00:28:49 -080019 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -080020esac
Junio C Hamano130fcca2006-02-05 00:07:44 -080021
22refuse_partial () {
23 echo >&2 "$1"
Junio C Hamano130fcca2006-02-05 00:07:44 -080024 echo >&2 "You might have meant to say 'git commit -i paths...', perhaps?"
25 exit 1
26}
27
Junio C Hamanocf7bb582006-02-10 00:45:59 -080028THIS_INDEX="$GIT_DIR/index"
29NEXT_INDEX="$GIT_DIR/next-index$$"
30rm -f "$NEXT_INDEX"
Junio C Hamano5a798fb2006-02-05 16:08:01 -080031save_index () {
Junio C Hamanocc7d5bc2006-06-29 14:48:22 -070032 cp -p "$THIS_INDEX" "$NEXT_INDEX"
Junio C Hamanocf7bb582006-02-10 00:45:59 -080033}
34
Junio C Hamano5a798fb2006-02-05 16:08:01 -080035run_status () {
Junio C Hamanocf7bb582006-02-10 00:45:59 -080036 # 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
Martin Waitz3de63c32006-10-07 21:07:40 +020044 GIT_INDEX_FILE="$TMP_INDEX"
45 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080046 elif test -f "$NEXT_INDEX"
47 then
Martin Waitz3de63c32006-10-07 21:07:40 +020048 GIT_INDEX_FILE="$NEXT_INDEX"
49 export GIT_INDEX_FILE
Junio C Hamanocf7bb582006-02-10 00:45:59 -080050 fi
51
Brian Hetro09b0d9d2007-08-26 14:35:26 -040052 if test "$status_only" = "t" -o "$use_status_color" = "t"; then
53 color=
54 else
55 color=--nocolor
56 fi
Junio C Hamano5be60072007-07-02 22:52:14 -070057 git runstatus ${color} \
Martin Waitz3de63c32006-10-07 21:07:40 +020058 ${verbose:+--verbose} \
59 ${amend:+--amend} \
Johannes Schindelin2074cb02006-09-12 22:45:12 +020060 ${untracked_files:+--untracked}
Junio C Hamano5a798fb2006-02-05 16:08:01 -080061}
62
Junio C Hamanocf7bb582006-02-10 00:45:59 -080063trap '
64 test -z "$TMP_INDEX" || {
65 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
66 }
67 rm -f "$NEXT_INDEX"
68' 0
69
70################################################################
71# Command line argument parsing and sanity checking
72
Junio C Hamano130fcca2006-02-05 00:07:44 -080073all=
74also=
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +010075interactive=
Junio C Hamano5a798fb2006-02-05 16:08:01 -080076only=
Junio C Hamano130fcca2006-02-05 00:07:44 -080077logfile=
78use_commit=
Junio C Hamanob4019f02006-03-02 21:04:05 -080079amend=
Jeff Kingcda8ab52006-06-23 09:43:38 -040080edit_flag=
Junio C Hamano130fcca2006-02-05 00:07:44 -080081no_edit=
82log_given=
83log_message=
84verify=t
Nicolas Pitreebd124c2006-12-14 23:15:44 -050085quiet=
Junio C Hamanocf7bb582006-02-10 00:45:59 -080086verbose=
Junio C Hamano130fcca2006-02-05 00:07:44 -080087signoff=
88force_author=
Junio C Hamanobba319b2006-02-14 12:40:20 -080089only_include_assumed=
Matthias Lederhofer443f8332006-05-22 23:02:06 +020090untracked_files=
Steven Grimmd1cc1302007-07-22 21:17:42 -070091templatefile="`git config commit.template`"
Junio C Hamano0c091292005-08-08 17:03:14 -070092while case "$#" in 0) break;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -070093do
Martin Waitz3de63c32006-10-07 21:07:40 +020094 case "$1" in
95 -F|--F|-f|--f|--fi|--fil|--file)
96 case "$#" in 1) usage ;; esac
97 shift
98 no_edit=t
99 log_given=t$log_given
100 logfile="$1"
101 shift
102 ;;
103 -F*|-f*)
104 no_edit=t
105 log_given=t$log_given
106 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
107 shift
108 ;;
109 --F=*|--f=*|--fi=*|--fil=*|--file=*)
110 no_edit=t
111 log_given=t$log_given
112 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
113 shift
114 ;;
115 -a|--a|--al|--all)
116 all=t
117 shift
118 ;;
119 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
120 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
121 shift
122 ;;
123 --au|--aut|--auth|--autho|--author)
124 case "$#" in 1) usage ;; esac
125 shift
126 force_author="$1"
127 shift
128 ;;
129 -e|--e|--ed|--edi|--edit)
130 edit_flag=t
131 shift
132 ;;
133 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
134 also=t
135 shift
136 ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100137 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
138 --interactiv|--interactive)
139 interactive=t
140 shift
141 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200142 -o|--o|--on|--onl|--only)
143 only=t
144 shift
145 ;;
146 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
147 case "$#" in 1) usage ;; esac
148 shift
149 log_given=m$log_given
150 if test "$log_message" = ''
151 then
152 log_message="$1"
153 else
154 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400155
156$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200157 fi
158 no_edit=t
159 shift
160 ;;
161 -m*)
162 log_given=m$log_given
163 if test "$log_message" = ''
164 then
165 log_message=`expr "z$1" : 'z-m\(.*\)'`
166 else
167 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400168
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200169`expr "z$1" : 'z-m\(.*\)'`"
Martin Waitz3de63c32006-10-07 21:07:40 +0200170 fi
171 no_edit=t
172 shift
173 ;;
174 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
175 log_given=m$log_given
176 if test "$log_message" = ''
177 then
178 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
179 else
180 log_message="$log_message
Shawn Pearce68912812006-05-29 04:45:49 -0400181
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200182`expr "z$1" : 'zq-[^=]*=\(.*\)'`"
Martin Waitz3de63c32006-10-07 21:07:40 +0200183 fi
184 no_edit=t
185 shift
186 ;;
187 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
188 --no-verify)
189 verify=
190 shift
191 ;;
192 --a|--am|--ame|--amen|--amend)
193 amend=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200194 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 ;;
Steven Grimmd1cc1302007-07-22 21:17:42 -0700252 -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 ;;
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500259 -q|--q|--qu|--qui|--quie|--quiet)
260 quiet=t
261 shift
262 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200263 -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
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700284done
Jeff Kingcda8ab52006-06-23 09:43:38 -0400285case "$edit_flag" in t) no_edit= ;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700286
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800287################################################################
288# Sanity check options
289
Junio C Hamanob4019f02006-03-02 21:04:05 -0800290case "$amend,$initial_commit" in
291t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200292 die "You do not have anything to amend." ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800293t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200294 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
295 die "You are in the middle of a merge -- cannot amend."
296 fi ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800297esac
298
Junio C Hamano0c091292005-08-08 17:03:14 -0700299case "$log_given" in
300tt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700301 die "Only one of -c/-C/-F can be used." ;;
Shawn Pearce68912812006-05-29 04:45:49 -0400302*tm*|*mt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700303 die "Option -m cannot be combined with -c/-C/-F." ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700304esac
305
Junio C Hamano6a746422006-04-20 01:20:56 -0700306case "$#,$also,$only,$amend" in
307*,t,t,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200308 die "Only one of --include/--only can be used." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003090,t,,* | 0,,t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200310 die "No paths with --include/--only does not make sense." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003110,,t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200312 only_include_assumed="# Clever... amending the last one with dirty index." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07003130,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200314 ;;
Junio C Hamano6a746422006-04-20 01:20:56 -0700315*,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200316 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
317 also=
318 ;;
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800319esac
320unset only
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100321case "$all,$interactive,$also,$#" in
322*t,*t,*)
323 die "Cannot use -a, --interactive or -i at the same time." ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800324t,,[1-9]*)
325 die "Paths with -a does not make sense." ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100326,t,[1-9]*)
327 die "Paths with --interactive does not make sense." ;;
328,,t,0)
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800329 die "No paths with -i does not make sense." ;;
330esac
331
Steven Grimmd1cc1302007-07-22 21:17:42 -0700332if test ! -z "$templatefile" -a -z "$log_given"
333then
334 if test ! -f "$templatefile"
335 then
336 die "Commit template file does not exist."
337 fi
338fi
339
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800340################################################################
341# Prepare index to have a tree to be committed
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800342
Junio C Hamano130fcca2006-02-05 00:07:44 -0800343case "$all,$also" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800344t,)
Fredrik Kuivinen755b99d2007-02-22 21:28:12 +0100345 if test ! -f "$THIS_INDEX"
346 then
347 die 'nothing to commit (use "git add file1 file2" to include for commit)'
348 fi
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800349 save_index &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800350 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800351 cd_to_toplevel &&
352 GIT_INDEX_FILE="$NEXT_INDEX" &&
353 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700354 git diff-files --name-only -z |
355 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800356 ) || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800357 ;;
358,t)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800359 save_index &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700360 git ls-files --error-unmatch -- "$@" >/dev/null || exit
Junio C Hamanobba319b2006-02-14 12:40:20 -0800361
Junio C Hamano5be60072007-07-02 22:52:14 -0700362 git diff-files --name-only -z -- "$@" |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800363 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800364 cd_to_toplevel &&
365 GIT_INDEX_FILE="$NEXT_INDEX" &&
366 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700367 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800368 ) || exit
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700369 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800370,)
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100371 if test "$interactive" = t; then
372 git add --interactive || exit
373 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800374 case "$#" in
375 0)
Martin Waitz3de63c32006-10-07 21:07:40 +0200376 ;; # commit as-is
Junio C Hamano130fcca2006-02-05 00:07:44 -0800377 *)
Martin Waitz3de63c32006-10-07 21:07:40 +0200378 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$$"
Junio C Hamano5be60072007-07-02 22:52:14 -0700383 commit_only=`git ls-files --error-unmatch -- "$@"` || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800384
Junio C Hamano158d0572006-12-09 22:32:43 -0800385 # Build a temporary index and update the real index
Martin Waitz3de63c32006-10-07 21:07:40 +0200386 # the same way.
387 if test -z "$initial_commit"
388 then
Junio C Hamano5e7f56a2007-03-31 23:27:41 -0700389 GIT_INDEX_FILE="$THIS_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700390 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200391 else
392 rm -f "$TMP_INDEX"
393 fi || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800394
Junio C Hamano293623e2007-05-25 22:00:54 -0700395 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200396 GIT_INDEX_FILE="$TMP_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700397 git update-index --add --remove --stdin &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800398
Martin Waitz3de63c32006-10-07 21:07:40 +0200399 save_index &&
Junio C Hamano293623e2007-05-25 22:00:54 -0700400 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200401 (
402 GIT_INDEX_FILE="$NEXT_INDEX"
403 export GIT_INDEX_FILE
Junio C Hamano5be60072007-07-02 22:52:14 -0700404 git update-index --remove --stdin
Martin Waitz3de63c32006-10-07 21:07:40 +0200405 ) || exit
406 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800407 esac
Junio C Hamanof678dd12005-11-25 13:33:14 -0800408 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800409esac
410
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800411################################################################
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.
Junio C Hamano0c091292005-08-08 17:03:14 -0700415
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800416if test -f "$NEXT_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800417then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800418 USE_INDEX="$NEXT_INDEX"
419else
420 USE_INDEX="$THIS_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800421fi
422
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800423case "$status_only" in
424t)
425 # This will silently fail in a read-only repository, which is
426 # what we want.
Junio C Hamano5be60072007-07-02 22:52:14 -0700427 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800428 run_status
429 exit $?
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800430 ;;
431'')
Junio C Hamano5be60072007-07-02 22:52:14 -0700432 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800433 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800434esac
435
436################################################################
437# Grab commit message, write out tree and make commit.
438
Junio C Hamano130fcca2006-02-05 00:07:44 -0800439if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
440then
441 if test "$TMP_INDEX"
442 then
443 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
444 else
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800445 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800446 fi || exit
447fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700448
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700449if test "$log_message" != ''
450then
Junio C Hamano293623e2007-05-25 22:00:54 -0700451 printf '%s\n' "$log_message"
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700452elif test "$logfile" != ""
453then
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
462elif test "$use_commit" != ""
463then
Tom Princee0d10e12007-01-28 16:16:53 -0800464 encoding=$(git config i18n.commitencoding || echo UTF-8)
Junio C Hamano5ac27152007-01-13 13:33:07 -0800465 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
466 sed -e '1,/^$/d' -e 's/^ //'
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700467elif test -f "$GIT_DIR/MERGE_MSG"
Junio C Hamanoe5215802005-11-01 22:01:28 -0800468then
469 cat "$GIT_DIR/MERGE_MSG"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700470elif test -f "$GIT_DIR/SQUASH_MSG"
471then
472 cat "$GIT_DIR/SQUASH_MSG"
Steven Grimmd1cc1302007-07-22 21:17:42 -0700473elif test "$templatefile" != ""
474then
475 cat "$templatefile"
Junio C Hamano5be60072007-07-02 22:52:14 -0700476fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700477
478case "$signoff" in
479t)
Gerrit Papea7342912007-07-06 14:42:27 +0000480 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
481 s/>.*/>/
482 s/^/Signed-off-by: /
483 ')
484 blank_before_signoff=
Junio C Hamanodbaa06a2007-01-29 01:06:27 -0800485 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
Gerrit Papea7342912007-07-06 14:42:27 +0000486 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
Junio C Hamano2d569932005-08-31 17:15:25 -0700492 ;;
493esac
494
Junio C Hamano475443c2006-04-12 11:45:18 -0700495if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
Junio C Hamano2d569932005-08-31 17:15:25 -0700496 echo "#"
Alex Riesenb6ae5402006-01-05 12:44:59 +0100497 echo "# It looks like you may be committing a MERGE."
Junio C Hamano2d569932005-08-31 17:15:25 -0700498 echo "# If this is not correct, please remove the file"
Junio C Hamano293623e2007-05-25 22:00:54 -0700499 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
Junio C Hamano2d569932005-08-31 17:15:25 -0700500 echo "# and try again"
501 echo "#"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700502fi >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700503
Junio C Hamano130fcca2006-02-05 00:07:44 -0800504# Author
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800505if test '' != "$use_commit"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800506then
Johannes Schindelin0cae2342007-06-25 01:04:11 +0100507 eval "$(get_author_ident_from_commit "$use_commit")"
508 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800509fi
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800510if test '' != "$force_author"
511then
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
518fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800519
Linus Torvalds96069cf2005-06-14 10:20:14 -0700520PARENTS="-p HEAD"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800521if test -z "$initial_commit"
Junio C Hamano8098a172005-09-30 14:26:57 -0700522then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400523 rloga='commit'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700524 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400525 rloga='commit (merge)'
Junio C Hamano91063bb2005-09-08 13:47:12 -0700526 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
Junio C Hamanob4019f02006-03-02 21:04:05 -0800527 elif test -n "$amend"; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400528 rloga='commit (amend)'
Junio C Hamano5be60072007-07-02 22:52:14 -0700529 PARENTS=$(git cat-file commit HEAD |
Junio C Hamanob4019f02006-03-02 21:04:05 -0800530 sed -n -e '/^$/q' -e 's/^parent /-p /p')
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700531 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700532 current="$(git rev-parse --verify HEAD)"
Junio C Hamano8098a172005-09-30 14:26:57 -0700533else
Junio C Hamano5be60072007-07-02 22:52:14 -0700534 if [ -z "$(git ls-files)" ]; then
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500535 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
Junio C Hamano8098a172005-09-30 14:26:57 -0700536 exit 1
537 fi
538 PARENTS=""
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400539 rloga='commit (initial)'
Junio C Hamanocede7522006-09-27 02:06:31 -0700540 current=''
Linus Torvalds96069cf2005-06-14 10:20:14 -0700541fi
Junio C Hamano23913dc2007-01-19 17:12:11 -0800542set_reflog_action "$rloga"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800543
Junio C Hamano475443c2006-04-12 11:45:18 -0700544if test -z "$no_edit"
545then
546 {
Martin Waitz88a15312006-05-26 01:42:18 +0200547 echo ""
548 echo "# Please enter the commit message for your changes."
549 echo "# (Comment lines starting with '#' will not be included)"
Junio C Hamano475443c2006-04-12 11:45:18 -0700550 test -z "$only_include_assumed" || echo "$only_include_assumed"
551 run_status
552 } >>"$GIT_DIR"/COMMIT_EDITMSG
553else
554 # we need to check if there is anything to commit
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700555 run_status >/dev/null
Junio C Hamano475443c2006-04-12 11:45:18 -0700556fi
Junio C Hamano85884522006-03-04 20:36:28 -0800557if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700558then
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700559 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Brian Hetro09b0d9d2007-08-26 14:35:26 -0400560 use_status_color=t
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800561 run_status
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700562 exit 1
563fi
Junio C Hamano475443c2006-04-12 11:45:18 -0700564
Junio C Hamano0c091292005-08-08 17:03:14 -0700565case "$no_edit" in
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700566'')
Seanec4e69c2006-05-13 23:09:32 -0400567 git-var GIT_AUTHOR_IDENT > /dev/null || die
568 git-var GIT_COMMITTER_IDENT > /dev/null || die
Adam Robenef0c2ab2007-07-19 22:09:35 -0700569 git_editor "$GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700570 ;;
571esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700572
573case "$verify" in
574t)
575 if test -x "$GIT_DIR"/hooks/commit-msg
576 then
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700577 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700578 fi
579esac
580
Yann Dirson29f4ad82006-06-24 00:04:05 +0200581if 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 |
Junio C Hamano5be60072007-07-02 22:52:14 -0700593git stripspace >"$GIT_DIR"/COMMIT_MSG
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700594
Steven Grimmd1cc1302007-07-22 21:17:42 -0700595# Test whether the commit message has any content we didn't supply.
596have_commitmsg=
597grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
598 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
599
600# Is the commit message totally empty?
601if test -s "$GIT_DIR"/COMMIT_BAREMSG
602then
603 if test "$templatefile" != ""
604 then
605 # Test whether this is just the unaltered template.
606 if cnt=`sed -e '/^#/d' < "$templatefile" |
607 git stripspace |
608 diff "$GIT_DIR"/COMMIT_BAREMSG - |
609 wc -l` &&
610 test 0 -lt $cnt
611 then
612 have_commitmsg=t
613 fi
614 else
615 # No template, so the content in the commit message must
616 # have come from the user.
617 have_commitmsg=t
618 fi
619fi
620
621rm -f "$GIT_DIR"/COMMIT_BAREMSG
622
623if test "$have_commitmsg" = "t"
Junio C Hamano0c091292005-08-08 17:03:14 -0700624then
Junio C Hamano130fcca2006-02-05 00:07:44 -0800625 if test -z "$TMP_INDEX"
626 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700627 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800628 else
Junio C Hamano5be60072007-07-02 22:52:14 -0700629 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800630 rm -f "$TMP_INDEX"
631 fi &&
Josh Triplett9d6f2202007-07-14 01:05:43 -0700632 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
Shawn Pearce67644a42006-05-19 05:16:18 -0400633 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700634 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700635 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800636 if test -f "$NEXT_INDEX"
637 then
638 mv "$NEXT_INDEX" "$THIS_INDEX"
639 else
640 : ;# happy
641 fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700642else
643 echo >&2 "* no commit message? aborting commit."
644 false
645fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700646ret="$?"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700647rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamanoc93d88a2007-03-05 12:35:41 -0800648
649cd_to_toplevel
650
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100651git rerere
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700652
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500653if test "$ret" = 0
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700654then
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500655 if test -x "$GIT_DIR"/hooks/post-commit
656 then
657 "$GIT_DIR"/hooks/post-commit
658 fi
659 if test -z "$quiet"
660 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700661 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
Michael S. Tsirkinc7263d42007-04-16 08:51:11 +0300662 --summary --root HEAD --`
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500663 echo "Created${initial_commit:+ initial} commit $commit"
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500664 fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700665fi
Junio C Hamano61f5cb72006-10-24 21:48:55 -0700666
Linus Torvalds170241b2005-06-19 19:57:01 -0700667exit "$ret"