chiark / gitweb /
Check for git-send-email and die if not found
[topgit.git] / tg-export.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6 name=
7 branches=
8 output=
9 driver=collapse
10 flatten=false
11 numbered=false
12
13
14 ## Parse options
15
16 while [ -n "$1" ]; do
17         arg="$1"; shift
18         case "$arg" in
19         -b)
20                 branches="$1"; shift;;
21         --flatten)
22                 flatten=true;;
23         --numbered)
24                 flatten=true;
25                 numbered=true;;
26         --quilt)
27                 driver=quilt;;
28         --collapse)
29                 driver=collapse;;
30         -*)
31                 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
32                 exit 1;;
33         *)
34                 [ -z "$output" ] || die "output already specified ($output)"
35                 output="$arg";;
36         esac
37 done
38
39
40
41 [ -z "$branches" -o "$driver" = "quilt" ] ||
42         die "-b works only with the quilt driver"
43
44 [ "$driver" = "quilt" ] || ! "$numbered" ||
45         die "--numbered works only with the quilt driver";
46
47 [ "$driver" = "quilt" ] || ! "$flatten" ||
48         die "--flatten works only with the quilt driver"
49
50 if [ -z "$branches" ]; then
51         # this check is only needed when no branches have been passed
52         name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
53         base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
54                 die "not on a TopGit-controlled branch"
55 fi
56
57
58 playground="$(mktemp -d -t tg-export.XXXXXX)"
59 trap 'rm -rf "$playground"' EXIT
60
61
62 ## Collapse driver
63
64 # pretty_tree NAME
65 # Output tree ID of a cleaned-up tree without tg's artifacts.
66 pretty_tree()
67 {
68         (export GIT_INDEX_FILE="$playground/^index"
69          git read-tree "$1"
70          git update-index --force-remove ".topmsg" ".topdeps"
71          git write-tree)
72 }
73
74 # collapsed_commit NAME
75 # Produce a collapsed commit of branch NAME.
76 collapsed_commit()
77 {
78         name="$1"
79
80         rm -f "$playground/^pre" "$playground/^post"
81         >"$playground/^body"
82
83         # Get commit message and authorship information
84         git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
85
86         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
87         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
88         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
89         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
90
91         test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
92         test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
93         test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
94
95         # Determine parent
96         parent="$(cut -f 1 "$playground/$name^parents")"
97         if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
98                 # Produce a merge commit first
99                 parent="$({
100                         echo "TopGit-driven merge of branches:"
101                         echo
102                         cut -f 2 "$playground/$name^parents"
103                 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
104                         $(for p in $parent; do echo -p $p; done))"
105         fi
106
107         if branch_empty "$name"; then
108                 echo "$parent";
109         else
110                 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
111                 git stripspace |
112                 git commit-tree "$(pretty_tree "$name")" -p "$parent"
113         fi;
114
115         echo "$name" >>"$playground/^ticker"
116 }
117
118 # collapse
119 # This will collapse a single branch, using information about
120 # previously collapsed branches stored in $playground.
121 collapse()
122 {
123         if [ -s "$playground/$_dep" ]; then
124                 # We've already seen this dep
125                 commit="$(cat "$playground/$_dep")"
126
127         elif [ -z "$_dep_is_tgish" ]; then
128                 # This dep is not for rewrite
129                 commit="$(git rev-parse --verify "$_dep")"
130
131         else
132                 # First time hitting this dep; the common case
133                 echo "Collapsing $_dep"
134                 commit="$(collapsed_commit "$_dep")"
135                 mkdir -p "$playground/$(dirname "$_dep")"
136                 echo "$commit" >"$playground/$_dep"
137         fi
138
139         # Propagate our work through the dependency chain
140         mkdir -p "$playground/$(dirname "$_name")"
141         echo "$commit   $_dep" >>"$playground/$_name^parents"
142 }
143
144
145 ## Quilt driver
146
147 quilt()
148 {
149         if [ -z "$_dep_is_tgish" ]; then
150                 # This dep is not for rewrite
151                 return
152         fi
153
154         if "$flatten"; then
155                 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
156                 dn="";
157         else
158                 bn="$(basename "$_dep.diff")";
159                 dn="$(dirname "$_dep.diff")/";
160                 if [ "x$dn" = "x./" ]; then
161                         dn="";
162                 fi;
163         fi;
164
165         if [ -e "$playground/$_dep" ]; then
166                 # We've already seen this dep
167                 return
168         fi
169
170         mkdir -p "$playground/$(dirname "$_dep")";
171         touch "$playground/$_dep";
172
173         if branch_empty "$_dep"; then
174                 echo "Skip empty patch $_dep";
175         else
176                 if "$numbered"; then
177                         number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
178                         bn="$number-$bn";
179                         echo "$number" >"$playground/^number";
180                 fi;
181
182                 echo "Exporting $_dep"
183                 mkdir -p "$output/$dn";
184                 $tg patch "$_dep" >"$output/$dn$bn"
185                 echo "$dn$bn -p1" >>"$output/series"
186         fi
187 }
188
189
190 ## Machinery
191
192 if [ "$driver" = "collapse" ]; then
193         [ -n "$output" ] ||
194                 die "no target branch specified"
195         ! ref_exists "$output"  ||
196                 die "target branch '$output' already exists; first run: git branch -D $output"
197
198 elif [ "$driver" = "quilt" ]; then
199         [ -n "$output" ] ||
200                 die "no target directory specified"
201         [ ! -e "$output" ] ||
202                 die "target directory already exists: $output"
203
204         mkdir -p "$output"
205 fi
206
207
208 driver()
209 {
210         case $_dep in refs/remotes/*) return;; esac
211         branch_needs_update >/dev/null
212         [ "$_ret" -eq 0 ] ||
213                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
214
215         $driver
216 }
217
218 # Call driver on all the branches - this will happen
219 # in topological order.
220 if [ -z "$branches" ]; then
221         recurse_deps driver "$name"
222         (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
223 else
224         echo "$branches" | tr ',' '\n' | while read _dep; do
225                 _dep_is_tgish=1
226                 $driver
227         done
228         name="$(echo "$branches" | sed 's/.*,//')"
229 fi
230
231
232 if [ "$driver" = "collapse" ]; then
233         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
234
235         depcount="$(cat "$playground/^ticker" | wc -l)"
236         echo "Exported topic branch $name (total $depcount topics) to branch $output"
237
238 elif [ "$driver" = "quilt" ]; then
239         depcount="$(cat "$output/series" | wc -l)"
240         echo "Exported topic branch $name (total $depcount topics) to directory $output"
241 fi
242
243 # vim:noet