2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
20 branches
="$1"; shift;;
33 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY | --linearize NEWBRANCH)" >&2
36 [ -z "$output" ] || die
"output already specified ($output)"
43 [ -z "$branches" -o "$driver" = "quilt" ] ||
44 die
"-b works only with the quilt driver"
46 [ "$driver" = "quilt" ] ||
! "$numbered" ||
47 die
"--numbered works only with the quilt driver";
49 [ "$driver" = "quilt" ] ||
! "$flatten" ||
50 die
"--flatten works only with the quilt driver"
52 if [ -z "$branches" ]; then
53 # this check is only needed when no branches have been passed
54 name
="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
55 base_rev
="$(git rev-parse --short --verify "refs
/top-bases
/$name" 2>/dev/null)" ||
56 die
"not on a TopGit-controlled branch"
60 playground
="$(mktemp -d -t tg-export.XXXXXX)"
61 trap 'rm -rf "$playground"' EXIT
67 # Output tree ID of a cleaned-up tree without tg's artifacts.
70 (export GIT_INDEX_FILE
="$playground/^index"
72 git update-index
--force-remove ".topmsg" ".topdeps"
82 # Get commit message and authorship information
83 git cat-file blob
"$name:.topmsg" | git mailinfo
"$playground/^msg" /dev
/null
> "$playground/^info"
85 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$playground/^info
")"
86 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$playground/^info
")"
87 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$playground/^info
")"
88 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info
")"
90 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
91 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
92 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
94 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
96 git commit-tree
"$tree" -p "$parent"
99 # collapsed_commit NAME
100 # Produce a collapsed commit of branch NAME.
105 rm -f "$playground/^pre" "$playground/^post"
109 parent
="$(cut -f 1 "$playground/$name^parents
")"
110 if [ "$(cat "$playground/$name^parents
" | wc -l)" -gt 1 ]; then
111 # Produce a merge commit first
113 echo "TopGit-driven merge of branches
:"
115 cut -f 2 "$playground/$name^parents
"
116 } | git commit-tree "$
(pretty_tree
"refs/top-bases/$name")" \
117 $(for p in $parent; do echo -p $p; done))"
120 if branch_empty
"$name"; then
123 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
126 echo "$name" >>"$playground/^ticker"
130 # This will collapse a single branch, using information about
131 # previously collapsed branches stored in $playground.
134 if [ -s "$playground/$_dep" ]; then
135 # We've already seen this dep
136 commit
="$(cat "$playground/$_dep")"
138 elif [ -z "$_dep_is_tgish" ]; then
139 # This dep is not for rewrite
140 commit
="$(git rev-parse --verify "$_dep")"
143 # First time hitting this dep; the common case
144 echo "Collapsing $_dep"
145 commit
="$(collapsed_commit "$_dep")"
146 mkdir
-p "$playground/$(dirname "$_dep")"
147 echo "$commit" >"$playground/$_dep"
150 # Propagate our work through the dependency chain
151 mkdir
-p "$playground/$(dirname "$_name")"
152 echo "$commit $_dep" >>"$playground/$_name^parents"
160 if [ -z "$_dep_is_tgish" ]; then
161 # This dep is not for rewrite
166 bn
="$(echo "$_dep.
diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
169 bn
="$(basename "$_dep.
diff")";
170 dn
="$(dirname "$_dep.
diff")/";
171 if [ "x$dn" = "x./" ]; then
176 if [ -e "$playground/$_dep" ]; then
177 # We've already seen this dep
181 mkdir
-p "$playground/$(dirname "$_dep")";
182 touch "$playground/$_dep";
184 if branch_empty
"$_dep"; then
185 echo "Skip empty patch $_dep";
188 number
="$(printf "%04u" $(($(cat "$playground/^number
" 2>/dev/null) + 1)))";
190 echo "$number" >"$playground/^number";
193 echo "Exporting $_dep"
194 mkdir
-p "$output/$dn";
195 $tg patch "$_dep" >"$output/$dn$bn"
196 echo "$dn$bn -p1" >>"$output/series"
202 if test ! -f "$playground/^BASE"; then
203 head="$(git rev-parse --verify "$_dep")"
204 echo "$head" > "$playground/^BASE"
205 git checkout
-q "$head"
209 head=$
(git rev-parse
--verify HEAD
)
211 if [ -z "$_dep_is_tgish" ]; then
212 # merge in $_dep unless already included
213 rev="$(git rev-parse --verify "$_dep")";
214 common
="$(git merge-base --all HEAD "$_dep")";
215 if test "$rev" = "$common"; then
216 # already included, just skip
219 git merge
-s recursive
"$_dep";
221 if test "x$retmerge" != "x0"; then
222 echo fix up the merge
, commit and
then exit;
228 git merge-recursive
"$(pretty_tree "refs
/top-bases
/$_dep")" -- HEAD
"$(pretty_tree "refs
/heads
/$_dep")";
231 if test "x$retmerge" != "x0"; then
232 echo "fix up the merge and update the index. Don't commit!"
237 result_tree
=$
(git write-tree
)
238 # testing branch_empty might not always give the right answer.
239 # It can happen that the patch is non-empty but still after
240 # linearizing there is no change. So compare the trees.
241 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
242 echo "skip empty commit $_dep";
244 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
245 git update-ref HEAD
$newcommit $head
246 echo "exported commit $_dep";
253 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
255 die
"no target branch specified"
256 ! ref_exists
"$output" ||
257 die
"target branch '$output' already exists; first run: git branch -D $output"
259 elif [ "$driver" = "quilt" ]; then
261 die
"no target directory specified"
262 [ ! -e "$output" ] ||
263 die
"target directory already exists: $output"
271 case $_dep in refs
/remotes
/*) return;; esac
272 branch_needs_update
>/dev
/null
274 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
279 # Call driver on all the branches - this will happen
280 # in topological order.
281 if [ -z "$branches" ]; then
282 recurse_deps driver
"$name"
283 (_ret
=0; _dep
="$name"; _name
=""; _dep_is_tgish
=1; driver
)
285 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
289 name
="$(echo "$branches" | sed 's/.*,//')"
293 if [ "$driver" = "collapse" ]; then
294 git update-ref
"refs/heads/$output" "$(cat "$playground/$name")" ""
296 depcount
="$(cat "$playground/^ticker
" | wc -l)"
297 echo "Exported topic branch $name (total $depcount topics) to branch $output"
299 elif [ "$driver" = "quilt" ]; then
300 depcount
="$(cat "$output/series
" | wc -l)"
301 echo "Exported topic branch $name (total $depcount topics) to directory $output"
303 elif [ "$driver" = "linearize" ]; then
304 git checkout
-q -b $output
307 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
308 echo "Warning: Exported result doesn't match";
309 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")";
310 #git diff $head HEAD;