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^commit" ]; then
203 # We've already seen this dep
204 commit
="$(cat "$playground/$_dep^commit
")"
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 test -d "$playground/${_dep%/*}" || mkdir
-p "$playground/${_dep%/*}"
214 commit
="$(collapsed_commit "$_dep")"
216 echol
"$commit" >"$playground/$_dep^commit"
219 # Propagate our work through the dependency chain
220 test -d "$playground/${_name%/*}" || mkdir
-p "$playground/${_name%/*}"
221 echo "$commit $_dep" >>"$playground/$_name^parents"
229 if [ -z "$_dep_is_tgish" ]; then
230 # This dep is not for rewrite
238 while [ "$i" -gt 0 ]; do
239 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
240 _dep_tmp
=${_dep_tmp#*/}
245 dn
="${_dep_tmp%/}.diff"
246 case "$dn" in */*);;*) dn
="./$dn"; esac
249 [ "x$dn" = "x./" ] && dn
=""
251 if "$flatten" && [ "$dn" ]; then
252 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
258 if [ -e "$playground/$_dep^commit" ]; then
259 # We've already seen this dep
263 test -d "$playground/${_dep%/*}" || mkdir
-p "$playground/${_dep%/*}"
264 >>"$playground/$_dep^commit"
266 if branch_empty
"$_dep"; then
267 echo "Skip empty patch $_dep"
270 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))"
271 bn
="$(printf "%04u-$bn" $number)"
272 echo "$number" >"$playground/^number"
275 echo "Exporting $_dep"
276 mkdir
-p "$output/$dn"
277 tg
patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
278 echol
"$dn$bn -p1" >>"$output/series"
284 if test ! -f "$playground/^BASE"; then
285 if [ -n "$_dep_is_tgish" ]; then
286 head="$(git rev-parse --verify "refs
/$topbases/$_dep^
0" --)"
288 head="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
290 echol
"$head" > "$playground/^BASE"
291 git checkout
-q $iowopt "$head"
292 [ -n "$_dep_is_tgish" ] ||
return 0
295 head=$
(git rev-parse
--verify HEAD
--)
297 if [ -z "$_dep_is_tgish" ]; then
298 # merge in $_dep unless already included
299 rev="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
300 common
="$(git merge-base --all HEAD "$rev")" ||
:
301 if test "$rev" = "$common"; then
302 # already included, just skip
307 git merge
$auhopt -m "tgexport: merge $_dep into base" -s recursive
"refs/heads/$_dep^0" || retmerge
="$?"
308 if test "x$retmerge" != "x0"; then
309 echo "fix up the merge, commit and then exit."
311 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
317 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
319 if test "x$retmerge" != "x0"; then
321 echo "fix up the merge, update the index and then exit. Don't commit!"
323 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
327 result_tree
=$
(git write-tree
)
328 # testing branch_empty might not always give the right answer.
329 # It can happen that the patch is non-empty but still after
330 # linearizing there is no change. So compare the trees.
331 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
332 echo "skip empty commit $_dep"
334 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
336 git update-ref HEAD
$newcommit $head
337 echo "exported commit $_dep"
344 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
346 die
"no target branch specified"
347 if ! ref_exists
"refs/heads/$output"; then
349 elif [ -z "$forcebranch" ]; then
350 die
"target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
354 ensure_ident_available
356 elif [ "$driver" = "quilt" ]; then
358 die
"no target directory specified"
359 [ ! -e "$output" ] ||
360 die
"target directory already exists: $output"
368 # FIXME should we abort on missing dependency?
369 [ -z "$_dep_missing" ] ||
return 0
371 [ -z "$_dep_is_tgish" ] ||
[ -z "$_dep_annihilated" ] ||
return 0
373 case $_dep in ":"*) return; esac
374 branch_needs_update
>/dev
/null
376 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
381 # Call driver on all the branches - this will happen
382 # in topological order.
383 if "$allbranches" ; then
385 non_annihilated_branches |
390 elif [ -z "$branches" ]; then
391 recurse_deps driver
"$name"
392 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
395 while read _dep
&& [ -n "$_dep" ]; do
399 $(sed 'y/ /\n/' <<-LIST
405 case "$branches" in *" "*) pl
="es"; esac
409 if [ "$driver" = "collapse" ]; then
410 cmd
='git update-ref "refs/heads/$output" "$(cat "$playground/$name^commit")"'
411 [ -n "$forcebranch" ] || cmd
="$cmd \"\""
414 depcount
=$
(( $
(cat "$playground/^ticker" |
wc -l) ))
415 echo "Exported topic branch $name (total $depcount topics) to branch $output"
417 elif [ "$driver" = "quilt" ]; then
418 depcount
=$
(( $
(cat "$output/series" |
wc -l) ))
419 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
421 elif [ "$driver" = "linearize" ]; then
422 git checkout
-q $iowopt $checkout_opt $output
425 if test $
(git rev-parse
--verify "$(pretty_tree "$name")^{tree}" --) != $
(git rev-parse
--verify "HEAD^{tree}" --); then
426 echo "Warning: Exported result doesn't match"
427 echo "tg-head=$(git rev-parse --verify "refs
/heads
/$name" --), exported=$(git rev-parse --verify "HEAD
" --)"