2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
20 branches
="$1"; shift;;
33 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY | --linearize NEWBRANCH)" >&2
36 [ -z "$output" ] || die
"output already specified ($output)"
43 [ -z "$branches" -o "$driver" = "quilt" ] ||
44 die
"-b works only with the quilt driver"
46 [ "$driver" = "quilt" ] ||
! "$numbered" ||
47 die
"--numbered works only with the quilt driver";
49 [ "$driver" = "quilt" ] ||
! "$flatten" ||
50 die
"--flatten works only with the quilt driver"
52 if [ -z "$branches" ]; then
53 # this check is only needed when no branches have been passed
54 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
55 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
56 die
"not on a TopGit-controlled branch"
60 playground
="$(get_temp tg-export -d)"
71 # Get commit message and authorship information
72 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
74 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
75 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
76 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
77 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
79 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
80 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
81 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
83 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
85 git commit-tree
"$tree" -p "$parent"
88 # collapsed_commit NAME
89 # Produce a collapsed commit of branch NAME.
94 rm -f "$playground/^pre" "$playground/^post"
98 parent
="$(cut -f 1 "$playground/$name^parents
" | \
99 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)"
100 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
101 # Produce a merge commit first
103 echo "TopGit-driven merge of branches
:"
105 cut -f 2 "$playground/$name^parents
"
106 } | git commit-tree "$
(pretty_tree
"$name" -b)" \
107 $(for p in $parent; do echo -p $p; done))"
110 if branch_empty
"$name"; then
113 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
116 echo "$name" >>"$playground/^ticker"
120 # This will collapse a single branch, using information about
121 # previously collapsed branches stored in $playground.
124 if [ -s "$playground/$_dep" ]; then
125 # We've already seen this dep
126 commit
="$(cat "$playground/$_dep")"
128 elif [ -z "$_dep_is_tgish" ]; then
129 # This dep is not for rewrite
130 commit
="$(git rev-parse --verify "$_dep")"
133 # First time hitting this dep; the common case
134 echo "Collapsing $_dep"
135 commit
="$(collapsed_commit "$_dep")"
136 mkdir
-p "$playground/$(dirname "$_dep")"
137 echo "$commit" >"$playground/$_dep"
140 # Propagate our work through the dependency chain
141 mkdir
-p "$playground/$(dirname "$_name")"
142 echo "$commit $_dep" >>"$playground/$_name^parents"
150 if [ -z "$_dep_is_tgish" ]; then
151 # This dep is not for rewrite
156 bn
="$(echo "$_dep.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
159 bn
="$(basename "$_dep.
diff")";
160 dn
="$(dirname "$_dep.
diff")/";
161 if [ "x$dn" = "x./" ]; then
166 if [ -e "$playground/$_dep" ]; then
167 # We've already seen this dep
171 mkdir
-p "$playground/$(dirname "$_dep")";
172 touch "$playground/$_dep";
174 if branch_empty
"$_dep"; then
175 echo "Skip empty patch $_dep";
178 number
="$(echo $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
179 bn
="$(printf "%04u-$bn" $number)";
180 echo "$number" >"$playground/^number";
183 echo "Exporting $_dep"
184 mkdir
-p "$output/$dn";
185 $tg patch "$_dep" >"$output/$dn$bn"
186 echo "$dn$bn -p1" >>"$output/series"
192 if test ! -f "$playground/^BASE"; then
193 head="$(git rev-parse --verify "$_dep")"
194 echo "$head" > "$playground/^BASE"
195 git checkout
-q "$head"
199 head=$
(git rev-parse
--verify HEAD
)
201 if [ -z "$_dep_is_tgish" ]; then
202 # merge in $_dep unless already included
203 rev="$(git rev-parse --verify "$_dep")";
204 common
="$(git merge-base --all HEAD "$_dep")";
205 if test "$rev" = "$common"; then
206 # already included, just skip
211 git merge
-s recursive
"$_dep" || retmerge
="$?";
212 if test "x$retmerge" != "x0"; then
213 echo fix up the merge
, commit and
then exit;
221 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "refs
/heads
/$_dep")" || retmerge
="$?";
223 if test "x$retmerge" != "x0"; then
225 echo "fix up the merge and update the index. Don't commit!"
230 result_tree
=$
(git write-tree
)
231 # testing branch_empty might not always give the right answer.
232 # It can happen that the patch is non-empty but still after
233 # linearizing there is no change. So compare the trees.
234 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
235 echo "skip empty commit $_dep";
237 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
238 git update-ref HEAD
$newcommit $head
239 echo "exported commit $_dep";
246 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
248 die
"no target branch specified"
249 ! ref_exists
"$output" ||
250 die
"target branch '$output' already exists; first run: git branch -D $output"
252 elif [ "$driver" = "quilt" ]; then
254 die
"no target directory specified"
255 [ ! -e "$output" ] ||
256 die
"target directory already exists: $output"
264 case $_dep in refs
/remotes
/*) return;; esac
265 branch_needs_update
>/dev
/null
267 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
272 # Call driver on all the branches - this will happen
273 # in topological order.
274 if [ -z "$branches" ]; then
275 recurse_deps driver
"$name"
276 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
278 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
282 name
="$(echo "$branches" | sed 's/.*,//')"
286 if [ "$driver" = "collapse" ]; then
287 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
289 depcount
="$(cat "$playground/^ticker
" | wc -l)"
290 echo "Exported topic branch $name (total $depcount topics) to branch $output"
292 elif [ "$driver" = "quilt" ]; then
293 depcount
="$(cat "$output/series
" | wc -l)"
294 echo "Exported topic branch $name (total $depcount topics) to directory $output"
296 elif [ "$driver" = "linearize" ]; then
297 git checkout
-q -b $output
300 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
301 echo "Warning: Exported result doesn't match";
302 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")";
303 #git diff $head HEAD;