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] [-m <message>] [-F <logfile>] [(-C|-c) <commit>] [<path>...]'
10}
11
12all= logfile= use_commit= no_edit= log_given= log_message= verify= 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 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
68 signoff=t
69 shift ;;
70 -v|--v|--ve|--ver|--veri|--verif|--verify)
71 verify=t
72 shift ;;
73 --)
74 shift
75 break ;;
76 -*)
77 usage ;;
78 *)
79 break ;;
80 esac
81done
82
83case "$log_given" in
84tt*)
85 die "Only one of -c/-C/-F/-m can be used." ;;
86esac
87
88case "$all,$#" in
89t,*)
90 git-diff-files --name-only -z |
91 xargs -0 git-update-cache -q --remove --
92 ;;
93,0)
94 ;;
95*)
96 git-diff-files --name-only -z "$@" |
97 xargs -0 git-update-cache -q --remove --
98 ;;
99esac || exit 1
100git-update-cache -q --refresh || exit 1
101
102case "$verify" in
103t)
104 # This is slightly modified from Andrew Morton's Perfect Patch.
105 # Lines you introduce should not have trailing whitespace.
106 # Also check for an indentation that has SP before a TAB.
107 perl -e '
108 my $fh;
109 my $found_bad = 0;
110 my $filename;
111 my $reported_filename = "";
112 my $lineno;
113 sub bad_line {
114 my ($why, $line) = @_;
115 if (!$found_bad) {
116 print "*\n";
117 print "* You have some suspicious patch lines:\n";
118 print "*\n";
119 $found_bad = 1;
120 }
121 if ($reported_filename ne $filename) {
122 print "* In $filename\n";
123 $reported_filename = $filename;
124 }
125 print "* $why (line $lineno)\n$line\n";
126 }
127 open $fh, "-|", qw(git-diff-cache -p -M --cached HEAD);
128 while (<$fh>) {
129 if (m|^diff --git a/(.*) b/\1$|) {
130 $filename = $1;
131 next;
132 }
133 if (/^@@ -\S+ \+(\d+)/) {
134 $lineno = $1 - 1;
135 next;
136 }
137 if (/^ /) {
138 $lineno++;
139 next;
140 }
141 if (s/^\+//) {
142 $lineno++;
143 chomp;
144 if (/\s$/) {
145 bad_line("trailing whitespace", $_);
146 }
147 if (/^\s* /) {
148 bad_line("indent SP followed by a TAB", $_);
149 }
150 }
151 }
152 exit($found_bad);
153 ' || exit ;;
154esac
155
156PARENTS="-p HEAD"
157if [ ! -r "$GIT_DIR/HEAD" ]; then
158 if [ -z "$(git-ls-files)" ]; then
159 echo Nothing to commit 1>&2
160 exit 1
161 fi
162 {
163 echo "#"
164 echo "# Initial commit"
165 case "$no_edit" in
166 t) echo "# (ignoring your commit message for initial commit)"
167 no_edit=
168 esac
169 echo "#"
170 git-ls-files | sed 's/^/# New file: /'
171 echo "#"
172 } >.editmsg
173 PARENTS=""
174 no_edit=
175else
176 if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
177 {
178 echo "#"
179 echo "# It looks like your may be committing a MERGE."
180 echo "# If this is not correct, please remove the file"
181 echo "# $GIT_DIR/MERGE_HEAD"
182 echo "# and try again"
183 case "$no_edit" in
184 t) echo "# (ignoring your commit message for merge commit)"
185 no_edit=
186 esac
187 echo "#"
188 } |
189 git-stripspace >.editmsg
190 PARENTS="-p HEAD -p MERGE_HEAD"
191 elif test "$log_message" != ''
192 then
193 echo "$log_message" |
194 git-stripspace >.editmsg
195 elif test "$logfile" != ""
196 then
197 if test "$logfile" = -
198 then
199 test -t 0 &&
200 echo >&2 "(reading log message from standard input)"
201 cat
202 else
203 cat <"$logfile"
204 fi |
205 git-stripspace >.editmsg
206 elif test "$use_commit" != ""
207 then
208 pick_author_script='
209 /^author /{
210 h
211 s/^author \([^<]*\) <[^>]*> .*$/\1/
212 s/'\''/'\''\'\'\''/g
213 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
214
215 g
216 s/^author [^<]* <\([^>]*\)> .*$/\1/
217 s/'\''/'\''\'\'\''/g
218 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
219
220 g
221 s/^author [^<]* <[^>]*> \(.*\)$/\1/
222 s/'\''/'\''\'\'\''/g
223 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
224
225 q
226 }
227 '
228 set_author_env=`git-cat-file commit "$use_commit" |
229 sed -ne "$pick_author_script"`
230 eval "$set_author_env"
231 export GIT_AUTHOR_NAME
232 export GIT_AUTHOR_EMAIL
233 export GIT_AUTHOR_DATE
234 git-cat-file commit "$use_commit" |
235 sed -e '1,/^$/d' |
236 git-stripspace >.editmsg
237 fi
238
239 case "$signoff" in
240 t)
241 git-var GIT_COMMITTER_IDENT | sed -e '
242 s/>.*/>/
243 s/^/Signed-off-by: /' >>.editmsg ;;
244 esac
245 git-status-script >>.editmsg
246fi
247if [ "$?" != "0" -a ! -f $GIT_DIR/MERGE_HEAD ]
248then
249 cat .editmsg
250 rm .editmsg
251 exit 1
252fi
253case "$no_edit" in
254'')
255 ${VISUAL:-${EDITOR:-vi}} .editmsg
256 ;;
257esac
258grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
259grep -v -i '^Signed-off-by' .cmitmsg >.cmitchk
260if test -s .cmitchk
261then
262 tree=$(git-write-tree) &&
263 commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
264 echo $commit > "$GIT_DIR/HEAD" &&
265 rm -f -- "$GIT_DIR/MERGE_HEAD"
266else
267 echo >&2 "* no commit message? aborting commit."
268 false
269fi
270ret="$?"
271rm -f .cmitmsg .editmsg .cmitchk
272exit "$ret"