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"
59 name="${branches##*,}" # the last of the comma-separated items
61 # $name holds the current branch
62 # $branches holds the comma-separated list of branches
63 # $name is equal to the last of the list of branches
66 playground="$(get_temp tg-export -d)"
77 # Get commit message and authorship information
78 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
80 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
81 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
82 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
83 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
85 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
86 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
87 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
89 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
91 git commit-tree "$tree" -p "$parent"
94 # collapsed_commit NAME
95 # Produce a collapsed commit of branch NAME.
100 rm -f "$playground/^pre" "$playground/^post"
104 parent="$(cut -f 1 "$playground/$name^parents")"
105 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
106 # Produce a merge commit first
108 echo "TopGit-driven merge of branches:"
110 cut -f 2 "$playground/$name^parents"
111 } | git commit-tree "$(pretty_tree "$name" -b)" \
112 $(for p in $parent; do echo -p $p; done))"
115 if branch_empty "$name"; then
118 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
121 echo "$name" >>"$playground/^ticker"
125 # This will collapse a single branch, using information about
126 # previously collapsed branches stored in $playground.
129 if [ -s "$playground/$_dep" ]; then
130 # We've already seen this dep
131 commit="$(cat "$playground/$_dep")"
133 elif [ -z "$_dep_is_tgish" ]; then
134 # This dep is not for rewrite
135 commit="$(git rev-parse --verify "$_dep")"
138 # First time hitting this dep; the common case
139 echo "Collapsing $_dep"
140 commit="$(collapsed_commit "$_dep")"
141 mkdir -p "$playground/$(dirname "$_dep")"
142 echo "$commit" >"$playground/$_dep"
145 # Propagate our work through the dependency chain
146 mkdir -p "$playground/$(dirname "$_name")"
147 echo "$commit $_dep" >>"$playground/$_name^parents"
155 if [ -z "$_dep_is_tgish" ]; then
156 # This dep is not for rewrite
161 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
164 bn="$(basename "$_dep.diff")";
165 dn="$(dirname "$_dep.diff")/";
166 if [ "x$dn" = "x./" ]; then
171 if [ -e "$playground/$_dep" ]; then
172 # We've already seen this dep
176 mkdir -p "$playground/$(dirname "$_dep")";
177 touch "$playground/$_dep";
179 if branch_empty "$_dep"; then
180 echo "Skip empty patch $_dep";
183 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
184 bn="$(printf "%04u-$bn" $number)";
185 echo "$number" >"$playground/^number";
188 echo "Exporting $_dep"
189 mkdir -p "$output/$dn";
190 $tg patch "$_dep" >"$output/$dn$bn"
191 echo "$dn$bn -p1" >>"$output/series"
197 if test ! -f "$playground/^BASE"; then
198 head="$(git rev-parse --verify "$_dep")"
199 echo "$head" > "$playground/^BASE"
200 git checkout -q "$head"
204 head=$(git rev-parse --verify HEAD)
206 if [ -z "$_dep_is_tgish" ]; then
207 # merge in $_dep unless already included
208 rev="$(git rev-parse --verify "$_dep")";
209 common="$(git merge-base --all HEAD "$_dep")";
210 if test "$rev" = "$common"; then
211 # already included, just skip
216 git merge -s recursive "$_dep" || retmerge="$?";
217 if test "x$retmerge" != "x0"; then
218 echo fix up the merge, commit and then exit;
226 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
228 if test "x$retmerge" != "x0"; then
230 echo "fix up the merge and update the index. Don't commit!"
235 result_tree=$(git write-tree)
236 # testing branch_empty might not always give the right answer.
237 # It can happen that the patch is non-empty but still after
238 # linearizing there is no change. So compare the trees.
239 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
240 echo "skip empty commit $_dep";
242 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
243 git update-ref HEAD $newcommit $head
244 echo "exported commit $_dep";
251 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
253 die "no target branch specified"
254 ! ref_exists "$output" ||
255 die "target branch '$output' already exists; first run: git branch -D $output"
257 elif [ "$driver" = "quilt" ]; then
259 die "no target directory specified"
260 [ ! -e "$output" ] ||
261 die "target directory already exists: $output"
269 case $_dep in refs/remotes/*) return;; esac
270 branch_needs_update >/dev/null
272 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
277 # Call driver on all the branches - this will happen
278 # in topological order.
279 echo "$branches" | tr , '\n' | while read name; do
280 recurse_deps driver "$name"
281 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
284 if [ "$driver" = "collapse" ]; then
285 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
287 depcount="$(cat "$playground/^ticker" | wc -l)"
288 echo "Exported topic branch $name (total $depcount topics) to branch $output"
290 elif [ "$driver" = "quilt" ]; then
291 depcount="$(cat "$output/series" | wc -l)"
292 echo "Exported topic branch $name (total $depcount topics) to directory $output"
294 elif [ "$driver" = "linearize" ]; then
295 git checkout -q -b $output
298 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
299 echo "Warning: Exported result doesn't match";
300 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
301 #git diff $head HEAD;