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