blob: cb14f0621651d2006b08d1eddf67ab3269df84d0 [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`"
David Kastrup822f7c72007-09-23 22:42:08 +020092while test $# != 0
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"
Martin Waitz3de63c32006-10-07 21:07:40 +0200101 ;;
102 -F*|-f*)
103 no_edit=t
104 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200105 logfile="${1#-[Ff]}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200106 ;;
107 --F=*|--f=*|--fi=*|--fil=*|--file=*)
108 no_edit=t
109 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200110 logfile="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200111 ;;
112 -a|--a|--al|--all)
113 all=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200114 ;;
115 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
David Kastrupd2884042007-09-17 22:56:44 +0200116 force_author="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200117 ;;
118 --au|--aut|--auth|--autho|--author)
119 case "$#" in 1) usage ;; esac
120 shift
121 force_author="$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200122 ;;
123 -e|--e|--ed|--edi|--edit)
124 edit_flag=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200125 ;;
126 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
127 also=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200128 ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100129 --int|--inte|--inter|--intera|--interac|--interact|--interacti|\
130 --interactiv|--interactive)
131 interactive=t
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100132 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200133 -o|--o|--on|--onl|--only)
134 only=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200135 ;;
136 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
137 case "$#" in 1) usage ;; esac
138 shift
139 log_given=m$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200140 log_message="${log_message:+${log_message}
Shawn Pearce68912812006-05-29 04:45:49 -0400141
David Kastrupd2884042007-09-17 22:56:44 +0200142}$1"
Martin Waitz3de63c32006-10-07 21:07:40 +0200143 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200144 ;;
145 -m*)
146 log_given=m$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200147 log_message="${log_message:+${log_message}
Shawn Pearce68912812006-05-29 04:45:49 -0400148
David Kastrupd2884042007-09-17 22:56:44 +0200149}${1#-m}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200150 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200151 ;;
152 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
153 log_given=m$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200154 log_message="${log_message:+${log_message}
Shawn Pearce68912812006-05-29 04:45:49 -0400155
David Kastrupd2884042007-09-17 22:56:44 +0200156}${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200157 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200158 ;;
159 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|\
160 --no-verify)
161 verify=
Martin Waitz3de63c32006-10-07 21:07:40 +0200162 ;;
163 --a|--am|--ame|--amen|--amend)
164 amend=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200165 use_commit=HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200166 ;;
167 -c)
168 case "$#" in 1) usage ;; esac
169 shift
170 log_given=t$log_given
171 use_commit="$1"
172 no_edit=
Martin Waitz3de63c32006-10-07 21:07:40 +0200173 ;;
174 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
175 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
176 --reedit-messag=*|--reedit-message=*)
177 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200178 use_commit="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200179 no_edit=
Martin Waitz3de63c32006-10-07 21:07:40 +0200180 ;;
181 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
182 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|\
183 --reedit-message)
184 case "$#" in 1) usage ;; esac
185 shift
186 log_given=t$log_given
187 use_commit="$1"
188 no_edit=
Martin Waitz3de63c32006-10-07 21:07:40 +0200189 ;;
190 -C)
191 case "$#" in 1) usage ;; esac
192 shift
193 log_given=t$log_given
194 use_commit="$1"
195 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200196 ;;
197 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
198 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
199 --reuse-message=*)
200 log_given=t$log_given
David Kastrupd2884042007-09-17 22:56:44 +0200201 use_commit="${1#*=}"
Martin Waitz3de63c32006-10-07 21:07:40 +0200202 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200203 ;;
204 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
205 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
206 case "$#" in 1) usage ;; esac
207 shift
208 log_given=t$log_given
209 use_commit="$1"
210 no_edit=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200211 ;;
212 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
213 signoff=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200214 ;;
Steven Grimmd1cc1302007-07-22 21:17:42 -0700215 -t|--t|--te|--tem|--temp|--templ|--templa|--templat|--template)
216 case "$#" in 1) usage ;; esac
217 shift
218 templatefile="$1"
219 no_edit=
Steven Grimmd1cc1302007-07-22 21:17:42 -0700220 ;;
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500221 -q|--q|--qu|--qui|--quie|--quiet)
222 quiet=t
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500223 ;;
Martin Waitz3de63c32006-10-07 21:07:40 +0200224 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
225 verbose=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200226 ;;
227 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|\
228 --untracked|--untracked-|--untracked-f|--untracked-fi|--untracked-fil|\
229 --untracked-file|--untracked-files)
230 untracked_files=t
Martin Waitz3de63c32006-10-07 21:07:40 +0200231 ;;
232 --)
233 shift
234 break
235 ;;
236 -*)
237 usage
238 ;;
239 *)
240 break
241 ;;
242 esac
David Kastrupd2884042007-09-17 22:56:44 +0200243 shift
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700244done
Jeff Kingcda8ab52006-06-23 09:43:38 -0400245case "$edit_flag" in t) no_edit= ;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700246
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800247################################################################
248# Sanity check options
249
Junio C Hamanob4019f02006-03-02 21:04:05 -0800250case "$amend,$initial_commit" in
251t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200252 die "You do not have anything to amend." ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800253t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200254 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
255 die "You are in the middle of a merge -- cannot amend."
256 fi ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800257esac
258
Junio C Hamano0c091292005-08-08 17:03:14 -0700259case "$log_given" in
260tt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700261 die "Only one of -c/-C/-F can be used." ;;
Shawn Pearce68912812006-05-29 04:45:49 -0400262*tm*|*mt*)
Junio C Hamano6d4bbeb2007-08-01 18:14:41 -0700263 die "Option -m cannot be combined with -c/-C/-F." ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700264esac
265
Junio C Hamano6a746422006-04-20 01:20:56 -0700266case "$#,$also,$only,$amend" in
267*,t,t,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200268 die "Only one of --include/--only can be used." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07002690,t,,* | 0,,t,)
Martin Waitz3de63c32006-10-07 21:07:40 +0200270 die "No paths with --include/--only does not make sense." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07002710,,t,t)
Martin Waitz3de63c32006-10-07 21:07:40 +0200272 only_include_assumed="# Clever... amending the last one with dirty index." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07002730,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200274 ;;
Junio C Hamano6a746422006-04-20 01:20:56 -0700275*,,,*)
Martin Waitz3de63c32006-10-07 21:07:40 +0200276 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
277 also=
278 ;;
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800279esac
280unset only
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100281case "$all,$interactive,$also,$#" in
282*t,*t,*)
283 die "Cannot use -a, --interactive or -i at the same time." ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800284t,,[1-9]*)
285 die "Paths with -a does not make sense." ;;
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100286,t,[1-9]*)
287 die "Paths with --interactive does not make sense." ;;
288,,t,0)
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800289 die "No paths with -i does not make sense." ;;
290esac
291
Steven Grimmd1cc1302007-07-22 21:17:42 -0700292if test ! -z "$templatefile" -a -z "$log_given"
293then
294 if test ! -f "$templatefile"
295 then
296 die "Commit template file does not exist."
297 fi
298fi
299
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800300################################################################
301# Prepare index to have a tree to be committed
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800302
Junio C Hamano130fcca2006-02-05 00:07:44 -0800303case "$all,$also" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800304t,)
Fredrik Kuivinen755b99d2007-02-22 21:28:12 +0100305 if test ! -f "$THIS_INDEX"
306 then
307 die 'nothing to commit (use "git add file1 file2" to include for commit)'
308 fi
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800309 save_index &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800310 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800311 cd_to_toplevel &&
312 GIT_INDEX_FILE="$NEXT_INDEX" &&
313 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700314 git diff-files --name-only -z |
315 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800316 ) || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800317 ;;
318,t)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800319 save_index &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700320 git ls-files --error-unmatch -- "$@" >/dev/null || exit
Junio C Hamanobba319b2006-02-14 12:40:20 -0800321
Junio C Hamano5be60072007-07-02 22:52:14 -0700322 git diff-files --name-only -z -- "$@" |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800323 (
Junio C Hamano514c09f2007-01-12 12:49:05 -0800324 cd_to_toplevel &&
325 GIT_INDEX_FILE="$NEXT_INDEX" &&
326 export GIT_INDEX_FILE &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700327 git update-index --remove -z --stdin
Junio C Hamano514c09f2007-01-12 12:49:05 -0800328 ) || exit
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700329 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800330,)
Paolo Bonzini6cbf07e2007-03-05 08:57:53 +0100331 if test "$interactive" = t; then
332 git add --interactive || exit
333 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800334 case "$#" in
335 0)
Martin Waitz3de63c32006-10-07 21:07:40 +0200336 ;; # commit as-is
Junio C Hamano130fcca2006-02-05 00:07:44 -0800337 *)
Martin Waitz3de63c32006-10-07 21:07:40 +0200338 if test -f "$GIT_DIR/MERGE_HEAD"
339 then
340 refuse_partial "Cannot do a partial commit during a merge."
341 fi
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700342
Martin Waitz3de63c32006-10-07 21:07:40 +0200343 TMP_INDEX="$GIT_DIR/tmp-index$$"
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700344 W=
345 test -z "$initial_commit" && W=--with-tree=HEAD
346 commit_only=`git ls-files --error-unmatch $W -- "$@"` || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800347
Junio C Hamano158d0572006-12-09 22:32:43 -0800348 # Build a temporary index and update the real index
Martin Waitz3de63c32006-10-07 21:07:40 +0200349 # the same way.
350 if test -z "$initial_commit"
351 then
Junio C Hamano5e7f56a2007-03-31 23:27:41 -0700352 GIT_INDEX_FILE="$THIS_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700353 git read-tree --index-output="$TMP_INDEX" -i -m HEAD
Martin Waitz3de63c32006-10-07 21:07:40 +0200354 else
355 rm -f "$TMP_INDEX"
356 fi || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800357
Junio C Hamano293623e2007-05-25 22:00:54 -0700358 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200359 GIT_INDEX_FILE="$TMP_INDEX" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700360 git update-index --add --remove --stdin &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800361
Martin Waitz3de63c32006-10-07 21:07:40 +0200362 save_index &&
Junio C Hamano293623e2007-05-25 22:00:54 -0700363 printf '%s\n' "$commit_only" |
Martin Waitz3de63c32006-10-07 21:07:40 +0200364 (
365 GIT_INDEX_FILE="$NEXT_INDEX"
366 export GIT_INDEX_FILE
Junio C Hamanodb33af02007-09-14 16:53:58 -0700367 git update-index --add --remove --stdin
Martin Waitz3de63c32006-10-07 21:07:40 +0200368 ) || exit
369 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800370 esac
Junio C Hamanof678dd12005-11-25 13:33:14 -0800371 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800372esac
373
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800374################################################################
375# If we do as-is commit, the index file will be THIS_INDEX,
376# otherwise NEXT_INDEX after we make this commit. We leave
377# the index as is if we abort.
Junio C Hamano0c091292005-08-08 17:03:14 -0700378
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800379if test -f "$NEXT_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800380then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800381 USE_INDEX="$NEXT_INDEX"
382else
383 USE_INDEX="$THIS_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800384fi
385
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800386case "$status_only" in
387t)
388 # This will silently fail in a read-only repository, which is
389 # what we want.
Junio C Hamano5be60072007-07-02 22:52:14 -0700390 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --unmerged --refresh
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800391 run_status
392 exit $?
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800393 ;;
394'')
Junio C Hamano5be60072007-07-02 22:52:14 -0700395 GIT_INDEX_FILE="$USE_INDEX" git update-index -q --refresh || exit
Junio C Hamano2b5f9a82007-02-22 00:28:49 -0800396 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800397esac
398
399################################################################
400# Grab commit message, write out tree and make commit.
401
Junio C Hamano130fcca2006-02-05 00:07:44 -0800402if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
403then
David Kastrupd2884042007-09-17 22:56:44 +0200404 GIT_INDEX_FILE="${TMP_INDEX:-${USE_INDEX}}" "$GIT_DIR"/hooks/pre-commit \
405 || exit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800406fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700407
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700408if test "$log_message" != ''
409then
Junio C Hamano293623e2007-05-25 22:00:54 -0700410 printf '%s\n' "$log_message"
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700411elif test "$logfile" != ""
412then
413 if test "$logfile" = -
414 then
415 test -t 0 &&
416 echo >&2 "(reading log message from standard input)"
417 cat
418 else
419 cat <"$logfile"
420 fi
421elif test "$use_commit" != ""
422then
Tom Princee0d10e12007-01-28 16:16:53 -0800423 encoding=$(git config i18n.commitencoding || echo UTF-8)
Junio C Hamano5ac27152007-01-13 13:33:07 -0800424 git show -s --pretty=raw --encoding="$encoding" "$use_commit" |
425 sed -e '1,/^$/d' -e 's/^ //'
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700426elif test -f "$GIT_DIR/MERGE_MSG"
Junio C Hamanoe5215802005-11-01 22:01:28 -0800427then
428 cat "$GIT_DIR/MERGE_MSG"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700429elif test -f "$GIT_DIR/SQUASH_MSG"
430then
431 cat "$GIT_DIR/SQUASH_MSG"
Steven Grimmd1cc1302007-07-22 21:17:42 -0700432elif test "$templatefile" != ""
433then
434 cat "$templatefile"
Junio C Hamano5be60072007-07-02 22:52:14 -0700435fi | git stripspace >"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700436
437case "$signoff" in
438t)
Gerrit Papea7342912007-07-06 14:42:27 +0000439 sign=$(git-var GIT_COMMITTER_IDENT | sed -e '
440 s/>.*/>/
441 s/^/Signed-off-by: /
442 ')
443 blank_before_signoff=
Junio C Hamanodbaa06a2007-01-29 01:06:27 -0800444 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
Gerrit Papea7342912007-07-06 14:42:27 +0000445 grep 'Signed-off-by:' >/dev/null || blank_before_signoff='
446'
447 tail -n 1 "$GIT_DIR"/COMMIT_EDITMSG |
448 grep "$sign"$ >/dev/null ||
449 printf '%s%s\n' "$blank_before_signoff" "$sign" \
450 >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700451 ;;
452esac
453
Junio C Hamano475443c2006-04-12 11:45:18 -0700454if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
Junio C Hamano2d569932005-08-31 17:15:25 -0700455 echo "#"
Alex Riesenb6ae5402006-01-05 12:44:59 +0100456 echo "# It looks like you may be committing a MERGE."
Junio C Hamano2d569932005-08-31 17:15:25 -0700457 echo "# If this is not correct, please remove the file"
Junio C Hamano293623e2007-05-25 22:00:54 -0700458 printf '%s\n' "# $GIT_DIR/MERGE_HEAD"
Junio C Hamano2d569932005-08-31 17:15:25 -0700459 echo "# and try again"
460 echo "#"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700461fi >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700462
Junio C Hamano130fcca2006-02-05 00:07:44 -0800463# Author
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800464if test '' != "$use_commit"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800465then
Johannes Schindelin0cae2342007-06-25 01:04:11 +0100466 eval "$(get_author_ident_from_commit "$use_commit")"
467 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800468fi
Junio C Hamano83e24dc2007-01-22 13:03:31 -0800469if test '' != "$force_author"
470then
471 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
472 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
473 test '' != "$GIT_AUTHOR_NAME" &&
474 test '' != "$GIT_AUTHOR_EMAIL" ||
475 die "malformed --author parameter"
476 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
477fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800478
Linus Torvalds96069cf2005-06-14 10:20:14 -0700479PARENTS="-p HEAD"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800480if test -z "$initial_commit"
Junio C Hamano8098a172005-09-30 14:26:57 -0700481then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400482 rloga='commit'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700483 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400484 rloga='commit (merge)'
Junio C Hamano91063bb2005-09-08 13:47:12 -0700485 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
Junio C Hamanob4019f02006-03-02 21:04:05 -0800486 elif test -n "$amend"; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400487 rloga='commit (amend)'
Junio C Hamano5be60072007-07-02 22:52:14 -0700488 PARENTS=$(git cat-file commit HEAD |
Junio C Hamanob4019f02006-03-02 21:04:05 -0800489 sed -n -e '/^$/q' -e 's/^parent /-p /p')
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700490 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700491 current="$(git rev-parse --verify HEAD)"
Junio C Hamano8098a172005-09-30 14:26:57 -0700492else
Junio C Hamano5be60072007-07-02 22:52:14 -0700493 if [ -z "$(git ls-files)" ]; then
Shawn O. Pearceaeb80c72006-12-15 21:53:09 -0500494 echo >&2 'nothing to commit (use "git add file1 file2" to include for commit)'
Junio C Hamano8098a172005-09-30 14:26:57 -0700495 exit 1
496 fi
497 PARENTS=""
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400498 rloga='commit (initial)'
Junio C Hamanocede7522006-09-27 02:06:31 -0700499 current=''
Linus Torvalds96069cf2005-06-14 10:20:14 -0700500fi
Junio C Hamano23913dc2007-01-19 17:12:11 -0800501set_reflog_action "$rloga"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800502
Junio C Hamano475443c2006-04-12 11:45:18 -0700503if test -z "$no_edit"
504then
505 {
Martin Waitz88a15312006-05-26 01:42:18 +0200506 echo ""
507 echo "# Please enter the commit message for your changes."
508 echo "# (Comment lines starting with '#' will not be included)"
Junio C Hamano475443c2006-04-12 11:45:18 -0700509 test -z "$only_include_assumed" || echo "$only_include_assumed"
510 run_status
511 } >>"$GIT_DIR"/COMMIT_EDITMSG
512else
513 # we need to check if there is anything to commit
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700514 run_status >/dev/null
Junio C Hamano475443c2006-04-12 11:45:18 -0700515fi
Dmitry V. Levin4fb5fd52007-09-12 20:14:22 +0400516if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" ]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700517then
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700518 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Brian Hetro09b0d9d2007-08-26 14:35:26 -0400519 use_status_color=t
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800520 run_status
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700521 exit 1
522fi
Junio C Hamano475443c2006-04-12 11:45:18 -0700523
Junio C Hamano0c091292005-08-08 17:03:14 -0700524case "$no_edit" in
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700525'')
Seanec4e69c2006-05-13 23:09:32 -0400526 git-var GIT_AUTHOR_IDENT > /dev/null || die
527 git-var GIT_COMMITTER_IDENT > /dev/null || die
Adam Robenef0c2ab2007-07-19 22:09:35 -0700528 git_editor "$GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700529 ;;
530esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700531
532case "$verify" in
533t)
534 if test -x "$GIT_DIR"/hooks/commit-msg
535 then
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700536 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700537 fi
538esac
539
Yann Dirson29f4ad82006-06-24 00:04:05 +0200540if test -z "$no_edit"
541then
542 sed -e '
543 /^diff --git a\/.*/{
544 s///
545 q
546 }
547 /^#/d
548 ' "$GIT_DIR"/COMMIT_EDITMSG
549else
550 cat "$GIT_DIR"/COMMIT_EDITMSG
551fi |
Junio C Hamano5be60072007-07-02 22:52:14 -0700552git stripspace >"$GIT_DIR"/COMMIT_MSG
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700553
Steven Grimmd1cc1302007-07-22 21:17:42 -0700554# Test whether the commit message has any content we didn't supply.
555have_commitmsg=
556grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
557 git stripspace > "$GIT_DIR"/COMMIT_BAREMSG
558
559# Is the commit message totally empty?
560if test -s "$GIT_DIR"/COMMIT_BAREMSG
561then
562 if test "$templatefile" != ""
563 then
564 # Test whether this is just the unaltered template.
565 if cnt=`sed -e '/^#/d' < "$templatefile" |
566 git stripspace |
567 diff "$GIT_DIR"/COMMIT_BAREMSG - |
568 wc -l` &&
569 test 0 -lt $cnt
570 then
571 have_commitmsg=t
572 fi
573 else
574 # No template, so the content in the commit message must
575 # have come from the user.
576 have_commitmsg=t
577 fi
578fi
579
580rm -f "$GIT_DIR"/COMMIT_BAREMSG
581
582if test "$have_commitmsg" = "t"
Junio C Hamano0c091292005-08-08 17:03:14 -0700583then
Junio C Hamano130fcca2006-02-05 00:07:44 -0800584 if test -z "$TMP_INDEX"
585 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700586 tree=$(GIT_INDEX_FILE="$USE_INDEX" git write-tree)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800587 else
Junio C Hamano5be60072007-07-02 22:52:14 -0700588 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git write-tree) &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800589 rm -f "$TMP_INDEX"
590 fi &&
Josh Triplett9d6f2202007-07-14 01:05:43 -0700591 commit=$(git commit-tree $tree $PARENTS <"$GIT_DIR/COMMIT_MSG") &&
Shawn Pearce67644a42006-05-19 05:16:18 -0400592 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700593 git update-ref -m "$GIT_REFLOG_ACTION: $rlogm" HEAD $commit "$current" &&
Luben Tuikova9cb3c62006-10-12 14:52:42 -0700594 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800595 if test -f "$NEXT_INDEX"
596 then
597 mv "$NEXT_INDEX" "$THIS_INDEX"
598 else
599 : ;# happy
600 fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700601else
602 echo >&2 "* no commit message? aborting commit."
603 false
604fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700605ret="$?"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700606rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamanoc93d88a2007-03-05 12:35:41 -0800607
608cd_to_toplevel
609
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100610git rerere
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700611
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500612if test "$ret" = 0
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700613then
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500614 if test -x "$GIT_DIR"/hooks/post-commit
615 then
616 "$GIT_DIR"/hooks/post-commit
617 fi
618 if test -z "$quiet"
619 then
Junio C Hamano5be60072007-07-02 22:52:14 -0700620 commit=`git diff-tree --always --shortstat --pretty="format:%h: %s"\
Michael S. Tsirkinc7263d42007-04-16 08:51:11 +0300621 --summary --root HEAD --`
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500622 echo "Created${initial_commit:+ initial} commit $commit"
Nicolas Pitreebd124c2006-12-14 23:15:44 -0500623 fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700624fi
Junio C Hamano61f5cb72006-10-24 21:48:55 -0700625
Linus Torvalds170241b2005-06-19 19:57:01 -0700626exit "$ret"