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="$(mktemp -d -t tg-export.XXXXXX)"
67 trap 'rm -rf "$playground"' EXIT
73 # Output tree ID of a cleaned-up tree without tg's artifacts.
76 (export GIT_INDEX_FILE="$playground/^index"
78 git update-index --force-remove ".topmsg" ".topdeps"
88 # Get commit message and authorship information
89 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
91 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
92 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
93 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
94 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
96 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
97 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
98 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
100 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
102 git commit-tree "$tree" -p "$parent"
105 # collapsed_commit NAME
106 # Produce a collapsed commit of branch NAME.
109 local name; name="$1"
111 rm -f "$playground/^pre" "$playground/^post"
115 parent="$(cut -f 1 "$playground/$name^parents")"
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 "refs/top-bases/$name")" \
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
172 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
175 bn="$(basename "$_dep.diff")";
176 dn="$(dirname "$_dep.diff")/";
177 if [ "x$dn" = "x./" ]; then
182 if [ -e "$playground/$_dep" ]; then
183 # We've already seen this dep
187 mkdir -p "$playground/$(dirname "$_dep")";
188 touch "$playground/$_dep";
190 if branch_empty "$_dep"; then
191 echo "Skip empty patch $_dep";
194 number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
196 echo "$number" >"$playground/^number";
199 echo "Exporting $_dep"
200 mkdir -p "$output/$dn";
201 $tg patch "$_dep" >"$output/$dn$bn"
202 echo "$dn$bn -p1" >>"$output/series"
208 if test ! -f "$playground/^BASE"; then
209 head="$(git rev-parse --verify "$_dep")"
210 echo "$head" > "$playground/^BASE"
211 git checkout -q "$head"
215 head=$(git rev-parse --verify HEAD)
217 if [ -z "$_dep_is_tgish" ]; then
218 # merge in $_dep unless already included
219 rev="$(git rev-parse --verify "$_dep")";
220 common="$(git merge-base --all HEAD "$_dep")";
221 if test "$rev" = "$common"; then
222 # already included, just skip
225 git merge -s recursive "$_dep";
227 if test "x$retmerge" != "x0"; then
228 echo fix up the merge, commit and then exit;
234 git merge-recursive "$(pretty_tree "refs/top-bases/$_dep")" -- HEAD "$(pretty_tree "refs/heads/$_dep")";
237 if test "x$retmerge" != "x0"; then
238 echo "fix up the merge and update the index. Don't commit!"
243 result_tree=$(git write-tree)
244 # testing branch_empty might not always give the right answer.
245 # It can happen that the patch is non-empty but still after
246 # linearizing there is no change. So compare the trees.
247 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
248 echo "skip empty commit $_dep";
250 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
251 git update-ref HEAD $newcommit $head
252 echo "exported commit $_dep";
259 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
261 die "no target branch specified"
262 ! ref_exists "$output" ||
263 die "target branch '$output' already exists; first run: git branch -D $output"
265 elif [ "$driver" = "quilt" ]; then
267 die "no target directory specified"
268 [ ! -e "$output" ] ||
269 die "target directory already exists: $output"
277 case $_dep in refs/remotes/*) return;; esac
278 branch_needs_update >/dev/null
280 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
285 # Call driver on all the branches - this will happen
286 # in topological order.
287 echo "$branches" | tr , '\n' | while read name; do
288 recurse_deps driver "$name"
289 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
292 if [ "$driver" = "collapse" ]; then
293 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
295 depcount="$(cat "$playground/^ticker" | wc -l)"
296 echo "Exported topic branch $name (total $depcount topics) to branch $output"
298 elif [ "$driver" = "quilt" ]; then
299 depcount="$(cat "$output/series" | wc -l)"
300 echo "Exported topic branch $name (total $depcount topics) to directory $output"
302 elif [ "$driver" = "linearize" ]; then
303 git checkout -q -b $output
306 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
307 echo "Warning: Exported result doesn't match";
308 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
309 #git diff $head HEAD;