chiark / gitweb /
put die() messages to stderr
[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         --linearize)
31                 driver=linearize;;
32         -*)
33                 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY | --linearize NEWBRANCH)" >&2
34                 exit 1;;
35         *)
36                 [ -z "$output" ] || die "output already specified ($output)"
37                 output="$arg";;
38         esac
39 done
40
41
42
43 [ -z "$branches" -o "$driver" = "quilt" ] ||
44         die "-b works only with the quilt driver"
45
46 [ "$driver" = "quilt" ] || ! "$numbered" ||
47         die "--numbered works only with the quilt driver";
48
49 [ "$driver" = "quilt" ] || ! "$flatten" ||
50         die "--flatten works only with the quilt driver"
51
52 if [ -z "$branches" ]; then
53         # this check is only needed when no branches have been passed
54         name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
55         base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
56                 die "not on a TopGit-controlled branch"
57 fi
58
59
60 playground="$(mktemp -d -t tg-export.XXXXXX)"
61 trap 'rm -rf "$playground"' EXIT
62
63
64 ## Collapse driver
65
66 # pretty_tree NAME
67 # Output tree ID of a cleaned-up tree without tg's artifacts.
68 pretty_tree()
69 {
70         git ls-tree --full-tree "$1" \
71         | awk -F '      ' '$2 !~ /^.top/' \
72         | git mktree
73 }
74
75 create_tg_commit()
76 {
77         name="$1"
78         tree="$2"
79         parent="$3"
80
81         # Get commit message and authorship information
82         git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
83
84         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
85         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
86         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
87         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
88
89         test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
90         test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
91         test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
92
93         (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
94         git stripspace |
95         git commit-tree "$tree" -p "$parent"
96 }
97
98 # collapsed_commit NAME
99 # Produce a collapsed commit of branch NAME.
100 collapsed_commit()
101 {
102         name="$1"
103
104         rm -f "$playground/^pre" "$playground/^post"
105         >"$playground/^body"
106
107         # Determine parent
108         parent="$(cut -f 1 "$playground/$name^parents")"
109         if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
110                 # Produce a merge commit first
111                 parent="$({
112                         echo "TopGit-driven merge of branches:"
113                         echo
114                         cut -f 2 "$playground/$name^parents"
115                 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
116                         $(for p in $parent; do echo -p $p; done))"
117         fi
118
119         if branch_empty "$name"; then
120                 echo "$parent";
121         else
122                 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
123         fi;
124
125         echo "$name" >>"$playground/^ticker"
126 }
127
128 # collapse
129 # This will collapse a single branch, using information about
130 # previously collapsed branches stored in $playground.
131 collapse()
132 {
133         if [ -s "$playground/$_dep" ]; then
134                 # We've already seen this dep
135                 commit="$(cat "$playground/$_dep")"
136
137         elif [ -z "$_dep_is_tgish" ]; then
138                 # This dep is not for rewrite
139                 commit="$(git rev-parse --verify "$_dep")"
140
141         else
142                 # First time hitting this dep; the common case
143                 echo "Collapsing $_dep"
144                 commit="$(collapsed_commit "$_dep")"
145                 mkdir -p "$playground/$(dirname "$_dep")"
146                 echo "$commit" >"$playground/$_dep"
147         fi
148
149         # Propagate our work through the dependency chain
150         mkdir -p "$playground/$(dirname "$_name")"
151         echo "$commit   $_dep" >>"$playground/$_name^parents"
152 }
153
154
155 ## Quilt driver
156
157 quilt()
158 {
159         if [ -z "$_dep_is_tgish" ]; then
160                 # This dep is not for rewrite
161                 return
162         fi
163
164         if "$flatten"; then
165                 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
166                 dn="";
167         else
168                 bn="$(basename "$_dep.diff")";
169                 dn="$(dirname "$_dep.diff")/";
170                 if [ "x$dn" = "x./" ]; then
171                         dn="";
172                 fi;
173         fi;
174
175         if [ -e "$playground/$_dep" ]; then
176                 # We've already seen this dep
177                 return
178         fi
179
180         mkdir -p "$playground/$(dirname "$_dep")";
181         touch "$playground/$_dep";
182
183         if branch_empty "$_dep"; then
184                 echo "Skip empty patch $_dep";
185         else
186                 if "$numbered"; then
187                         number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
188                         bn="$(printf "%04u-$bn" $number)";
189                         echo "$number" >"$playground/^number";
190                 fi;
191
192                 echo "Exporting $_dep"
193                 mkdir -p "$output/$dn";
194                 $tg patch "$_dep" >"$output/$dn$bn"
195                 echo "$dn$bn -p1" >>"$output/series"
196         fi
197 }
198
199 linearize()
200 {
201         if test ! -f "$playground/^BASE"; then
202                 head="$(git rev-parse --verify "$_dep")"
203                 echo "$head" > "$playground/^BASE"
204                 git checkout -q "$head"
205                 return;
206         fi;
207
208         head=$(git rev-parse --verify HEAD)
209
210         if [ -z "$_dep_is_tgish" ]; then
211                 # merge in $_dep unless already included
212                 rev="$(git rev-parse --verify "$_dep")";
213                 common="$(git merge-base --all HEAD "$_dep")";
214                 if test "$rev" = "$common"; then
215                         # already included, just skip
216                         :;
217                 else
218                         retmerge=0;
219
220                         git merge -s recursive "$_dep" || retmerge="$?";
221                         if test "x$retmerge" != "x0"; then
222                                 echo fix up the merge, commit and then exit;
223                                 #todo error handling
224                                 sh -i </dev/tty;
225                         fi;
226                 fi;
227         else
228                 retmerge=0;
229
230                 git merge-recursive "$(pretty_tree "refs/top-bases/$_dep")" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
231
232                 if test "x$retmerge" != "x0"; then
233                         git rerere;
234                         echo "fix up the merge and update the index.  Don't commit!"
235                         #todo error handling
236                         sh -i </dev/tty;
237                 fi
238
239                 result_tree=$(git write-tree)
240                 # testing branch_empty might not always give the right answer.
241                 # It can happen that the patch is non-empty but still after
242                 # linearizing there is no change.  So compare the trees.
243                 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
244                         echo "skip empty commit $_dep";
245                 else
246                         newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
247                         git update-ref HEAD $newcommit $head
248                         echo "exported commit $_dep";
249                 fi
250         fi
251 }
252
253 ## Machinery
254
255 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
256         [ -n "$output" ] ||
257                 die "no target branch specified"
258         ! ref_exists "$output"  ||
259                 die "target branch '$output' already exists; first run: git branch -D $output"
260
261 elif [ "$driver" = "quilt" ]; then
262         [ -n "$output" ] ||
263                 die "no target directory specified"
264         [ ! -e "$output" ] ||
265                 die "target directory already exists: $output"
266
267         mkdir -p "$output"
268 fi
269
270
271 driver()
272 {
273         case $_dep in refs/remotes/*) return;; esac
274         branch_needs_update >/dev/null
275         [ "$_ret" -eq 0 ] ||
276                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
277
278         $driver
279 }
280
281 # Call driver on all the branches - this will happen
282 # in topological order.
283 if [ -z "$branches" ]; then
284         recurse_deps driver "$name"
285         (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
286 else
287         echo "$branches" | tr ',' '\n' | while read _dep; do
288                 _dep_is_tgish=1
289                 $driver
290         done
291         name="$(echo "$branches" | sed 's/.*,//')"
292 fi
293
294
295 if [ "$driver" = "collapse" ]; then
296         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
297
298         depcount="$(cat "$playground/^ticker" | wc -l)"
299         echo "Exported topic branch $name (total $depcount topics) to branch $output"
300
301 elif [ "$driver" = "quilt" ]; then
302         depcount="$(cat "$output/series" | wc -l)"
303         echo "Exported topic branch $name (total $depcount topics) to directory $output"
304
305 elif [ "$driver" = "linearize" ]; then
306         git checkout -q -b $output
307
308         echo $name
309         if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
310                 echo "Warning: Exported result doesn't match";
311                 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
312                 #git diff $head HEAD;
313         fi;
314
315 fi
316
317 # vim:noet