tg.sh: next version is 0.19.13
[topgit/pro.git] / tg-export.sh
blob4fb5c433c490141c4dbe1ba90427f3aa82fc6d97
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=
31 numbered=
32 strip=
33 stripval=0
34 smode=
35 allbranches=
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=1;;
48 -b)
49 branches="${branches:+$branches }$1"; shift;;
50 --force)
51 forceoutput=1;;
52 --flatten)
53 flatten=1;;
54 --binary)
55 binary=1;;
56 --numbered)
57 flatten=1
58 numbered=1;;
59 --strip*)
60 val=${arg#*=}
61 if [ "$val" = "--strip" ]; then
62 strip=1
63 stripval=9999
64 elif [ -n "$val" ] && [ "${val#*[!0-9]}" = "$val" ]; then
65 strip=1
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" ] || [ -z "$numbered" ] ||
104 die "--numbered works only with the quilt driver"
106 [ "$driver" = "quilt" ] || [ -z "$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" ] || [ -z "$strip" ] ||
113 die "--strip works only with the quilt driver"
115 [ "$driver" = "quilt" ] || [ -z "$allbranches" ] ||
116 die "--all works only with the quilt driver"
118 [ -z "$branches" ] || [ -z "$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" ] && [ -z "$allbranches" ]; then
131 # this check is only needed when no branches have been passed
132 v_verify_topgit_branch name HEAD
135 if [ -n "$branches" ]; then
136 oldbranches="$branches"
137 branches=
138 while read bname && [ -n "$bname" ]; do
139 v_verify_topgit_branch bname "$bname"
140 branches="${branches:+$branches }$bname"
141 done <<-EOT
142 $(sed 'y/ /\n/' <<-LIST
143 $oldbranches
144 LIST
147 unset oldbranches bname
150 read -r nowsecs nowtzoff <<EOT
151 $(date '+%s %z')
153 playground="$(get_temp tg-export -d)"
156 ## Collapse driver
158 bump_timestamp()
160 nowsecs=$(( $nowsecs + 1 ))
163 setup_smode()
165 mik=
166 test z"$smode" = z"mailinfo" || { mik=-k &&
167 test z"$smode" = z"keep"; } ||
168 sprefix="$(git config topgit.subjectprefix)" || :
171 create_tg_commit()
173 name="$1"
174 tree="$2"
175 parent="$3"
177 # Get commit message and authorship information
178 git cat-file blob "$name:.topmsg" 2>/dev/null |
179 git mailinfo $mik "$playground/^msg" /dev/null > "$playground/^info"
181 unset GIT_AUTHOR_NAME
182 unset GIT_AUTHOR_EMAIL
184 GIT_AUTHOR_NAME="$(sed -n "/^Author/ s/Author:[ $tab]*//p" "$playground/^info")"
185 GIT_AUTHOR_EMAIL="$(sed -n "/^Email/ s/Email:[ $tab]*//p" "$playground/^info")"
186 GIT_AUTHOR_DATE="$(sed -n "/^Date/ s/Date:[ $tab]*//p" "$playground/^info")"
187 SUBJECT="$(sed -n "/^Subject/ s/Subject:[ $tab]*//p" "$playground/^info")"
188 if test z"$smode" != z"mailinfo" && test z"$smode" != z"keep"; then
189 SUBJECT="$(printf '%s\n' "$SUBJECT" |
190 awk -v mode="$smode" -v prefix="$sprefix" '{
191 gsub(/[ \t]+/, " ")
192 sub(/^[ \t]+/, "")
193 sub(/[ \t]+$/, "")
194 if (mode != "trim") {
195 if (prefix != "" &&
196 tolower(substr($0, 1, (lp = 1 + length(prefix)))) == "[" tolower(prefix) &&
197 index($0, "]")) {
198 if (substr($0, 1 + lp, 1) == " ") ++lp
199 lead = tolower(substr($0, 1 + lp, 8))
200 if (lead ~ /^patch/ || (mode == "topgit" &&
201 lead ~ /^(base|root|stage|release)\]/))
202 $0 = "[" substr($0, 1 + lp)
204 if (!sub(/^\[[Pp][Aa][Tt][Cc][Hh][^]]*\][ \t]*/, "") &&
205 mode == "topgit")
206 sub(/^\[([Bb][Aa][Ss][Ee]|[Rr][Oo][Oo][Tt]|[Ss][Tt][Aa][Gg][Ee]|[Rr][Ee][Ll][Ee][Aa][Ss][Ee])\][ \t]*/, "")
208 print
209 exit
210 }')"
213 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
214 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
216 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
217 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
218 export GIT_AUTHOR_DATE
219 export GIT_COMMITTER_DATE
221 (printf '%s\n\n' "${SUBJECT:-$name}"; cat "$playground/^msg") |
222 git stripspace |
223 git commit-tree "$tree" -p "$parent"
225 unset GIT_AUTHOR_NAME
226 unset GIT_AUTHOR_EMAIL
227 unset GIT_AUTHOR_DATE
228 unset GIT_COMMITTER_DATE
231 v_get_p_arg_list()
233 _vname="$1"
234 shift
235 eval "$_vname="
236 while [ $# -gt 0 ]; do
237 [ -z "$1" ] || eval "$_vname=\"\${$_vname:+\$$_vname }-p \$1\""
238 shift
239 done
242 # collapsed_commit NAME
243 # Produce a collapsed commit of branch NAME.
244 collapsed_commit()
246 name="$1"
248 rm -f "$playground/^pre" "$playground/^post"
249 >"$playground/^body"
251 # Determine parent
252 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
253 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
254 while read -r p; do git rev-parse --quiet --verify "$p^0" -- || :; done)"
255 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
256 # Produce a merge commit first
257 v_pretty_tree prtytree "$name" -b
258 v_get_p_arg_list plist $parent
259 parent="$({
260 echo "TopGit-driven merge of branches:"
261 echo
262 cut -f 2 "$playground/$name^parents"
263 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
264 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
265 git commit-tree "$prtytree" $plist)"
268 if branch_empty "$name"; then
269 echol "$parent"
270 else
271 v_pretty_tree prtytree "$name"
272 create_tg_commit "$name" "$prtytree" "$parent"
275 echol "$name" >>"$playground/^ticker"
278 # collapse
279 # This will collapse a single branch, using information about
280 # previously collapsed branches stored in $playground.
281 collapse()
283 if [ -s "$playground/$_dep^commit" ]; then
284 # We've already seen this dep
285 commit="$(cat "$playground/$_dep^commit")"
287 elif [ -z "$_dep_is_tgish" ]; then
288 # This dep is not for rewrite
289 commit="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
291 else
292 # First time hitting this dep; the common case
293 echo "Collapsing $_dep"
294 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
295 commit="$(collapsed_commit "$_dep")"
296 bump_timestamp
297 echol "$commit" >"$playground/$_dep^commit"
300 # Propagate our work through the dependency chain
301 test -d "$playground/${_name%/*}" || mkdir -p "$playground/${_name%/*}"
302 echo "$commit $_dep" >>"$playground/$_name^parents"
306 ## Quilt driver
308 quilt()
310 if [ -z "$_dep_is_tgish" ]; then
311 # This dep is not for rewrite
312 return
315 _dep_tmp=$_dep
317 if [ -n "$strip" ]; then
318 i="$stripval"
319 while [ "$i" -gt 0 ]; do
320 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
321 _dep_tmp=${_dep_tmp#*/}
322 i="$(( $i - 1 ))"
323 done
326 dn="${_dep_tmp%/}.diff"
327 case "$dn" in */*);;*) dn="./$dn"; esac
328 bn="${dn##*/}"
329 dn="${dn%/*}/"
330 [ "x$dn" = "x./" ] && dn=""
332 if [ -n "$flatten" ] && [ "$dn" ]; then
333 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
334 dn=""
337 unset _dep_tmp
339 if [ -e "$playground/$_dep^commit" ]; then
340 # We've already seen this dep
341 return
344 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
345 >>"$playground/$_dep^commit"
347 if branch_empty "$_dep"; then
348 echo "Skip empty patch $_dep"
349 else
350 if [ -n "$numbered" ]; then
351 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
352 bn="$(printf "%04u-$bn" $number)"
353 echo "$number" >"$playground/^number"
356 echo "Exporting $_dep"
357 mkdir -p "$output/$dn"
358 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
359 echol "$dn$bn -p1" >>"$output/series"
363 linearize()
365 if test ! -f "$playground/^BASE"; then
366 if [ -n "$_dep_is_tgish" ]; then
367 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
368 else
369 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
371 echol "$head" > "$playground/^BASE"
372 git checkout -q $iowopt "$head"
373 [ -n "$_dep_is_tgish" ] || return 0
376 head=$(git rev-parse --verify HEAD --)
378 if [ -z "$_dep_is_tgish" ]; then
379 # merge in $_dep unless already included
380 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
381 common="$(git merge-base --all HEAD "$rev")" || :
382 if test "$rev" = "$common"; then
383 # already included, just skip
385 else
386 retmerge=0
388 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
389 if test "x$retmerge" != "x0"; then
390 echo "fix up the merge, commit and then exit."
391 #todo error handling
392 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
395 else
396 retmerge=0
398 v_pretty_tree _deptree "$_dep"
399 v_pretty_tree _depbasetree "$_dep" -b
400 git merge-recursive "$_depbasetree" -- HEAD "$_deptree" || retmerge="$?"
402 if test "x$retmerge" != "x0"; then
403 git rerere
404 echo "fix up the merge, update the index and then exit. Don't commit!"
405 #todo error handling
406 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
407 git rerere
410 result_tree=$(git write-tree)
411 # testing branch_empty might not always give the right answer.
412 # It can happen that the patch is non-empty but still after
413 # linearizing there is no change. So compare the trees.
414 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
415 echo "skip empty commit $_dep"
416 else
417 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
418 bump_timestamp
419 git update-ref HEAD $newcommit $head
420 echo "exported commit $_dep"
425 ## Machinery
427 wayback_push=
428 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
429 [ -n "$output" ] ||
430 die "no target branch specified"
431 if ! ref_exists "refs/heads/$output"; then
433 elif [ -z "$forceoutput" ]; then
434 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
435 else
436 checkout_opt=-B
438 ensure_ident_available
439 setup_smode
440 [ -z "$wayback" ] || wayback_push="$(git config --get remote.wayback.url 2>/dev/null)" || :
441 [ -z "$wayback" ] || [ -n "$wayback_push" ] || die "enable to configure wayback export"
443 elif [ "$driver" = "quilt" ]; then
444 [ -n "$output" ] ||
445 die "no target directory specified"
446 [ -n "$forceoutput" ] || [ ! -e "$output" ] || is_empty_dir "$output" . ||
447 die "non-empty target directory already exists (use --force to override): $output"
449 mkdir -p "$output"
453 driver()
455 # FIXME should we abort on missing dependency?
456 [ -z "$_dep_missing" ] || return 0
458 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
460 case $_dep in ":"*) return; esac
461 branch_needs_update >/dev/null
462 [ "$_ret" -eq 0 ] ||
463 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
465 $driver
468 # Call driver on all the branches - this will happen
469 # in topological order.
470 if [ -n "$allbranches" ]; then
471 _dep_is_tgish=1
472 non_annihilated_branches |
473 while read _dep; do
474 driver
475 done
476 test $? -eq 0
477 elif [ -z "$branches" ]; then
478 recurse_deps driver "$name"
479 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
480 test $? -eq 0
481 else
482 while read _dep && [ -n "$_dep" ]; do
483 _dep_is_tgish=1
484 $driver
485 done <<-EOT
486 $(sed 'y/ /\n/' <<-LIST
487 $branches
488 LIST
491 name="$branches"
492 case "$branches" in *" "*) pl="es"; esac
496 if [ "$driver" = "collapse" ]; then
497 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name^commit")"'
498 [ -n "$forceoutput" ] || cmd="$cmd \"\""
499 eval "$cmd"
500 [ -z "$wayback_push" ] || git -c "remote.wayback.url=$wayback_push" push -q ${forceoutput:+--force} wayback "refs/heads/$output:refs/heads/$output"
502 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
503 echo "Exported topic branch $name (total $depcount topics) to branch $output"
505 elif [ "$driver" = "quilt" ]; then
506 depcount=$(( $(cat "$output/series" | wc -l) ))
507 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
509 elif [ "$driver" = "linearize" ]; then
510 git checkout -q --no-track $iowopt $checkout_opt $output
511 [ -z "$wayback_push" ] || git -c "remote.wayback.url=$wayback_push" push -q ${forceoutput:+--force} wayback "refs/heads/$output:refs/heads/$output"
513 echol "$name"
514 v_pretty_tree nametree "$name"
515 if test $(git rev-parse --verify "$nametree^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
516 echo "Warning: Exported result doesn't match"
517 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
518 #git diff $head HEAD
522 ec=$?
523 tmpdir_cleanup || :
524 git gc --auto || :
525 exit $ec