tg: activate ref logs for top-bases when requested
[topgit/pro.git] / tg-export.sh
bloba8588e344f6b38d321e28274f1a37b03af1dffbf
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: ${tgname:-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 if [ -n "$_dep_is_tgish" ]; then
234 head="$(git rev-parse --verify "refs/top-bases/$_dep")"
235 else
236 head="$(git rev-parse --verify "refs/heads/$_dep")"
238 echo "$head" > "$playground/^BASE"
239 git checkout -q "$head"
240 [ -n "$_dep_is_tgish" ] || return 0;
243 head=$(git rev-parse --verify HEAD)
245 if [ -z "$_dep_is_tgish" ]; then
246 # merge in $_dep unless already included
247 rev="$(git rev-parse --verify "$_dep")";
248 common="$(git merge-base --all HEAD "$_dep")";
249 if test "$rev" = "$common"; then
250 # already included, just skip
252 else
253 retmerge=0;
255 git merge -s recursive "$_dep" || retmerge="$?";
256 if test "x$retmerge" != "x0"; then
257 echo fix up the merge, commit and then exit;
258 #todo error handling
259 "${SHELL:-/bin/sh}" -i </dev/tty;
262 else
263 retmerge=0;
265 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
267 if test "x$retmerge" != "x0"; then
268 git rerere;
269 echo "fix up the merge and update the index. Don't commit!"
270 #todo error handling
271 "${SHELL:-/bin/sh}" -i </dev/tty;
274 result_tree=$(git write-tree)
275 # testing branch_empty might not always give the right answer.
276 # It can happen that the patch is non-empty but still after
277 # linearizing there is no change. So compare the trees.
278 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
279 echo "skip empty commit $_dep";
280 else
281 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
282 git update-ref HEAD $newcommit $head
283 echo "exported commit $_dep";
288 ## Machinery
290 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
291 [ -n "$output" ] ||
292 die "no target branch specified"
293 if ! ref_exists $output; then
295 elif ! "$forcebranch"; then
296 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
297 else
298 checkout_opt=-B
301 elif [ "$driver" = "quilt" ]; then
302 [ -n "$output" ] ||
303 die "no target directory specified"
304 [ ! -e "$output" ] ||
305 die "target directory already exists: $output"
307 mkdir -p "$output"
311 driver()
313 # FIXME should we abort on missing dependency?
314 [ -z "$_dep_missing" ] || return 0
316 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
318 case $_dep in refs/remotes/*) return;; esac
319 branch_needs_update >/dev/null
320 [ "$_ret" -eq 0 ] ||
321 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
323 $driver
326 # Call driver on all the branches - this will happen
327 # in topological order.
328 if "$allbranches" ; then
329 _dep_is_tgish=1
330 non_annihilated_branches |
331 while read _dep; do
332 driver
333 done
334 elif [ -z "$branches" ]; then
335 recurse_deps driver "$name"
336 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
337 else
338 echo "$branches" | tr ',' '\n' | while read _dep; do
339 _dep_is_tgish=1
340 $driver
341 done
342 name="$(echo "$branches" | sed 's/.*,//')"
346 if [ "$driver" = "collapse" ]; then
347 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
349 depcount="$(cat "$playground/^ticker" | wc_l)"
350 echo "Exported topic branch $name (total $depcount topics) to branch $output"
352 elif [ "$driver" = "quilt" ]; then
353 depcount="$(cat "$output/series" | wc_l)"
354 echo "Exported topic branch $name (total $depcount topics) to directory $output"
356 elif [ "$driver" = "linearize" ]; then
357 git checkout -q $checkout_opt $output
359 echo $name
360 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
361 echo "Warning: Exported result doesn't match";
362 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
363 #git diff $head HEAD;
368 # vim:noet