blob: 4cf3fab05cd3c3367173c86a65665ddfc1d89805 [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
Matthias Lederhofer443f8332006-05-22 23:02:06 +02006USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-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
Linus Torvaldsb33e9662005-07-08 10:57:21 -07009
Junio C Hamanocf7bb582006-02-10 00:45:59 -080010git-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
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
35report () {
36 header="#
37# $1:
38# ($2)
39#
40"
41 trailer=""
42 while read status name newname
43 do
44 printf '%s' "$header"
45 header=""
46 trailer="#
47"
48 case "$status" in
49 M ) echo "# modified: $name";;
50 D*) echo "# deleted: $name";;
51 T ) echo "# typechange: $name";;
52 C*) echo "# copied: $name -> $newname";;
53 R*) echo "# renamed: $name -> $newname";;
54 A*) echo "# new file: $name";;
55 U ) echo "# unmerged: $name";;
56 esac
57 done
58 printf '%s' "$trailer"
59 [ "$header" ]
Junio C Hamano5a798fb2006-02-05 16:08:01 -080060}
61
62run_status () {
Junio C Hamanocf7bb582006-02-10 00:45:59 -080063 (
64 # We always show status for the whole tree.
65 cd "$TOP"
66
Junio C Hamanob4019f02006-03-02 21:04:05 -080067 IS_INITIAL="$initial_commit"
68 REFERENCE=HEAD
69 case "$amend" in
70 t)
71 # If we are amending the initial commit, there
72 # is no HEAD^1.
73 if git-rev-parse --verify "HEAD^1" >/dev/null 2>&1
74 then
75 REFERENCE="HEAD^1"
76 IS_INITIAL=
77 else
78 IS_INITIAL=t
79 fi
80 ;;
81 esac
82
Junio C Hamanocf7bb582006-02-10 00:45:59 -080083 # If TMP_INDEX is defined, that means we are doing
84 # "--only" partial commit, and that index file is used
85 # to build the tree for the commit. Otherwise, if
86 # NEXT_INDEX exists, that is the index file used to
87 # make the commit. Otherwise we are using as-is commit
88 # so the regular index file is what we use to compare.
89 if test '' != "$TMP_INDEX"
90 then
91 GIT_INDEX_FILE="$TMP_INDEX"
92 export GIT_INDEX_FILE
93 elif test -f "$NEXT_INDEX"
94 then
95 GIT_INDEX_FILE="$NEXT_INDEX"
96 export GIT_INDEX_FILE
97 fi
98
99 case "$branch" in
100 refs/heads/master) ;;
101 *) echo "# On branch $branch" ;;
102 esac
103
Junio C Hamanob4019f02006-03-02 21:04:05 -0800104 if test -z "$IS_INITIAL"
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800105 then
Junio C Hamano9ae6be82006-02-10 18:38:24 -0800106 git-diff-index -M --cached --name-status \
Junio C Hamanob4019f02006-03-02 21:04:05 -0800107 --diff-filter=MDTCRA $REFERENCE |
Junio C Hamano9ae6be82006-02-10 18:38:24 -0800108 sed -e '
109 s/\\/\\\\/g
110 s/ /\\ /g
111 ' |
112 report "Updated but not checked in" "will commit"
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800113 committable="$?"
114 else
115 echo '#
116# Initial commit
117#'
118 git-ls-files |
119 sed -e '
120 s/\\/\\\\/g
121 s/ /\\ /g
122 s/^/A /
123 ' |
124 report "Updated but not checked in" "will commit"
125
126 committable="$?"
127 fi
128
129 git-diff-files --name-status |
130 sed -e '
131 s/\\/\\\\/g
132 s/ /\\ /g
133 ' |
134 report "Changed but not updated" \
135 "use git-update-index to mark for commit"
136
Matthias Lederhofer443f8332006-05-22 23:02:06 +0200137 option=""
138 if test -z "$untracked_files"; then
139 option="--directory --no-empty-directory"
140 fi
Alex Riesen3dffd2c2006-07-13 10:30:43 +0200141 hdr_shown=
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800142 if test -f "$GIT_DIR/info/exclude"
143 then
Alex Riesen3dffd2c2006-07-13 10:30:43 +0200144 git-ls-files --others $option \
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800145 --exclude-from="$GIT_DIR/info/exclude" \
146 --exclude-per-directory=.gitignore
147 else
Alex Riesen3dffd2c2006-07-13 10:30:43 +0200148 git-ls-files --others $option \
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800149 --exclude-per-directory=.gitignore
150 fi |
Alex Riesen3dffd2c2006-07-13 10:30:43 +0200151 while read line; do
152 if [ -z "$hdr_shown" ]; then
153 echo '#'
154 echo '# Untracked files:'
155 echo '# (use "git add" to add to commit)'
156 echo '#'
157 hdr_shown=1
158 fi
159 echo "# $line"
160 done
Junio C Hamano9ae6be82006-02-10 18:38:24 -0800161
Yasushi SHOJI1fa7a682006-03-20 22:11:12 +0900162 if test -n "$verbose" -a -z "$IS_INITIAL"
Junio C Hamano9ae6be82006-02-10 18:38:24 -0800163 then
Junio C Hamanob4019f02006-03-02 21:04:05 -0800164 git-diff-index --cached -M -p --diff-filter=MDTCRA $REFERENCE
Junio C Hamano9ae6be82006-02-10 18:38:24 -0800165 fi
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800166 case "$committable" in
167 0)
Junio C Hamano6a746422006-04-20 01:20:56 -0700168 case "$amend" in
169 t)
170 echo "# No changes" ;;
171 *)
172 echo "nothing to commit" ;;
173 esac
174 exit 1 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800175 esac
176 exit 0
177 )
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800178}
179
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800180trap '
181 test -z "$TMP_INDEX" || {
182 test -f "$TMP_INDEX" && rm -f "$TMP_INDEX"
183 }
184 rm -f "$NEXT_INDEX"
185' 0
186
187################################################################
188# Command line argument parsing and sanity checking
189
Junio C Hamano130fcca2006-02-05 00:07:44 -0800190all=
191also=
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800192only=
Junio C Hamano130fcca2006-02-05 00:07:44 -0800193logfile=
194use_commit=
Junio C Hamanob4019f02006-03-02 21:04:05 -0800195amend=
Jeff Kingcda8ab52006-06-23 09:43:38 -0400196edit_flag=
Junio C Hamano130fcca2006-02-05 00:07:44 -0800197no_edit=
198log_given=
199log_message=
200verify=t
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800201verbose=
Junio C Hamano130fcca2006-02-05 00:07:44 -0800202signoff=
203force_author=
Junio C Hamanobba319b2006-02-14 12:40:20 -0800204only_include_assumed=
Matthias Lederhofer443f8332006-05-22 23:02:06 +0200205untracked_files=
Junio C Hamano0c091292005-08-08 17:03:14 -0700206while case "$#" in 0) break;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700207do
Junio C Hamano0c091292005-08-08 17:03:14 -0700208 case "$1" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800209 -F|--F|-f|--f|--fi|--fil|--file)
210 case "$#" in 1) usage ;; esac
211 shift
212 no_edit=t
213 log_given=t$log_given
214 logfile="$1"
215 shift
216 ;;
217 -F*|-f*)
218 no_edit=t
219 log_given=t$log_given
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200220 logfile=`expr "z$1" : 'z-[Ff]\(.*\)'`
Junio C Hamano130fcca2006-02-05 00:07:44 -0800221 shift
222 ;;
223 --F=*|--f=*|--fi=*|--fil=*|--file=*)
224 no_edit=t
225 log_given=t$log_given
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200226 logfile=`expr "z$1" : 'z-[^=]*=\(.*\)'`
Junio C Hamano130fcca2006-02-05 00:07:44 -0800227 shift
228 ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700229 -a|--a|--al|--all)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800230 all=t
231 shift
232 ;;
233 --au=*|--aut=*|--auth=*|--autho=*|--author=*)
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200234 force_author=`expr "z$1" : 'z-[^=]*=\(.*\)'`
Junio C Hamano130fcca2006-02-05 00:07:44 -0800235 shift
236 ;;
237 --au|--aut|--auth|--autho|--author)
238 case "$#" in 1) usage ;; esac
239 shift
240 force_author="$1"
241 shift
242 ;;
243 -e|--e|--ed|--edi|--edit)
Jeff Kingcda8ab52006-06-23 09:43:38 -0400244 edit_flag=t
Junio C Hamano130fcca2006-02-05 00:07:44 -0800245 shift
246 ;;
247 -i|--i|--in|--inc|--incl|--inclu|--includ|--include)
248 also=t
249 shift
250 ;;
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800251 -o|--o|--on|--onl|--only)
252 only=t
253 shift
254 ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700255 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800256 case "$#" in 1) usage ;; esac
257 shift
Shawn Pearce68912812006-05-29 04:45:49 -0400258 log_given=m$log_given
259 if test "$log_message" = ''
260 then
261 log_message="$1"
262 else
263 log_message="$log_message
264
265$1"
266 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800267 no_edit=t
268 shift
269 ;;
270 -m*)
Shawn Pearce68912812006-05-29 04:45:49 -0400271 log_given=m$log_given
272 if test "$log_message" = ''
273 then
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200274 log_message=`expr "z$1" : 'z-m\(.*\)'`
Shawn Pearce68912812006-05-29 04:45:49 -0400275 else
276 log_message="$log_message
277
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200278`expr "z$1" : 'z-m\(.*\)'`"
Shawn Pearce68912812006-05-29 04:45:49 -0400279 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800280 no_edit=t
281 shift
282 ;;
283 --m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
Shawn Pearce68912812006-05-29 04:45:49 -0400284 log_given=m$log_given
285 if test "$log_message" = ''
286 then
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200287 log_message=`expr "z$1" : 'z-[^=]*=\(.*\)'`
Shawn Pearce68912812006-05-29 04:45:49 -0400288 else
289 log_message="$log_message
290
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200291`expr "z$1" : 'zq-[^=]*=\(.*\)'`"
Shawn Pearce68912812006-05-29 04:45:49 -0400292 fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800293 no_edit=t
294 shift
295 ;;
296 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
297 verify=
298 shift
299 ;;
Junio C Hamanob4019f02006-03-02 21:04:05 -0800300 --a|--am|--ame|--amen|--amend)
301 amend=t
302 log_given=t$log_given
303 use_commit=HEAD
304 shift
305 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800306 -c)
307 case "$#" in 1) usage ;; esac
308 shift
309 log_given=t$log_given
310 use_commit="$1"
311 no_edit=
312 shift
313 ;;
314 --ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
Junio C Hamano0c091292005-08-08 17:03:14 -0700315 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
316 --reedit-messag=*|--reedit-message=*)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800317 log_given=t$log_given
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200318 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
Junio C Hamano130fcca2006-02-05 00:07:44 -0800319 no_edit=
320 shift
321 ;;
322 --ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
Junio C Hamano0c091292005-08-08 17:03:14 -0700323 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800324 case "$#" in 1) usage ;; esac
325 shift
326 log_given=t$log_given
327 use_commit="$1"
328 no_edit=
329 shift
330 ;;
331 -C)
332 case "$#" in 1) usage ;; esac
333 shift
334 log_given=t$log_given
335 use_commit="$1"
336 no_edit=t
337 shift
338 ;;
339 --reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
Junio C Hamano0c091292005-08-08 17:03:14 -0700340 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
341 --reuse-message=*)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800342 log_given=t$log_given
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200343 use_commit=`expr "z$1" : 'z-[^=]*=\(.*\)'`
Junio C Hamano130fcca2006-02-05 00:07:44 -0800344 no_edit=t
345 shift
346 ;;
347 --reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
Junio C Hamano0c091292005-08-08 17:03:14 -0700348 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800349 case "$#" in 1) usage ;; esac
350 shift
351 log_given=t$log_given
352 use_commit="$1"
353 no_edit=t
354 shift
355 ;;
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700356 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800357 signoff=t
358 shift
359 ;;
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800360 -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
361 verbose=t
362 shift
Junio C Hamano130fcca2006-02-05 00:07:44 -0800363 ;;
Matthias Lederhofer443f8332006-05-22 23:02:06 +0200364 -u|--u|--un|--unt|--untr|--untra|--untrac|--untrack|--untracke|--untracked|\
365 --untracked-|--untracked-f|--untracked-fi|--untracked-fil|--untracked-file|\
366 --untracked-files)
367 untracked_files=t
368 shift
369 ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700370 --)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800371 shift
372 break
373 ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700374 -*)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800375 usage
376 ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700377 *)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800378 break
379 ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700380 esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700381done
Jeff Kingcda8ab52006-06-23 09:43:38 -0400382case "$edit_flag" in t) no_edit= ;; esac
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700383
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800384################################################################
385# Sanity check options
386
Junio C Hamanob4019f02006-03-02 21:04:05 -0800387case "$amend,$initial_commit" in
388t,t)
389 die "You do not have anything to amend." ;;
390t,)
391 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
392 die "You are in the middle of a merge -- cannot amend."
393 fi ;;
394esac
395
Junio C Hamano0c091292005-08-08 17:03:14 -0700396case "$log_given" in
397tt*)
Shawn Pearce68912812006-05-29 04:45:49 -0400398 die "Only one of -c/-C/-F can be used." ;;
399*tm*|*mt*)
400 die "Option -m cannot be combined with -c/-C/-F." ;;
Junio C Hamano0c091292005-08-08 17:03:14 -0700401esac
402
Junio C Hamano6a746422006-04-20 01:20:56 -0700403case "$#,$also,$only,$amend" in
404*,t,t,*)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800405 die "Only one of --include/--only can be used." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07004060,t,,* | 0,,t,)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800407 die "No paths with --include/--only does not make sense." ;;
Junio C Hamano6a746422006-04-20 01:20:56 -07004080,,t,t)
409 only_include_assumed="# Clever... amending the last one with dirty index." ;;
4100,,,*)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800411 ;;
Junio C Hamano6a746422006-04-20 01:20:56 -0700412*,,,*)
Junio C Hamano756e3ee2006-02-14 17:51:02 -0800413 only_include_assumed="# Explicit paths specified without -i nor -o; assuming --only paths..."
Junio C Hamano4170a192006-02-12 23:55:07 -0800414 also=
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800415 ;;
416esac
417unset only
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800418case "$all,$also,$#" in
419t,t,*)
420 die "Cannot use -a and -i at the same time." ;;
421t,,[1-9]*)
422 die "Paths with -a does not make sense." ;;
423,t,0)
424 die "No paths with -i does not make sense." ;;
425esac
426
427################################################################
428# Prepare index to have a tree to be committed
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800429
Junio C Hamano130fcca2006-02-05 00:07:44 -0800430TOP=`git-rev-parse --show-cdup`
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800431if test -z "$TOP"
432then
433 TOP=./
434fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800435
436case "$all,$also" in
Junio C Hamano130fcca2006-02-05 00:07:44 -0800437t,)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800438 save_index &&
Junio C Hamano130fcca2006-02-05 00:07:44 -0800439 (
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800440 cd "$TOP"
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800441 GIT_INDEX_FILE="$NEXT_INDEX"
442 export GIT_INDEX_FILE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800443 git-diff-files --name-only -z |
444 git-update-index --remove -z --stdin
445 )
446 ;;
447,t)
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800448 save_index &&
Junio C Hamanobba319b2006-02-14 12:40:20 -0800449 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
450
Junio C Hamano130fcca2006-02-05 00:07:44 -0800451 git-diff-files --name-only -z -- "$@" |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800452 (
453 cd "$TOP"
454 GIT_INDEX_FILE="$NEXT_INDEX"
455 export GIT_INDEX_FILE
456 git-update-index --remove -z --stdin
457 )
Junio C Hamano22cff6a2005-08-16 18:08:19 -0700458 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800459,)
460 case "$#" in
461 0)
462 ;; # commit as-is
463 *)
464 if test -f "$GIT_DIR/MERGE_HEAD"
465 then
466 refuse_partial "Cannot do a partial commit during a merge."
467 fi
468 TMP_INDEX="$GIT_DIR/tmp-index$$"
469 if test -z "$initial_commit"
470 then
471 # make sure index is clean at the specified paths, or
472 # they are additions.
473 dirty_in_index=`git-diff-index --cached --name-status \
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800474 --diff-filter=DMTU HEAD -- "$@"`
Junio C Hamano130fcca2006-02-05 00:07:44 -0800475 test -z "$dirty_in_index" ||
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800476 refuse_partial "Different in index and the last commit:
Junio C Hamano130fcca2006-02-05 00:07:44 -0800477$dirty_in_index"
478 fi
Junio C Hamanobba319b2006-02-14 12:40:20 -0800479 commit_only=`git-ls-files --error-unmatch -- "$@"` || exit
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800480
481 # Build the temporary index and update the real index
482 # the same way.
483 if test -z "$initial_commit"
484 then
485 cp "$THIS_INDEX" "$TMP_INDEX"
486 GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -m HEAD
487 else
488 rm -f "$TMP_INDEX"
489 fi || exit
490
491 echo "$commit_only" |
492 GIT_INDEX_FILE="$TMP_INDEX" \
493 git-update-index --add --remove --stdin &&
494
495 save_index &&
496 echo "$commit_only" |
497 (
498 GIT_INDEX_FILE="$NEXT_INDEX"
499 export GIT_INDEX_FILE
500 git-update-index --remove --stdin
501 ) || exit
502 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800503 esac
Junio C Hamanof678dd12005-11-25 13:33:14 -0800504 ;;
Junio C Hamano130fcca2006-02-05 00:07:44 -0800505esac
506
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800507################################################################
508# If we do as-is commit, the index file will be THIS_INDEX,
509# otherwise NEXT_INDEX after we make this commit. We leave
510# the index as is if we abort.
Junio C Hamano0c091292005-08-08 17:03:14 -0700511
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800512if test -f "$NEXT_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800513then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800514 USE_INDEX="$NEXT_INDEX"
515else
516 USE_INDEX="$THIS_INDEX"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800517fi
518
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800519GIT_INDEX_FILE="$USE_INDEX" \
520 git-update-index -q $unmerged_ok_if_status --refresh || exit
521
522################################################################
523# If the request is status, just show it and exit.
524
525case "$0" in
526*status)
527 run_status
528 exit $?
529esac
530
531################################################################
532# Grab commit message, write out tree and make commit.
533
Junio C Hamano130fcca2006-02-05 00:07:44 -0800534if test t = "$verify" && test -x "$GIT_DIR"/hooks/pre-commit
535then
536 if test "$TMP_INDEX"
537 then
538 GIT_INDEX_FILE="$TMP_INDEX" "$GIT_DIR"/hooks/pre-commit
539 else
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800540 GIT_INDEX_FILE="$USE_INDEX" "$GIT_DIR"/hooks/pre-commit
Junio C Hamano130fcca2006-02-05 00:07:44 -0800541 fi || exit
542fi
Junio C Hamano0cfe1d32005-08-12 23:39:15 -0700543
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700544if test "$log_message" != ''
545then
546 echo "$log_message"
547elif test "$logfile" != ""
548then
549 if test "$logfile" = -
550 then
551 test -t 0 &&
552 echo >&2 "(reading log message from standard input)"
553 cat
554 else
555 cat <"$logfile"
556 fi
557elif test "$use_commit" != ""
558then
559 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
Junio C Hamanoe5215802005-11-01 22:01:28 -0800560elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
561then
562 cat "$GIT_DIR/MERGE_MSG"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700563elif test -f "$GIT_DIR/SQUASH_MSG"
564then
565 cat "$GIT_DIR/SQUASH_MSG"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700566fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700567
568case "$signoff" in
569t)
Junio C Hamanof6413392005-10-03 23:49:46 -0700570 {
571 echo
572 git-var GIT_COMMITTER_IDENT | sed -e '
573 s/>.*/>/
574 s/^/Signed-off-by: /
575 '
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700576 } >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamano2d569932005-08-31 17:15:25 -0700577 ;;
578esac
579
Junio C Hamano475443c2006-04-12 11:45:18 -0700580if test -f "$GIT_DIR/MERGE_HEAD" && test -z "$no_edit"; then
Junio C Hamano2d569932005-08-31 17:15:25 -0700581 echo "#"
Alex Riesenb6ae5402006-01-05 12:44:59 +0100582 echo "# It looks like you may be committing a MERGE."
Junio C Hamano2d569932005-08-31 17:15:25 -0700583 echo "# If this is not correct, please remove the file"
584 echo "# $GIT_DIR/MERGE_HEAD"
585 echo "# and try again"
586 echo "#"
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700587fi >>"$GIT_DIR"/COMMIT_EDITMSG
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700588
Junio C Hamano130fcca2006-02-05 00:07:44 -0800589# Author
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800590if test '' != "$force_author"
591then
Mark Woodingf327dbc2006-04-13 22:01:24 +0000592 GIT_AUTHOR_NAME=`expr "z$force_author" : 'z\(.*[^ ]\) *<.*'` &&
593 GIT_AUTHOR_EMAIL=`expr "z$force_author" : '.*\(<.*\)'` &&
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800594 test '' != "$GIT_AUTHOR_NAME" &&
595 test '' != "$GIT_AUTHOR_EMAIL" ||
Pavel Roskin82e5a822006-07-10 01:50:18 -0400596 die "malformed --author parameter"
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800597 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
598elif test '' != "$use_commit"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800599then
600 pick_author_script='
601 /^author /{
602 s/'\''/'\''\\'\'\''/g
603 h
604 s/^author \([^<]*\) <[^>]*> .*$/\1/
605 s/'\''/'\''\'\'\''/g
606 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
607
608 g
609 s/^author [^<]* <\([^>]*\)> .*$/\1/
610 s/'\''/'\''\'\'\''/g
611 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
612
613 g
614 s/^author [^<]* <[^>]*> \(.*\)$/\1/
615 s/'\''/'\''\'\'\''/g
616 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
617
618 q
619 }
620 '
621 set_author_env=`git-cat-file commit "$use_commit" |
622 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
623 eval "$set_author_env"
624 export GIT_AUTHOR_NAME
625 export GIT_AUTHOR_EMAIL
626 export GIT_AUTHOR_DATE
Junio C Hamano130fcca2006-02-05 00:07:44 -0800627fi
628
Linus Torvalds96069cf2005-06-14 10:20:14 -0700629PARENTS="-p HEAD"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800630if test -z "$initial_commit"
Junio C Hamano8098a172005-09-30 14:26:57 -0700631then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400632 rloga='commit'
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700633 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400634 rloga='commit (merge)'
Junio C Hamano91063bb2005-09-08 13:47:12 -0700635 PARENTS="-p HEAD "`sed -e 's/^/-p /' "$GIT_DIR/MERGE_HEAD"`
Junio C Hamanob4019f02006-03-02 21:04:05 -0800636 elif test -n "$amend"; then
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400637 rloga='commit (amend)'
Junio C Hamanob4019f02006-03-02 21:04:05 -0800638 PARENTS=$(git-cat-file commit HEAD |
639 sed -n -e '/^$/q' -e 's/^parent /-p /p')
Junio C Hamanoaba2da12005-08-25 18:57:35 -0700640 fi
Junio C Hamanob8310152006-03-02 21:13:24 -0800641 current=$(git-rev-parse --verify HEAD)
Junio C Hamano8098a172005-09-30 14:26:57 -0700642else
643 if [ -z "$(git-ls-files)" ]; then
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800644 echo >&2 Nothing to commit
Junio C Hamano8098a172005-09-30 14:26:57 -0700645 exit 1
646 fi
647 PARENTS=""
Junio C Hamanob8310152006-03-02 21:13:24 -0800648 current=
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400649 rloga='commit (initial)'
Linus Torvalds96069cf2005-06-14 10:20:14 -0700650fi
Junio C Hamano130fcca2006-02-05 00:07:44 -0800651
Junio C Hamano475443c2006-04-12 11:45:18 -0700652if test -z "$no_edit"
653then
654 {
Martin Waitz88a15312006-05-26 01:42:18 +0200655 echo ""
656 echo "# Please enter the commit message for your changes."
657 echo "# (Comment lines starting with '#' will not be included)"
Junio C Hamano475443c2006-04-12 11:45:18 -0700658 test -z "$only_include_assumed" || echo "$only_include_assumed"
659 run_status
660 } >>"$GIT_DIR"/COMMIT_EDITMSG
661else
662 # we need to check if there is anything to commit
663 run_status >/dev/null
664fi
Junio C Hamano85884522006-03-04 20:36:28 -0800665if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700666then
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700667 rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamano5a798fb2006-02-05 16:08:01 -0800668 run_status
Linus Torvaldsa3e870f2005-05-30 12:51:00 -0700669 exit 1
670fi
Junio C Hamano475443c2006-04-12 11:45:18 -0700671
Junio C Hamano0c091292005-08-08 17:03:14 -0700672case "$no_edit" in
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700673'')
Junio C Hamano7334f062006-02-04 22:10:32 -0800674 case "${VISUAL:-$EDITOR},$TERM" in
675 ,dumb)
676 echo >&2 "Terminal is dumb but no VISUAL nor EDITOR defined."
677 echo >&2 "Please supply the commit log message using either"
678 echo >&2 "-m or -F option. A boilerplate log message has"
679 echo >&2 "been prepared in $GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano130fcca2006-02-05 00:07:44 -0800680 exit 1
681 ;;
Junio C Hamano7334f062006-02-04 22:10:32 -0800682 esac
Seanec4e69c2006-05-13 23:09:32 -0400683 git-var GIT_AUTHOR_IDENT > /dev/null || die
684 git-var GIT_COMMITTER_IDENT > /dev/null || die
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700685 ${VISUAL:-${EDITOR:-vi}} "$GIT_DIR/COMMIT_EDITMSG"
Junio C Hamano5fec3ef2005-06-25 02:22:05 -0700686 ;;
687esac
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700688
689case "$verify" in
690t)
691 if test -x "$GIT_DIR"/hooks/commit-msg
692 then
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700693 "$GIT_DIR"/hooks/commit-msg "$GIT_DIR"/COMMIT_EDITMSG || exit
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700694 fi
695esac
696
Yann Dirson29f4ad82006-06-24 00:04:05 +0200697if test -z "$no_edit"
698then
699 sed -e '
700 /^diff --git a\/.*/{
701 s///
702 q
703 }
704 /^#/d
705 ' "$GIT_DIR"/COMMIT_EDITMSG
706else
707 cat "$GIT_DIR"/COMMIT_EDITMSG
708fi |
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800709git-stripspace >"$GIT_DIR"/COMMIT_MSG
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700710
711if cnt=`grep -v -i '^Signed-off-by' "$GIT_DIR"/COMMIT_MSG |
712 git-stripspace |
713 wc -l` &&
714 test 0 -lt $cnt
Junio C Hamano0c091292005-08-08 17:03:14 -0700715then
Junio C Hamano130fcca2006-02-05 00:07:44 -0800716 if test -z "$TMP_INDEX"
717 then
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800718 tree=$(GIT_INDEX_FILE="$USE_INDEX" git-write-tree)
Junio C Hamano130fcca2006-02-05 00:07:44 -0800719 else
720 tree=$(GIT_INDEX_FILE="$TMP_INDEX" git-write-tree) &&
721 rm -f "$TMP_INDEX"
722 fi &&
Santi_Béjarf8e2c542005-10-09 17:30:19 -0700723 commit=$(cat "$GIT_DIR"/COMMIT_MSG | git-commit-tree $tree $PARENTS) &&
Shawn Pearce67644a42006-05-19 05:16:18 -0400724 rlogm=$(sed -e 1q "$GIT_DIR"/COMMIT_MSG) &&
Shawn Pearcea3a733e2006-07-10 22:48:47 -0400725 git-update-ref -m "$rloga: $rlogm" HEAD $commit $current &&
Junio C Hamanocf7bb582006-02-10 00:45:59 -0800726 rm -f -- "$GIT_DIR/MERGE_HEAD" &&
727 if test -f "$NEXT_INDEX"
728 then
729 mv "$NEXT_INDEX" "$THIS_INDEX"
730 else
731 : ;# happy
732 fi
Junio C Hamano0c091292005-08-08 17:03:14 -0700733else
734 echo >&2 "* no commit message? aborting commit."
735 false
736fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700737ret="$?"
Junio C Hamano7d0c6882006-06-23 01:37:02 -0700738rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
Junio C Hamano1536dd9c62006-02-11 18:55:43 -0800739if test -d "$GIT_DIR/rr-cache"
740then
741 git-rerere
742fi
Junio C Hamano89e2c5f2005-08-18 17:20:08 -0700743
744if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
745then
746 "$GIT_DIR"/hooks/post-commit
747fi
Linus Torvalds170241b2005-06-19 19:57:01 -0700748exit "$ret"