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