2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
20 branches
="$1"; shift;;
31 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
34 [ -z "$output" ] || die
"output already specified ($output)"
41 [ -z "$branches" -o "$driver" = "quilt" ] ||
42 die
"-b works only with the quilt driver"
44 [ "$driver" = "quilt" ] ||
! "$numbered" ||
45 die
"--numbered works only with the quilt driver";
47 [ "$driver" = "quilt" ] ||
! "$flatten" ||
48 die
"--flatten works only with the quilt driver"
50 if [ -z "$branches" ]; then
51 # this check is only needed when no branches have been passed
52 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
53 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
54 die
"not on a TopGit-controlled branch"
58 playground
="$(mktemp -d -t tg-export.XXXXXX)"
59 trap 'rm -rf "$playground"' EXIT
65 # Output tree ID of a cleaned-up tree without tg's artifacts.
68 (export GIT_INDEX_FILE
="$playground/^index"
70 git update-index
--force-remove ".topmsg" ".topdeps"
80 # Get commit message and authorship information
81 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
83 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
84 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
85 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
86 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
88 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
89 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
90 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
92 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
94 git commit-tree
"$tree" -p "$parent"
97 # collapsed_commit NAME
98 # Produce a collapsed commit of branch NAME.
103 rm -f "$playground/^pre" "$playground/^post"
107 parent
="$(cut -f 1 "$playground/$name^parents
")"
108 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
109 # Produce a merge commit first
111 echo "TopGit-driven merge of branches
:"
113 cut -f 2 "$playground/$name^parents
"
114 } | git commit-tree "$
(pretty_tree
"refs/top-bases/$name")" \
115 $(for p in $parent; do echo -p $p; done))"
118 if branch_empty
"$name"; then
121 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
124 echo "$name" >>"$playground/^ticker"
128 # This will collapse a single branch, using information about
129 # previously collapsed branches stored in $playground.
132 if [ -s "$playground/$_dep" ]; then
133 # We've already seen this dep
134 commit
="$(cat "$playground/$_dep")"
136 elif [ -z "$_dep_is_tgish" ]; then
137 # This dep is not for rewrite
138 commit
="$(git rev-parse --verify "$_dep")"
141 # First time hitting this dep; the common case
142 echo "Collapsing $_dep"
143 commit
="$(collapsed_commit "$_dep")"
144 mkdir
-p "$playground/$(dirname "$_dep")"
145 echo "$commit" >"$playground/$_dep"
148 # Propagate our work through the dependency chain
149 mkdir
-p "$playground/$(dirname "$_name")"
150 echo "$commit $_dep" >>"$playground/$_name^parents"
158 if [ -z "$_dep_is_tgish" ]; then
159 # This dep is not for rewrite
164 bn
="$(echo "$_dep.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
167 bn
="$(basename "$_dep.
diff")";
168 dn
="$(dirname "$_dep.
diff")/";
169 if [ "x$dn" = "x./" ]; then
174 if [ -e "$playground/$_dep" ]; then
175 # We've already seen this dep
179 mkdir
-p "$playground/$(dirname "$_dep")";
180 touch "$playground/$_dep";
182 if branch_empty
"$_dep"; then
183 echo "Skip empty patch $_dep";
186 number
="$(printf "%04u" $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
188 echo "$number" >"$playground/^number";
191 echo "Exporting $_dep"
192 mkdir
-p "$output/$dn";
193 $tg patch "$_dep" >"$output/$dn$bn"
194 echo "$dn$bn -p1" >>"$output/series"
201 if [ "$driver" = "collapse" ]; then
203 die
"no target branch specified"
204 ! ref_exists
"$output" ||
205 die
"target branch '$output' already exists; first run: git branch -D $output"
207 elif [ "$driver" = "quilt" ]; then
209 die
"no target directory specified"
210 [ ! -e "$output" ] ||
211 die
"target directory already exists: $output"
219 case $_dep in refs
/remotes
/*) return;; esac
220 branch_needs_update
>/dev
/null
222 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
227 # Call driver on all the branches - this will happen
228 # in topological order.
229 if [ -z "$branches" ]; then
230 recurse_deps driver
"$name"
231 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
233 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
237 name
="$(echo "$branches" | sed 's/.*,//')"
241 if [ "$driver" = "collapse" ]; then
242 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
244 depcount
="$(cat "$playground/^ticker
" | wc -l)"
245 echo "Exported topic branch $name (total $depcount topics) to branch $output"
247 elif [ "$driver" = "quilt" ]; then
248 depcount
="$(cat "$output/series
" | wc -l)"
249 echo "Exported topic branch $name (total $depcount topics) to directory $output"