tg-update.sh: allow detached return
[topgit/pro.git] / tg-export.sh
blob82952d7ac54e1a834fb2f08e013ccd2b3e919c27
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=
18 pl=
21 ## Parse options
23 while [ -n "$1" ]; do
24 arg="$1"; shift
25 case "$arg" in
26 -a|--all)
27 allbranches=true;;
28 -b)
29 branches="${branches:+$branches }$1"; shift;;
30 --force)
31 forcebranch=1;;
32 --flatten)
33 flatten=true;;
34 --binary)
35 binary=1;;
36 --numbered)
37 flatten=true
38 numbered=true;;
39 --strip*)
40 val=${arg#*=}
41 if [ "$val" = "--strip" ]; then
42 strip=true
43 stripval=9999
44 elif [ -n "$val" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; then
45 strip=true
46 stripval=$val
47 else
48 die "invalid parameter $arg"
49 fi;;
50 --quilt)
51 driver=quilt;;
52 --collapse)
53 driver=collapse;;
54 --linearize)
55 driver=linearize;;
56 -*)
57 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
58 exit 1;;
60 [ -z "$output" ] || die "output already specified ($output)"
61 output="$arg";;
62 esac
63 done
67 [ -z "$branches" -o "$driver" = "quilt" ] ||
68 die "-b works only with the quilt driver"
70 [ "$driver" = "quilt" ] || ! "$numbered" ||
71 die "--numbered works only with the quilt driver"
73 [ "$driver" = "quilt" ] || ! "$flatten" ||
74 die "--flatten works only with the quilt driver"
76 [ "$driver" = "quilt" ] || [ -z "$binary" ] ||
77 die "--binary works only with the quilt driver"
79 [ "$driver" = "quilt" ] || ! "$strip" ||
80 die "--strip works only with the quilt driver"
82 [ "$driver" = "quilt" ] || ! "$allbranches" ||
83 die "--all works only with the quilt driver"
85 [ -z "$branches" ] || ! "$allbranches" ||
86 die "-b conflicts with the --all option"
88 if [ -z "$branches" ] && ! "$allbranches"; then
89 # this check is only needed when no branches have been passed
90 name="$(verify_topgit_branch HEAD)"
93 if [ -n "$branches" ]; then
94 oldbranches="$branches"
95 branches=
96 while read bname && [ -n "$bname" ]; do
97 branches="${branches:+$branches }$(verify_topgit_branch "$bname")"
98 done <<-EOT
99 $(sed 'y/ /\n/' <<-LIST
100 $oldbranches
101 LIST
104 unset oldbranches bname
107 read -r nowsecs nowtzoff <<EOT
108 $(date '+%s %z')
110 playground="$(get_temp tg-export -d)"
113 ## Collapse driver
115 bump_timestamp()
117 nowsecs=$(( $nowsecs + 1 ))
120 create_tg_commit()
122 name="$1"
123 tree="$2"
124 parent="$3"
126 # Get commit message and authorship information
127 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
129 unset GIT_AUTHOR_NAME
130 unset GIT_AUTHOR_EMAIL
132 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
133 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
134 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
135 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
137 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
138 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
140 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
141 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
142 export GIT_AUTHOR_DATE
143 export GIT_COMMITTER_DATE
145 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
146 git stripspace |
147 git commit-tree "$tree" -p "$parent"
149 unset GIT_AUTHOR_NAME
150 unset GIT_AUTHOR_EMAIL
151 unset GIT_AUTHOR_DATE
152 unset GIT_COMMITTER_DATE
155 # collapsed_commit NAME
156 # Produce a collapsed commit of branch NAME.
157 collapsed_commit()
159 name="$1"
161 rm -f "$playground/^pre" "$playground/^post"
162 >"$playground/^body"
164 # Determine parent
165 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name" -- >> "$playground/$name^parents"
166 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
167 while read p; do [ "$(git cat-file -t "$p" 2> /dev/null)" = tag ] && git cat-file tag "$p" | head -1 | cut -d' ' -f2 || echol "$p"; done)"
168 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
169 # Produce a merge commit first
170 parent="$({
171 echo "TopGit-driven merge of branches:"
172 echo
173 cut -f 2 "$playground/$name^parents"
174 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
175 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
176 git commit-tree "$(pretty_tree "$name" -b)" \
177 $(for p in $parent; do echo "-p $p"; done))"
180 if branch_empty "$name"; then
181 echol "$parent"
182 else
183 create_tg_commit "$name" "$(pretty_tree "$name")" "$parent"
186 echol "$name" >>"$playground/^ticker"
189 # collapse
190 # This will collapse a single branch, using information about
191 # previously collapsed branches stored in $playground.
192 collapse()
194 if [ -s "$playground/$_dep" ]; then
195 # We've already seen this dep
196 commit="$(cat "$playground/$_dep")"
198 elif [ -z "$_dep_is_tgish" ]; then
199 # This dep is not for rewrite
200 commit="$(git rev-parse --verify "refs/heads/$_dep" --)"
202 else
203 # First time hitting this dep; the common case
204 echo "Collapsing $_dep"
205 commit="$(collapsed_commit "$_dep")"
206 bump_timestamp
207 _depdn="${_dep%/}"
208 case "$_depdn" in */*);;*) _depdn="./$_depdn"; esac
209 mkdir -p "$playground/${_depdn%/*}"
210 echol "$commit" >"$playground/$_dep"
213 # Propagate our work through the dependency chain
214 _namedn="${_name%/}"
215 case "$_namedn" in */*);;*) _namedn="./$_namedn"; esac
216 mkdir -p "$playground/${_namedn%/*}"
217 echo "$commit $_dep" >>"$playground/$_name^parents"
221 ## Quilt driver
223 quilt()
225 if [ -z "$_dep_is_tgish" ]; then
226 # This dep is not for rewrite
227 return
230 _dep_tmp=$_dep
232 if "$strip"; then
233 i=$stripval
234 while [ "$i" -gt 0 ]; do
235 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
236 _dep_tmp=${_dep_tmp#*/}
237 i=$((i - 1))
238 done
241 dn="${_dep_tmp%/}.diff"
242 case "$dn" in */*);;*) dn="./$dn"; esac
243 bn="${dn##*/}"
244 dn="${dn%/*}/"
245 [ "x$dn" = "x./" ] && dn=""
247 if "$flatten" && [ "$dn" ]; then
248 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
249 dn=""
252 unset _dep_tmp
254 if [ -e "$playground/$_dep" ]; then
255 # We've already seen this dep
256 return
259 _depdn="${_dep%/}"
260 case "$_depdn" in */*);;*) _depdn="./$_depdn"; esac
261 mkdir -p "$playground/${_depdn%/*}"
262 >>"$playground/$_dep"
264 if branch_empty "$_dep"; then
265 echo "Skip empty patch $_dep"
266 else
267 if "$numbered"; then
268 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
269 bn="$(printf "%04u-$bn" $number)"
270 echo "$number" >"$playground/^number"
273 echo "Exporting $_dep"
274 mkdir -p "$output/$dn"
275 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
276 echol "$dn$bn -p1" >>"$output/series"
280 linearize()
282 if test ! -f "$playground/^BASE"; then
283 if [ -n "$_dep_is_tgish" ]; then
284 head="$(git rev-parse --verify "refs/$topbases/$_dep" --)"
285 else
286 head="$(git rev-parse --verify "refs/heads/$_dep" --)"
288 echol "$head" > "$playground/^BASE"
289 git checkout -q "$head"
290 [ -n "$_dep_is_tgish" ] || return 0
293 head=$(git rev-parse --verify HEAD --)
295 if [ -z "$_dep_is_tgish" ]; then
296 # merge in $_dep unless already included
297 rev="$(git rev-parse --verify "$_dep" --)"
298 common="$(git merge-base --all HEAD "$_dep")" || :
299 if test "$rev" = "$common"; then
300 # already included, just skip
302 else
303 retmerge=0
305 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "$_dep^0" || retmerge="$?"
306 if test "x$retmerge" != "x0"; then
307 echo "fix up the merge, commit and then exit."
308 #todo error handling
309 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
312 else
313 retmerge=0
315 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
317 if test "x$retmerge" != "x0"; then
318 git rerere
319 echo "fix up the merge, update the index and then exit. Don't commit!"
320 #todo error handling
321 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
322 git rerere
325 result_tree=$(git write-tree)
326 # testing branch_empty might not always give the right answer.
327 # It can happen that the patch is non-empty but still after
328 # linearizing there is no change. So compare the trees.
329 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
330 echo "skip empty commit $_dep"
331 else
332 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
333 bump_timestamp
334 git update-ref HEAD $newcommit $head
335 echo "exported commit $_dep"
340 ## Machinery
342 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
343 [ -n "$output" ] ||
344 die "no target branch specified"
345 if ! ref_exists "refs/heads/$output"; then
347 elif [ -z "$forcebranch" ]; then
348 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
349 else
350 checkout_opt=-B
352 ensure_ident_available
354 elif [ "$driver" = "quilt" ]; then
355 [ -n "$output" ] ||
356 die "no target directory specified"
357 [ ! -e "$output" ] ||
358 die "target directory already exists: $output"
360 mkdir -p "$output"
364 driver()
366 # FIXME should we abort on missing dependency?
367 [ -z "$_dep_missing" ] || return 0
369 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
371 case $_dep in refs/remotes/*) return;; esac
372 branch_needs_update >/dev/null
373 [ "$_ret" -eq 0 ] ||
374 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
376 $driver
379 # Call driver on all the branches - this will happen
380 # in topological order.
381 if "$allbranches" ; then
382 _dep_is_tgish=1
383 non_annihilated_branches |
384 while read _dep; do
385 driver
386 done
387 test $? -eq 0
388 elif [ -z "$branches" ]; then
389 recurse_deps driver "$name"
390 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
391 test $? -eq 0
392 else
393 while read _dep && [ -n "$_dep" ]; do
394 _dep_is_tgish=1
395 $driver
396 done <<-EOT
397 $(sed 'y/ /\n/' <<-LIST
398 $branches
399 LIST
402 name="$branches"
403 case "$branches" in *" "*) pl="es"; esac
407 if [ "$driver" = "collapse" ]; then
408 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
409 [ -n "$forcebranch" ] || cmd="$cmd \"\""
410 eval "$cmd"
412 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
413 echo "Exported topic branch $name (total $depcount topics) to branch $output"
415 elif [ "$driver" = "quilt" ]; then
416 depcount=$(( $(cat "$output/series" | wc -l) ))
417 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
419 elif [ "$driver" = "linearize" ]; then
420 git checkout -q $checkout_opt $output
422 echol "$name"
423 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
424 echo "Warning: Exported result doesn't match"
425 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
426 #git diff $head HEAD
431 # vim:noet