Allow adding arbitary lines in the mail header generated by format-patch.
[git.git] / git-format-patch.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 USAGE='[-n | -k] [-o <dir> | --stdout] [--signoff] [--check] [--diff-options] <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.
10
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.
14
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.
18
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.'
22
23 . git-sh-setup
24
25 # Force diff to run in C locale.
26 LANG=C LC_ALL=C
27 export LANG LC_ALL
28
29 diff_opts=
30 LF='
31 '
32
33 outdir=./
34 while case "$#" in 0) break;; esac
35 do
36     case "$1" in
37     -c|--c|--ch|--che|--chec|--check)
38     check=t ;;
39     -a|--a|--au|--aut|--auth|--autho|--author|\
40     -d|--d|--da|--dat|--date|\
41     -m|--m|--mb|--mbo|--mbox) # now noop
42     ;;
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 ;;
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
64         ;;
65     -*' '* | -*"$LF"* | -*'     '*)
66         # Ignore diff option that has whitespace for now.
67         ;;
68     -*) diff_opts="$diff_opts$1 " ;;
69     *) break ;;
70     esac
71     shift
72 done
73
74 case "$keep_subject$numbered" in
75 tt)
76         die '--keep-subject and --numbered are incompatible.' ;;
77 esac
78
79 tmp=.tmp-series$$
80 trap 'rm -f $tmp-*' 0 1 2 3 15
81
82 series=$tmp-series
83 commsg=$tmp-commsg
84 filelist=$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.
94 # Also, "rev1.." should mean "rev1..HEAD"; git-diff users are
95 # familiar with that syntax.
96
97 case "$#,$1$2" in
98 1,?*..?*)
99         # single "rev1..rev2"
100         ;;
101 1,?*..)
102         # single "rev1.." should mean "rev1..HEAD"
103         set x "$1"HEAD
104         shift
105         ;;
106 1,*)
107         # single rev1
108         set x "$1..HEAD"
109         shift
110         ;;
111 2,?*..?*)
112         # not traditional "rev1" "rev2"
113         ;;
114 2,*)
115         set x "$1..$2"
116         shift
117         ;;
118 esac
119
120 # Now we have what we want in $@
121 for revpair
122 do
123         case "$revpair" in
124         ?*..?*)
125                 rev1=`expr "$revpair" : '\(.*\)\.\.'`
126                 rev2=`expr "$revpair" : '.*\.\.\(.*\)'`
127                 ;;
128         *)
129                 rev1="$revpair^"
130                 rev2="$revpair"
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
149 done >$series
150
151 me=`git-var GIT_AUTHOR_IDENT | sed -e 's/>.*/>/'`
152 headers=`git-repo-config --get format.headers`
153
154 case "$outdir" in
155 */) ;;
156 *) outdir="$outdir/" ;;
157 esac
158 test -d "$outdir" || mkdir -p "$outdir" || exit
159
160 titleScript='
161         /./d
162         /^$/n
163         s/^\[PATCH[^]]*\] *//
164         s/[^-a-z.A-Z_0-9]/-/g
165         s/\.\.\.*/\./g
166         s/\.*$//
167         s/--*/-/g
168         s/^-//
169         s/-$//
170         s/$/./
171         p
172         q
173 '
174
175 process_one () {
176         perl -w -e '
177 my ($keep_subject, $num, $signoff, $headers, $commsg) = @ARGV;
178 my ($signoff_pattern, $done_header, $done_subject, $done_separator, $signoff_seen,
179     $last_was_signoff);
180
181 if ($signoff) {
182         $signoff = "Signed-off-by: " . `git-var GIT_COMMITTER_IDENT`;
183         $signoff =~ s/>.*/>/;
184         $signoff_pattern = quotemeta($signoff);
185 }
186
187 my @weekday_names = qw(Sun Mon Tue Wed Thu Fri Sat);
188 my @month_names = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
189
190 sub show_date {
191     my ($time, $tz) = @_;
192     my $minutes = abs($tz);
193     $minutes = int($minutes / 100) * 60 + ($minutes % 100);
194     if ($tz < 0) {
195         $minutes = -$minutes;
196     }
197     my $t = $time + $minutes * 60;
198     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($t);
199     return sprintf("%s %s %d %02d:%02d:%02d %d %+05d",
200                    $weekday_names[$wday],
201                    $month_names[$mon],
202                    $mday, $hour, $min, $sec,
203                    $year+1900, $tz);
204 }
205
206 print "From nobody Mon Sep 17 00:00:00 2001\n";
207 open FH, "git stripspace <$commsg |" or die "open $commsg pipe";
208 while (<FH>) {
209     unless ($done_header) {
210         if (/^$/) {
211             $done_header = 1;
212         }
213         elsif (/^author (.*>) (.*)$/) {
214             my ($author_ident, $author_date) = ($1, $2);
215             my ($utc, $off) = ($author_date =~ /^(\d+) ([-+]?\d+)$/);
216             $author_date = show_date($utc, $off);
217
218             print "From: $author_ident\n";
219             print "Date: $author_date\n";
220         }
221         next;
222     }
223     unless ($done_subject) {
224         unless ($keep_subject) {
225             s/^\[PATCH[^]]*\]\s*//;
226             s/^/[PATCH$num] /;
227         }
228         if ($headers) {
229             print "$headers\n";
230         }
231         print "Subject: $_";
232         $done_subject = 1;
233         next;
234     }
235     unless ($done_separator) {
236         print "\n";
237         $done_separator = 1;
238         next if (/^$/);
239     }
240
241     $last_was_signoff = 0;
242     if (/Signed-off-by:/i) {
243         if ($signoff ne "" && /Signed-off-by:\s*$signoff_pattern$/i) {
244             $signoff_seen = 1;
245         }
246     }
247     print $_;
248 }
249 if (!$signoff_seen && $signoff ne "") {
250     if (!$last_was_signoff) {
251         print "\n";
252     }
253     print "$signoff\n";
254 }
255 print "\n---\n\n";
256 close FH or die "close $commsg pipe";
257 ' "$keep_subject" "$num" "$signoff" "$headers" $commsg
258
259         git-diff-tree -p $diff_opts "$commit" | git-apply --stat --summary
260         echo
261         git-diff-tree -p $diff_opts "$commit"
262         echo "-- "
263         echo "@@GIT_VERSION@@"
264
265         echo
266 }
267
268 total=`wc -l <$series | tr -dc "[0-9]"`
269 case "$total,$numbered" in
270 1,*)
271         numfmt='' ;;
272 *,t)
273         numfmt=`echo "$total" | wc -c`
274         numfmt=$(($numfmt-1))
275         numfmt=" %0${numfmt}d/$total"
276 esac
277
278 i=1
279 while read commit
280 do
281     git-cat-file commit "$commit" | git-stripspace >$commsg
282     title=`sed -ne "$titleScript" <$commsg`
283     case "$numbered" in
284     '') num= ;;
285     *)
286         num=`printf "$numfmt" $i` ;;
287     esac
288
289     file=`printf '%04d-%stxt' $i "$title"`
290     if test '' = "$stdout"
291     then
292             echo "$file"
293             process_one >"$outdir$file"
294             if test t = "$check"
295             then
296                 # This is slightly modified from Andrew Morton's Perfect Patch.
297                 # Lines you introduce should not have trailing whitespace.
298                 # Also check for an indentation that has SP before a TAB.
299                 grep -n '^+\([  ]*      .*\|.*[         ]\)$' "$outdir$file"
300                 :
301             fi
302     else
303             echo >&2 "$file"
304             process_one
305     fi
306     i=`expr "$i" + 1`
307 done <$series