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>
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" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; 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!'
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"
137 while read bname
&& [ -n "$bname" ]; do
138 branches
="${branches:+$branches }$(verify_topgit_branch "$bname")"
140 $(sed 'y/ /\n/' <<-LIST
145 unset oldbranches bname
148 read -r nowsecs nowtzoff
<<EOT
151 playground
="$(get_temp tg-export -d)"
158 nowsecs
=$
(( $nowsecs + 1 ))
164 test z
"$smode" = z
"mailinfo" ||
{ mik
=-k &&
165 test z
"$smode" = z
"keep"; } ||
166 sprefix
="$(git config topgit.subjectprefix)" ||
:
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" '{
192 if (mode != "trim
") {
194 tolower(substr($0, 1, (lp = 1 + length(prefix)))) == "[" tolower(prefix) &&
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]*/, "") &&
204 sub(/^\[([Bb][Aa][Ss][Ee]|[Rr][Oo][Oo][Tt]|[Ss][Tt][Aa][Gg][Ee])\][ \t]*/, "")
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") |
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.
235 rm -f "$playground/^pre" "$playground/^post"
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
245 echo "TopGit-driven merge of branches
:"
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
257 create_tg_commit
"$name" "$(pretty_tree "$name")" "$parent"
260 echol
"$name" >>"$playground/^ticker"
264 # This will collapse a single branch, using information about
265 # previously collapsed branches stored in $playground.
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" --)"
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")"
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"
295 if [ -z "$_dep_is_tgish" ]; then
296 # This dep is not for rewrite
304 while [ "$i" -gt 0 ]; do
305 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
306 _dep_tmp
=${_dep_tmp#*/}
311 dn
="${_dep_tmp%/}.diff"
312 case "$dn" in */*);;*) dn
="./$dn"; esac
315 [ "x$dn" = "x./" ] && dn
=""
317 if "$flatten" && [ "$dn" ]; then
318 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
324 if [ -e "$playground/$_dep^commit" ]; then
325 # We've already seen this dep
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"
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"
350 if test ! -f "$playground/^BASE"; then
351 if [ -n "$_dep_is_tgish" ]; then
352 head="$(git rev-parse --verify "refs
/$topbases/$_dep^
0" --)"
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
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."
377 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
383 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
385 if test "x$retmerge" != "x0"; then
387 echo "fix up the merge, update the index and then exit. Don't commit!"
389 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
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"
400 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
402 git update-ref HEAD
$newcommit $head
403 echo "exported commit $_dep"
410 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
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"
420 ensure_ident_available
423 elif [ "$driver" = "quilt" ]; then
425 die
"no target directory specified"
426 [ -n "$forceoutput" ] ||
[ ! -e "$output" ] || is_empty_dir
"$output" . ||
427 die
"non-empty target directory already exists (use --force to override): $output"
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
443 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
448 # Call driver on all the branches - this will happen
449 # in topological order.
450 if "$allbranches" ; then
452 non_annihilated_branches |
457 elif [ -z "$branches" ]; then
458 recurse_deps driver
"$name"
459 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
462 while read _dep
&& [ -n "$_dep" ]; do
466 $(sed 'y/ /\n/' <<-LIST
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 \"\""
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
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
" --)"