2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
29 branches
="${branches:+$branches }$1"; shift;;
41 if [ "$val" = "--strip" ]; then
44 elif [ -n "$val" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; then
48 die
"invalid parameter $arg"
57 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
60 [ -z "$output" ] || die
"output already specified ($output)"
67 [ -z "$branches" -o "$driver" = "quilt" ] ||
68 die
"-b works only with the quilt driver"
70 [ "$driver" = "quilt" ] ||
! "$numbered" ||
71 die
"--numbered works only with the quilt driver"
73 [ "$driver" = "quilt" ] ||
! "$flatten" ||
74 die
"--flatten works only with the quilt driver"
76 [ "$driver" = "quilt" ] ||
[ -z "$binary" ] ||
77 die
"--binary works only with the quilt driver"
79 [ "$driver" = "quilt" ] ||
! "$strip" ||
80 die
"--strip works only with the quilt driver"
82 [ "$driver" = "quilt" ] ||
! "$allbranches" ||
83 die
"--all works only with the quilt driver"
85 [ -z "$branches" ] ||
! "$allbranches" ||
86 die
"-b conflicts with the --all option"
88 if [ -z "$branches" ] && ! "$allbranches"; then
89 # this check is only needed when no branches have been passed
90 name
="$(verify_topgit_branch HEAD)"
93 if [ -n "$branches" ]; then
94 oldbranches
="$branches"
96 while read bname
&& [ -n "$bname" ]; do
97 branches
="${branches:+$branches }$(verify_topgit_branch "$bname")"
99 $(sed 'y/ /\n/' <<-LIST
104 unset oldbranches bname
107 read -r nowsecs nowtzoff
<<EOT
110 playground
="$(get_temp tg-export -d)"
117 nowsecs
=$
(( $nowsecs + 1 ))
126 # Get commit message and authorship information
127 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
129 unset GIT_AUTHOR_NAME
130 unset GIT_AUTHOR_EMAIL
132 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
133 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
134 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
135 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
137 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
138 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
140 GIT_COMMITTER_DATE
="$nowsecs $nowtzoff"
141 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
142 export GIT_AUTHOR_DATE
143 export GIT_COMMITTER_DATE
145 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
147 git commit-tree
"$tree" -p "$parent"
149 unset GIT_AUTHOR_NAME
150 unset GIT_AUTHOR_EMAIL
151 unset GIT_AUTHOR_DATE
152 unset GIT_COMMITTER_DATE
155 # collapsed_commit NAME
156 # Produce a collapsed commit of branch NAME.
161 rm -f "$playground/^pre" "$playground/^post"
165 [ -s "$playground/$name^parents" ] || git rev-parse
--verify "refs/$topbases/$name" -- >> "$playground/$name^parents"
166 parent
="$(cut -f 1 "$playground/$name^parents
" 2> /dev/null |
167 while read p; do [ "$
(git cat-file
-t "$p" 2> /dev
/null
)" = tag ] && git cat-file tag "$p" | head -1 | cut -d' ' -f2 || echol "$p"; done)"
168 if [ $
(( $
(cat "$playground/$name^parents" 2>/dev
/null |
wc -l) )) -gt 1 ]; then
169 # Produce a merge commit first
171 echo "TopGit-driven merge of branches
:"
173 cut -f 2 "$playground/$name^parents
"
174 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
175 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
176 git commit-tree "$
(pretty_tree
"$name" -b)" \
177 $(for p in $parent; do echo "-p $p"; done))"
180 if branch_empty
"$name"; then
183 create_tg_commit
"$name" "$(pretty_tree "$name")" "$parent"
186 echol
"$name" >>"$playground/^ticker"
190 # This will collapse a single branch, using information about
191 # previously collapsed branches stored in $playground.
194 if [ -s "$playground/$_dep" ]; then
195 # We've already seen this dep
196 commit
="$(cat "$playground/$_dep")"
198 elif [ -z "$_dep_is_tgish" ]; then
199 # This dep is not for rewrite
200 commit
="$(git rev-parse --verify "refs
/heads
/$_dep" --)"
203 # First time hitting this dep; the common case
204 echo "Collapsing $_dep"
205 commit
="$(collapsed_commit "$_dep")"
208 case "$_depdn" in */*);;*) _depdn
="./$_depdn"; esac
209 mkdir
-p "$playground/${_depdn%/*}"
210 echol
"$commit" >"$playground/$_dep"
213 # Propagate our work through the dependency chain
215 case "$_namedn" in */*);;*) _namedn
="./$_namedn"; esac
216 mkdir
-p "$playground/${_namedn%/*}"
217 echo "$commit $_dep" >>"$playground/$_name^parents"
225 if [ -z "$_dep_is_tgish" ]; then
226 # This dep is not for rewrite
234 while [ "$i" -gt 0 ]; do
235 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
236 _dep_tmp
=${_dep_tmp#*/}
241 dn
="${_dep_tmp%/}.diff"
242 case "$dn" in */*);;*) dn
="./$dn"; esac
245 [ "x$dn" = "x./" ] && dn
=""
247 if "$flatten" && [ "$dn" ]; then
248 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
254 if [ -e "$playground/$_dep" ]; then
255 # We've already seen this dep
260 case "$_depdn" in */*);;*) _depdn
="./$_depdn"; esac
261 mkdir
-p "$playground/${_depdn%/*}"
262 >>"$playground/$_dep"
264 if branch_empty
"$_dep"; then
265 echo "Skip empty patch $_dep"
268 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))"
269 bn
="$(printf "%04u-$bn" $number)"
270 echo "$number" >"$playground/^number"
273 echo "Exporting $_dep"
274 mkdir
-p "$output/$dn"
275 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
276 echol
"$dn$bn -p1" >>"$output/series"
282 if test ! -f "$playground/^BASE"; then
283 if [ -n "$_dep_is_tgish" ]; then
284 head="$(git rev-parse --verify "refs
/$topbases/$_dep" --)"
286 head="$(git rev-parse --verify "refs
/heads
/$_dep" --)"
288 echol
"$head" > "$playground/^BASE"
289 git checkout
-q "$head"
290 [ -n "$_dep_is_tgish" ] ||
return 0
293 head=$
(git rev-parse
--verify HEAD
--)
295 if [ -z "$_dep_is_tgish" ]; then
296 # merge in $_dep unless already included
297 rev="$(git rev-parse --verify "$_dep" --)"
298 common
="$(git merge-base --all HEAD "$_dep")" ||
:
299 if test "$rev" = "$common"; then
300 # already included, just skip
305 git merge
$auhopt -m "tgexport: merge $_dep into base" -s recursive
"$_dep^0" || retmerge
="$?"
306 if test "x$retmerge" != "x0"; then
307 echo "fix up the merge, commit and then exit."
309 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
315 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
317 if test "x$retmerge" != "x0"; then
319 echo "fix up the merge, update the index and then exit. Don't commit!"
321 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
325 result_tree
=$
(git write-tree
)
326 # testing branch_empty might not always give the right answer.
327 # It can happen that the patch is non-empty but still after
328 # linearizing there is no change. So compare the trees.
329 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
330 echo "skip empty commit $_dep"
332 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
334 git update-ref HEAD
$newcommit $head
335 echo "exported commit $_dep"
342 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
344 die
"no target branch specified"
345 if ! ref_exists
"refs/heads/$output"; then
347 elif [ -z "$forcebranch" ]; then
348 die
"target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
352 ensure_ident_available
354 elif [ "$driver" = "quilt" ]; then
356 die
"no target directory specified"
357 [ ! -e "$output" ] ||
358 die
"target directory already exists: $output"
366 # FIXME should we abort on missing dependency?
367 [ -z "$_dep_missing" ] ||
return 0
369 [ -z "$_dep_is_tgish" ] ||
! branch_annihilated
"$_dep" ||
return 0
371 case $_dep in refs
/remotes
/*) return;; esac
372 branch_needs_update
>/dev
/null
374 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
379 # Call driver on all the branches - this will happen
380 # in topological order.
381 if "$allbranches" ; then
383 non_annihilated_branches |
388 elif [ -z "$branches" ]; then
389 recurse_deps driver
"$name"
390 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
393 while read _dep
&& [ -n "$_dep" ]; do
397 $(sed 'y/ /\n/' <<-LIST
403 case "$branches" in *" "*) pl
="es"; esac
407 if [ "$driver" = "collapse" ]; then
408 cmd
='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
409 [ -n "$forcebranch" ] || cmd
="$cmd \"\""
412 depcount
=$
(( $
(cat "$playground/^ticker" |
wc -l) ))
413 echo "Exported topic branch $name (total $depcount topics) to branch $output"
415 elif [ "$driver" = "quilt" ]; then
416 depcount
=$
(( $
(cat "$output/series" |
wc -l) ))
417 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
419 elif [ "$driver" = "linearize" ]; then
420 git checkout
-q $checkout_opt $output
423 if test $
(git rev-parse
--verify "$(pretty_tree "$name")^{tree}" --) != $
(git rev-parse
--verify "HEAD^{tree}" --); then
424 echo "Warning: Exported result doesn't match"
425 echo "tg-head=$(git rev-parse --verify "refs
/heads
/$name" --), exported=$(git rev-parse --verify "HEAD
" --)"