2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
28 branches
="$1"; shift;;
40 if [ "$val" = "--strip" ]; then
43 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
47 die
"invalid parameter $arg"
56 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
59 [ -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 [ -z "$branches" ] && ! "$allbranches"; then
88 # this check is only needed when no branches have been passed
89 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
90 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
91 die
"not on a TopGit-controlled branch"
95 playground
="$(get_temp tg-export -d)"
106 # Get commit message and authorship information
107 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
109 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
110 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
111 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
112 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
114 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
115 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
116 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
118 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
120 git commit-tree
"$tree" -p "$parent"
123 # collapsed_commit NAME
124 # Produce a collapsed commit of branch NAME.
129 rm -f "$playground/^pre" "$playground/^post"
133 [ -s "$playground/$name^parents" ] || git rev-parse
--verify "refs/top-bases/$name" >> "$playground/$name^parents"
134 parent
="$(cut -f 1 "$playground/$name^parents
" 2> /dev/null | \
135 while read p; do [ "$
(git cat-file
-t $p 2> /dev
/null
)" = tag ] && git cat-file tag $p | head -1 | cut -d' ' -f2 || echo $p; done)"
136 if [ "$(cat "$playground/$name^parents
" 2> /dev/null | wc_l)" -gt 1 ]; then
137 # Produce a merge commit first
139 echo "TopGit-driven merge of branches
:"
141 cut -f 2 "$playground/$name^parents
"
142 } | git commit-tree "$
(pretty_tree
"$name" -b)" \
143 $(for p in $parent; do echo -p $p; done))"
146 if branch_empty
"$name"; then
149 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
152 echo "$name" >>"$playground/^ticker"
156 # This will collapse a single branch, using information about
157 # previously collapsed branches stored in $playground.
160 if [ -s "$playground/$_dep" ]; then
161 # We've already seen this dep
162 commit
="$(cat "$playground/$_dep")"
164 elif [ -z "$_dep_is_tgish" ]; then
165 # This dep is not for rewrite
166 commit
="$(git rev-parse --verify "$_dep")"
169 # First time hitting this dep; the common case
170 echo "Collapsing $_dep"
171 commit
="$(collapsed_commit "$_dep")"
172 mkdir
-p "$playground/$(dirname "$_dep")"
173 echo "$commit" >"$playground/$_dep"
176 # Propagate our work through the dependency chain
177 mkdir
-p "$playground/$(dirname "$_name")"
178 echo "$commit $_dep" >>"$playground/$_name^parents"
186 if [ -z "$_dep_is_tgish" ]; then
187 # This dep is not for rewrite
195 while [ "$i" -gt 0 ]; do
196 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
197 _dep_tmp
=${_dep_tmp#*/}
202 bn
="$(basename "$_dep_tmp.
diff")"
203 dn
="$(dirname "$_dep_tmp.
diff")/"
204 [ "x$dn" = "x./" ] && dn
=""
206 if "$flatten" && [ "$dn" ]; then
207 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
213 if [ -e "$playground/$_dep" ]; then
214 # We've already seen this dep
218 mkdir
-p "$playground/$(dirname "$_dep")";
219 touch "$playground/$_dep";
221 if branch_empty
"$_dep"; then
222 echo "Skip empty patch $_dep";
225 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
226 bn
="$(printf "%04u-$bn" $number)";
227 echo "$number" >"$playground/^number";
230 echo "Exporting $_dep"
231 mkdir
-p "$output/$dn";
232 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
233 echo "$dn$bn -p1" >>"$output/series"
239 if test ! -f "$playground/^BASE"; then
240 if [ -n "$_dep_is_tgish" ]; then
241 head="$(git rev-parse --verify "refs
/top-bases
/$_dep")"
243 head="$(git rev-parse --verify "refs
/heads
/$_dep")"
245 echo "$head" > "$playground/^BASE"
246 git checkout
-q "$head"
247 [ -n "$_dep_is_tgish" ] ||
return 0;
250 head=$
(git rev-parse
--verify HEAD
)
252 if [ -z "$_dep_is_tgish" ]; then
253 # merge in $_dep unless already included
254 rev="$(git rev-parse --verify "$_dep")";
255 common
="$(git merge-base --all HEAD "$_dep")";
256 if test "$rev" = "$common"; then
257 # already included, just skip
262 git merge
-s recursive
"$_dep" || retmerge
="$?";
263 if test "x$retmerge" != "x0"; then
264 echo "fix up the merge, commit and then exit."
266 "${SHELL:-/bin/sh}" -i </dev
/tty
;
272 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "refs
/heads
/$_dep")" || retmerge
="$?";
274 if test "x$retmerge" != "x0"; then
276 echo "fix up the merge, update the index and then exit. Don't commit!"
278 "${SHELL:-/bin/sh}" -i </dev
/tty
;
281 result_tree
=$
(git write-tree
)
282 # testing branch_empty might not always give the right answer.
283 # It can happen that the patch is non-empty but still after
284 # linearizing there is no change. So compare the trees.
285 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
286 echo "skip empty commit $_dep";
288 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
289 git update-ref HEAD
$newcommit $head
290 echo "exported commit $_dep";
297 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
299 die
"no target branch specified"
300 if ! ref_exists
$output; then
302 elif [ -z "$forcebranch" ]; then
303 die
"target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
308 elif [ "$driver" = "quilt" ]; then
310 die
"no target directory specified"
311 [ ! -e "$output" ] ||
312 die
"target directory already exists: $output"
320 # FIXME should we abort on missing dependency?
321 [ -z "$_dep_missing" ] ||
return 0
323 [ -z "$_dep_is_tgish" ] ||
! branch_annihilated
"$_dep" ||
return 0
325 case $_dep in refs
/remotes
/*) return;; esac
326 branch_needs_update
>/dev
/null
328 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
333 # Call driver on all the branches - this will happen
334 # in topological order.
335 if "$allbranches" ; then
337 non_annihilated_branches |
341 elif [ -z "$branches" ]; then
342 recurse_deps driver
"$name"
343 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
345 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
349 name
="$(echo "$branches" | sed 's/.*,//')"
353 if [ "$driver" = "collapse" ]; then
354 cmd
='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
355 [ -n "$forcebranch" ] || cmd
="$cmd \"\""
358 depcount
="$(cat "$playground/^ticker
" | wc_l)"
359 echo "Exported topic branch $name (total $depcount topics) to branch $output"
361 elif [ "$driver" = "quilt" ]; then
362 depcount
="$(cat "$output/series
" | wc_l)"
363 echo "Exported topic branch $name (total $depcount topics) to directory $output"
365 elif [ "$driver" = "linearize" ]; then
366 git checkout
-q $checkout_opt $output
369 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
370 echo "Warning: Exported result doesn't match";
371 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")";
372 #git diff $head HEAD;