2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
19 branches
="$1"; shift;;
27 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
30 [ -z "$output" ] || die
"output already specified ($output)"
37 [ -z "$branches" -o "$driver" = "quilt" ] ||
38 die
"-b works only with the quilt driver"
40 [ "$driver" = "quilt" ] ||
! "$flatten" ||
41 die
"--flatten works only with the quilt driver"
43 if [ -z "$branches" ]; then
44 # this check is only needed when no branches have been passed
45 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
46 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
47 die
"not on a TopGit-controlled branch"
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 if branch_empty
"$name"; then
103 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
105 git commit-tree
"$(pretty_tree "$name")" -p "$parent"
108 echo "$name" >>"$playground/^ticker"
112 # This will collapse a single branch, using information about
113 # previously collapsed branches stored in $playground.
116 if [ -s "$playground/$_dep" ]; then
117 # We've already seen this dep
118 commit
="$(cat "$playground/$_dep")"
120 elif [ -z "$_dep_is_tgish" ]; then
121 # This dep is not for rewrite
122 commit
="$(git rev-parse --verify "$_dep")"
125 # First time hitting this dep; the common case
126 echo "Collapsing $_dep"
127 commit
="$(collapsed_commit "$_dep")"
128 mkdir
-p "$playground/$(dirname "$_dep")"
129 echo "$commit" >"$playground/$_dep"
132 # Propagate our work through the dependency chain
133 mkdir
-p "$playground/$(dirname "$_name")"
134 echo "$commit $_dep" >>"$playground/$_name^parents"
142 if [ -z "$_dep_is_tgish" ]; then
143 # This dep is not for rewrite
148 bn
="$(echo "$_dep.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
151 bn
="$(basename "$_dep.
diff")";
152 dn
="$(dirname "$_dep.
diff")/";
153 if [ "x$dn" = "x./" ]; then
158 filename
="$output/$dn$bn";
159 if [ -e "$filename" ]; then
160 # We've already seen this dep
164 if branch_empty
"$_dep"; then
165 echo "Skip empty patch $_dep";
167 echo "Exporting $_dep"
168 mkdir
-p "$output/$dn";
169 $tg patch "$_dep" >"$filename"
170 echo "$dn$bn -p1" >>"$output/series"
177 if [ "$driver" = "collapse" ]; then
179 die
"no target branch specified"
180 ! ref_exists
"$output" ||
181 die
"target branch '$output' already exists; first run: git branch -D $output"
183 elif [ "$driver" = "quilt" ]; then
185 die
"no target directory specified"
186 [ ! -e "$output" ] ||
187 die
"target directory already exists: $output"
195 case $_dep in refs
/remotes
/*) return;; esac
196 branch_needs_update
>/dev
/null
198 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
203 # Call driver on all the branches - this will happen
204 # in topological order.
205 if [ -z "$branches" ]; then
206 recurse_deps driver
"$name"
207 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
209 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
213 name
="$(echo "$branches" | sed 's/.*,//')"
217 if [ "$driver" = "collapse" ]; then
218 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
220 depcount
="$(cat "$playground/^ticker
" | wc -l)"
221 echo "Exported topic branch $name (total $depcount topics) to branch $output"
223 elif [ "$driver" = "quilt" ]; then
224 depcount
="$(cat "$output/series
" | wc -l)"
225 echo "Exported topic branch $name (total $depcount topics) to directory $output"