git-svnimport: if a limit is specified, respect it
[git/dscho.git] / git-format-patch.sh
blob2ebf7e8596eaf9253cf302a01d9f9fb8c08041cf
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 USAGE='[-n | -k] [-o <dir> | --stdout] [--signoff] [--check] [--diff-options] [--attach] <his> [<mine>]'
7 LONG_USAGE='Prepare each commit with its patch since <mine> head forked from
8 <his> head, one file per patch formatted to resemble UNIX mailbox
9 format, for e-mail submission or use with git-am.
11 Each output file is numbered sequentially from 1, and uses the
12 first line of the commit message (massaged for pathname safety)
13 as the filename.
15 When -o is specified, output files are created in <dir>; otherwise
16 they are created in the current working directory. This option
17 is ignored if --stdout is specified.
19 When -n is specified, instead of "[PATCH] Subject", the first
20 line is formatted as "[PATCH N/M] Subject", unless you have only
21 one patch.
23 When --attach is specified, patches are attached, not inlined.'
25 . git-sh-setup
27 # Force diff to run in C locale.
28 LANG=C LC_ALL=C
29 export LANG LC_ALL
31 diff_opts=
32 LF='
35 outdir=./
36 while case "$#" in 0) break;; esac
38 case "$1" in
39 -c|--c|--ch|--che|--chec|--check)
40 check=t ;;
41 -a|--a|--au|--aut|--auth|--autho|--author|\
42 -d|--d|--da|--dat|--date|\
43 -m|--m|--mb|--mbo|--mbox) # now noop
45 --at|--att|--atta|--attac|--attach)
46 attach=t ;;
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 ;;
50 -n|--n|--nu|--num|--numb|--numbe|--number|--numbere|--numbered)
51 numbered=t ;;
52 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
53 signoff=t ;;
54 --st|--std|--stdo|--stdou|--stdout)
55 stdout=t ;;
56 -o=*|--o=*|--ou=*|--out=*|--outp=*|--outpu=*|--output=*|--output-=*|\
57 --output-d=*|--output-di=*|--output-dir=*|--output-dire=*|\
58 --output-direc=*|--output-direct=*|--output-directo=*|\
59 --output-director=*|--output-directory=*)
60 outdir=`expr "$1" : '-[^=]*=\(.*\)'` ;;
61 -o|--o|--ou|--out|--outp|--outpu|--output|--output-|--output-d|\
62 --output-di|--output-dir|--output-dire|--output-direc|--output-direct|\
63 --output-directo|--output-director|--output-directory)
64 case "$#" in 1) usage ;; esac; shift
65 outdir="$1" ;;
66 -h|--h|--he|--hel|--help)
67 usage
69 -*' '* | -*"$LF"* | -*' '*)
70 # Ignore diff option that has whitespace for now.
72 -*) diff_opts="$diff_opts$1 " ;;
73 *) break ;;
74 esac
75 shift
76 done
78 case "$keep_subject$numbered" in
79 tt)
80 die '--keep-subject and --numbered are incompatible.' ;;
81 esac
83 tmp=.tmp-series$$
84 trap 'rm -f $tmp-*' 0 1 2 3 15
86 series=$tmp-series
87 commsg=$tmp-commsg
88 filelist=$tmp-files
90 # Backward compatible argument parsing hack.
92 # Historically, we supported:
93 # 1. "rev1" is equivalent to "rev1..HEAD"
94 # 2. "rev1..rev2"
95 # 3. "rev1" "rev2 is equivalent to "rev1..rev2"
97 # We want to take a sequence of "rev1..rev2" in general.
98 # Also, "rev1.." should mean "rev1..HEAD"; git-diff users are
99 # familiar with that syntax.
101 case "$#,$1$2" in
102 1,?*..?*)
103 # single "rev1..rev2"
105 1,?*..)
106 # single "rev1.." should mean "rev1..HEAD"
107 set x "$1"HEAD
108 shift
110 1,*)
111 # single rev1
112 set x "$1..HEAD"
113 shift
115 2,?*..?*)
116 # not traditional "rev1" "rev2"
118 2,*)
119 set x "$1..$2"
120 shift
122 esac
124 # Now we have what we want in $@
125 for revpair
127 case "$revpair" in
128 ?*..?*)
129 rev1=`expr "$revpair" : '\(.*\)\.\.'`
130 rev2=`expr "$revpair" : '.*\.\.\(.*\)'`
133 rev1="$revpair^"
134 rev2="$revpair"
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
144 case "$sign" in
145 '-')
146 echo >&2 "Merged already: $comment"
149 echo $rev
151 esac
152 done
153 done >$series
155 me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
156 headers=`git-repo-config --get format.headers`
157 case "$attach" in
158 "") ;;
160 mimemagic="050802040500080604070107"
161 esac
163 case "$outdir" in
164 */) ;;
165 *) outdir="$outdir/" ;;
166 esac
167 test -d "$outdir" || mkdir -p "$outdir" || exit
169 titleScript='
170 /./d
171 /^$/n
172 s/^\[PATCH[^]]*\] *//
173 s/[^-a-z.A-Z_0-9]/-/g
174 s/\.\.\.*/\./g
175 s/\.*$//
176 s/--*/-/g
177 s/^-//
178 s/-$//
179 s/$/./
184 process_one () {
185 perl -w -e '
186 my ($keep_subject, $num, $signoff, $headers, $mimemagic, $commsg) = @ARGV;
187 my ($signoff_pattern, $done_header, $done_subject, $done_separator, $signoff_seen,
188 $last_was_signoff);
190 if ($signoff) {
191 $signoff = "Signed-off-by: " . `git-var GIT_COMMITTER_IDENT`;
192 $signoff =~ s/>.*/>/;
193 $signoff_pattern = quotemeta($signoff);
196 my @weekday_names = qw(Sun Mon Tue Wed Thu Fri Sat);
197 my @month_names = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
199 sub show_date {
200 my ($time, $tz) = @_;
201 my $minutes = abs($tz);
202 $minutes = int($minutes / 100) * 60 + ($minutes % 100);
203 if ($tz < 0) {
204 $minutes = -$minutes;
206 my $t = $time + $minutes * 60;
207 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($t);
208 return sprintf("%s %s %d %02d:%02d:%02d %d %+05d",
209 $weekday_names[$wday],
210 $month_names[$mon],
211 $mday, $hour, $min, $sec,
212 $year+1900, $tz);
215 print "From nobody Mon Sep 17 00:00:00 2001\n";
216 open FH, "git stripspace <$commsg |" or die "open $commsg pipe";
217 while (<FH>) {
218 unless ($done_header) {
219 if (/^$/) {
220 $done_header = 1;
222 elsif (/^author (.*>) (.*)$/) {
223 my ($author_ident, $author_date) = ($1, $2);
224 my ($utc, $off) = ($author_date =~ /^(\d+) ([-+]?\d+)$/);
225 $author_date = show_date($utc, $off);
227 print "From: $author_ident\n";
228 print "Date: $author_date\n";
230 next;
232 unless ($done_subject) {
233 unless ($keep_subject) {
234 s/^\[PATCH[^]]*\]\s*//;
235 s/^/[PATCH$num] /;
237 if ($headers) {
238 print "$headers\n";
240 print "Subject: $_";
241 if ($mimemagic) {
242 print "MIME-Version: 1.0\n";
243 print "Content-Type: multipart/mixed;\n";
244 print " boundary=\"------------$mimemagic\"\n";
245 print "\n";
246 print "This is a multi-part message in MIME format.\n";
247 print "--------------$mimemagic\n";
248 print "Content-Type: text/plain; charset=UTF-8; format=fixed\n";
249 print "Content-Transfer-Encoding: 8bit\n";
251 $done_subject = 1;
252 next;
254 unless ($done_separator) {
255 print "\n";
256 $done_separator = 1;
257 next if (/^$/);
260 $last_was_signoff = 0;
261 if (/Signed-off-by:/i) {
262 if ($signoff ne "" && /Signed-off-by:\s*$signoff_pattern$/i) {
263 $signoff_seen = 1;
266 print $_;
268 if (!$signoff_seen && $signoff ne "") {
269 if (!$last_was_signoff) {
270 print "\n";
272 print "$signoff\n";
274 print "\n---\n\n";
275 close FH or die "close $commsg pipe";
276 ' "$keep_subject" "$num" "$signoff" "$headers" "$mimemagic" $commsg
278 git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
279 echo
280 case "$mimemagic" in
281 '');;
283 echo "--------------$mimemagic"
284 echo "Content-Type: text/x-patch;"
285 echo " name=\"$commit.diff\""
286 echo "Content-Transfer-Encoding: 8bit"
287 echo "Content-Disposition: inline;"
288 echo " filename=\"$commit.diff\""
289 echo
290 esac
291 git-diff-tree -p $diff_opts "$commit"
292 case "$mimemagic" in
294 echo "-- "
295 echo "@@GIT_VERSION@@"
298 echo
299 echo "--------------$mimemagic--"
300 echo
302 esac
303 echo
306 total=`wc -l <$series | tr -dc "[0-9]"`
307 case "$total,$numbered" in
308 1,*)
309 numfmt='' ;;
310 *,t)
311 numfmt=`echo "$total" | wc -c`
312 numfmt=$(($numfmt-1))
313 numfmt=" %0${numfmt}d/$total"
314 esac
317 while read commit
319 git-cat-file commit "$commit" | git-stripspace >$commsg
320 title=`sed -ne "$titleScript" <$commsg`
321 case "$numbered" in
322 '') num= ;;
324 num=`printf "$numfmt" $i` ;;
325 esac
327 file=`printf '%04d-%stxt' $i "$title"`
328 if test '' = "$stdout"
329 then
330 echo "$file"
331 process_one >"$outdir$file"
332 if test t = "$check"
333 then
334 # This is slightly modified from Andrew Morton's Perfect Patch.
335 # Lines you introduce should not have trailing whitespace.
336 # Also check for an indentation that has SP before a TAB.
337 grep -n '^+\([ ]* .*\|.*[ ]\)$' "$outdir$file"
340 else
341 echo >&2 "$file"
342 process_one
344 i=`expr "$i" + 1`
345 done <$series