tg.sh: fully enable caching as designed
[topgit/pro.git] / tg-export.sh
blob8f767f8253b6b3f4786cc9f7aad9665771b36484
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 ensure_work_tree
129 if [ -z "$branches" ] && ! "$allbranches"; then
130 # this check is only needed when no branches have been passed
131 name="$(verify_topgit_branch HEAD)"
134 if [ -n "$branches" ]; then
135 oldbranches="$branches"
136 branches=
137 while read bname && [ -n "$bname" ]; do
138 branches="${branches:+$branches }$(verify_topgit_branch "$bname")"
139 done <<-EOT
140 $(sed 'y/ /\n/' <<-LIST
141 $oldbranches
142 LIST
145 unset oldbranches bname
148 read -r nowsecs nowtzoff <<EOT
149 $(date '+%s %z')
151 playground="$(get_temp tg-export -d)"
154 ## Collapse driver
156 bump_timestamp()
158 nowsecs=$(( $nowsecs + 1 ))
161 setup_smode()
163 mik=
164 test z"$smode" = z"mailinfo" || { mik=-k &&
165 test z"$smode" = z"keep"; } ||
166 sprefix="$(git config topgit.subjectprefix)" || :
169 create_tg_commit()
171 name="$1"
172 tree="$2"
173 parent="$3"
175 # Get commit message and authorship information
176 git cat-file blob "$name:.topmsg" 2>/dev/null |
177 git mailinfo $mik "$playground/^msg" /dev/null > "$playground/^info"
179 unset GIT_AUTHOR_NAME
180 unset GIT_AUTHOR_EMAIL
182 GIT_AUTHOR_NAME="$(sed -n "/^Author/ s/Author:[ $tab]*//p" "$playground/^info")"
183 GIT_AUTHOR_EMAIL="$(sed -n "/^Email/ s/Email:[ $tab]*//p" "$playground/^info")"
184 GIT_AUTHOR_DATE="$(sed -n "/^Date/ s/Date:[ $tab]*//p" "$playground/^info")"
185 SUBJECT="$(sed -n "/^Subject/ s/Subject:[ $tab]*//p" "$playground/^info")"
186 if test z"$smode" != z"mailinfo" && test z"$smode" != z"keep"; then
187 SUBJECT="$(printf '%s\n' "$SUBJECT" |
188 awk -v mode="$smode" -v prefix="$sprefix" '{
189 gsub(/[ \t]+/, " ")
190 sub(/^[ \t]+/, "")
191 sub(/[ \t]+$/, "")
192 if (mode != "trim") {
193 if (prefix != "" &&
194 tolower(substr($0, 1, (lp = 1 + length(prefix)))) == "[" tolower(prefix) &&
195 index($0, "]")) {
196 if (substr($0, 1 + lp, 1) == " ") ++lp
197 lead = tolower(substr($0, 1 + lp, 6))
198 if (lead ~ /^patch/ || (mode == "topgit" &&
199 lead ~ /^(base|root|stage)\]/))
200 $0 = "[" substr($0, 1 + lp)
202 if (!sub(/^\[[Pp][Aa][Tt][Cc][Hh][^]]*\][ \t]*/, "") &&
203 mode == "topgit")
204 sub(/^\[([Bb][Aa][Ss][Ee]|[Rr][Oo][Oo][Tt]|[Ss][Tt][Aa][Gg][Ee])\][ \t]*/, "")
206 print
207 exit
208 }')"
211 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
212 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
214 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
215 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
216 export GIT_AUTHOR_DATE
217 export GIT_COMMITTER_DATE
219 (printf '%s\n\n' "${SUBJECT:-$name}"; cat "$playground/^msg") |
220 git stripspace |
221 git commit-tree "$tree" -p "$parent"
223 unset GIT_AUTHOR_NAME
224 unset GIT_AUTHOR_EMAIL
225 unset GIT_AUTHOR_DATE
226 unset GIT_COMMITTER_DATE
229 # collapsed_commit NAME
230 # Produce a collapsed commit of branch NAME.
231 collapsed_commit()
233 name="$1"
235 rm -f "$playground/^pre" "$playground/^post"
236 >"$playground/^body"
238 # Determine parent
239 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
240 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
241 while read -r p; do git rev-parse --quiet --verify "$p^0" -- || :; done)"
242 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
243 # Produce a merge commit first
244 parent="$({
245 echo "TopGit-driven merge of branches:"
246 echo
247 cut -f 2 "$playground/$name^parents"
248 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
249 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
250 git commit-tree "$(pretty_tree "$name" -b)" \
251 $(for p in $parent; do echo "-p $p"; done))"
254 if branch_empty "$name"; then
255 echol "$parent"
256 else
257 create_tg_commit "$name" "$(pretty_tree "$name")" "$parent"
260 echol "$name" >>"$playground/^ticker"
263 # collapse
264 # This will collapse a single branch, using information about
265 # previously collapsed branches stored in $playground.
266 collapse()
268 if [ -s "$playground/$_dep^commit" ]; then
269 # We've already seen this dep
270 commit="$(cat "$playground/$_dep^commit")"
272 elif [ -z "$_dep_is_tgish" ]; then
273 # This dep is not for rewrite
274 commit="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
276 else
277 # First time hitting this dep; the common case
278 echo "Collapsing $_dep"
279 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
280 commit="$(collapsed_commit "$_dep")"
281 bump_timestamp
282 echol "$commit" >"$playground/$_dep^commit"
285 # Propagate our work through the dependency chain
286 test -d "$playground/${_name%/*}" || mkdir -p "$playground/${_name%/*}"
287 echo "$commit $_dep" >>"$playground/$_name^parents"
291 ## Quilt driver
293 quilt()
295 if [ -z "$_dep_is_tgish" ]; then
296 # This dep is not for rewrite
297 return
300 _dep_tmp=$_dep
302 if "$strip"; then
303 i=$stripval
304 while [ "$i" -gt 0 ]; do
305 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
306 _dep_tmp=${_dep_tmp#*/}
307 i=$((i - 1))
308 done
311 dn="${_dep_tmp%/}.diff"
312 case "$dn" in */*);;*) dn="./$dn"; esac
313 bn="${dn##*/}"
314 dn="${dn%/*}/"
315 [ "x$dn" = "x./" ] && dn=""
317 if "$flatten" && [ "$dn" ]; then
318 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
319 dn=""
322 unset _dep_tmp
324 if [ -e "$playground/$_dep^commit" ]; then
325 # We've already seen this dep
326 return
329 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
330 >>"$playground/$_dep^commit"
332 if branch_empty "$_dep"; then
333 echo "Skip empty patch $_dep"
334 else
335 if "$numbered"; then
336 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
337 bn="$(printf "%04u-$bn" $number)"
338 echo "$number" >"$playground/^number"
341 echo "Exporting $_dep"
342 mkdir -p "$output/$dn"
343 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
344 echol "$dn$bn -p1" >>"$output/series"
348 linearize()
350 if test ! -f "$playground/^BASE"; then
351 if [ -n "$_dep_is_tgish" ]; then
352 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
353 else
354 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
356 echol "$head" > "$playground/^BASE"
357 git checkout -q $iowopt "$head"
358 [ -n "$_dep_is_tgish" ] || return 0
361 head=$(git rev-parse --verify HEAD --)
363 if [ -z "$_dep_is_tgish" ]; then
364 # merge in $_dep unless already included
365 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
366 common="$(git merge-base --all HEAD "$rev")" || :
367 if test "$rev" = "$common"; then
368 # already included, just skip
370 else
371 retmerge=0
373 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
374 if test "x$retmerge" != "x0"; then
375 echo "fix up the merge, commit and then exit."
376 #todo error handling
377 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
380 else
381 retmerge=0
383 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
385 if test "x$retmerge" != "x0"; then
386 git rerere
387 echo "fix up the merge, update the index and then exit. Don't commit!"
388 #todo error handling
389 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
390 git rerere
393 result_tree=$(git write-tree)
394 # testing branch_empty might not always give the right answer.
395 # It can happen that the patch is non-empty but still after
396 # linearizing there is no change. So compare the trees.
397 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
398 echo "skip empty commit $_dep"
399 else
400 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
401 bump_timestamp
402 git update-ref HEAD $newcommit $head
403 echo "exported commit $_dep"
408 ## Machinery
410 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
411 [ -n "$output" ] ||
412 die "no target branch specified"
413 if ! ref_exists "refs/heads/$output"; then
415 elif [ -z "$forceoutput" ]; then
416 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
417 else
418 checkout_opt=-B
420 ensure_ident_available
421 setup_smode
423 elif [ "$driver" = "quilt" ]; then
424 [ -n "$output" ] ||
425 die "no target directory specified"
426 [ -n "$forceoutput" ] || [ ! -e "$output" ] ||
427 die "target directory already exists (use --force to override): $output"
429 mkdir -p "$output"
433 driver()
435 # FIXME should we abort on missing dependency?
436 [ -z "$_dep_missing" ] || return 0
438 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
440 case $_dep in ":"*) return; esac
441 branch_needs_update >/dev/null
442 [ "$_ret" -eq 0 ] ||
443 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
445 $driver
448 # Call driver on all the branches - this will happen
449 # in topological order.
450 if "$allbranches" ; then
451 _dep_is_tgish=1
452 non_annihilated_branches |
453 while read _dep; do
454 driver
455 done
456 test $? -eq 0
457 elif [ -z "$branches" ]; then
458 recurse_deps driver "$name"
459 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
460 test $? -eq 0
461 else
462 while read _dep && [ -n "$_dep" ]; do
463 _dep_is_tgish=1
464 $driver
465 done <<-EOT
466 $(sed 'y/ /\n/' <<-LIST
467 $branches
468 LIST
471 name="$branches"
472 case "$branches" in *" "*) pl="es"; esac
476 if [ "$driver" = "collapse" ]; then
477 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name^commit")"'
478 [ -n "$forceoutput" ] || cmd="$cmd \"\""
479 eval "$cmd"
481 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
482 echo "Exported topic branch $name (total $depcount topics) to branch $output"
484 elif [ "$driver" = "quilt" ]; then
485 depcount=$(( $(cat "$output/series" | wc -l) ))
486 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
488 elif [ "$driver" = "linearize" ]; then
489 git checkout -q $iowopt $checkout_opt $output
491 echol "$name"
492 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
493 echo "Warning: Exported result doesn't match"
494 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
495 #git diff $head HEAD