format-patch: make sure header and body are separated.
[git/jnareb-git/bp-gitweb.git] / git-format-patch.sh
blobdaa3caea778e13e946c4ffe53c876f5222ce7dfe
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n | -k] [-o <dir> | --stdout] [--signoff] [--check] [--mbox] [--diff-options] <upstream> [<our-head>]'
7 LONG_USAGE='Prepare each commit with its patch since our-head forked from upstream,
8 one file per patch, for e-mail submission. Each output file is
9 numbered sequentially from 1, and uses the first line of the commit
10 message (massaged for pathname safety) as the filename.
12 When -o is specified, output files are created in that directory; otherwise in
13 the current working directory.
15 When -n is specified, instead of "[PATCH] Subject", the first line is formatted
16 as "[PATCH N/M] Subject", unless you have only one patch.
18 When --mbox is specified, the output is formatted to resemble
19 UNIX mailbox format, and can be concatenated together for processing
20 with applymbox.'
21 . git-sh-setup
23 # Force diff to run in C locale.
24 LANG=C LC_ALL=C
25 export LANG LC_ALL
27 diff_opts=
28 LF='
31 outdir=./
32 while case "$#" in 0) break;; esac
34 case "$1" in
35 -a|--a|--au|--aut|--auth|--autho|--author)
36 author=t ;;
37 -c|--c|--ch|--che|--chec|--check)
38 check=t ;;
39 -d|--d|--da|--dat|--date)
40 date=t ;;
41 -m|--m|--mb|--mbo|--mbox)
42 date=t author=t mbox=t ;;
43 -k|--k|--ke|--kee|--keep|--keep-|--keep-s|--keep-su|--keep-sub|\
44 --keep-subj|--keep-subje|--keep-subjec|--keep-subject)
45 keep_subject=t ;;
46 -n|--n|--nu|--num|--numb|--numbe|--number|--numbere|--numbered)
47 numbered=t ;;
48 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
49 signoff=t ;;
50 --st|--std|--stdo|--stdou|--stdout)
51 stdout=t mbox=t date=t author=t ;;
52 -o=*|--o=*|--ou=*|--out=*|--outp=*|--outpu=*|--output=*|--output-=*|\
53 --output-d=*|--output-di=*|--output-dir=*|--output-dire=*|\
54 --output-direc=*|--output-direct=*|--output-directo=*|\
55 --output-director=*|--output-directory=*)
56 outdir=`expr "$1" : '-[^=]*=\(.*\)'` ;;
57 -o|--o|--ou|--out|--outp|--outpu|--output|--output-|--output-d|\
58 --output-di|--output-dir|--output-dire|--output-direc|--output-direct|\
59 --output-directo|--output-director|--output-directory)
60 case "$#" in 1) usage ;; esac; shift
61 outdir="$1" ;;
62 -h|--h|--he|--hel|--help)
63 usage
65 -*' '* | -*"$LF"* | -*' '*)
66 # Ignore diff option that has whitespace for now.
68 -*) diff_opts="$diff_opts$1 " ;;
69 *) break ;;
70 esac
71 shift
72 done
74 case "$keep_subject$numbered" in
75 tt)
76 die '--keep-subject and --numbered are incompatible.' ;;
77 esac
79 tmp=.tmp-series$$
80 trap 'rm -f $tmp-*' 0 1 2 3 15
82 series=$tmp-series
83 commsg=$tmp-commsg
84 filelist=$tmp-files
86 # Backward compatible argument parsing hack.
88 # Historically, we supported:
89 # 1. "rev1" is equivalent to "rev1..HEAD"
90 # 2. "rev1..rev2"
91 # 3. "rev1" "rev2 is equivalent to "rev1..rev2"
93 # We want to take a sequence of "rev1..rev2" in general.
94 # Also, "rev1.." should mean "rev1..HEAD"; git-diff users are
95 # familiar with that syntax.
97 case "$#,$1$2" in
98 1,?*..?*)
99 # single "rev1..rev2"
101 1,?*..)
102 # single "rev1.." should mean "rev1..HEAD"
103 set x "$1"HEAD
104 shift
106 1,*)
107 # single rev1
108 set x "$1..HEAD"
109 shift
111 2,?*..?*)
112 # not traditional "rev1" "rev2"
114 2,*)
115 set x "$1..$2"
116 shift
118 esac
120 # Now we have what we want in $@
121 for revpair
123 case "$revpair" in
124 ?*..?*)
125 rev1=`expr "$revpair" : '\(.*\)\.\.'`
126 rev2=`expr "$revpair" : '.*\.\.\(.*\)'`
129 rev1="$revpair^"
130 rev2="$revpair"
132 esac
133 git-rev-parse --verify "$rev1^0" >/dev/null 2>&1 ||
134 die "Not a valid rev $rev1 ($revpair)"
135 git-rev-parse --verify "$rev2^0" >/dev/null 2>&1 ||
136 die "Not a valid rev $rev2 ($revpair)"
137 git-cherry -v "$rev1" "$rev2" |
138 while read sign rev comment
140 case "$sign" in
141 '-')
142 echo >&2 "Merged already: $comment"
145 echo $rev
147 esac
148 done
149 done >$series
151 me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
153 case "$outdir" in
154 */) ;;
155 *) outdir="$outdir/" ;;
156 esac
157 test -d "$outdir" || mkdir -p "$outdir" || exit
159 titleScript='
160 /./d
161 /^$/n
162 s/^\[PATCH[^]]*\] *//
163 s/[^-a-z.A-Z_0-9]/-/g
164 s/\.\.\.*/\./g
165 s/\.*$//
166 s/--*/-/g
167 s/^-//
168 s/-$//
169 s/$/./
174 whosepatchScript='
175 /^author /{
176 s/author \(.*>\) \(.*\)$/au='\''\1'\'' ad='\''\2'\''/p
180 process_one () {
181 mailScript='
182 /./d
183 /^$/n'
184 case "$keep_subject" in
185 t) ;;
187 mailScript="$mailScript"'
188 s|^\[PATCH[^]]*\] *||
189 s|^|[PATCH'"$num"'] |'
191 esac
192 mailScript="$mailScript"'
193 s|^|Subject: |'
194 case "$mbox" in
196 echo 'From nobody Mon Sep 17 00:00:00 2001' ;# UNIX "From" line
198 esac
200 eval "$(sed -ne "$whosepatchScript" $commsg)"
201 test "$author,$au" = ",$me" || {
202 mailScript="$mailScript"'
204 From: '"$au"
206 test "$date,$au" = ",$me" || {
207 mailScript="$mailScript"'
209 Date: '"$ad"
212 mailScript="$mailScript"'
215 : body
218 b body'
220 (cat $commsg ; echo; echo) |
221 sed -ne "$mailScript" |
222 git-stripspace
224 test "$signoff" = "t" && {
225 offsigner=`git-var GIT_COMMITTER_IDENT | sed -e 's/>.*/>/'`
226 line="Signed-off-by: $offsigner"
227 grep -q "^$line\$" $commsg || {
228 echo
229 echo "$line"
230 echo
233 echo
234 echo '---'
235 echo
236 git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
237 echo
238 git-diff-tree -p $diff_opts "$commit"
239 echo "-- "
240 echo "@@GIT_VERSION@@"
242 case "$mbox" in
244 echo
246 esac
249 total=`wc -l <$series | tr -dc "[0-9]"`
250 case "$total,$numbered" in
251 1,*)
252 numfmt='' ;;
253 *,t)
254 numfmt=`echo "$total" | wc -c`
255 numfmt=$(($numfmt-1))
256 numfmt=" %0${numfmt}d/$total"
257 esac
260 while read commit
262 git-cat-file commit "$commit" | git-stripspace >$commsg
263 title=`sed -ne "$titleScript" <$commsg`
264 case "$numbered" in
265 '') num= ;;
267 num=`printf "$numfmt" $i` ;;
268 esac
270 file=`printf '%04d-%stxt' $i "$title"`
271 if test '' = "$stdout"
272 then
273 echo "$file"
274 process_one >"$outdir$file"
275 if test t = "$check"
276 then
277 # This is slightly modified from Andrew Morton's Perfect Patch.
278 # Lines you introduce should not have trailing whitespace.
279 # Also check for an indentation that has SP before a TAB.
280 grep -n '^+\([ ]* .*\|.*[ ]\)$' "$outdir$file"
283 else
284 echo >&2 "$file"
285 process_one
287 i=`expr "$i" + 1`
288 done <$series