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" ] ||
! "$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)'
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"
138 while read bname
&& [ -n "$bname" ]; do
139 branches
="${branches:+$branches }$(verify_topgit_branch "$bname")"
141 $(sed 'y/ /\n/' <<-LIST
146 unset oldbranches bname
149 read -r nowsecs nowtzoff
<<EOT
152 playground
="$(get_temp tg-export -d)"
159 nowsecs
=$
(( $nowsecs + 1 ))
165 test z
"$smode" = z
"mailinfo" ||
{ mik
=-k &&
166 test z
"$smode" = z
"keep"; } ||
167 sprefix
="$(git config topgit.subjectprefix)" ||
:
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" '{
193 if (mode != "trim
") {
195 tolower(substr($0, 1, (lp = 1 + length(prefix)))) == "[" tolower(prefix) &&
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]*/, "") &&
205 sub(/^\[([Bb][Aa][Ss][Ee]|[Rr][Oo][Oo][Tt]|[Ss][Tt][Aa][Gg][Ee]|[Rr][Ee][Ll][Ee][Aa][Ss][Ee])\][ \t]*/, "")
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") |
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
235 while [ $# -gt 0 ]; do
236 [ -z "$1" ] ||
eval "$_vname=\"\${$_vname:+\$$_vname }-p \$1\""
241 # collapsed_commit NAME
242 # Produce a collapsed commit of branch NAME.
247 rm -f "$playground/^pre" "$playground/^post"
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
259 echo "TopGit-driven merge of branches
:"
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
270 v_pretty_tree prtytree
"$name"
271 create_tg_commit
"$name" "$prtytree" "$parent"
274 echol
"$name" >>"$playground/^ticker"
278 # This will collapse a single branch, using information about
279 # previously collapsed branches stored in $playground.
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" --)"
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")"
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"
309 if [ -z "$_dep_is_tgish" ]; then
310 # This dep is not for rewrite
318 while [ "$i" -gt 0 ]; do
319 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
320 _dep_tmp
=${_dep_tmp#*/}
325 dn
="${_dep_tmp%/}.diff"
326 case "$dn" in */*);;*) dn
="./$dn"; esac
329 [ "x$dn" = "x./" ] && dn
=""
331 if "$flatten" && [ "$dn" ]; then
332 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
338 if [ -e "$playground/$_dep^commit" ]; then
339 # We've already seen this dep
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"
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"
364 if test ! -f "$playground/^BASE"; then
365 if [ -n "$_dep_is_tgish" ]; then
366 head="$(git rev-parse --verify "refs
/$topbases/$_dep^
0" --)"
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
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."
391 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
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
403 echo "fix up the merge, update the index and then exit. Don't commit!"
405 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
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"
416 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
418 git update-ref HEAD
$newcommit $head
419 echo "exported commit $_dep"
427 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
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"
437 ensure_ident_available
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
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"
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
462 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
467 # Call driver on all the branches - this will happen
468 # in topological order.
469 if "$allbranches" ; then
471 non_annihilated_branches |
476 elif [ -z "$branches" ]; then
477 recurse_deps driver
"$name"
478 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
481 while read _dep
&& [ -n "$_dep" ]; do
485 $(sed 'y/ /\n/' <<-LIST
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 \"\""
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"
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
" --)"