]> git.ipfire.org Git - thirdparty/git.git/blame - git-format-patch.sh
[PATCH] Update git-clone documentation
[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 () {
af5260ee 9 echo >&2 "usage: $0"' [-n] [-o dir] [--keep-subject] [--mbox] [--check] [--signoff] [-<diff options>...] upstream [ our-head ]
0acfc972
JH
10
11Prepare each commit with its patch since our-head forked from upstream,
12one file per patch, for e-mail submission. Each output file is
13numbered sequentially from 1, and uses the first line of the commit
14message (massaged for pathname safety) as the filename.
15
16When -o is specified, output files are created in that directory; otherwise in
17the current working directory.
18
19When -n is specified, instead of "[PATCH] Subject", the first line is formatted
20as "[PATCH N/M] Subject", unless you have only one patch.
5c2c972f
JH
21
22When --mbox is specified, the output is formatted to resemble
23UNIX mailbox format, and can be concatenated together for processing
24with applymbox.
0acfc972
JH
25'
26 exit 1
27}
28
29diff_opts=
0acfc972
JH
30LF='
31'
0acfc972 32
5c2c972f 33outdir=./
0acfc972
JH
34while case "$#" in 0) break;; esac
35do
36 case "$1" in
5c2c972f
JH
37 -a|--a|--au|--aut|--auth|--autho|--author)
38 author=t ;;
39 -c|--c|--ch|--che|--chec|--check)
40 check=t ;;
44d2eb51
JH
41 -d|--d|--da|--dat|--date)
42 date=t ;;
5c2c972f
JH
43 -m|--m|--mb|--mbo|--mbox)
44 date=t author=t mbox=t ;;
af5260ee
JH
45 -k|--k|--ke|--kee|--keep|--keep-|--keep-s|--keep-su|--keep-sub|\
46 --keep-subj|--keep-subje|--keep-subjec|--keep-subject)
47 keep_subject=t ;;
0acfc972
JH
48 -n|--n|--nu|--num|--numb|--numbe|--number|--numbere|--numbered)
49 numbered=t ;;
d9ac9df4 50 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
b097584b 51 signoff=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" ;;
4a5b63e3
JH
62 -*' '* | -*"$LF"* | -*' '*)
63 # Ignore diff option that has whitespace for now.
64 ;;
65 -*) diff_opts="$diff_opts$1 " ;;
0acfc972
JH
66 *) break ;;
67 esac
68 shift
69done
70
af5260ee
JH
71case "$keep_subject$numbered" in
72tt)
73 die '--keep-subject and --numbered are incompatible.' ;;
74esac
75
4a5b63e3 76rev1= rev2=
0acfc972 77case "$#" in
4282c4fb 782)
4a5b63e3 79 rev1="$1" rev2="$2" ;;
4282c4fb
JH
801)
81 case "$1" in
82 *..*)
4a5b63e3
JH
83 rev1=`expr "$1" : '\(.*\)\.\.'`
84 rev2=`expr "$1" : '.*\.\.\(.*\)'`
85 ;;
4282c4fb 86 *)
4a5b63e3
JH
87 rev1="$1"
88 rev2="HEAD"
89 ;;
4282c4fb
JH
90 esac ;;
91*)
92 usage ;;
0acfc972
JH
93esac
94
44d2eb51
JH
95me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
96
0acfc972
JH
97case "$outdir" in
98*/) ;;
99*) outdir="$outdir/" ;;
100esac
101test -d "$outdir" || mkdir -p "$outdir" || exit
102
103tmp=.tmp-series$$
104trap 'rm -f $tmp-*' 0 1 2 3 15
105
106series=$tmp-series
44d2eb51 107commsg=$tmp-commsg
5c2c972f 108filelist=$tmp-files
0acfc972
JH
109
110titleScript='
1855c044
JH
111 /./d
112 /^$/n
113 s/^\[PATCH[^]]*\] *//
0acfc972
JH
114 s/[^-a-z.A-Z_0-9]/-/g
115 s/\.\.\.*/\./g
116 s/\.*$//
117 s/--*/-/g
118 s/^-//
119 s/-$//
120 s/$/./
1855c044 121 p
0acfc972
JH
122 q
123'
124
44d2eb51
JH
125whosepatchScript='
126/^author /{
127 s/author \(.*>\) \(.*\)$/au='\''\1'\'' ad='\''\2'\''/p
128 q
129}'
130
0acfc972
JH
131_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
132_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
133stripCommitHead='/^'"$_x40"' (from '"$_x40"')$/d'
134
4a5b63e3
JH
135git-cherry -v "$rev1" "$rev2" |
136while read sign rev comment
137do
138 case "$sign" in
139 '-')
140 echo >&2 "Merged already: $comment"
141 ;;
142 *)
143 echo $rev
144 ;;
145 esac
146done >$series
147
9b75e9fa 148total=`wc -l <$series | tr -dc "[0-9]"`
4a5b63e3 149i=1
0acfc972
JH
150while read commit
151do
44d2eb51
JH
152 git-cat-file commit "$commit" | git-stripspace >$commsg
153 title=`sed -ne "$titleScript" <$commsg`
0acfc972
JH
154 case "$numbered" in
155 '') num= ;;
156 *)
157 case $total in
158 1) num= ;;
159 *) num=' '`printf "%d/%d" $i $total` ;;
160 esac
161 esac
44d2eb51 162
0acfc972 163 file=`printf '%04d-%stxt' $i "$title"`
4a5b63e3 164 i=`expr "$i" + 1`
f01c0fce 165 echo "* $file"
0acfc972
JH
166 {
167 mailScript='
1855c044 168 /./d
af5260ee
JH
169 /^$/n'
170 case "$keep_subject" in
171 t) ;;
5c2c972f
JH
172 *)
173 mailScript="$mailScript"'
af5260ee 174 s|^\[PATCH[^]]*\] *||
5c2c972f
JH
175 s|^|[PATCH'"$num"'] |'
176 ;;
177 esac
af5260ee
JH
178 mailScript="$mailScript"'
179 s|^|Subject: |'
180 case "$mbox" in
181 t)
182 echo 'From nobody Mon Sep 17 00:00:00 2001' ;# UNIX "From" line
183 ;;
184 esac
44d2eb51 185 eval "$(sed -ne "$whosepatchScript" $commsg)"
5c2c972f 186 test "$author,$au" = ",$me" || {
44d2eb51
JH
187 mailScript="$mailScript"'
188 a\
189From: '"$au"
190 }
191 test "$date,$au" = ",$me" || {
192 mailScript="$mailScript"'
193 a\
194Date: '"$ad"
195 }
196
197 mailScript="$mailScript"'
0acfc972
JH
198 : body
199 p
200 n
201 b body'
202
44d2eb51 203 sed -ne "$mailScript" <$commsg
b097584b
JS
204
205 test "$signoff" = "t" && {
206 offsigner=`git-var GIT_COMMITTER_IDENT | sed -e 's/>.*/>/'`
d9ac9df4
JS
207 line="Signed-off-by: $offsigner"
208 grep -q "^$line\$" $commsg || {
209 echo
210 echo "$line"
211 echo
212 }
b097584b 213 }
cc5625a5 214 echo
0acfc972
JH
215 echo '---'
216 echo
217 git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
218 echo
219 git-diff-tree -p $diff_opts "$commit" | sed -e "$stripCommitHead"
5c2c972f
JH
220
221 case "$mbox" in
222 t)
223 echo
224 ;;
225 esac
0acfc972 226 } >"$outdir$file"
5c2c972f
JH
227 case "$check" in
228 t)
229 # This is slightly modified from Andrew Morton's Perfect Patch.
230 # Lines you introduce should not have trailing whitespace.
231 # Also check for an indentation that has SP before a TAB.
232 grep -n '^+\([ ]* .*\|.*[ ]\)$' "$outdir$file"
233
234 : do not exit with non-zero because we saw no problem in the last one.
235 esac
0acfc972 236done <$series