tg: reduce subshell creation phase II
[topgit/pro.git] / tg-export.sh
blob75877f41876ff38a5cc33bfa6070f61a64d37fe0
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) 2008 Petr Baudis <pasky@suse.cz>
4 # Copyright (C) 2015-2018 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" ] && [ "${val#*[!0-9]}" = "$val" ]; 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 v_get_p_arg_list()
232 _vname="$1"
233 shift
234 eval "$_vname="
235 while [ $# -gt 0 ]; do
236 [ -z "$1" ] || eval "$_vname=\"\${$_vname:+\$$_vname }-p \$1\""
237 shift
238 done
241 # collapsed_commit NAME
242 # Produce a collapsed commit of branch NAME.
243 collapsed_commit()
245 name="$1"
247 rm -f "$playground/^pre" "$playground/^post"
248 >"$playground/^body"
250 # Determine parent
251 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
252 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
253 while read -r p; do git rev-parse --quiet --verify "$p^0" -- || :; done)"
254 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
255 # Produce a merge commit first
256 v_pretty_tree prtytree "$name" -b
257 v_get_p_arg_list plist $parent
258 parent="$({
259 echo "TopGit-driven merge of branches:"
260 echo
261 cut -f 2 "$playground/$name^parents"
262 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
263 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
264 git commit-tree "$prtytree" $plist)"
267 if branch_empty "$name"; then
268 echol "$parent"
269 else
270 v_pretty_tree prtytree "$name"
271 create_tg_commit "$name" "$prtytree" "$parent"
274 echol "$name" >>"$playground/^ticker"
277 # collapse
278 # This will collapse a single branch, using information about
279 # previously collapsed branches stored in $playground.
280 collapse()
282 if [ -s "$playground/$_dep^commit" ]; then
283 # We've already seen this dep
284 commit="$(cat "$playground/$_dep^commit")"
286 elif [ -z "$_dep_is_tgish" ]; then
287 # This dep is not for rewrite
288 commit="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
290 else
291 # First time hitting this dep; the common case
292 echo "Collapsing $_dep"
293 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
294 commit="$(collapsed_commit "$_dep")"
295 bump_timestamp
296 echol "$commit" >"$playground/$_dep^commit"
299 # Propagate our work through the dependency chain
300 test -d "$playground/${_name%/*}" || mkdir -p "$playground/${_name%/*}"
301 echo "$commit $_dep" >>"$playground/$_name^parents"
305 ## Quilt driver
307 quilt()
309 if [ -z "$_dep_is_tgish" ]; then
310 # This dep is not for rewrite
311 return
314 _dep_tmp=$_dep
316 if "$strip"; then
317 i=$stripval
318 while [ "$i" -gt 0 ]; do
319 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
320 _dep_tmp=${_dep_tmp#*/}
321 i=$((i - 1))
322 done
325 dn="${_dep_tmp%/}.diff"
326 case "$dn" in */*);;*) dn="./$dn"; esac
327 bn="${dn##*/}"
328 dn="${dn%/*}/"
329 [ "x$dn" = "x./" ] && dn=""
331 if "$flatten" && [ "$dn" ]; then
332 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
333 dn=""
336 unset _dep_tmp
338 if [ -e "$playground/$_dep^commit" ]; then
339 # We've already seen this dep
340 return
343 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
344 >>"$playground/$_dep^commit"
346 if branch_empty "$_dep"; then
347 echo "Skip empty patch $_dep"
348 else
349 if "$numbered"; then
350 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
351 bn="$(printf "%04u-$bn" $number)"
352 echo "$number" >"$playground/^number"
355 echo "Exporting $_dep"
356 mkdir -p "$output/$dn"
357 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
358 echol "$dn$bn -p1" >>"$output/series"
362 linearize()
364 if test ! -f "$playground/^BASE"; then
365 if [ -n "$_dep_is_tgish" ]; then
366 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
367 else
368 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
370 echol "$head" > "$playground/^BASE"
371 git checkout -q $iowopt "$head"
372 [ -n "$_dep_is_tgish" ] || return 0
375 head=$(git rev-parse --verify HEAD --)
377 if [ -z "$_dep_is_tgish" ]; then
378 # merge in $_dep unless already included
379 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
380 common="$(git merge-base --all HEAD "$rev")" || :
381 if test "$rev" = "$common"; then
382 # already included, just skip
384 else
385 retmerge=0
387 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
388 if test "x$retmerge" != "x0"; then
389 echo "fix up the merge, commit and then exit."
390 #todo error handling
391 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
394 else
395 retmerge=0
397 v_pretty_tree _deptree "$_dep"
398 v_pretty_tree _depbasetree "$_dep" -b
399 git merge-recursive "$_depbasetree" -- HEAD "$_deptree" || retmerge="$?"
401 if test "x$retmerge" != "x0"; then
402 git rerere
403 echo "fix up the merge, update the index and then exit. Don't commit!"
404 #todo error handling
405 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
406 git rerere
409 result_tree=$(git write-tree)
410 # testing branch_empty might not always give the right answer.
411 # It can happen that the patch is non-empty but still after
412 # linearizing there is no change. So compare the trees.
413 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
414 echo "skip empty commit $_dep"
415 else
416 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
417 bump_timestamp
418 git update-ref HEAD $newcommit $head
419 echo "exported commit $_dep"
424 ## Machinery
426 wayback_push=
427 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
428 [ -n "$output" ] ||
429 die "no target branch specified"
430 if ! ref_exists "refs/heads/$output"; then
432 elif [ -z "$forceoutput" ]; then
433 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
434 else
435 checkout_opt=-B
437 ensure_ident_available
438 setup_smode
439 [ -z "$wayback" ] || wayback_push="$(git config --get remote.wayback.url 2>/dev/null)" || :
440 [ -z "$wayback" ] || [ -n "$wayback_push" ] || die "enable to configure wayback export"
442 elif [ "$driver" = "quilt" ]; then
443 [ -n "$output" ] ||
444 die "no target directory specified"
445 [ -n "$forceoutput" ] || [ ! -e "$output" ] || is_empty_dir "$output" . ||
446 die "non-empty target directory already exists (use --force to override): $output"
448 mkdir -p "$output"
452 driver()
454 # FIXME should we abort on missing dependency?
455 [ -z "$_dep_missing" ] || return 0
457 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
459 case $_dep in ":"*) return; esac
460 branch_needs_update >/dev/null
461 [ "$_ret" -eq 0 ] ||
462 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
464 $driver
467 # Call driver on all the branches - this will happen
468 # in topological order.
469 if "$allbranches" ; then
470 _dep_is_tgish=1
471 non_annihilated_branches |
472 while read _dep; do
473 driver
474 done
475 test $? -eq 0
476 elif [ -z "$branches" ]; then
477 recurse_deps driver "$name"
478 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
479 test $? -eq 0
480 else
481 while read _dep && [ -n "$_dep" ]; do
482 _dep_is_tgish=1
483 $driver
484 done <<-EOT
485 $(sed 'y/ /\n/' <<-LIST
486 $branches
487 LIST
490 name="$branches"
491 case "$branches" in *" "*) pl="es"; esac
495 if [ "$driver" = "collapse" ]; then
496 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name^commit")"'
497 [ -n "$forceoutput" ] || cmd="$cmd \"\""
498 eval "$cmd"
499 [ -z "$wayback_push" ] || git -c "remote.wayback.url=$wayback_push" push -q ${forceoutput:+--force} wayback "refs/heads/$output:refs/heads/$output"
501 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
502 echo "Exported topic branch $name (total $depcount topics) to branch $output"
504 elif [ "$driver" = "quilt" ]; then
505 depcount=$(( $(cat "$output/series" | wc -l) ))
506 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
508 elif [ "$driver" = "linearize" ]; then
509 git checkout -q --no-track $iowopt $checkout_opt $output
510 [ -z "$wayback_push" ] || git -c "remote.wayback.url=$wayback_push" push -q ${forceoutput:+--force} wayback "refs/heads/$output:refs/heads/$output"
512 echol "$name"
513 v_pretty_tree nametree "$name"
514 if test $(git rev-parse --verify "$nametree^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
515 echo "Warning: Exported result doesn't match"
516 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
517 #git diff $head HEAD
521 ec=$?
522 tmpdir_cleanup || :
523 git gc --auto || :
524 exit $ec