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>
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>"
16 if [ "${1:-0}" != 0 ]; then
17 printf '%s\n' "$USAGE" >&2
19 printf '%s\n' "$USAGE"
49 branches
="${branches:+$branches }$1"; shift;;
61 if [ "$val" = "--strip" ]; then
64 elif [ -n "$val" ] && [ "${val#*[!0-9]}" = "$val" ]; then
68 die
"invalid parameter $arg"
71 test $# -gt 0 && test -n "$1" || die
"-s requires an argument"
82 [ -z "$output" ] || die
"output already specified ($output)"
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
95 topgit|
patch|mailinfo|trim|keep
);;
96 *) die
"invalid subject mode: $smode"
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)'
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"
138 while read bname
&& [ -n "$bname" ]; do
139 v_verify_topgit_branch bname
"$bname"
140 branches
="${branches:+$branches }$bname"
142 $(sed 'y/ /\n/' <<-LIST
147 unset oldbranches bname
150 read -r nowsecs nowtzoff
<<EOT
153 playground
="$(get_temp tg-export -d)"
160 nowsecs
=$
(( $nowsecs + 1 ))
166 test z
"$smode" = z
"mailinfo" ||
{ mik
=-k &&
167 test z
"$smode" = z
"keep"; } ||
168 sprefix
="$(git config topgit.subjectprefix)" ||
:
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" '{
194 if (mode != "trim
") {
196 tolower(substr($0, 1, (lp = 1 + length(prefix)))) == "[" tolower(prefix) &&
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]*/, "") &&
206 sub(/^\[([Bb][Aa][Ss][Ee]|[Rr][Oo][Oo][Tt]|[Ss][Tt][Aa][Gg][Ee]|[Rr][Ee][Ll][Ee][Aa][Ss][Ee])\][ \t]*/, "")
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") |
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
236 while [ $# -gt 0 ]; do
237 [ -z "$1" ] ||
eval "$_vname=\"\${$_vname:+\$$_vname }-p \$1\""
242 # collapsed_commit NAME
243 # Produce a collapsed commit of branch NAME.
248 rm -f "$playground/^pre" "$playground/^post"
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
260 echo "TopGit-driven merge of branches
:"
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
271 v_pretty_tree prtytree
"$name"
272 create_tg_commit
"$name" "$prtytree" "$parent"
275 echol
"$name" >>"$playground/^ticker"
279 # This will collapse a single branch, using information about
280 # previously collapsed branches stored in $playground.
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" --)"
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")"
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"
310 if [ -z "$_dep_is_tgish" ]; then
311 # This dep is not for rewrite
317 if [ -n "$strip" ]; then
319 while [ "$i" -gt 0 ]; do
320 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
321 _dep_tmp
=${_dep_tmp#*/}
326 dn
="${_dep_tmp%/}.diff"
327 case "$dn" in */*);;*) dn
="./$dn"; esac
330 [ "x$dn" = "x./" ] && dn
=""
332 if [ -n "$flatten" ] && [ "$dn" ]; then
333 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
339 if [ -e "$playground/$_dep^commit" ]; then
340 # We've already seen this dep
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"
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"
365 if test ! -f "$playground/^BASE"; then
366 if [ -n "$_dep_is_tgish" ]; then
367 head="$(git rev-parse --verify "refs
/$topbases/$_dep^
0" --)"
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
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."
392 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
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
404 echo "fix up the merge, update the index and then exit. Don't commit!"
406 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
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"
417 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
419 git update-ref HEAD
$newcommit $head
420 echo "exported commit $_dep"
428 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
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"
438 ensure_ident_available
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
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"
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
463 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
468 # Call driver on all the branches - this will happen
469 # in topological order.
470 if [ -n "$allbranches" ]; then
472 non_annihilated_branches |
477 elif [ -z "$branches" ]; then
478 recurse_deps driver
"$name"
479 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
482 while read _dep
&& [ -n "$_dep" ]; do
486 $(sed 'y/ /\n/' <<-LIST
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 \"\""
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"
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
" --)"