README: update the help for tg annihilate
[topgit/pro.git] / tg-export.sh
blob2c3c456ab1e72b90ebc1e2c1ef8303ae68b9b0ab
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
19 ## Parse options
21 while [ -n "$1" ]; do
22 arg="$1"; shift
23 case "$arg" in
24 -a|--all)
25 allbranches=true;;
26 -b)
27 branches="$1"; shift;;
28 --force)
29 forcebranch=true;;
30 --flatten)
31 flatten=true;;
32 --numbered)
33 flatten=true;
34 numbered=true;;
35 --strip*)
36 val=${arg#*=}
37 if [ "$val" = "--strip" ]; then
38 strip=true
39 stripval=9999
40 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
41 strip=true
42 stripval=$val
43 else
44 die "invalid parameter $arg"
45 fi;;
46 --quilt)
47 driver=quilt;;
48 --collapse)
49 driver=collapse;;
50 --linearize)
51 driver=linearize;;
52 -*)
53 echo "Usage: tg [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] --quilt <directory> | --linearize <newbranch> [--force])" >&2
54 exit 1;;
56 [ -z "$output" ] || die "output already specified ($output)"
57 output="$arg";;
58 esac
59 done
63 [ -z "$branches" -o "$driver" = "quilt" ] ||
64 die "-b works only with the quilt driver"
66 [ "$driver" = "quilt" ] || ! "$numbered" ||
67 die "--numbered works only with the quilt driver";
69 [ "$driver" = "quilt" ] || ! "$flatten" ||
70 die "--flatten works only with the quilt driver"
72 [ "$driver" = "quilt" ] || ! "$strip" ||
73 die "--strip works only with the quilt driver"
75 [ "$driver" = "quilt" ] || ! "$allbranches" ||
76 die "--all works only with the quilt driver";
78 [ -z "$branches" ] || ! "$allbranches" ||
79 die "-b conflicts with the --all option";
81 if [ -z "$branches" ] && ! "$allbranches"; then
82 # this check is only needed when no branches have been passed
83 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
84 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
85 die "not on a TopGit-controlled branch"
89 playground="$(get_temp tg-export -d)"
92 ## Collapse driver
94 create_tg_commit()
96 name="$1"
97 tree="$2"
98 parent="$3"
100 # Get commit message and authorship information
101 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
103 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
104 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
105 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
106 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
108 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
109 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
110 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
112 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
113 git stripspace |
114 git commit-tree "$tree" -p "$parent"
117 # collapsed_commit NAME
118 # Produce a collapsed commit of branch NAME.
119 collapsed_commit()
121 name="$1"
123 rm -f "$playground/^pre" "$playground/^post"
124 >"$playground/^body"
126 # Determine parent
127 parent="$(cut -f 1 "$playground/$name^parents" | \
128 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)"
129 if [ "$(cat "$playground/$name^parents" | wc_l)" -gt 1 ]; then
130 # Produce a merge commit first
131 parent="$({
132 echo "TopGit-driven merge of branches:"
133 echo
134 cut -f 2 "$playground/$name^parents"
135 } | git commit-tree "$(pretty_tree "$name" -b)" \
136 $(for p in $parent; do echo -p $p; done))"
139 if branch_empty "$name"; then
140 echo "$parent";
141 else
142 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
145 echo "$name" >>"$playground/^ticker"
148 # collapse
149 # This will collapse a single branch, using information about
150 # previously collapsed branches stored in $playground.
151 collapse()
153 if [ -s "$playground/$_dep" ]; then
154 # We've already seen this dep
155 commit="$(cat "$playground/$_dep")"
157 elif [ -z "$_dep_is_tgish" ]; then
158 # This dep is not for rewrite
159 commit="$(git rev-parse --verify "$_dep")"
161 else
162 # First time hitting this dep; the common case
163 echo "Collapsing $_dep"
164 commit="$(collapsed_commit "$_dep")"
165 mkdir -p "$playground/$(dirname "$_dep")"
166 echo "$commit" >"$playground/$_dep"
169 # Propagate our work through the dependency chain
170 mkdir -p "$playground/$(dirname "$_name")"
171 echo "$commit $_dep" >>"$playground/$_name^parents"
175 ## Quilt driver
177 quilt()
179 if [ -z "$_dep_is_tgish" ]; then
180 # This dep is not for rewrite
181 return
184 _dep_tmp=$_dep
186 if "$strip"; then
187 i=$stripval
188 while [ "$i" -gt 0 ]; do
189 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
190 _dep_tmp=${_dep_tmp#*/}
191 i=$((i - 1))
192 done
195 bn="$(basename "$_dep_tmp.diff")"
196 dn="$(dirname "$_dep_tmp.diff")/"
197 [ "x$dn" = "x./" ] && dn=""
199 if "$flatten" && [ "$dn" ]; then
200 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
201 dn=""
204 unset _dep_tmp
206 if [ -e "$playground/$_dep" ]; then
207 # We've already seen this dep
208 return
211 mkdir -p "$playground/$(dirname "$_dep")";
212 touch "$playground/$_dep";
214 if branch_empty "$_dep"; then
215 echo "Skip empty patch $_dep";
216 else
217 if "$numbered"; then
218 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
219 bn="$(printf "%04u-$bn" $number)";
220 echo "$number" >"$playground/^number";
223 echo "Exporting $_dep"
224 mkdir -p "$output/$dn";
225 $tg patch "$_dep" >"$output/$dn$bn"
226 echo "$dn$bn -p1" >>"$output/series"
230 linearize()
232 if test ! -f "$playground/^BASE"; then
233 head="$(git rev-parse --verify "$_dep")"
234 echo "$head" > "$playground/^BASE"
235 git checkout -q "$head"
236 return;
239 head=$(git rev-parse --verify HEAD)
241 if [ -z "$_dep_is_tgish" ]; then
242 # merge in $_dep unless already included
243 rev="$(git rev-parse --verify "$_dep")";
244 common="$(git merge-base --all HEAD "$_dep")";
245 if test "$rev" = "$common"; then
246 # already included, just skip
248 else
249 retmerge=0;
251 git merge -s recursive "$_dep" || retmerge="$?";
252 if test "x$retmerge" != "x0"; then
253 echo fix up the merge, commit and then exit;
254 #todo error handling
255 sh -i </dev/tty;
258 else
259 retmerge=0;
261 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
263 if test "x$retmerge" != "x0"; then
264 git rerere;
265 echo "fix up the merge and update the index. Don't commit!"
266 #todo error handling
267 sh -i </dev/tty;
270 result_tree=$(git write-tree)
271 # testing branch_empty might not always give the right answer.
272 # It can happen that the patch is non-empty but still after
273 # linearizing there is no change. So compare the trees.
274 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
275 echo "skip empty commit $_dep";
276 else
277 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
278 git update-ref HEAD $newcommit $head
279 echo "exported commit $_dep";
284 ## Machinery
286 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
287 [ -n "$output" ] ||
288 die "no target branch specified"
289 if ! ref_exists $output; then
291 elif ! "$forcebranch"; then
292 die "target branch '$output' already exists; first run: git branch -D $output, or run tg export with --force"
293 else
294 checkout_opt=-B
297 elif [ "$driver" = "quilt" ]; then
298 [ -n "$output" ] ||
299 die "no target directory specified"
300 [ ! -e "$output" ] ||
301 die "target directory already exists: $output"
303 mkdir -p "$output"
307 driver()
309 # FIXME should we abort on missing dependency?
310 [ -z "$_dep_missing" ] || return 0
312 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
314 case $_dep in refs/remotes/*) return;; esac
315 branch_needs_update >/dev/null
316 [ "$_ret" -eq 0 ] ||
317 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
319 $driver
322 # Call driver on all the branches - this will happen
323 # in topological order.
324 if "$allbranches" ; then
325 _dep_is_tgish=1
326 non_annihilated_branches |
327 while read _dep; do
328 driver
329 done
330 elif [ -z "$branches" ]; then
331 recurse_deps driver "$name"
332 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
333 else
334 echo "$branches" | tr ',' '\n' | while read _dep; do
335 _dep_is_tgish=1
336 $driver
337 done
338 name="$(echo "$branches" | sed 's/.*,//')"
342 if [ "$driver" = "collapse" ]; then
343 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
345 depcount="$(cat "$playground/^ticker" | wc_l)"
346 echo "Exported topic branch $name (total $depcount topics) to branch $output"
348 elif [ "$driver" = "quilt" ]; then
349 depcount="$(cat "$output/series" | wc_l)"
350 echo "Exported topic branch $name (total $depcount topics) to directory $output"
352 elif [ "$driver" = "linearize" ]; then
353 git checkout -q $checkout_opt $output
355 echo $name
356 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
357 echo "Warning: Exported result doesn't match";
358 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
359 #git diff $head HEAD;
364 # vim:noet