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
42 # Output tree ID of a cleaned-up tree without tg's artifacts.
45 (export GIT_INDEX_FILE
="$playground/^index"
47 git update-index
--force-remove ".topmsg" ".topdeps"
51 # collapsed_commit NAME
52 # Produce a collapsed commit of branch NAME.
57 rm -f "$playground/^pre" "$playground/^post"
60 # Get commit message and authorship information
61 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
63 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
64 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
65 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
66 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
68 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
69 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
70 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
73 parent
="$(cut -f 1 "$playground/$name^parents
")"
74 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
75 # Produce a merge commit first
77 echo "TopGit-driven merge of branches
:"
79 cut -f 2 "$playground/$name^parents
"
80 } | git commit-tree "$
(pretty_tree
"refs/top-bases/$name")" \
81 $(for p in $parent; do echo -p $p; done))"
84 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
86 git commit-tree
"$(pretty_tree "$name")" -p "$parent"
88 echo "$name" >>"$playground/^ticker"
92 # This will collapse a single branch, using information about
93 # previously collapsed branches stored in $playground.
96 if [ -s "$playground/$_dep" ]; then
97 # We've already seen this dep
98 commit
="$(cat "$playground/$_dep")"
100 elif [ -z "$_dep_is_tgish" ]; then
101 # This dep is not for rewrite
102 commit
="$(git rev-parse --verify "$_dep")"
105 # First time hitting this dep; the common case
106 echo "Collapsing $_dep"
107 commit
="$(collapsed_commit "$_dep")"
108 mkdir
-p "$playground/$(dirname "$_dep")"
109 echo "$commit" >"$playground/$_dep"
112 # Propagate our work through the dependency chain
113 mkdir
-p "$playground/$(dirname "$_name")"
114 echo "$commit $_dep" >>"$playground/$_name^parents"
122 if [ -z "$_dep_is_tgish" ]; then
123 # This dep is not for rewrite
127 filename
="$output/$_dep.diff"
128 if [ -e "$filename" ]; then
129 # We've already seen this dep
133 echo "Exporting $_dep"
134 mkdir
-p "$(dirname "$filename")"
135 $tg patch "$_dep" >"$filename"
136 echo "$_dep.diff -p1" >>"$output/series"
142 if [ "$driver" = "collapse" ]; then
144 die
"no target branch specified"
145 ! ref_exists
"$output" ||
146 die
"target branch '$output' already exists; first run: git branch -D $output"
148 elif [ "$driver" = "quilt" ]; then
150 die
"no target directory specified"
151 [ ! -e "$output" ] ||
152 die
"target directory already exists: $output"
160 branch_needs_update
>/dev
/null
162 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
167 # Call driver on all the branches - this will happen
168 # in topological order.
169 recurse_deps driver
"$name"
170 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
173 if [ "$driver" = "collapse" ]; then
174 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
176 depcount
="$(cat "$playground/^ticker
" | wc -l)"
177 echo "Exported topic branch $name (total $depcount topics) to branch $output"
179 elif [ "$driver" = "quilt" ]; then
180 depcount
="$(cat "$output/series
" | wc -l)"
181 echo "Exported topic branch $name (total $depcount topics) to directory $output"