tg-update.sh: show correct blocking ref in message
[topgit/pro.git] / tg-export.sh
blob2e667797155eb48baf967116c71f78a63201177d
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) 2008 Petr Baudis <pasky@suse.cz>
4 # Copyright (C) 2015-2017 Kyle J. McKay <mackyle@gmail.com>
5 # All rights reserved
6 # GPLv2
8 USAGE="\
9 Usage: ${tgname:-tg} [...] export [--collapse] [--force] [-s <mode>] <newbranch>
10 Or: ${tgname:-tg} [...] export --linearize [--force] [-s <mode>] <newbranch>
11 Or: ${tgname:-tg} [...] export --quilt [--force] [-a | --all | -b <branch>...]
12 [--binary] [--flatten] [--numbered] [--strip[=N]] <directory>"
14 usage()
16 if [ "${1:-0}" != 0 ]; then
17 printf '%s\n' "$USAGE" >&2
18 else
19 printf '%s\n' "$USAGE"
21 exit ${1:-0}
24 name=
25 branches=
26 forceoutput=
27 checkout_opt=-b
28 output=
29 driver=collapse
30 flatten=false
31 numbered=false
32 strip=false
33 stripval=0
34 smode=
35 allbranches=false
36 binary=
37 pl=
39 ## Parse options
41 while [ -n "$1" ]; do
42 arg="$1"; shift
43 case "$arg" in
44 -h|--help)
45 usage;;
46 -a|--all)
47 allbranches=true;;
48 -b)
49 branches="${branches:+$branches }$1"; shift;;
50 --force)
51 forceoutput=1;;
52 --flatten)
53 flatten=true;;
54 --binary)
55 binary=1;;
56 --numbered)
57 flatten=true
58 numbered=true;;
59 --strip*)
60 val=${arg#*=}
61 if [ "$val" = "--strip" ]; then
62 strip=true
63 stripval=9999
64 elif [ -n "$val" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; then
65 strip=true
66 stripval=$val
67 else
68 die "invalid parameter $arg"
69 fi;;
70 -s)
71 test $# -gt 0 && test -n "$1" || die "-s requires an argument"
72 smode="$1"; shift;;
73 --quilt)
74 driver=quilt;;
75 --collapse)
76 driver=collapse;;
77 --linearize)
78 driver=linearize;;
79 -*)
80 usage 1;;
82 [ -z "$output" ] || die "output already specified ($output)"
83 output="$arg";;
84 esac
85 done
87 [ -z "$smode" ] || [ "$driver" != "quilt" ] ||
88 die "-s works only with the collapse/linearize driver"
90 if [ "$driver" != "quilt" ]; then
91 test -n "$smode" || smode="$(git config topgit.subjectmode)" || :
92 case "${smode:-tg}" in
93 tg) smode="topgit";;
94 ws) smode="trim";;
95 topgit|patch|mailinfo|trim|keep);;
96 *) die "invalid subject mode: $smode"
97 esac
100 [ -z "$branches" ] || [ "$driver" = "quilt" ] ||
101 die "-b works only with the quilt driver"
103 [ "$driver" = "quilt" ] || ! "$numbered" ||
104 die "--numbered works only with the quilt driver"
106 [ "$driver" = "quilt" ] || ! "$flatten" ||
107 die "--flatten works only with the quilt driver"
109 [ "$driver" = "quilt" ] || [ -z "$binary" ] ||
110 die "--binary works only with the quilt driver"
112 [ "$driver" = "quilt" ] || ! "$strip" ||
113 die "--strip works only with the quilt driver"
115 [ "$driver" = "quilt" ] || ! "$allbranches" ||
116 die "--all works only with the quilt driver"
118 [ -z "$branches" ] || ! "$allbranches" ||
119 die "-b conflicts with the --all option"
121 if [ "$driver" = "linearize" ]; then
122 setup_git_dir_is_bare
123 if [ -n "$git_dir_is_bare" ]; then
124 fatal 'export --linearize does not work on a bare repository...yet!'
125 fatal '(but you can use `tg -w : export --linearize` instead for now)'
126 ensure_work_tree
130 if [ -z "$branches" ] && ! "$allbranches"; then
131 # this check is only needed when no branches have been passed
132 name="$(verify_topgit_branch HEAD)"
135 if [ -n "$branches" ]; then
136 oldbranches="$branches"
137 branches=
138 while read bname && [ -n "$bname" ]; do
139 branches="${branches:+$branches }$(verify_topgit_branch "$bname")"
140 done <<-EOT
141 $(sed 'y/ /\n/' <<-LIST
142 $oldbranches
143 LIST
146 unset oldbranches bname
149 read -r nowsecs nowtzoff <<EOT
150 $(date '+%s %z')
152 playground="$(get_temp tg-export -d)"
155 ## Collapse driver
157 bump_timestamp()
159 nowsecs=$(( $nowsecs + 1 ))
162 setup_smode()
164 mik=
165 test z"$smode" = z"mailinfo" || { mik=-k &&
166 test z"$smode" = z"keep"; } ||
167 sprefix="$(git config topgit.subjectprefix)" || :
170 create_tg_commit()
172 name="$1"
173 tree="$2"
174 parent="$3"
176 # Get commit message and authorship information
177 git cat-file blob "$name:.topmsg" 2>/dev/null |
178 git mailinfo $mik "$playground/^msg" /dev/null > "$playground/^info"
180 unset GIT_AUTHOR_NAME
181 unset GIT_AUTHOR_EMAIL
183 GIT_AUTHOR_NAME="$(sed -n "/^Author/ s/Author:[ $tab]*//p" "$playground/^info")"
184 GIT_AUTHOR_EMAIL="$(sed -n "/^Email/ s/Email:[ $tab]*//p" "$playground/^info")"
185 GIT_AUTHOR_DATE="$(sed -n "/^Date/ s/Date:[ $tab]*//p" "$playground/^info")"
186 SUBJECT="$(sed -n "/^Subject/ s/Subject:[ $tab]*//p" "$playground/^info")"
187 if test z"$smode" != z"mailinfo" && test z"$smode" != z"keep"; then
188 SUBJECT="$(printf '%s\n' "$SUBJECT" |
189 awk -v mode="$smode" -v prefix="$sprefix" '{
190 gsub(/[ \t]+/, " ")
191 sub(/^[ \t]+/, "")
192 sub(/[ \t]+$/, "")
193 if (mode != "trim") {
194 if (prefix != "" &&
195 tolower(substr($0, 1, (lp = 1 + length(prefix)))) == "[" tolower(prefix) &&
196 index($0, "]")) {
197 if (substr($0, 1 + lp, 1) == " ") ++lp
198 lead = tolower(substr($0, 1 + lp, 8))
199 if (lead ~ /^patch/ || (mode == "topgit" &&
200 lead ~ /^(base|root|stage|release)\]/))
201 $0 = "[" substr($0, 1 + lp)
203 if (!sub(/^\[[Pp][Aa][Tt][Cc][Hh][^]]*\][ \t]*/, "") &&
204 mode == "topgit")
205 sub(/^\[([Bb][Aa][Ss][Ee]|[Rr][Oo][Oo][Tt]|[Ss][Tt][Aa][Gg][Ee]|[Rr][Ee][Ll][Ee][Aa][Ss][Ee])\][ \t]*/, "")
207 print
208 exit
209 }')"
212 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
213 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
215 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
216 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
217 export GIT_AUTHOR_DATE
218 export GIT_COMMITTER_DATE
220 (printf '%s\n\n' "${SUBJECT:-$name}"; cat "$playground/^msg") |
221 git stripspace |
222 git commit-tree "$tree" -p "$parent"
224 unset GIT_AUTHOR_NAME
225 unset GIT_AUTHOR_EMAIL
226 unset GIT_AUTHOR_DATE
227 unset GIT_COMMITTER_DATE
230 # collapsed_commit NAME
231 # Produce a collapsed commit of branch NAME.
232 collapsed_commit()
234 name="$1"
236 rm -f "$playground/^pre" "$playground/^post"
237 >"$playground/^body"
239 # Determine parent
240 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
241 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
242 while read -r p; do git rev-parse --quiet --verify "$p^0" -- || :; done)"
243 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
244 # Produce a merge commit first
245 parent="$({
246 echo "TopGit-driven merge of branches:"
247 echo
248 cut -f 2 "$playground/$name^parents"
249 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
250 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
251 git commit-tree "$(pretty_tree "$name" -b)" \
252 $(for p in $parent; do echo "-p $p"; done))"
255 if branch_empty "$name"; then
256 echol "$parent"
257 else
258 create_tg_commit "$name" "$(pretty_tree "$name")" "$parent"
261 echol "$name" >>"$playground/^ticker"
264 # collapse
265 # This will collapse a single branch, using information about
266 # previously collapsed branches stored in $playground.
267 collapse()
269 if [ -s "$playground/$_dep^commit" ]; then
270 # We've already seen this dep
271 commit="$(cat "$playground/$_dep^commit")"
273 elif [ -z "$_dep_is_tgish" ]; then
274 # This dep is not for rewrite
275 commit="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
277 else
278 # First time hitting this dep; the common case
279 echo "Collapsing $_dep"
280 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
281 commit="$(collapsed_commit "$_dep")"
282 bump_timestamp
283 echol "$commit" >"$playground/$_dep^commit"
286 # Propagate our work through the dependency chain
287 test -d "$playground/${_name%/*}" || mkdir -p "$playground/${_name%/*}"
288 echo "$commit $_dep" >>"$playground/$_name^parents"
292 ## Quilt driver
294 quilt()
296 if [ -z "$_dep_is_tgish" ]; then
297 # This dep is not for rewrite
298 return
301 _dep_tmp=$_dep
303 if "$strip"; then
304 i=$stripval
305 while [ "$i" -gt 0 ]; do
306 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
307 _dep_tmp=${_dep_tmp#*/}
308 i=$((i - 1))
309 done
312 dn="${_dep_tmp%/}.diff"
313 case "$dn" in */*);;*) dn="./$dn"; esac
314 bn="${dn##*/}"
315 dn="${dn%/*}/"
316 [ "x$dn" = "x./" ] && dn=""
318 if "$flatten" && [ "$dn" ]; then
319 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
320 dn=""
323 unset _dep_tmp
325 if [ -e "$playground/$_dep^commit" ]; then
326 # We've already seen this dep
327 return
330 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
331 >>"$playground/$_dep^commit"
333 if branch_empty "$_dep"; then
334 echo "Skip empty patch $_dep"
335 else
336 if "$numbered"; then
337 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
338 bn="$(printf "%04u-$bn" $number)"
339 echo "$number" >"$playground/^number"
342 echo "Exporting $_dep"
343 mkdir -p "$output/$dn"
344 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
345 echol "$dn$bn -p1" >>"$output/series"
349 linearize()
351 if test ! -f "$playground/^BASE"; then
352 if [ -n "$_dep_is_tgish" ]; then
353 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
354 else
355 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
357 echol "$head" > "$playground/^BASE"
358 git checkout -q $iowopt "$head"
359 [ -n "$_dep_is_tgish" ] || return 0
362 head=$(git rev-parse --verify HEAD --)
364 if [ -z "$_dep_is_tgish" ]; then
365 # merge in $_dep unless already included
366 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
367 common="$(git merge-base --all HEAD "$rev")" || :
368 if test "$rev" = "$common"; then
369 # already included, just skip
371 else
372 retmerge=0
374 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
375 if test "x$retmerge" != "x0"; then
376 echo "fix up the merge, commit and then exit."
377 #todo error handling
378 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
381 else
382 retmerge=0
384 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
386 if test "x$retmerge" != "x0"; then
387 git rerere
388 echo "fix up the merge, update the index and then exit. Don't commit!"
389 #todo error handling
390 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
391 git rerere
394 result_tree=$(git write-tree)
395 # testing branch_empty might not always give the right answer.
396 # It can happen that the patch is non-empty but still after
397 # linearizing there is no change. So compare the trees.
398 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
399 echo "skip empty commit $_dep"
400 else
401 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
402 bump_timestamp
403 git update-ref HEAD $newcommit $head
404 echo "exported commit $_dep"
409 ## Machinery
411 wayback_push=
412 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
413 [ -n "$output" ] ||
414 die "no target branch specified"
415 if ! ref_exists "refs/heads/$output"; then
417 elif [ -z "$forceoutput" ]; then
418 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
419 else
420 checkout_opt=-B
422 ensure_ident_available
423 setup_smode
424 [ -z "$wayback" ] || wayback_push="$(git config --get remote.wayback.url 2>/dev/null)" || :
425 [ -z "$wayback" ] || [ -n "$wayback_push" ] || die "enable to configure wayback export"
427 elif [ "$driver" = "quilt" ]; then
428 [ -n "$output" ] ||
429 die "no target directory specified"
430 [ -n "$forceoutput" ] || [ ! -e "$output" ] || is_empty_dir "$output" . ||
431 die "non-empty target directory already exists (use --force to override): $output"
433 mkdir -p "$output"
437 driver()
439 # FIXME should we abort on missing dependency?
440 [ -z "$_dep_missing" ] || return 0
442 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
444 case $_dep in ":"*) return; esac
445 branch_needs_update >/dev/null
446 [ "$_ret" -eq 0 ] ||
447 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
449 $driver
452 # Call driver on all the branches - this will happen
453 # in topological order.
454 if "$allbranches" ; then
455 _dep_is_tgish=1
456 non_annihilated_branches |
457 while read _dep; do
458 driver
459 done
460 test $? -eq 0
461 elif [ -z "$branches" ]; then
462 recurse_deps driver "$name"
463 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
464 test $? -eq 0
465 else
466 while read _dep && [ -n "$_dep" ]; do
467 _dep_is_tgish=1
468 $driver
469 done <<-EOT
470 $(sed 'y/ /\n/' <<-LIST
471 $branches
472 LIST
475 name="$branches"
476 case "$branches" in *" "*) pl="es"; esac
480 if [ "$driver" = "collapse" ]; then
481 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name^commit")"'
482 [ -n "$forceoutput" ] || cmd="$cmd \"\""
483 eval "$cmd"
484 [ -z "$wayback_push" ] || git -c "remote.wayback.url=$wayback_push" push -q ${forceoutput:+--force} wayback "refs/heads/$output:refs/heads/$output"
486 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
487 echo "Exported topic branch $name (total $depcount topics) to branch $output"
489 elif [ "$driver" = "quilt" ]; then
490 depcount=$(( $(cat "$output/series" | wc -l) ))
491 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
493 elif [ "$driver" = "linearize" ]; then
494 git checkout -q --no-track $iowopt $checkout_opt $output
495 [ -z "$wayback_push" ] || git -c "remote.wayback.url=$wayback_push" push -q ${forceoutput:+--force} wayback "refs/heads/$output:refs/heads/$output"
497 echol "$name"
498 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
499 echo "Warning: Exported result doesn't match"
500 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
501 #git diff $head HEAD