tg.sh: change incorrect 'continue' into correct 'return'
[topgit/pro.git] / tg-export.sh
blob93bd9656d6ea7e85955515c32d6f51131efd0621
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 branches=
8 forcebranch=
9 checkout_opt=-b
10 output=
11 driver=collapse
12 flatten=false
13 numbered=false
14 strip=false
15 stripval=0
16 allbranches=false
17 binary=
20 ## Parse options
22 while [ -n "$1" ]; do
23 arg="$1"; shift
24 case "$arg" in
25 -a|--all)
26 allbranches=true;;
27 -b)
28 branches="$1"; shift;;
29 --force)
30 forcebranch=1;;
31 --flatten)
32 flatten=true;;
33 --binary)
34 binary=1;;
35 --numbered)
36 flatten=true;
37 numbered=true;;
38 --strip*)
39 val=${arg#*=}
40 if [ "$val" = "--strip" ]; then
41 strip=true
42 stripval=9999
43 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
44 strip=true
45 stripval=$val
46 else
47 die "invalid parameter $arg"
48 fi;;
49 --quilt)
50 driver=quilt;;
51 --collapse)
52 driver=collapse;;
53 --linearize)
54 driver=linearize;;
55 -*)
56 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
57 exit 1;;
59 [ -z "$output" ] || die "output already specified ($output)"
60 output="$arg";;
61 esac
62 done
66 [ -z "$branches" -o "$driver" = "quilt" ] ||
67 die "-b works only with the quilt driver"
69 [ "$driver" = "quilt" ] || ! "$numbered" ||
70 die "--numbered works only with the quilt driver";
72 [ "$driver" = "quilt" ] || ! "$flatten" ||
73 die "--flatten works only with the quilt driver"
75 [ "$driver" = "quilt" ] || [ -z "$binary" ] ||
76 die "--binary works only with the quilt driver"
78 [ "$driver" = "quilt" ] || ! "$strip" ||
79 die "--strip works only with the quilt driver"
81 [ "$driver" = "quilt" ] || ! "$allbranches" ||
82 die "--all works only with the quilt driver";
84 [ -z "$branches" ] || ! "$allbranches" ||
85 die "-b conflicts with the --all option";
87 if [ -z "$branches" ] && ! "$allbranches"; then
88 # this check is only needed when no branches have been passed
89 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
90 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
91 die "not on a TopGit-controlled branch"
95 playground="$(get_temp tg-export -d)"
98 ## Collapse driver
100 create_tg_commit()
102 name="$1"
103 tree="$2"
104 parent="$3"
106 # Get commit message and authorship information
107 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
109 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
110 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
111 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
112 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
114 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
115 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
116 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
118 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
119 git stripspace |
120 git commit-tree "$tree" -p "$parent"
123 # collapsed_commit NAME
124 # Produce a collapsed commit of branch NAME.
125 collapsed_commit()
127 name="$1"
129 rm -f "$playground/^pre" "$playground/^post"
130 >"$playground/^body"
132 # Determine parent
133 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/top-bases/$name" >> "$playground/$name^parents"
134 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null | \
135 while read p; do [ "$(git cat-file -t $p 2> /dev/null)" = tag ] && git cat-file tag $p | head -1 | cut -d' ' -f2 || echo $p; done)"
136 if [ "$(cat "$playground/$name^parents" 2> /dev/null | wc_l)" -gt 1 ]; then
137 # Produce a merge commit first
138 parent="$({
139 echo "TopGit-driven merge of branches:"
140 echo
141 cut -f 2 "$playground/$name^parents"
142 } | git commit-tree "$(pretty_tree "$name" -b)" \
143 $(for p in $parent; do echo -p $p; done))"
146 if branch_empty "$name"; then
147 echo "$parent";
148 else
149 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
152 echo "$name" >>"$playground/^ticker"
155 # collapse
156 # This will collapse a single branch, using information about
157 # previously collapsed branches stored in $playground.
158 collapse()
160 if [ -s "$playground/$_dep" ]; then
161 # We've already seen this dep
162 commit="$(cat "$playground/$_dep")"
164 elif [ -z "$_dep_is_tgish" ]; then
165 # This dep is not for rewrite
166 commit="$(git rev-parse --verify "$_dep")"
168 else
169 # First time hitting this dep; the common case
170 echo "Collapsing $_dep"
171 commit="$(collapsed_commit "$_dep")"
172 mkdir -p "$playground/$(dirname "$_dep")"
173 echo "$commit" >"$playground/$_dep"
176 # Propagate our work through the dependency chain
177 mkdir -p "$playground/$(dirname "$_name")"
178 echo "$commit $_dep" >>"$playground/$_name^parents"
182 ## Quilt driver
184 quilt()
186 if [ -z "$_dep_is_tgish" ]; then
187 # This dep is not for rewrite
188 return
191 _dep_tmp=$_dep
193 if "$strip"; then
194 i=$stripval
195 while [ "$i" -gt 0 ]; do
196 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
197 _dep_tmp=${_dep_tmp#*/}
198 i=$((i - 1))
199 done
202 bn="$(basename "$_dep_tmp.diff")"
203 dn="$(dirname "$_dep_tmp.diff")/"
204 [ "x$dn" = "x./" ] && dn=""
206 if "$flatten" && [ "$dn" ]; then
207 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
208 dn=""
211 unset _dep_tmp
213 if [ -e "$playground/$_dep" ]; then
214 # We've already seen this dep
215 return
218 mkdir -p "$playground/$(dirname "$_dep")";
219 touch "$playground/$_dep";
221 if branch_empty "$_dep"; then
222 echo "Skip empty patch $_dep";
223 else
224 if "$numbered"; then
225 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
226 bn="$(printf "%04u-$bn" $number)";
227 echo "$number" >"$playground/^number";
230 echo "Exporting $_dep"
231 mkdir -p "$output/$dn";
232 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
233 echo "$dn$bn -p1" >>"$output/series"
237 linearize()
239 if test ! -f "$playground/^BASE"; then
240 if [ -n "$_dep_is_tgish" ]; then
241 head="$(git rev-parse --verify "refs/top-bases/$_dep")"
242 else
243 head="$(git rev-parse --verify "refs/heads/$_dep")"
245 echo "$head" > "$playground/^BASE"
246 git checkout -q "$head"
247 [ -n "$_dep_is_tgish" ] || return 0;
250 head=$(git rev-parse --verify HEAD)
252 if [ -z "$_dep_is_tgish" ]; then
253 # merge in $_dep unless already included
254 rev="$(git rev-parse --verify "$_dep")";
255 common="$(git merge-base --all HEAD "$_dep")";
256 if test "$rev" = "$common"; then
257 # already included, just skip
259 else
260 retmerge=0;
262 git merge -s recursive "$_dep" || retmerge="$?";
263 if test "x$retmerge" != "x0"; then
264 echo "fix up the merge, commit and then exit."
265 #todo error handling
266 "${SHELL:-/bin/sh}" -i </dev/tty;
269 else
270 retmerge=0;
272 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
274 if test "x$retmerge" != "x0"; then
275 git rerere;
276 echo "fix up the merge, update the index and then exit. Don't commit!"
277 #todo error handling
278 "${SHELL:-/bin/sh}" -i </dev/tty;
281 result_tree=$(git write-tree)
282 # testing branch_empty might not always give the right answer.
283 # It can happen that the patch is non-empty but still after
284 # linearizing there is no change. So compare the trees.
285 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
286 echo "skip empty commit $_dep";
287 else
288 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
289 git update-ref HEAD $newcommit $head
290 echo "exported commit $_dep";
295 ## Machinery
297 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
298 [ -n "$output" ] ||
299 die "no target branch specified"
300 if ! ref_exists $output; then
302 elif [ -z "$forcebranch" ]; then
303 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
304 else
305 checkout_opt=-B
308 elif [ "$driver" = "quilt" ]; then
309 [ -n "$output" ] ||
310 die "no target directory specified"
311 [ ! -e "$output" ] ||
312 die "target directory already exists: $output"
314 mkdir -p "$output"
318 driver()
320 # FIXME should we abort on missing dependency?
321 [ -z "$_dep_missing" ] || return 0
323 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
325 case $_dep in refs/remotes/*) return;; esac
326 branch_needs_update >/dev/null
327 [ "$_ret" -eq 0 ] ||
328 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
330 $driver
333 # Call driver on all the branches - this will happen
334 # in topological order.
335 if "$allbranches" ; then
336 _dep_is_tgish=1
337 non_annihilated_branches |
338 while read _dep; do
339 driver
340 done
341 elif [ -z "$branches" ]; then
342 recurse_deps driver "$name"
343 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
344 else
345 echo "$branches" | tr ',' '\n' | while read _dep; do
346 _dep_is_tgish=1
347 $driver
348 done
349 name="$(echo "$branches" | sed 's/.*,//')"
353 if [ "$driver" = "collapse" ]; then
354 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
355 [ -n "$forcebranch" ] || cmd="$cmd \"\""
356 eval "$cmd"
358 depcount="$(cat "$playground/^ticker" | wc_l)"
359 echo "Exported topic branch $name (total $depcount topics) to branch $output"
361 elif [ "$driver" = "quilt" ]; then
362 depcount="$(cat "$output/series" | wc_l)"
363 echo "Exported topic branch $name (total $depcount topics) to directory $output"
365 elif [ "$driver" = "linearize" ]; then
366 git checkout -q $checkout_opt $output
368 echo $name
369 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
370 echo "Warning: Exported result doesn't match";
371 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
372 #git diff $head HEAD;
377 # vim:noet