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"
45 playground
="$(mktemp -d -t tg-export.XXXXXX)"
46 trap 'rm -rf "$playground"' EXIT
52 # Output tree ID of a cleaned-up tree without tg's artifacts.
55 (export GIT_INDEX_FILE
="$playground/^index"
57 git update-index
--force-remove ".topmsg" ".topdeps"
61 # collapsed_commit NAME
62 # Produce a collapsed commit of branch NAME.
67 rm -f "$playground/^pre" "$playground/^post"
70 # Get commit message and authorship information
71 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
73 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
74 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
75 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
76 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
78 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
79 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
80 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
83 parent
="$(cut -f 1 "$playground/$name^parents
")"
84 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
85 # Produce a merge commit first
87 echo "TopGit-driven merge of branches
:"
89 cut -f 2 "$playground/$name^parents
"
90 } | git commit-tree "$
(pretty_tree
"refs/top-bases/$name")" \
91 $(for p in $parent; do echo -p $p; done))"
94 if branch_empty
"$name"; then
97 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
99 git commit-tree
"$(pretty_tree "$name")" -p "$parent"
102 echo "$name" >>"$playground/^ticker"
106 # This will collapse a single branch, using information about
107 # previously collapsed branches stored in $playground.
110 if [ -s "$playground/$_dep" ]; then
111 # We've already seen this dep
112 commit
="$(cat "$playground/$_dep")"
114 elif [ -z "$_dep_is_tgish" ]; then
115 # This dep is not for rewrite
116 commit
="$(git rev-parse --verify "$_dep")"
119 # First time hitting this dep; the common case
120 echo "Collapsing $_dep"
121 commit
="$(collapsed_commit "$_dep")"
122 mkdir
-p "$playground/$(dirname "$_dep")"
123 echo "$commit" >"$playground/$_dep"
126 # Propagate our work through the dependency chain
127 mkdir
-p "$playground/$(dirname "$_name")"
128 echo "$commit $_dep" >>"$playground/$_name^parents"
136 if [ -z "$_dep_is_tgish" ]; then
137 # This dep is not for rewrite
141 filename
="$output/$_dep.diff"
142 if [ -e "$filename" ]; then
143 # We've already seen this dep
147 if branch_empty
"$_dep"; then
148 echo "Skip empty patch $_dep";
150 echo "Exporting $_dep"
151 mkdir
-p "$(dirname "$filename")"
152 $tg patch "$_dep" >"$filename"
153 echo "$_dep.diff -p1" >>"$output/series"
160 if [ "$driver" = "collapse" ]; then
162 die
"no target branch specified"
163 ! ref_exists
"$output" ||
164 die
"target branch '$output' already exists; first run: git branch -D $output"
166 elif [ "$driver" = "quilt" ]; then
168 die
"no target directory specified"
169 [ ! -e "$output" ] ||
170 die
"target directory already exists: $output"
178 case $_dep in refs
/remotes
/*) return;; esac
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 if [ -z "$branches" ]; then
189 recurse_deps driver
"$name"
190 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
192 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
196 name
="$(echo "$branches" | sed 's/.*,//')"
200 if [ "$driver" = "collapse" ]; then
201 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
203 depcount
="$(cat "$playground/^ticker
" | wc -l)"
204 echo "Exported topic branch $name (total $depcount topics) to branch $output"
206 elif [ "$driver" = "quilt" ]; then
207 depcount
="$(cat "$output/series
" | wc -l)"
208 echo "Exported topic branch $name (total $depcount topics) to directory $output"