1#!/bin/sh
2#
3# Copyright (c) 2005 Linus Torvalds
4#
5
6. git-sh-setup-script || die "Not a git archive"
7
8usage () {
9 die 'git commit [-a] [-v | --no-verify] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'
10}
11
12all= logfile= use_commit= no_edit= log_given= log_message= verify=t signoff=
13while case "$#" in 0) break;; esac
14do
15 case "$1" in
16 -a|--a|--al|--all)
17 all=t
18 shift ;;
19 -F=*|--f=*|--fi=*|--fil=*|--file=*)
20 log_given=t$log_given
21 logfile=`expr "$1" : '-[^=]*=\(.*\)'`
22 no_edit=t
23 shift ;;
24 -F|--f|--fi|--fil|--file)
25 case "$#" in 1) usage ;; esac; shift
26 log_given=t$log_given
27 logfile="$1"
28 no_edit=t
29 shift ;;
30 -m=*|--m=*|--me=*|--mes=*|--mess=*|--messa=*|--messag=*|--message=*)
31 log_given=t$log_given
32 log_message=`expr "$1" : '-[^=]*=\(.*\)'`
33 no_edit=t
34 shift ;;
35 -m|--m|--me|--mes|--mess|--messa|--messag|--message)
36 case "$#" in 1) usage ;; esac; shift
37 log_given=t$log_given
38 log_message="$1"
39 no_edit=t
40 shift ;;
41 -c=*|--ree=*|--reed=*|--reedi=*|--reedit=*|--reedit-=*|--reedit-m=*|\
42 --reedit-me=*|--reedit-mes=*|--reedit-mess=*|--reedit-messa=*|\
43 --reedit-messag=*|--reedit-message=*)
44 log_given=t$log_given
45 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
46 shift ;;
47 -c|--ree|--reed|--reedi|--reedit|--reedit-|--reedit-m|--reedit-me|\
48 --reedit-mes|--reedit-mess|--reedit-messa|--reedit-messag|--reedit-message)
49 case "$#" in 1) usage ;; esac; shift
50 log_given=t$log_given
51 use_commit="$1"
52 shift ;;
53 -C=*|--reu=*|--reus=*|--reuse=*|--reuse-=*|--reuse-m=*|--reuse-me=*|\
54 --reuse-mes=*|--reuse-mess=*|--reuse-messa=*|--reuse-messag=*|\
55 --reuse-message=*)
56 log_given=t$log_given
57 use_commit=`expr "$1" : '-[^=]*=\(.*\)'`
58 no_edit=t
59 shift ;;
60 -C|--reu|--reus|--reuse|--reuse-|--reuse-m|--reuse-me|--reuse-mes|\
61 --reuse-mess|--reuse-messa|--reuse-messag|--reuse-message)
62 case "$#" in 1) usage ;; esac; shift
63 log_given=t$log_given
64 use_commit="$1"
65 no_edit=t
66 shift ;;
67 -e|--e|--ed|--edi|--edit)
68 no_edit=
69 shift ;;
70 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
71 signoff=t
72 shift ;;
73 -n|--n|--no|--no-|--no-v|--no-ve|--no-ver|--no-veri|--no-verif|--no-verify)
74 verify=
75 shift ;;
76 -v|--v|--ve|--ver|--veri|--verif|--verify)
77 verify=t
78 shift ;;
79 --)
80 shift
81 break ;;
82 -*)
83 usage ;;
84 *)
85 break ;;
86 esac
87done
88
89case "$log_given" in
90tt*)
91 die "Only one of -c/-C/-F/-m can be used." ;;
92esac
93
94case "$all,$#" in
95t,*)
96 git-diff-files --name-only -z |
97 xargs -0 git-update-cache -q --remove --
98 ;;
99,0)
100 ;;
101*)
102 git-diff-files --name-only -z "$@" |
103 xargs -0 git-update-cache -q --remove --
104 ;;
105esac || exit 1
106git-update-cache -q --refresh || exit 1
107
108case "$verify" in
109t)
110 if test -x "$GIT_DIR"/hooks/pre-commit
111 then
112 "$GIT_DIR"/hooks/pre-commit || exit
113 fi
114esac
115
116if [ ! -r "$GIT_DIR/HEAD" ]
117then
118 echo "#"
119 echo "# Initial commit"
120 echo "#"
121 git-ls-files | sed 's/^/# New file: /'
122 echo "#"
123elif [ -f "$GIT_DIR/MERGE_HEAD" ]; then
124 echo "#"
125 echo "# It looks like your may be committing a MERGE."
126 echo "# If this is not correct, please remove the file"
127 echo "# $GIT_DIR/MERGE_HEAD"
128 echo "# and try again"
129 echo "#"
130fi >.editmsg
131if test "$log_message" != ''
132then
133 echo "$log_message"
134elif test "$logfile" != ""
135then
136 if test "$logfile" = -
137 then
138 test -t 0 &&
139 echo >&2 "(reading log message from standard input)"
140 cat
141 else
142 cat <"$logfile"
143 fi
144elif test "$use_commit" != ""
145then
146 git-cat-file commit "$use_commit" | sed -e '1,/^$/d'
147fi | git-stripspace >>.editmsg
148
149PARENTS="-p HEAD"
150if [ ! -r "$GIT_DIR/HEAD" ]; then
151 if [ -z "$(git-ls-files)" ]; then
152 echo Nothing to commit 1>&2
153 exit 1
154 fi
155 PARENTS=""
156else
157 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
158 PARENTS="-p HEAD -p MERGE_HEAD"
159 fi
160 if test "$use_commit" != ""
161 then
162 pick_author_script='
163 /^author /{
164 h
165 s/^author \([^<]*\) <[^>]*> .*$/\1/
166 s/'\''/'\''\'\'\''/g
167 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
168
169 g
170 s/^author [^<]* <\([^>]*\)> .*$/\1/
171 s/'\''/'\''\'\'\''/g
172 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
173
174 g
175 s/^author [^<]* <[^>]*> \(.*\)$/\1/
176 s/'\''/'\''\'\'\''/g
177 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
178
179 q
180 }
181 '
182 set_author_env=`git-cat-file commit "$use_commit" |
183 sed -ne "$pick_author_script"`
184 eval "$set_author_env"
185 export GIT_AUTHOR_NAME
186 export GIT_AUTHOR_EMAIL
187 export GIT_AUTHOR_DATE
188 fi
189 case "$signoff" in
190 t)
191 git-var GIT_COMMITTER_IDENT | sed -e '
192 s/>.*/>/
193 s/^/Signed-off-by: /
194 ' >>.editmsg
195 ;;
196 esac
197 git-status-script >>.editmsg
198fi
199if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]
200then
201 rm -f .editmsg
202 git-status-script
203 exit 1
204fi
205case "$no_edit" in
206'')
207 ${VISUAL:-${EDITOR:-vi}} .editmsg
208 ;;
209esac
210
211case "$verify" in
212t)
213 if test -x "$GIT_DIR"/hooks/commit-msg
214 then
215 "$GIT_DIR"/hooks/commit-msg .editmsg || exit
216 fi
217esac
218
219grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
220grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk
221if test -s .cmitchk
222then
223 tree=$(git-write-tree) &&
224 commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
225 echo $commit > "$GIT_DIR/HEAD" &&
226 rm -f -- "$GIT_DIR/MERGE_HEAD"
227else
228 echo >&2 "* no commit message? aborting commit."
229 false
230fi
231ret="$?"
232rm -f .cmitmsg .editmsg .cmitchk
233
234if test -x "$GIT_DIR"/hooks/post-commit && test "$ret" = 0
235then
236 "$GIT_DIR"/hooks/post-commit
237fi
238exit "$ret"