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>
30 branches
="${branches:+$branches }$1"; shift;;
42 if [ "$val" = "--strip" ]; then
45 elif [ -n "$val" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; then
49 die
"invalid parameter $arg"
58 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
61 [ -z "$output" ] || die
"output already specified ($output)"
66 [ -z "$branches" -o "$driver" = "quilt" ] ||
67 die
"-b works only with the quilt driver"
69 [ "$driver" = "quilt" ] ||
! "$numbered" ||
70 die
"--numbered works only with the quilt driver"
72 [ "$driver" = "quilt" ] ||
! "$flatten" ||
73 die
"--flatten works only with the quilt driver"
75 [ "$driver" = "quilt" ] ||
[ -z "$binary" ] ||
76 die
"--binary works only with the quilt driver"
78 [ "$driver" = "quilt" ] ||
! "$strip" ||
79 die
"--strip works only with the quilt driver"
81 [ "$driver" = "quilt" ] ||
! "$allbranches" ||
82 die
"--all works only with the quilt driver"
84 [ -z "$branches" ] ||
! "$allbranches" ||
85 die
"-b conflicts with the --all option"
87 if [ "$driver" = "linearize" ]; then
89 if [ -n "$git_dir_is_bare" ]; then
90 fatal
'export --linearize does not work on a bare repository...yet!'
95 if [ -z "$branches" ] && ! "$allbranches"; then
96 # this check is only needed when no branches have been passed
97 name
="$(verify_topgit_branch HEAD)"
100 if [ -n "$branches" ]; then
101 oldbranches
="$branches"
103 while read bname
&& [ -n "$bname" ]; do
104 branches
="${branches:+$branches }$(verify_topgit_branch "$bname")"
106 $(sed 'y/ /\n/' <<-LIST
111 unset oldbranches bname
114 read -r nowsecs nowtzoff
<<EOT
117 playground
="$(get_temp tg-export -d)"
124 nowsecs
=$
(( $nowsecs + 1 ))
133 # Get commit message and authorship information
134 git cat-file blob
"$name:.topmsg" 2>/dev
/null |
135 git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
137 unset GIT_AUTHOR_NAME
138 unset GIT_AUTHOR_EMAIL
140 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
141 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
142 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
143 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
145 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
146 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
148 GIT_COMMITTER_DATE
="$nowsecs $nowtzoff"
149 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
150 export GIT_AUTHOR_DATE
151 export GIT_COMMITTER_DATE
153 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
155 git commit-tree
"$tree" -p "$parent"
157 unset GIT_AUTHOR_NAME
158 unset GIT_AUTHOR_EMAIL
159 unset GIT_AUTHOR_DATE
160 unset GIT_COMMITTER_DATE
163 # collapsed_commit NAME
164 # Produce a collapsed commit of branch NAME.
169 rm -f "$playground/^pre" "$playground/^post"
173 [ -s "$playground/$name^parents" ] || git rev-parse
--verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
174 parent
="$(cut -f 1 "$playground/$name^parents
" 2> /dev/null |
175 while read -r p; do git rev-parse --quiet --verify "$p^
0" -- || :; done)"
176 if [ $
(( $
(cat "$playground/$name^parents" 2>/dev
/null |
wc -l) )) -gt 1 ]; then
177 # Produce a merge commit first
179 echo "TopGit-driven merge of branches
:"
181 cut -f 2 "$playground/$name^parents
"
182 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
183 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
184 git commit-tree "$
(pretty_tree
"$name" -b)" \
185 $(for p in $parent; do echo "-p $p"; done))"
188 if branch_empty
"$name"; then
191 create_tg_commit
"$name" "$(pretty_tree "$name")" "$parent"
194 echol
"$name" >>"$playground/^ticker"
198 # This will collapse a single branch, using information about
199 # previously collapsed branches stored in $playground.
202 if [ -s "$playground/$_dep" ]; then
203 # We've already seen this dep
204 commit
="$(cat "$playground/$_dep")"
206 elif [ -z "$_dep_is_tgish" ]; then
207 # This dep is not for rewrite
208 commit
="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
211 # First time hitting this dep; the common case
212 echo "Collapsing $_dep"
213 commit
="$(collapsed_commit "$_dep")"
216 case "$_depdn" in */*);;*) _depdn
="./$_depdn"; esac
217 mkdir
-p "$playground/${_depdn%/*}"
218 echol
"$commit" >"$playground/$_dep"
221 # Propagate our work through the dependency chain
223 case "$_namedn" in */*);;*) _namedn
="./$_namedn"; esac
224 mkdir
-p "$playground/${_namedn%/*}"
225 echo "$commit $_dep" >>"$playground/$_name^parents"
233 if [ -z "$_dep_is_tgish" ]; then
234 # This dep is not for rewrite
242 while [ "$i" -gt 0 ]; do
243 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
244 _dep_tmp
=${_dep_tmp#*/}
249 dn
="${_dep_tmp%/}.diff"
250 case "$dn" in */*);;*) dn
="./$dn"; esac
253 [ "x$dn" = "x./" ] && dn
=""
255 if "$flatten" && [ "$dn" ]; then
256 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
262 if [ -e "$playground/$_dep" ]; then
263 # We've already seen this dep
268 case "$_depdn" in */*);;*) _depdn
="./$_depdn"; esac
269 mkdir
-p "$playground/${_depdn%/*}"
270 >>"$playground/$_dep"
272 if branch_empty
"$_dep"; then
273 echo "Skip empty patch $_dep"
276 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))"
277 bn
="$(printf "%04u-$bn" $number)"
278 echo "$number" >"$playground/^number"
281 echo "Exporting $_dep"
282 mkdir
-p "$output/$dn"
283 tg
patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
284 echol
"$dn$bn -p1" >>"$output/series"
290 if test ! -f "$playground/^BASE"; then
291 if [ -n "$_dep_is_tgish" ]; then
292 head="$(git rev-parse --verify "refs
/$topbases/$_dep^
0" --)"
294 head="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
296 echol
"$head" > "$playground/^BASE"
297 git checkout
-q $iowopt "$head"
298 [ -n "$_dep_is_tgish" ] ||
return 0
301 head=$
(git rev-parse
--verify HEAD
--)
303 if [ -z "$_dep_is_tgish" ]; then
304 # merge in $_dep unless already included
305 rev="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
306 common
="$(git merge-base --all HEAD "$rev")" ||
:
307 if test "$rev" = "$common"; then
308 # already included, just skip
313 git merge
$auhopt -m "tgexport: merge $_dep into base" -s recursive
"refs/heads/$_dep^0" || retmerge
="$?"
314 if test "x$retmerge" != "x0"; then
315 echo "fix up the merge, commit and then exit."
317 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
323 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
325 if test "x$retmerge" != "x0"; then
327 echo "fix up the merge, update the index and then exit. Don't commit!"
329 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
333 result_tree
=$
(git write-tree
)
334 # testing branch_empty might not always give the right answer.
335 # It can happen that the patch is non-empty but still after
336 # linearizing there is no change. So compare the trees.
337 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
338 echo "skip empty commit $_dep"
340 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
342 git update-ref HEAD
$newcommit $head
343 echo "exported commit $_dep"
350 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
352 die
"no target branch specified"
353 if ! ref_exists
"refs/heads/$output"; then
355 elif [ -z "$forcebranch" ]; then
356 die
"target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
360 ensure_ident_available
362 elif [ "$driver" = "quilt" ]; then
364 die
"no target directory specified"
365 [ ! -e "$output" ] ||
366 die
"target directory already exists: $output"
374 # FIXME should we abort on missing dependency?
375 [ -z "$_dep_missing" ] ||
return 0
377 [ -z "$_dep_is_tgish" ] ||
[ -z "$_dep_annihilated" ] ||
return 0
379 case $_dep in refs
/remotes
/*) return;; esac
380 branch_needs_update
>/dev
/null
382 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
387 # Call driver on all the branches - this will happen
388 # in topological order.
389 if "$allbranches" ; then
391 non_annihilated_branches |
396 elif [ -z "$branches" ]; then
397 recurse_deps driver
"$name"
398 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
401 while read _dep
&& [ -n "$_dep" ]; do
405 $(sed 'y/ /\n/' <<-LIST
411 case "$branches" in *" "*) pl
="es"; esac
415 if [ "$driver" = "collapse" ]; then
416 cmd
='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
417 [ -n "$forcebranch" ] || cmd
="$cmd \"\""
420 depcount
=$
(( $
(cat "$playground/^ticker" |
wc -l) ))
421 echo "Exported topic branch $name (total $depcount topics) to branch $output"
423 elif [ "$driver" = "quilt" ]; then
424 depcount
=$
(( $
(cat "$output/series" |
wc -l) ))
425 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
427 elif [ "$driver" = "linearize" ]; then
428 git checkout
-q $iowopt $checkout_opt $output
431 if test $
(git rev-parse
--verify "$(pretty_tree "$name")^{tree}" --) != $
(git rev-parse
--verify "HEAD^{tree}" --); then
432 echo "Warning: Exported result doesn't match"
433 echo "tg-head=$(git rev-parse --verify "refs
/heads
/$name" --), exported=$(git rev-parse --verify "HEAD
" --)"