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" 2>/dev
/null |
128 git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
130 unset GIT_AUTHOR_NAME
131 unset GIT_AUTHOR_EMAIL
133 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
134 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
135 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
136 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
138 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
139 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
141 GIT_COMMITTER_DATE
="$nowsecs $nowtzoff"
142 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
143 export GIT_AUTHOR_DATE
144 export GIT_COMMITTER_DATE
146 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
148 git commit-tree
"$tree" -p "$parent"
150 unset GIT_AUTHOR_NAME
151 unset GIT_AUTHOR_EMAIL
152 unset GIT_AUTHOR_DATE
153 unset GIT_COMMITTER_DATE
156 # collapsed_commit NAME
157 # Produce a collapsed commit of branch NAME.
162 rm -f "$playground/^pre" "$playground/^post"
166 [ -s "$playground/$name^parents" ] || git rev-parse
--verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
167 parent
="$(cut -f 1 "$playground/$name^parents
" 2> /dev/null |
168 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)"
169 if [ $
(( $
(cat "$playground/$name^parents" 2>/dev
/null |
wc -l) )) -gt 1 ]; then
170 # Produce a merge commit first
172 echo "TopGit-driven merge of branches
:"
174 cut -f 2 "$playground/$name^parents
"
175 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
176 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
177 git commit-tree "$
(pretty_tree
"$name" -b)" \
178 $(for p in $parent; do echo "-p $p"; done))"
181 if branch_empty
"$name"; then
184 create_tg_commit
"$name" "$(pretty_tree "$name")" "$parent"
187 echol
"$name" >>"$playground/^ticker"
191 # This will collapse a single branch, using information about
192 # previously collapsed branches stored in $playground.
195 if [ -s "$playground/$_dep" ]; then
196 # We've already seen this dep
197 commit
="$(cat "$playground/$_dep")"
199 elif [ -z "$_dep_is_tgish" ]; then
200 # This dep is not for rewrite
201 commit
="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
204 # First time hitting this dep; the common case
205 echo "Collapsing $_dep"
206 commit
="$(collapsed_commit "$_dep")"
209 case "$_depdn" in */*);;*) _depdn
="./$_depdn"; esac
210 mkdir
-p "$playground/${_depdn%/*}"
211 echol
"$commit" >"$playground/$_dep"
214 # Propagate our work through the dependency chain
216 case "$_namedn" in */*);;*) _namedn
="./$_namedn"; esac
217 mkdir
-p "$playground/${_namedn%/*}"
218 echo "$commit $_dep" >>"$playground/$_name^parents"
226 if [ -z "$_dep_is_tgish" ]; then
227 # This dep is not for rewrite
235 while [ "$i" -gt 0 ]; do
236 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
237 _dep_tmp
=${_dep_tmp#*/}
242 dn
="${_dep_tmp%/}.diff"
243 case "$dn" in */*);;*) dn
="./$dn"; esac
246 [ "x$dn" = "x./" ] && dn
=""
248 if "$flatten" && [ "$dn" ]; then
249 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
255 if [ -e "$playground/$_dep" ]; then
256 # We've already seen this dep
261 case "$_depdn" in */*);;*) _depdn
="./$_depdn"; esac
262 mkdir
-p "$playground/${_depdn%/*}"
263 >>"$playground/$_dep"
265 if branch_empty
"$_dep"; then
266 echo "Skip empty patch $_dep"
269 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))"
270 bn
="$(printf "%04u-$bn" $number)"
271 echo "$number" >"$playground/^number"
274 echo "Exporting $_dep"
275 mkdir
-p "$output/$dn"
276 tg
patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
277 echol
"$dn$bn -p1" >>"$output/series"
283 if test ! -f "$playground/^BASE"; then
284 if [ -n "$_dep_is_tgish" ]; then
285 head="$(git rev-parse --verify "refs
/$topbases/$_dep^
0" --)"
287 head="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
289 echol
"$head" > "$playground/^BASE"
290 git checkout
-q $iowopt "$head"
291 [ -n "$_dep_is_tgish" ] ||
return 0
294 head=$
(git rev-parse
--verify HEAD
--)
296 if [ -z "$_dep_is_tgish" ]; then
297 # merge in $_dep unless already included
298 rev="$(git rev-parse --verify "refs
/heads
/$_dep^
0" --)"
299 common
="$(git merge-base --all HEAD "$rev")" ||
:
300 if test "$rev" = "$common"; then
301 # already included, just skip
306 git merge
$auhopt -m "tgexport: merge $_dep into base" -s recursive
"refs/heads/$_dep^0" || retmerge
="$?"
307 if test "x$retmerge" != "x0"; then
308 echo "fix up the merge, commit and then exit."
310 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
316 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
318 if test "x$retmerge" != "x0"; then
320 echo "fix up the merge, update the index and then exit. Don't commit!"
322 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
326 result_tree
=$
(git write-tree
)
327 # testing branch_empty might not always give the right answer.
328 # It can happen that the patch is non-empty but still after
329 # linearizing there is no change. So compare the trees.
330 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
331 echo "skip empty commit $_dep"
333 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
335 git update-ref HEAD
$newcommit $head
336 echo "exported commit $_dep"
343 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
345 die
"no target branch specified"
346 if ! ref_exists
"refs/heads/$output"; then
348 elif [ -z "$forcebranch" ]; then
349 die
"target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
353 ensure_ident_available
355 elif [ "$driver" = "quilt" ]; then
357 die
"no target directory specified"
358 [ ! -e "$output" ] ||
359 die
"target directory already exists: $output"
367 # FIXME should we abort on missing dependency?
368 [ -z "$_dep_missing" ] ||
return 0
370 [ -z "$_dep_is_tgish" ] ||
[ -z "$_dep_annihilated" ] ||
return 0
372 case $_dep in refs
/remotes
/*) return;; esac
373 branch_needs_update
>/dev
/null
375 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
380 # Call driver on all the branches - this will happen
381 # in topological order.
382 if "$allbranches" ; then
384 non_annihilated_branches |
389 elif [ -z "$branches" ]; then
390 recurse_deps driver
"$name"
391 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
394 while read _dep
&& [ -n "$_dep" ]; do
398 $(sed 'y/ /\n/' <<-LIST
404 case "$branches" in *" "*) pl
="es"; esac
408 if [ "$driver" = "collapse" ]; then
409 cmd
='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
410 [ -n "$forcebranch" ] || cmd
="$cmd \"\""
413 depcount
=$
(( $
(cat "$playground/^ticker" |
wc -l) ))
414 echo "Exported topic branch $name (total $depcount topics) to branch $output"
416 elif [ "$driver" = "quilt" ]; then
417 depcount
=$
(( $
(cat "$output/series" |
wc -l) ))
418 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
420 elif [ "$driver" = "linearize" ]; then
421 git checkout
-q $iowopt $checkout_opt $output
424 if test $
(git rev-parse
--verify "$(pretty_tree "$name")^{tree}" --) != $
(git rev-parse
--verify "HEAD^{tree}" --); then
425 echo "Warning: Exported result doesn't match"
426 echo "tg-head=$(git rev-parse --verify "refs
/heads
/$name" --), exported=$(git rev-parse --verify "HEAD
" --)"