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"
74 # collapsed_commit NAME
75 # Produce a collapsed commit of branch NAME.
80 rm -f "$playground/^pre" "$playground/^post"
83 # Get commit message and authorship information
84 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
86 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
87 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
88 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
89 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
91 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
92 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
93 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
96 parent
="$(cut -f 1 "$playground/$name^parents
")"
97 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
98 # Produce a merge commit first
100 echo "TopGit-driven merge of branches
:"
102 cut -f 2 "$playground/$name^parents
"
103 } | git commit-tree "$
(pretty_tree
"refs/top-bases/$name")" \
104 $(for p in $parent; do echo -p $p; done))"
107 if branch_empty
"$name"; then
110 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
112 git commit-tree
"$(pretty_tree "$name")" -p "$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
="$(printf "%04u" $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
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"
192 if [ "$driver" = "collapse" ]; then
194 die
"no target branch specified"
195 ! ref_exists
"$output" ||
196 die
"target branch '$output' already exists; first run: git branch -D $output"
198 elif [ "$driver" = "quilt" ]; then
200 die
"no target directory specified"
201 [ ! -e "$output" ] ||
202 die
"target directory already exists: $output"
210 case $_dep in refs
/remotes
/*) return;; esac
211 branch_needs_update
>/dev
/null
213 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
218 # Call driver on all the branches - this will happen
219 # in topological order.
220 if [ -z "$branches" ]; then
221 recurse_deps driver
"$name"
222 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
224 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
228 name
="$(echo "$branches" | sed 's/.*,//')"
232 if [ "$driver" = "collapse" ]; then
233 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
235 depcount
="$(cat "$playground/^ticker
" | wc -l)"
236 echo "Exported topic branch $name (total $depcount topics) to branch $output"
238 elif [ "$driver" = "quilt" ]; then
239 depcount
="$(cat "$output/series
" | wc -l)"
240 echo "Exported topic branch $name (total $depcount topics) to directory $output"