tg-annihilate.sh: autostash and support --stash and --no-stash
[topgit/pro.git] / tg-export.sh
blob7e15341a4fda8130457f7309ec5c5035ed117499
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" 2>/dev/null |
128 git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
130 unset GIT_AUTHOR_NAME
131 unset GIT_AUTHOR_EMAIL
133 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
134 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
135 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
136 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
138 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
139 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
141 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
142 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
143 export GIT_AUTHOR_DATE
144 export GIT_COMMITTER_DATE
146 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
147 git stripspace |
148 git commit-tree "$tree" -p "$parent"
150 unset GIT_AUTHOR_NAME
151 unset GIT_AUTHOR_EMAIL
152 unset GIT_AUTHOR_DATE
153 unset GIT_COMMITTER_DATE
156 # collapsed_commit NAME
157 # Produce a collapsed commit of branch NAME.
158 collapsed_commit()
160 name="$1"
162 rm -f "$playground/^pre" "$playground/^post"
163 >"$playground/^body"
165 # Determine parent
166 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
167 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
168 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)"
169 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
170 # Produce a merge commit first
171 parent="$({
172 echo "TopGit-driven merge of branches:"
173 echo
174 cut -f 2 "$playground/$name^parents"
175 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
176 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
177 git commit-tree "$(pretty_tree "$name" -b)" \
178 $(for p in $parent; do echo "-p $p"; done))"
181 if branch_empty "$name"; then
182 echol "$parent"
183 else
184 create_tg_commit "$name" "$(pretty_tree "$name")" "$parent"
187 echol "$name" >>"$playground/^ticker"
190 # collapse
191 # This will collapse a single branch, using information about
192 # previously collapsed branches stored in $playground.
193 collapse()
195 if [ -s "$playground/$_dep" ]; then
196 # We've already seen this dep
197 commit="$(cat "$playground/$_dep")"
199 elif [ -z "$_dep_is_tgish" ]; then
200 # This dep is not for rewrite
201 commit="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
203 else
204 # First time hitting this dep; the common case
205 echo "Collapsing $_dep"
206 commit="$(collapsed_commit "$_dep")"
207 bump_timestamp
208 _depdn="${_dep%/}"
209 case "$_depdn" in */*);;*) _depdn="./$_depdn"; esac
210 mkdir -p "$playground/${_depdn%/*}"
211 echol "$commit" >"$playground/$_dep"
214 # Propagate our work through the dependency chain
215 _namedn="${_name%/}"
216 case "$_namedn" in */*);;*) _namedn="./$_namedn"; esac
217 mkdir -p "$playground/${_namedn%/*}"
218 echo "$commit $_dep" >>"$playground/$_name^parents"
222 ## Quilt driver
224 quilt()
226 if [ -z "$_dep_is_tgish" ]; then
227 # This dep is not for rewrite
228 return
231 _dep_tmp=$_dep
233 if "$strip"; then
234 i=$stripval
235 while [ "$i" -gt 0 ]; do
236 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
237 _dep_tmp=${_dep_tmp#*/}
238 i=$((i - 1))
239 done
242 dn="${_dep_tmp%/}.diff"
243 case "$dn" in */*);;*) dn="./$dn"; esac
244 bn="${dn##*/}"
245 dn="${dn%/*}/"
246 [ "x$dn" = "x./" ] && dn=""
248 if "$flatten" && [ "$dn" ]; then
249 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
250 dn=""
253 unset _dep_tmp
255 if [ -e "$playground/$_dep" ]; then
256 # We've already seen this dep
257 return
260 _depdn="${_dep%/}"
261 case "$_depdn" in */*);;*) _depdn="./$_depdn"; esac
262 mkdir -p "$playground/${_depdn%/*}"
263 >>"$playground/$_dep"
265 if branch_empty "$_dep"; then
266 echo "Skip empty patch $_dep"
267 else
268 if "$numbered"; then
269 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
270 bn="$(printf "%04u-$bn" $number)"
271 echo "$number" >"$playground/^number"
274 echo "Exporting $_dep"
275 mkdir -p "$output/$dn"
276 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
277 echol "$dn$bn -p1" >>"$output/series"
281 linearize()
283 if test ! -f "$playground/^BASE"; then
284 if [ -n "$_dep_is_tgish" ]; then
285 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
286 else
287 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
289 echol "$head" > "$playground/^BASE"
290 git checkout -q $iowopt "$head"
291 [ -n "$_dep_is_tgish" ] || return 0
294 head=$(git rev-parse --verify HEAD --)
296 if [ -z "$_dep_is_tgish" ]; then
297 # merge in $_dep unless already included
298 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
299 common="$(git merge-base --all HEAD "$rev")" || :
300 if test "$rev" = "$common"; then
301 # already included, just skip
303 else
304 retmerge=0
306 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
307 if test "x$retmerge" != "x0"; then
308 echo "fix up the merge, commit and then exit."
309 #todo error handling
310 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
313 else
314 retmerge=0
316 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
318 if test "x$retmerge" != "x0"; then
319 git rerere
320 echo "fix up the merge, update the index and then exit. Don't commit!"
321 #todo error handling
322 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
323 git rerere
326 result_tree=$(git write-tree)
327 # testing branch_empty might not always give the right answer.
328 # It can happen that the patch is non-empty but still after
329 # linearizing there is no change. So compare the trees.
330 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
331 echo "skip empty commit $_dep"
332 else
333 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
334 bump_timestamp
335 git update-ref HEAD $newcommit $head
336 echo "exported commit $_dep"
341 ## Machinery
343 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
344 [ -n "$output" ] ||
345 die "no target branch specified"
346 if ! ref_exists "refs/heads/$output"; then
348 elif [ -z "$forcebranch" ]; then
349 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
350 else
351 checkout_opt=-B
353 ensure_ident_available
355 elif [ "$driver" = "quilt" ]; then
356 [ -n "$output" ] ||
357 die "no target directory specified"
358 [ ! -e "$output" ] ||
359 die "target directory already exists: $output"
361 mkdir -p "$output"
365 driver()
367 # FIXME should we abort on missing dependency?
368 [ -z "$_dep_missing" ] || return 0
370 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
372 case $_dep in refs/remotes/*) return;; esac
373 branch_needs_update >/dev/null
374 [ "$_ret" -eq 0 ] ||
375 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
377 $driver
380 # Call driver on all the branches - this will happen
381 # in topological order.
382 if "$allbranches" ; then
383 _dep_is_tgish=1
384 non_annihilated_branches |
385 while read _dep; do
386 driver
387 done
388 test $? -eq 0
389 elif [ -z "$branches" ]; then
390 recurse_deps driver "$name"
391 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
392 test $? -eq 0
393 else
394 while read _dep && [ -n "$_dep" ]; do
395 _dep_is_tgish=1
396 $driver
397 done <<-EOT
398 $(sed 'y/ /\n/' <<-LIST
399 $branches
400 LIST
403 name="$branches"
404 case "$branches" in *" "*) pl="es"; esac
408 if [ "$driver" = "collapse" ]; then
409 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
410 [ -n "$forcebranch" ] || cmd="$cmd \"\""
411 eval "$cmd"
413 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
414 echo "Exported topic branch $name (total $depcount topics) to branch $output"
416 elif [ "$driver" = "quilt" ]; then
417 depcount=$(( $(cat "$output/series" | wc -l) ))
418 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
420 elif [ "$driver" = "linearize" ]; then
421 git checkout -q $iowopt $checkout_opt $output
423 echol "$name"
424 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
425 echo "Warning: Exported result doesn't match"
426 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
427 #git diff $head HEAD
432 # vim:noet