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 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
100 # Produce a merge commit first
102 echo "TopGit-driven merge of branches:"
104 cut -f 2 "$playground/$name^parents"
105 } | git commit-tree "$(pretty_tree "$name" -b)" \
106 $(for p in $parent; do echo -p $p; done))"
109 if branch_empty "$name"; then
112 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
115 echo "$name" >>"$playground/^ticker"
119 # This will collapse a single branch, using information about
120 # previously collapsed branches stored in $playground.
123 if [ -s "$playground/$_dep" ]; then
124 # We've already seen this dep
125 commit="$(cat "$playground/$_dep")"
127 elif [ -z "$_dep_is_tgish" ]; then
128 # This dep is not for rewrite
129 commit="$(git rev-parse --verify "$_dep")"
132 # First time hitting this dep; the common case
133 echo "Collapsing $_dep"
134 commit="$(collapsed_commit "$_dep")"
135 mkdir -p "$playground/$(dirname "$_dep")"
136 echo "$commit" >"$playground/$_dep"
139 # Propagate our work through the dependency chain
140 mkdir -p "$playground/$(dirname "$_name")"
141 echo "$commit $_dep" >>"$playground/$_name^parents"
149 if [ -z "$_dep_is_tgish" ]; then
150 # This dep is not for rewrite
155 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
158 bn="$(basename "$_dep.diff")";
159 dn="$(dirname "$_dep.diff")/";
160 if [ "x$dn" = "x./" ]; then
165 if [ -e "$playground/$_dep" ]; then
166 # We've already seen this dep
170 mkdir -p "$playground/$(dirname "$_dep")";
171 touch "$playground/$_dep";
173 if branch_empty "$_dep"; then
174 echo "Skip empty patch $_dep";
177 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
178 bn="$(printf "%04u-$bn" $number)";
179 echo "$number" >"$playground/^number";
182 echo "Exporting $_dep"
183 mkdir -p "$output/$dn";
184 $tg patch "$_dep" >"$output/$dn$bn"
185 echo "$dn$bn -p1" >>"$output/series"
191 if test ! -f "$playground/^BASE"; then
192 head="$(git rev-parse --verify "$_dep")"
193 echo "$head" > "$playground/^BASE"
194 git checkout -q "$head"
198 head=$(git rev-parse --verify HEAD)
200 if [ -z "$_dep_is_tgish" ]; then
201 # merge in $_dep unless already included
202 rev="$(git rev-parse --verify "$_dep")";
203 common="$(git merge-base --all HEAD "$_dep")";
204 if test "$rev" = "$common"; then
205 # already included, just skip
210 git merge -s recursive "$_dep" || retmerge="$?";
211 if test "x$retmerge" != "x0"; then
212 echo fix up the merge, commit and then exit;
220 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
222 if test "x$retmerge" != "x0"; then
224 echo "fix up the merge and update the index. Don't commit!"
229 result_tree=$(git write-tree)
230 # testing branch_empty might not always give the right answer.
231 # It can happen that the patch is non-empty but still after
232 # linearizing there is no change. So compare the trees.
233 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
234 echo "skip empty commit $_dep";
236 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
237 git update-ref HEAD $newcommit $head
238 echo "exported commit $_dep";
245 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
247 die "no target branch specified"
248 ! ref_exists "$output" ||
249 die "target branch '$output' already exists; first run: git branch -D $output"
251 elif [ "$driver" = "quilt" ]; then
253 die "no target directory specified"
254 [ ! -e "$output" ] ||
255 die "target directory already exists: $output"
263 case $_dep in refs/remotes/*) return;; esac
264 branch_needs_update >/dev/null
266 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
271 # Call driver on all the branches - this will happen
272 # in topological order.
273 if [ -z "$branches" ]; then
274 recurse_deps driver "$name"
275 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
277 echo "$branches" | tr ',' '\n' | while read _dep; do
281 name="$(echo "$branches" | sed 's/.*,//')"
285 if [ "$driver" = "collapse" ]; then
286 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
288 depcount="$(cat "$playground/^ticker" | wc -l)"
289 echo "Exported topic branch $name (total $depcount topics) to branch $output"
291 elif [ "$driver" = "quilt" ]; then
292 depcount="$(cat "$output/series" | wc -l)"
293 echo "Exported topic branch $name (total $depcount topics) to directory $output"
295 elif [ "$driver" = "linearize" ]; then
296 git checkout -q -b $output
299 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
300 echo "Warning: Exported result doesn't match";
301 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
302 #git diff $head HEAD;