2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
27 branches
="$1"; shift;;
37 if [ "$val" = "--strip" ]; then
40 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
44 die
"invalid parameter $arg"
53 echo "Usage: tg [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] --quilt <directory> | --linearize <newbranch> [--force])" >&2
56 [ -z "$output" ] || die
"output already specified ($output)"
63 [ -z "$branches" -o "$driver" = "quilt" ] ||
64 die
"-b works only with the quilt driver"
66 [ "$driver" = "quilt" ] ||
! "$numbered" ||
67 die
"--numbered works only with the quilt driver";
69 [ "$driver" = "quilt" ] ||
! "$flatten" ||
70 die
"--flatten works only with the quilt driver"
72 [ "$driver" = "quilt" ] ||
! "$strip" ||
73 die
"--strip works only with the quilt driver"
75 [ "$driver" = "quilt" ] ||
! "$allbranches" ||
76 die
"--all works only with the quilt driver";
78 [ -z "$branches" ] ||
! "$allbranches" ||
79 die
"-b conflicts with the --all option";
81 if [ -z "$branches" ] && ! "$allbranches"; then
82 # this check is only needed when no branches have been passed
83 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
84 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
85 die
"not on a TopGit-controlled branch"
89 playground
="$(get_temp tg-export -d)"
100 # Get commit message and authorship information
101 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
103 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
104 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
105 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
106 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
108 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
109 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
110 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
112 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
114 git commit-tree
"$tree" -p "$parent"
117 # collapsed_commit NAME
118 # Produce a collapsed commit of branch NAME.
123 rm -f "$playground/^pre" "$playground/^post"
127 parent
="$(cut -f 1 "$playground/$name^parents
" | \
128 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)"
129 if [ "$(cat "$playground/$name^parents
" | wc_l)" -gt 1 ]; then
130 # Produce a merge commit first
132 echo "TopGit-driven merge of branches
:"
134 cut -f 2 "$playground/$name^parents
"
135 } | git commit-tree "$
(pretty_tree
"$name" -b)" \
136 $(for p in $parent; do echo -p $p; done))"
139 if branch_empty
"$name"; then
142 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
145 echo "$name" >>"$playground/^ticker"
149 # This will collapse a single branch, using information about
150 # previously collapsed branches stored in $playground.
153 if [ -s "$playground/$_dep" ]; then
154 # We've already seen this dep
155 commit
="$(cat "$playground/$_dep")"
157 elif [ -z "$_dep_is_tgish" ]; then
158 # This dep is not for rewrite
159 commit
="$(git rev-parse --verify "$_dep")"
162 # First time hitting this dep; the common case
163 echo "Collapsing $_dep"
164 commit
="$(collapsed_commit "$_dep")"
165 mkdir
-p "$playground/$(dirname "$_dep")"
166 echo "$commit" >"$playground/$_dep"
169 # Propagate our work through the dependency chain
170 mkdir
-p "$playground/$(dirname "$_name")"
171 echo "$commit $_dep" >>"$playground/$_name^parents"
179 if [ -z "$_dep_is_tgish" ]; then
180 # This dep is not for rewrite
188 while [ "$i" -gt 0 ]; do
189 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
190 _dep_tmp
=${_dep_tmp#*/}
195 bn
="$(basename "$_dep_tmp.
diff")"
196 dn
="$(dirname "$_dep_tmp.
diff")/"
197 [ "x$dn" = "x./" ] && dn
=""
199 if "$flatten" && [ "$dn" ]; then
200 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
206 if [ -e "$playground/$_dep" ]; then
207 # We've already seen this dep
211 mkdir
-p "$playground/$(dirname "$_dep")";
212 touch "$playground/$_dep";
214 if branch_empty
"$_dep"; then
215 echo "Skip empty patch $_dep";
218 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
219 bn
="$(printf "%04u-$bn" $number)";
220 echo "$number" >"$playground/^number";
223 echo "Exporting $_dep"
224 mkdir
-p "$output/$dn";
225 $tg patch "$_dep" >"$output/$dn$bn"
226 echo "$dn$bn -p1" >>"$output/series"
232 if test ! -f "$playground/^BASE"; then
233 head="$(git rev-parse --verify "$_dep")"
234 echo "$head" > "$playground/^BASE"
235 git checkout
-q "$head"
239 head=$
(git rev-parse
--verify HEAD
)
241 if [ -z "$_dep_is_tgish" ]; then
242 # merge in $_dep unless already included
243 rev="$(git rev-parse --verify "$_dep")";
244 common
="$(git merge-base --all HEAD "$_dep")";
245 if test "$rev" = "$common"; then
246 # already included, just skip
251 git merge
-s recursive
"$_dep" || retmerge
="$?";
252 if test "x$retmerge" != "x0"; then
253 echo fix up the merge
, commit and
then exit;
261 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "refs
/heads
/$_dep")" || retmerge
="$?";
263 if test "x$retmerge" != "x0"; then
265 echo "fix up the merge and update the index. Don't commit!"
270 result_tree
=$
(git write-tree
)
271 # testing branch_empty might not always give the right answer.
272 # It can happen that the patch is non-empty but still after
273 # linearizing there is no change. So compare the trees.
274 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
275 echo "skip empty commit $_dep";
277 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
278 git update-ref HEAD
$newcommit $head
279 echo "exported commit $_dep";
286 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
288 die
"no target branch specified"
289 if ! ref_exists
$output; then
291 elif ! "$forcebranch"; then
292 die
"target branch '$output' already exists; first run: git branch -D $output, or run tg export with --force"
297 elif [ "$driver" = "quilt" ]; then
299 die
"no target directory specified"
300 [ ! -e "$output" ] ||
301 die
"target directory already exists: $output"
309 # FIXME should we abort on missing dependency?
310 [ -z "$_dep_missing" ] ||
return 0
312 [ -z "$_dep_is_tgish" ] ||
! branch_annihilated
"$_dep" ||
return 0
314 case $_dep in refs
/remotes
/*) return;; esac
315 branch_needs_update
>/dev
/null
317 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
322 # Call driver on all the branches - this will happen
323 # in topological order.
324 if "$allbranches" ; then
326 non_annihilated_branches |
330 elif [ -z "$branches" ]; then
331 recurse_deps driver
"$name"
332 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
334 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
338 name
="$(echo "$branches" | sed 's/.*,//')"
342 if [ "$driver" = "collapse" ]; then
343 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
345 depcount
="$(cat "$playground/^ticker
" | wc_l)"
346 echo "Exported topic branch $name (total $depcount topics) to branch $output"
348 elif [ "$driver" = "quilt" ]; then
349 depcount
="$(cat "$output/series
" | wc_l)"
350 echo "Exported topic branch $name (total $depcount topics) to directory $output"
352 elif [ "$driver" = "linearize" ]; then
353 git checkout
-q $checkout_opt $output
356 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
357 echo "Warning: Exported result doesn't match";
358 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")";
359 #git diff $head HEAD;