2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
22 branches
="$1"; shift;;
30 if [ "$val" = "--strip" ]; then
33 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
37 die
"invalid parameter $arg"
46 echo "Usage: tg [...] export ([--collapse] <newbranch> | [-b <branch1>...] --quilt <directory> | --linearize <newbranch>)" >&2
49 [ -z "$output" ] || die
"output already specified ($output)"
56 [ -z "$branches" -o "$driver" = "quilt" ] ||
57 die
"-b works only with the quilt driver"
59 [ "$driver" = "quilt" ] ||
! "$numbered" ||
60 die
"--numbered works only with the quilt driver";
62 [ "$driver" = "quilt" ] ||
! "$flatten" ||
63 die
"--flatten works only with the quilt driver"
65 [ "$driver" = "quilt" ] ||
! "$strip" ||
66 die
"--strip works only with the quilt driver"
68 if [ -z "$branches" ]; then
69 # this check is only needed when no branches have been passed
70 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
71 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
72 die
"not on a TopGit-controlled branch"
76 playground
="$(get_temp tg-export -d)"
87 # Get commit message and authorship information
88 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
90 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
91 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
92 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
93 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
95 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
96 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
97 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
99 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
101 git commit-tree
"$tree" -p "$parent"
104 # collapsed_commit NAME
105 # Produce a collapsed commit of branch NAME.
110 rm -f "$playground/^pre" "$playground/^post"
114 parent
="$(cut -f 1 "$playground/$name^parents
" | \
115 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)"
116 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
117 # Produce a merge commit first
119 echo "TopGit-driven merge of branches
:"
121 cut -f 2 "$playground/$name^parents
"
122 } | git commit-tree "$
(pretty_tree
"$name" -b)" \
123 $(for p in $parent; do echo -p $p; done))"
126 if branch_empty
"$name"; then
129 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
132 echo "$name" >>"$playground/^ticker"
136 # This will collapse a single branch, using information about
137 # previously collapsed branches stored in $playground.
140 if [ -s "$playground/$_dep" ]; then
141 # We've already seen this dep
142 commit
="$(cat "$playground/$_dep")"
144 elif [ -z "$_dep_is_tgish" ]; then
145 # This dep is not for rewrite
146 commit
="$(git rev-parse --verify "$_dep")"
149 # First time hitting this dep; the common case
150 echo "Collapsing $_dep"
151 commit
="$(collapsed_commit "$_dep")"
152 mkdir
-p "$playground/$(dirname "$_dep")"
153 echo "$commit" >"$playground/$_dep"
156 # Propagate our work through the dependency chain
157 mkdir
-p "$playground/$(dirname "$_name")"
158 echo "$commit $_dep" >>"$playground/$_name^parents"
166 if [ -z "$_dep_is_tgish" ]; then
167 # This dep is not for rewrite
175 while [ "$i" -gt 0 ]; do
176 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
177 _dep_tmp
=${_dep_tmp#*/}
182 bn
="$(basename "$_dep_tmp.
diff")"
183 dn
="$(dirname "$_dep_tmp.
diff")/"
184 [ "x$dn" = "x./" ] && dn
=""
186 if "$flatten" && [ "$dn" ]; then
187 bn
="$(echo "$_dep_tmp.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
193 if [ -e "$playground/$_dep" ]; then
194 # We've already seen this dep
198 mkdir
-p "$playground/$(dirname "$_dep")";
199 touch "$playground/$_dep";
201 if branch_empty
"$_dep"; then
202 echo "Skip empty patch $_dep";
205 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
206 bn
="$(printf "%04u-$bn" $number)";
207 echo "$number" >"$playground/^number";
210 echo "Exporting $_dep"
211 mkdir
-p "$output/$dn";
212 $tg patch "$_dep" >"$output/$dn$bn"
213 echo "$dn$bn -p1" >>"$output/series"
219 if test ! -f "$playground/^BASE"; then
220 head="$(git rev-parse --verify "$_dep")"
221 echo "$head" > "$playground/^BASE"
222 git checkout
-q "$head"
226 head=$
(git rev-parse
--verify HEAD
)
228 if [ -z "$_dep_is_tgish" ]; then
229 # merge in $_dep unless already included
230 rev="$(git rev-parse --verify "$_dep")";
231 common
="$(git merge-base --all HEAD "$_dep")";
232 if test "$rev" = "$common"; then
233 # already included, just skip
238 git merge
-s recursive
"$_dep" || retmerge
="$?";
239 if test "x$retmerge" != "x0"; then
240 echo fix up the merge
, commit and
then exit;
248 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "refs
/heads
/$_dep")" || retmerge
="$?";
250 if test "x$retmerge" != "x0"; then
252 echo "fix up the merge and update the index. Don't commit!"
257 result_tree
=$
(git write-tree
)
258 # testing branch_empty might not always give the right answer.
259 # It can happen that the patch is non-empty but still after
260 # linearizing there is no change. So compare the trees.
261 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
262 echo "skip empty commit $_dep";
264 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
265 git update-ref HEAD
$newcommit $head
266 echo "exported commit $_dep";
273 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
275 die
"no target branch specified"
276 ! ref_exists
"$output" ||
277 die
"target branch '$output' already exists; first run: git branch -D $output"
279 elif [ "$driver" = "quilt" ]; then
281 die
"no target directory specified"
282 [ ! -e "$output" ] ||
283 die
"target directory already exists: $output"
291 # FIXME should we abort on missing dependency?
292 [ -z "$_dep_missing" ] ||
return 0
294 case $_dep in refs
/remotes
/*) return;; esac
295 branch_needs_update
>/dev
/null
297 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
302 # Call driver on all the branches - this will happen
303 # in topological order.
304 if [ -z "$branches" ]; then
305 recurse_deps driver
"$name"
306 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
308 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
312 name
="$(echo "$branches" | sed 's/.*,//')"
316 if [ "$driver" = "collapse" ]; then
317 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
319 depcount
="$(cat "$playground/^ticker
" | wc -l)"
320 echo "Exported topic branch $name (total $depcount topics) to branch $output"
322 elif [ "$driver" = "quilt" ]; then
323 depcount
="$(cat "$output/series
" | wc -l)"
324 echo "Exported topic branch $name (total $depcount topics) to directory $output"
326 elif [ "$driver" = "linearize" ]; then
327 git checkout
-q -b $output
330 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
331 echo "Warning: Exported result doesn't match";
332 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")";
333 #git diff $head HEAD;