2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
18 branches="$1"; shift;;
24 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
27 [ -z "$output" ] || die "output already specified ($output)"
34 [ -z "$branches" -o "$driver" = "quilt" ] ||
35 die "-b works only with the quilt driver"
37 if [ -z "$branches" ]; then
38 # this check is only needed when no branches have been passed
39 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
40 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
41 die "not on a TopGit-controlled branch"
44 name="${branches##*,}" # the last of the comma-separated items
46 # $name holds the current branch
47 # $branches holds the comma-separated list of branches
48 # $name is equal to the last of the list of branches
51 playground="$(mktemp -d -t tg-export.XXXXXX)"
52 trap 'rm -rf "$playground"' EXIT
58 # Output tree ID of a cleaned-up tree without tg's artifacts.
61 (export GIT_INDEX_FILE="$playground/^index"
63 git update-index --force-remove ".topmsg" ".topdeps"
67 # collapsed_commit NAME
68 # Produce a collapsed commit of branch NAME.
73 rm -f "$playground/^pre" "$playground/^post"
76 # Get commit message and authorship information
77 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
79 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
80 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
81 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
82 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
84 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
85 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
86 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
89 parent="$(cut -f 1 "$playground/$name^parents")"
90 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
91 # Produce a merge commit first
93 echo "TopGit-driven merge of branches:"
95 cut -f 2 "$playground/$name^parents"
96 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
97 $(for p in $parent; do echo -p $p; done))"
100 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
102 git commit-tree "$(pretty_tree "$name")" -p "$parent"
104 echo "$name" >>"$playground/^ticker"
108 # This will collapse a single branch, using information about
109 # previously collapsed branches stored in $playground.
112 if [ -s "$playground/$_dep" ]; then
113 # We've already seen this dep
114 commit="$(cat "$playground/$_dep")"
116 elif [ -z "$_dep_is_tgish" ]; then
117 # This dep is not for rewrite
118 commit="$(git rev-parse --verify "$_dep")"
121 # First time hitting this dep; the common case
122 echo "Collapsing $_dep"
123 commit="$(collapsed_commit "$_dep")"
124 mkdir -p "$playground/$(dirname "$_dep")"
125 echo "$commit" >"$playground/$_dep"
128 # Propagate our work through the dependency chain
129 mkdir -p "$playground/$(dirname "$_name")"
130 echo "$commit $_dep" >>"$playground/$_name^parents"
138 if [ -z "$_dep_is_tgish" ]; then
139 # This dep is not for rewrite
143 filename="$output/$_dep.diff"
144 if [ -e "$filename" ]; then
145 # We've already seen this dep
149 echo "Exporting $_dep"
150 mkdir -p "$(dirname "$filename")"
151 $tg patch "$_dep" >"$filename"
152 echo "$_dep.diff -p1" >>"$output/series"
158 if [ "$driver" = "collapse" ]; then
160 die "no target branch specified"
161 ! ref_exists "$output" ||
162 die "target branch '$output' already exists; first run: git branch -D $output"
164 elif [ "$driver" = "quilt" ]; then
166 die "no target directory specified"
167 [ ! -e "$output" ] ||
168 die "target directory already exists: $output"
176 case $_dep in refs/remotes/*) return;; esac
177 branch_needs_update >/dev/null
179 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
184 # Call driver on all the branches - this will happen
185 # in topological order.
186 echo "$branches" | tr , '\n' | while read name; do
187 recurse_deps driver "$name"
188 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
191 if [ "$driver" = "collapse" ]; then
192 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
194 depcount="$(cat "$playground/^ticker" | wc -l)"
195 echo "Exported topic branch $name (total $depcount topics) to branch $output"
197 elif [ "$driver" = "quilt" ]; then
198 depcount="$(cat "$output/series" | wc -l)"
199 echo "Exported topic branch $name (total $depcount topics) to directory $output"