]> git.ipfire.org Git - thirdparty/git.git/blame - git-format-patch.sh
Big tool rename.
[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=
30IFS='
31'
32LF='
33'
0acfc972 34
5c2c972f 35outdir=./
0acfc972
JH
36while case "$#" in 0) break;; esac
37do
38 case "$1" in
5c2c972f
JH
39 -a|--a|--au|--aut|--auth|--autho|--author)
40 author=t ;;
41 -c|--c|--ch|--che|--chec|--check)
42 check=t ;;
44d2eb51
JH
43 -d|--d|--da|--dat|--date)
44 date=t ;;
5c2c972f
JH
45 -m|--m|--mb|--mbo|--mbox)
46 date=t author=t mbox=t ;;
af5260ee
JH
47 -k|--k|--ke|--kee|--keep|--keep-|--keep-s|--keep-su|--keep-sub|\
48 --keep-subj|--keep-subje|--keep-subjec|--keep-subject)
49 keep_subject=t ;;
0acfc972
JH
50 -n|--n|--nu|--num|--numb|--numbe|--number|--numbere|--numbered)
51 numbered=t ;;
d9ac9df4 52 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
b097584b 53 signoff=t ;;
0acfc972
JH
54 -o=*|--o=*|--ou=*|--out=*|--outp=*|--outpu=*|--output=*|--output-=*|\
55 --output-d=*|--output-di=*|--output-dir=*|--output-dire=*|\
56 --output-direc=*|--output-direct=*|--output-directo=*|\
57 --output-director=*|--output-directory=*)
58 outdir=`expr "$1" : '-[^=]*=\(.*\)'` ;;
59 -o|--o|--ou|--out|--outp|--outpu|--output|--output-|--output-d|\
60 --output-di|--output-dir|--output-dire|--output-direc|--output-direct|\
61 --output-directo|--output-director|--output-directory)
62 case "$#" in 1) usage ;; esac; shift
63 outdir="$1" ;;
64 -*) diff_opts="$diff_opts$LF$1" ;;
65 *) break ;;
66 esac
67 shift
68done
69
af5260ee
JH
70case "$keep_subject$numbered" in
71tt)
72 die '--keep-subject and --numbered are incompatible.' ;;
73esac
74
4282c4fb 75revpair=
0acfc972 76case "$#" in
4282c4fb
JH
772)
78 revpair="$1..$2" ;;
791)
80 case "$1" in
81 *..*)
82 revpair="$1";;
83 *)
84 revpair="$1..HEAD";;
85 esac ;;
86*)
87 usage ;;
0acfc972
JH
88esac
89
44d2eb51
JH
90me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
91
0acfc972
JH
92case "$outdir" in
93*/) ;;
94*) outdir="$outdir/" ;;
95esac
96test -d "$outdir" || mkdir -p "$outdir" || exit
97
98tmp=.tmp-series$$
99trap 'rm -f $tmp-*' 0 1 2 3 15
100
101series=$tmp-series
44d2eb51 102commsg=$tmp-commsg
5c2c972f 103filelist=$tmp-files
0acfc972
JH
104
105titleScript='
1855c044
JH
106 /./d
107 /^$/n
108 s/^\[PATCH[^]]*\] *//
0acfc972
JH
109 s/[^-a-z.A-Z_0-9]/-/g
110 s/\.\.\.*/\./g
111 s/\.*$//
112 s/--*/-/g
113 s/^-//
114 s/-$//
115 s/$/./
1855c044 116 p
0acfc972
JH
117 q
118'
119
44d2eb51
JH
120whosepatchScript='
121/^author /{
122 s/author \(.*>\) \(.*\)$/au='\''\1'\'' ad='\''\2'\''/p
123 q
124}'
125
0acfc972
JH
126_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
127_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
128stripCommitHead='/^'"$_x40"' (from '"$_x40"')$/d'
129
c35a7b8d
JH
130git-rev-list --no-merges --merge-order \
131 $(git-rev-parse --revs-only "$revpair") >$series
9b75e9fa 132total=`wc -l <$series | tr -dc "[0-9]"`
0acfc972
JH
133i=$total
134while read commit
135do
44d2eb51
JH
136 git-cat-file commit "$commit" | git-stripspace >$commsg
137 title=`sed -ne "$titleScript" <$commsg`
0acfc972
JH
138 case "$numbered" in
139 '') num= ;;
140 *)
141 case $total in
142 1) num= ;;
143 *) num=' '`printf "%d/%d" $i $total` ;;
144 esac
145 esac
44d2eb51 146
0acfc972
JH
147 file=`printf '%04d-%stxt' $i "$title"`
148 i=`expr "$i" - 1`
f01c0fce 149 echo "* $file"
0acfc972
JH
150 {
151 mailScript='
1855c044 152 /./d
af5260ee
JH
153 /^$/n'
154 case "$keep_subject" in
155 t) ;;
5c2c972f
JH
156 *)
157 mailScript="$mailScript"'
af5260ee 158 s|^\[PATCH[^]]*\] *||
5c2c972f
JH
159 s|^|[PATCH'"$num"'] |'
160 ;;
161 esac
af5260ee
JH
162 mailScript="$mailScript"'
163 s|^|Subject: |'
164 case "$mbox" in
165 t)
166 echo 'From nobody Mon Sep 17 00:00:00 2001' ;# UNIX "From" line
167 ;;
168 esac
44d2eb51 169 eval "$(sed -ne "$whosepatchScript" $commsg)"
5c2c972f 170 test "$author,$au" = ",$me" || {
44d2eb51
JH
171 mailScript="$mailScript"'
172 a\
173From: '"$au"
174 }
175 test "$date,$au" = ",$me" || {
176 mailScript="$mailScript"'
177 a\
178Date: '"$ad"
179 }
180
181 mailScript="$mailScript"'
0acfc972
JH
182 : body
183 p
184 n
185 b body'
186
44d2eb51 187 sed -ne "$mailScript" <$commsg
b097584b
JS
188
189 test "$signoff" = "t" && {
190 offsigner=`git-var GIT_COMMITTER_IDENT | sed -e 's/>.*/>/'`
d9ac9df4
JS
191 line="Signed-off-by: $offsigner"
192 grep -q "^$line\$" $commsg || {
193 echo
194 echo "$line"
195 echo
196 }
b097584b 197 }
cc5625a5 198 echo
0acfc972
JH
199 echo '---'
200 echo
201 git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
202 echo
203 git-diff-tree -p $diff_opts "$commit" | sed -e "$stripCommitHead"
5c2c972f
JH
204
205 case "$mbox" in
206 t)
207 echo
208 ;;
209 esac
0acfc972 210 } >"$outdir$file"
5c2c972f
JH
211 case "$check" in
212 t)
213 # This is slightly modified from Andrew Morton's Perfect Patch.
214 # Lines you introduce should not have trailing whitespace.
215 # Also check for an indentation that has SP before a TAB.
216 grep -n '^+\([ ]* .*\|.*[ ]\)$' "$outdir$file"
217
218 : do not exit with non-zero because we saw no problem in the last one.
219 esac
0acfc972 220done <$series