2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
21 echo "Usage: tg export ([--collapse] NEWBRANCH | --quilt DIRECTORY)" >&2
24 [ -z "$output" ] || die "output already specified ($output)"
30 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
31 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
32 die "not on a TopGit-controlled branch"
35 playground="$(mktemp -d -t tg-export.XXXXXX)"
36 trap 'rm -rf "$playground"' EXIT
44 if [ -z "$GIT_AUTHOR_NAME" ] && echo "$1" | grep -q '^[^< ]'; then
45 export GIT_AUTHOR_NAME="$(echo "$1" | sed 's/ *<.*//')"
47 if [ -z "$GIT_AUTHOR_EMAIL" ] && echo "$1" | grep -q '<.*>'; then
48 export GIT_AUTHOR_EMAIL="$(echo "$1" | sed 's/.*<\(.*\)>.*/\1/')"
53 # Output tree ID of a cleaned-up tree without tg's artifacts.
56 (export GIT_INDEX_FILE="$playground/^index"
58 git update-index --force-remove ".topmsg" ".topdeps"
62 # collapsed_commit NAME
63 # Produce a collapsed commit of branch NAME.
68 rm -f "$playground/^pre" "$playground/^post"
71 # Get commit message and authorship information
72 git cat-file blob "$name:.topmsg" >"$playground/^msg"
74 if [ -z "$line" ]; then
76 cat >"$playground/^body"
80 From:*) load_author "${line#From: }";;
81 Subject:*) echo "${line#Subject: }" >>"$playground/^pre";;
82 *) echo "$line" >>"$playground/^post";;
84 done <"$playground/^msg"
87 parent="$(cut -f 1 "$playground/$name^parents")"
88 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
89 # Produce a merge commit first
91 echo "TopGit-driven merge of branches:"
93 cut -f 2 "$playground/$name^parents"
94 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
95 $(for p in $parent; do echo -p $p; done))"
99 if [ -s "$playground/^pre" ]; then
100 cat "$playground/^pre"
103 cat "$playground/^body"
104 [ ! -s "$playground/^post" ] || cat "$playground/^post"
105 } | git commit-tree "$(pretty_tree "$name")" -p "$parent"
107 echo "$name" >>"$playground/^ticker"
111 # This will collapse a single branch, using information about
112 # previously collapsed branches stored in $playground.
115 if [ -s "$playground/$_dep" ]; then
116 # We've already seen this dep
117 commit="$(cat "$playground/$_dep")"
119 elif [ -z "$_dep_is_tgish" ]; then
120 # This dep is not for rewrite
121 commit="$(git rev-parse --verify "$_dep")"
124 # First time hitting this dep; the common case
125 echo "Collapsing $_dep"
126 commit="$(collapsed_commit "$_dep")"
127 mkdir -p "$playground/$(dirname "$_dep")"
128 echo "$commit" >"$playground/$_dep"
131 # Propagate our work through the dependency chain
132 mkdir -p "$playground/$(dirname "$_name")"
133 echo "$commit $_dep" >>"$playground/$_name^parents"
141 if [ -z "$_dep_is_tgish" ]; then
142 # This dep is not for rewrite
146 filename="$output/$_dep.diff"
147 if [ -e "$filename" ]; then
148 # We've already seen this dep
152 echo "Exporting $_dep"
153 mkdir -p "$(dirname "$filename")"
154 tg patch "$_dep" >"$filename"
155 echo "$_dep.diff -p1" >>"$output/series"
161 if [ "$driver" = "collapse" ]; then
163 die "no target branch specified"
164 ! git rev-parse --verify "$output" >/dev/null 2>&1 ||
165 die "target branch '$output' already exists; first run: git branch -D $output"
167 elif [ "$driver" = "quilt" ]; then
169 die "no target directory specified"
170 [ ! -e "$output" ] ||
171 die "target directory already exists: $output"
179 branch_needs_update >/dev/null
181 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
186 # Call driver on all the branches - this will happen
187 # in topological order.
188 recurse_deps driver "$name"
189 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
192 if [ "$driver" = "collapse" ]; then
193 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
195 depcount="$(cat "$playground/^ticker" | wc -l)"
196 echo "Exported topic branch $name (total $depcount topics) to branch $output"
198 elif [ "$driver" = "quilt" ]; then
199 depcount="$(cat "$output/series" | wc -l)"
200 echo "Exported topic branch $name (total $depcount topics) to directory $output"