]> git.ipfire.org Git - thirdparty/git.git/blame - git-format-patch.sh
Fix hooks/update template.
[thirdparty/git.git] / git-format-patch.sh
CommitLineData
0acfc972
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano
4#
5
215a7ad1 6. git-sh-setup || die "Not a git archive."
4282c4fb 7
0acfc972 8usage () {
603d8745
JH
9 echo >&2 "usage: $0"' [-n] [-o dir | --stdout] [--keep-subject] [--mbox]
10 [--check] [--signoff] [-<diff options>...]
93d69d86 11 [--help]
603d8745 12 ( from..to ... | upstream [ our-head ] )
0acfc972
JH
13
14Prepare each commit with its patch since our-head forked from upstream,
15one file per patch, for e-mail submission. Each output file is
16numbered sequentially from 1, and uses the first line of the commit
17message (massaged for pathname safety) as the filename.
18
19When -o is specified, output files are created in that directory; otherwise in
20the current working directory.
21
22When -n is specified, instead of "[PATCH] Subject", the first line is formatted
23as "[PATCH N/M] Subject", unless you have only one patch.
5c2c972f
JH
24
25When --mbox is specified, the output is formatted to resemble
26UNIX mailbox format, and can be concatenated together for processing
27with applymbox.
0acfc972
JH
28'
29 exit 1
30}
31
32diff_opts=
0acfc972
JH
33LF='
34'
0acfc972 35
5c2c972f 36outdir=./
0acfc972
JH
37while case "$#" in 0) break;; esac
38do
39 case "$1" in
5c2c972f
JH
40 -a|--a|--au|--aut|--auth|--autho|--author)
41 author=t ;;
42 -c|--c|--ch|--che|--chec|--check)
43 check=t ;;
44d2eb51
JH
44 -d|--d|--da|--dat|--date)
45 date=t ;;
5c2c972f
JH
46 -m|--m|--mb|--mbo|--mbox)
47 date=t author=t mbox=t ;;
af5260ee
JH
48 -k|--k|--ke|--kee|--keep|--keep-|--keep-s|--keep-su|--keep-sub|\
49 --keep-subj|--keep-subje|--keep-subjec|--keep-subject)
50 keep_subject=t ;;
0acfc972
JH
51 -n|--n|--nu|--num|--numb|--numbe|--number|--numbere|--numbered)
52 numbered=t ;;
d9ac9df4 53 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
b097584b 54 signoff=t ;;
655c7470
JH
55 --st|--std|--stdo|--stdou|--stdout)
56 stdout=t mbox=t date=t author=t ;;
0acfc972
JH
57 -o=*|--o=*|--ou=*|--out=*|--outp=*|--outpu=*|--output=*|--output-=*|\
58 --output-d=*|--output-di=*|--output-dir=*|--output-dire=*|\
59 --output-direc=*|--output-direct=*|--output-directo=*|\
60 --output-director=*|--output-directory=*)
61 outdir=`expr "$1" : '-[^=]*=\(.*\)'` ;;
62 -o|--o|--ou|--out|--outp|--outpu|--output|--output-|--output-d|\
63 --output-di|--output-dir|--output-dire|--output-direc|--output-direct|\
64 --output-directo|--output-director|--output-directory)
65 case "$#" in 1) usage ;; esac; shift
66 outdir="$1" ;;
93d69d86
JL
67 -h|--h|--he|--hel|--help)
68 usage
69 ;;
4a5b63e3
JH
70 -*' '* | -*"$LF"* | -*' '*)
71 # Ignore diff option that has whitespace for now.
72 ;;
73 -*) diff_opts="$diff_opts$1 " ;;
0acfc972
JH
74 *) break ;;
75 esac
76 shift
77done
78
af5260ee
JH
79case "$keep_subject$numbered" in
80tt)
81 die '--keep-subject and --numbered are incompatible.' ;;
82esac
83
603d8745
JH
84tmp=.tmp-series$$
85trap 'rm -f $tmp-*' 0 1 2 3 15
86
87series=$tmp-series
88commsg=$tmp-commsg
89filelist=$tmp-files
90
91# Backward compatible argument parsing hack.
92#
93# Historically, we supported:
94# 1. "rev1" is equivalent to "rev1..HEAD"
95# 2. "rev1..rev2"
96# 3. "rev1" "rev2 is equivalent to "rev1..rev2"
97#
98# We want to take a sequence of "rev1..rev2" in general.
bd7c8aab
JH
99# Also, "rev1.." should mean "rev1..HEAD"; git-diff users are
100# familiar with that syntax.
603d8745
JH
101
102case "$#,$1" in
1031,?*..?*)
104 # single "rev1..rev2"
105 ;;
bd7c8aab
JH
1061,?*..)
107 # single "rev1.." should mean "rev1..HEAD"
b748421a 108 set x "$1"HEAD
bd7c8aab
JH
109 shift
110 ;;
603d8745
JH
1111,*)
112 # single rev1
113 set x "$1..HEAD"
114 shift
115 ;;
1162,?*..?*)
117 # not traditional "rev1" "rev2"
4a5b63e3 118 ;;
603d8745
JH
1192,*)
120 set x "$1..$2"
121 shift
4a5b63e3 122 ;;
0acfc972
JH
123esac
124
603d8745
JH
125# Now we have what we want in $@
126for revpair
127do
128 case "$revpair" in
129 ?*..?*)
130 rev1=`expr "$revpair" : '\(.*\)\.\.'`
131 rev2=`expr "$revpair" : '.*\.\.\(.*\)'`
132 ;;
133 *)
134 usage
135 ;;
136 esac
137 git-rev-parse --verify "$rev1^0" >/dev/null 2>&1 ||
138 die "Not a valid rev $rev1 ($revpair)"
139 git-rev-parse --verify "$rev2^0" >/dev/null 2>&1 ||
140 die "Not a valid rev $rev2 ($revpair)"
141 git-cherry -v "$rev1" "$rev2" |
142 while read sign rev comment
143 do
144 case "$sign" in
145 '-')
146 echo >&2 "Merged already: $comment"
147 ;;
148 *)
149 echo $rev
150 ;;
151 esac
152 done
153done >$series
154
44d2eb51
JH
155me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
156
0acfc972
JH
157case "$outdir" in
158*/) ;;
159*) outdir="$outdir/" ;;
160esac
161test -d "$outdir" || mkdir -p "$outdir" || exit
162
0acfc972 163titleScript='
1855c044
JH
164 /./d
165 /^$/n
166 s/^\[PATCH[^]]*\] *//
0acfc972
JH
167 s/[^-a-z.A-Z_0-9]/-/g
168 s/\.\.\.*/\./g
169 s/\.*$//
170 s/--*/-/g
171 s/^-//
172 s/-$//
173 s/$/./
1855c044 174 p
0acfc972
JH
175 q
176'
177
44d2eb51
JH
178whosepatchScript='
179/^author /{
180 s/author \(.*>\) \(.*\)$/au='\''\1'\'' ad='\''\2'\''/p
181 q
182}'
183
655c7470 184process_one () {
0acfc972 185 mailScript='
1855c044 186 /./d
af5260ee
JH
187 /^$/n'
188 case "$keep_subject" in
189 t) ;;
5c2c972f
JH
190 *)
191 mailScript="$mailScript"'
af5260ee 192 s|^\[PATCH[^]]*\] *||
5c2c972f
JH
193 s|^|[PATCH'"$num"'] |'
194 ;;
195 esac
af5260ee
JH
196 mailScript="$mailScript"'
197 s|^|Subject: |'
198 case "$mbox" in
199 t)
200 echo 'From nobody Mon Sep 17 00:00:00 2001' ;# UNIX "From" line
201 ;;
202 esac
655c7470 203
e3e291fc 204 eval "$(LANG=C LC_ALL=C sed -ne "$whosepatchScript" $commsg)"
5c2c972f 205 test "$author,$au" = ",$me" || {
44d2eb51
JH
206 mailScript="$mailScript"'
207 a\
208From: '"$au"
209 }
210 test "$date,$au" = ",$me" || {
211 mailScript="$mailScript"'
212 a\
213Date: '"$ad"
214 }
215
216 mailScript="$mailScript"'
0acfc972
JH
217 : body
218 p
219 n
220 b body'
221
655c7470
JH
222 (cat $commsg ; echo; echo) |
223 sed -ne "$mailScript" |
224 git-stripspace
b097584b
JS
225
226 test "$signoff" = "t" && {
227 offsigner=`git-var GIT_COMMITTER_IDENT | sed -e 's/>.*/>/'`
d9ac9df4
JS
228 line="Signed-off-by: $offsigner"
229 grep -q "^$line\$" $commsg || {
230 echo
231 echo "$line"
232 echo
233 }
b097584b 234 }
cc5625a5 235 echo
0acfc972
JH
236 echo '---'
237 echo
238 git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
239 echo
3ff8cbed 240 git-cat-file commit "$commit^" | sed -e 's/^tree /applies-to: /' -e q
b10c1a74 241 git-diff-tree -p $diff_opts "$commit"
3ff8cbed
JH
242 echo "---"
243 echo "@@GIT_VERSION@@"
5c2c972f
JH
244
245 case "$mbox" in
246 t)
247 echo
248 ;;
249 esac
655c7470
JH
250}
251
252total=`wc -l <$series | tr -dc "[0-9]"`
253i=1
254while read commit
255do
256 git-cat-file commit "$commit" | git-stripspace >$commsg
257 title=`sed -ne "$titleScript" <$commsg`
258 case "$numbered" in
259 '') num= ;;
260 *)
261 case $total in
262 1) num= ;;
263 *) num=' '`printf "%d/%d" $i $total` ;;
264 esac
5c2c972f 265 esac
655c7470
JH
266
267 file=`printf '%04d-%stxt' $i "$title"`
268 if test '' = "$stdout"
269 then
270 echo "* $file"
271 process_one >"$outdir$file"
272 if test t = "$check"
273 then
274 # This is slightly modified from Andrew Morton's Perfect Patch.
275 # Lines you introduce should not have trailing whitespace.
276 # Also check for an indentation that has SP before a TAB.
277 grep -n '^+\([ ]* .*\|.*[ ]\)$' "$outdir$file"
278 :
279 fi
280 else
281 echo >&2 "* $file"
282 process_one
283 fi
284 i=`expr "$i" + 1`
0acfc972 285done <$series