Test commit
[cogito/jonas.git] / cg-mkpatch
blob5d4c67436d26435a015aa0c41404f4968b437ba2
1 #!/usr/bin/env bash
3 # Create a patch from a commit or a series of commits
4 # Copyright (c) Petr Baudis, 2005
6 # Generate a patch with diff statistics and meta info about each commit.
8 # Note that if you want to use this as the output interface for your
9 # GIT tree containing changes against upstream GIT tree, please consider
10 # using the StGIT tool ("quilt for GIT"), which will enable you to
11 # update your patches seamlessly, rebase them against the latest upstream
12 # version, directly send them over mail, etc.
14 # OPTIONS
15 # -------
16 # -d DIRNAME:: Create patches in the DIRNAME directory
17 # Split the patches to separate files with their names in the
18 # format "%02d.patch", created in directory DIRNAME (will be
19 # created if non-existent). Note that this makes sense only
20 # when generating patch series, that is when you use the -r
21 # argument.
23 # -f FORMAT:: Specify patch file name format
24 # Format string used for generating the patch filename when
25 # outputting the split-out patches (that is, passed the -d
26 # option). This is by default "%s/%02d-%s.patch". The first %s
27 # represents the directory name and %d represents the patch
28 # sequence number. The last %s is mangled first line of the
29 # commit message - kind of patch title.
31 # -m:: Base the diff at the merge base
32 # Base the patches at the merge base of the -r arguments
33 # (defaulting to HEAD and 'origin' or the current branch's default
34 # remote branch, see `cg-fetch` for details).
36 # -r FROM_ID[..TO_ID]:: Limit to revision range
37 # Specify a set of commits to make patches from using either
38 # '-r FROM_ID[..TO_ID]' or '-r FROM_ID -r TO_ID'. In both cases the
39 # option expects IDs which resolve to commits and will include the
40 # specified IDs. If 'TO_ID' is omitted patches for all commits
41 # from 'FROM_ID' to the initial commit will be generated. If the
42 # `-r` option is not given the commit ID defaults to 'HEAD'.
44 # -s:: Omit patch header
45 # Specify whether to print a short version of the patch without
46 # a patch header with meta info such as author and committer.
48 # EXAMPLE USAGE
49 # -------------
50 # To make patches for all commits between two releases tagged as
51 # 'releasetag-0.9' and 'releasetag-0.10' do:
53 # $ cg-mkpatch -r releasetag-0.9..releasetag-0.10
55 # The output will be a continuous dump of patches each separated by
56 # the line:
58 # !-------------------------------------------------------------flip-
60 # NOTES
61 # -----
62 # The ':' is equivalent to '..' in revisions range specification (to make
63 # things more comfortable to SVN users). See cogito(7) for more details
64 # about revision specification.
66 # Testsuite: TODO
68 USAGE="cg-mkpatch [-m] [-s] [-r FROM_ID[..TO_ID] [-d DIRNAME]]"
69 _git_requires_root=1
70 _git_wc_unneeded=1
72 . "${COGITO_LIB}"cg-Xlib || exit 1
74 showpatch()
76 header="$(mktemp -t gitpatch.XXXXXX)"
77 patch="$(mktemp -t gitpatch.XXXXXX)"
78 id="$1"
79 cg-diff -p -r "$id" >"$patch"
80 git-cat-file commit "$id" | while read -r key rest; do
81 case "$key" in
82 "author"|"committer")
83 date=(${rest#*> })
84 showdate ${date[*]}; pdate="$_showdate"
85 [ "$pdate" ] && rest="${rest%> *}> $pdate"
86 echo "$key" "$rest" >>"$header"
88 "")
89 cat
90 if [ ! "$omit_header" ]; then
91 echo
92 echo ---
94 echo commit "$id"
95 cat "$header"
96 echo
97 cat "$patch" | git-apply --stat
101 echo "$key" "$rest" >>"$header"
103 esac
104 done
105 echo
106 cat "$patch"
107 rm "$header" "$patch"
111 omit_header=
112 log_start=
113 log_end=
114 mergebase=
115 outdir=
116 fileformat="%s/%02d-%s.patch"
117 while optparse; do
118 if optparse -s; then
119 omit_header=1
120 elif optparse -r=; then
121 if echo "$OPTARG" | fgrep -q '..'; then
122 log_end="${OPTARG#*..}"
123 [ "$log_end" ] || log_end="HEAD"
124 log_start="${OPTARG%..*}"
125 elif echo "$OPTARG" | grep -q ':'; then
126 log_end="${OPTARG#*:}"
127 [ "$log_end" ] || log_end="HEAD"
128 log_start="${OPTARG%:*}"
129 elif [ -z "$log_start" ]; then
130 log_start="$OPTARG"
131 else
132 log_end="$OPTARG"
134 elif optparse -m; then
135 mergebase=1
136 elif optparse -d=; then
137 outdir="$OPTARG"
138 elif optparse -f=; then
139 fileformat="$OPTARG"
140 else
141 optfail
143 done
145 if [ "$mergebase" ]; then
146 [ "$log_start" ] || log_start="HEAD"
147 [ "$log_end" ] || { log_end="$(choose_origin refs/heads "what to mkpatch against?")" || exit 1; }
148 id1="$(cg-object-id -c "$log_start")" || exit 1
149 id2="$(cg-object-id -c "$log_end")" || exit 1
150 conservative_merge_base "$id1" "$id2" || exit 1
151 [ "$_cg_base_conservative" ] &&
152 warn -b "multiple merge bases, picking the most conservative one"
153 log_start="$_cg_baselist"
156 if [ "$log_end" ]; then
157 id1="$(cg-object-id -c "$log_start")" || exit 1
158 id2="$(cg-object-id -c "$log_end")" || exit 1
160 if [ "$outdir" ]; then
161 mkdir -p "$outdir" || die "cannot create patch directory"
162 pnum=001
165 git-rev-list --topo-order "$id2" "^$id1" | tac | while read id; do
166 if [ "$outdir" ]; then
167 title="$(git-cat-file commit "$id" |
168 sed -n '/^$/{n;
169 s/^[ \t]*\[[^]]*\][ \t]*//;
170 s/[:., \t][:., \t]*/-/g;
171 s/_/-/g;
172 # *cough*
173 y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/;
174 s/[^a-zA-Z0-9-]//g;
175 p;q;}')"
176 filename="$(printf "$fileformat" "$outdir" "$pnum" "$title")"
177 echo "$filename"
178 showpatch "$id" >"$filename"
179 pnum=$((pnum+1))
180 else
181 showpatch "$id"
182 echo
183 echo
184 echo -e '\014'
185 echo '!-------------------------------------------------------------flip-'
186 echo
187 echo
189 done
191 else
192 [ "$outdir" ] && die "-d makes sense only for patch series"
193 id="$(cg-object-id -c "$log_start")" || exit 1
194 showpatch "$id"