2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
25 branches
="$1"; shift;;
33 if [ "$val" = "--strip" ]; then
36 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
40 die
"invalid parameter $arg"
49 echo "Usage: tg [...] export ([--collapse] <newbranch> | [-a | --all | -b <branch1>...] --quilt <directory> | --linearize <newbranch>)" >&2
52 [ -z "$output" ] || die
"output already specified ($output)"
59 [ -z "$branches" -o "$driver" = "quilt" ] ||
60 die
"-b works only with the quilt driver"
62 [ "$driver" = "quilt" ] ||
! "$numbered" ||
63 die
"--numbered works only with the quilt driver";
65 [ "$driver" = "quilt" ] ||
! "$flatten" ||
66 die
"--flatten works only with the quilt driver"
68 [ "$driver" = "quilt" ] ||
! "$strip" ||
69 die
"--strip works only with the quilt driver"
71 [ "$driver" = "quilt" ] ||
! "$allbranches" ||
72 die
"--all works only with the quilt driver";
74 [ -z "$branches" ] ||
! "$allbranches" ||
75 die
"-b conflicts with the --all option";
77 if [ -z "$branches" ] && ! "$allbranches"; then
78 # this check is only needed when no branches have been passed
79 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
80 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
81 die
"not on a TopGit-controlled branch"
85 playground
="$(get_temp tg-export -d)"
96 # Get commit message and authorship information
97 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
99 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
100 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
101 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
102 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
104 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
105 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
106 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
108 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
110 git commit-tree
"$tree" -p "$parent"
113 # collapsed_commit NAME
114 # Produce a collapsed commit of branch NAME.
119 rm -f "$playground/^pre" "$playground/^post"
123 parent
="$(cut -f 1 "$playground/$name^parents
" | \
124 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)"
125 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
126 # Produce a merge commit first
128 echo "TopGit-driven merge of branches
:"
130 cut -f 2 "$playground/$name^parents
"
131 } | git commit-tree "$
(pretty_tree
"$name" -b)" \
132 $(for p in $parent; do echo -p $p; done))"
135 if branch_empty
"$name"; then
138 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
141 echo "$name" >>"$playground/^ticker"
145 # This will collapse a single branch, using information about
146 # previously collapsed branches stored in $playground.
149 if [ -s "$playground/$_dep" ]; then
150 # We've already seen this dep
151 commit
="$(cat "$playground/$_dep")"
153 elif [ -z "$_dep_is_tgish" ]; then
154 # This dep is not for rewrite
155 commit
="$(git rev-parse --verify "$_dep")"
158 # First time hitting this dep; the common case
159 echo "Collapsing $_dep"
160 commit
="$(collapsed_commit "$_dep")"
161 mkdir
-p "$playground/$(dirname "$_dep")"
162 echo "$commit" >"$playground/$_dep"
165 # Propagate our work through the dependency chain
166 mkdir
-p "$playground/$(dirname "$_name")"
167 echo "$commit $_dep" >>"$playground/$_name^parents"
175 if [ -z "$_dep_is_tgish" ]; then
176 # This dep is not for rewrite
184 while [ "$i" -gt 0 ]; do
185 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
186 _dep_tmp
=${_dep_tmp#*/}
191 bn
="$(basename "$_dep_tmp.
diff")"
192 dn
="$(dirname "$_dep_tmp.
diff")/"
193 [ "x$dn" = "x./" ] && dn
=""
195 if "$flatten" && [ "$dn" ]; then
196 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
202 if [ -e "$playground/$_dep" ]; then
203 # We've already seen this dep
207 mkdir
-p "$playground/$(dirname "$_dep")";
208 touch "$playground/$_dep";
210 if branch_empty
"$_dep"; then
211 echo "Skip empty patch $_dep";
214 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
215 bn
="$(printf "%04u-$bn" $number)";
216 echo "$number" >"$playground/^number";
219 echo "Exporting $_dep"
220 mkdir
-p "$output/$dn";
221 $tg patch "$_dep" >"$output/$dn$bn"
222 echo "$dn$bn -p1" >>"$output/series"
228 if test ! -f "$playground/^BASE"; then
229 head="$(git rev-parse --verify "$_dep")"
230 echo "$head" > "$playground/^BASE"
231 git checkout
-q "$head"
235 head=$
(git rev-parse
--verify HEAD
)
237 if [ -z "$_dep_is_tgish" ]; then
238 # merge in $_dep unless already included
239 rev="$(git rev-parse --verify "$_dep")";
240 common
="$(git merge-base --all HEAD "$_dep")";
241 if test "$rev" = "$common"; then
242 # already included, just skip
247 git merge
-s recursive
"$_dep" || retmerge
="$?";
248 if test "x$retmerge" != "x0"; then
249 echo fix up the merge
, commit and
then exit;
257 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "refs
/heads
/$_dep")" || retmerge
="$?";
259 if test "x$retmerge" != "x0"; then
261 echo "fix up the merge and update the index. Don't commit!"
266 result_tree
=$
(git write-tree
)
267 # testing branch_empty might not always give the right answer.
268 # It can happen that the patch is non-empty but still after
269 # linearizing there is no change. So compare the trees.
270 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
271 echo "skip empty commit $_dep";
273 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
274 git update-ref HEAD
$newcommit $head
275 echo "exported commit $_dep";
282 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
284 die
"no target branch specified"
285 ! ref_exists
"$output" ||
286 die
"target branch '$output' already exists; first run: git branch -D $output"
288 elif [ "$driver" = "quilt" ]; then
290 die
"no target directory specified"
291 [ ! -e "$output" ] ||
292 die
"target directory already exists: $output"
300 # FIXME should we abort on missing dependency?
301 [ -z "$_dep_missing" ] ||
return 0
303 [ -z "$_dep_is_tgish" ] ||
! branch_annihilated
"$_dep" ||
return 0
305 case $_dep in refs
/remotes
/*) return;; esac
306 branch_needs_update
>/dev
/null
308 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
313 # Call driver on all the branches - this will happen
314 # in topological order.
315 if "$allbranches" ; then
317 non_annihilated_branches |
321 elif [ -z "$branches" ]; then
322 recurse_deps driver
"$name"
323 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
325 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
329 name
="$(echo "$branches" | sed 's/.*,//')"
333 if [ "$driver" = "collapse" ]; then
334 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
336 depcount
="$(cat "$playground/^ticker
" | wc -l)"
337 echo "Exported topic branch $name (total $depcount topics) to branch $output"
339 elif [ "$driver" = "quilt" ]; then
340 depcount
="$(cat "$output/series
" | wc -l)"
341 echo "Exported topic branch $name (total $depcount topics) to directory $output"
343 elif [ "$driver" = "linearize" ]; then
344 git checkout
-q -b $output
347 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
348 echo "Warning: Exported result doesn't match";
349 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")";
350 #git diff $head HEAD;