tg-update: strip refs/top-bases/ from PATTERNs just in case
[topgit.git] / tg-export.sh
blob45748827538dede3f2fc9f26016a49f9a6a7913e
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 branches=
8 output=
9 driver=collapse
10 flatten=false
11 numbered=false
14 ## Parse options
16 while [ -n "$1" ]; do
17 arg="$1"; shift
18 case "$arg" in
19 -b)
20 branches="$1"; shift;;
21 --flatten)
22 flatten=true;;
23 --numbered)
24 flatten=true;
25 numbered=true;;
26 --quilt)
27 driver=quilt;;
28 --collapse)
29 driver=collapse;;
30 --linearize)
31 driver=linearize;;
32 -*)
33 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY | --linearize NEWBRANCH)" >&2
34 exit 1;;
36 [ -z "$output" ] || die "output already specified ($output)"
37 output="$arg";;
38 esac
39 done
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="$(get_temp tg-export -d)"
63 ## Collapse driver
65 create_tg_commit()
67 name="$1"
68 tree="$2"
69 parent="$3"
71 # Get commit message and authorship information
72 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
74 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
75 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
76 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
77 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
79 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
80 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
81 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
83 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
84 git stripspace |
85 git commit-tree "$tree" -p "$parent"
88 # collapsed_commit NAME
89 # Produce a collapsed commit of branch NAME.
90 collapsed_commit()
92 name="$1"
94 rm -f "$playground/^pre" "$playground/^post"
95 >"$playground/^body"
97 # Determine parent
98 parent="$(cut -f 1 "$playground/$name^parents" | \
99 while read p; do [ $(git cat-file -t $p 2> /dev/null) = tag ] && git cat-file tag $p | head -1 | cut -d' ' -f2 || echo $p; done)"
100 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
101 # Produce a merge commit first
102 parent="$({
103 echo "TopGit-driven merge of branches:"
104 echo
105 cut -f 2 "$playground/$name^parents"
106 } | git commit-tree "$(pretty_tree "$name" -b)" \
107 $(for p in $parent; do echo -p $p; done))"
110 if branch_empty "$name"; then
111 echo "$parent";
112 else
113 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
116 echo "$name" >>"$playground/^ticker"
119 # collapse
120 # This will collapse a single branch, using information about
121 # previously collapsed branches stored in $playground.
122 collapse()
124 if [ -s "$playground/$_dep" ]; then
125 # We've already seen this dep
126 commit="$(cat "$playground/$_dep")"
128 elif [ -z "$_dep_is_tgish" ]; then
129 # This dep is not for rewrite
130 commit="$(git rev-parse --verify "$_dep")"
132 else
133 # First time hitting this dep; the common case
134 echo "Collapsing $_dep"
135 commit="$(collapsed_commit "$_dep")"
136 mkdir -p "$playground/$(dirname "$_dep")"
137 echo "$commit" >"$playground/$_dep"
140 # Propagate our work through the dependency chain
141 mkdir -p "$playground/$(dirname "$_name")"
142 echo "$commit $_dep" >>"$playground/$_name^parents"
146 ## Quilt driver
148 quilt()
150 if [ -z "$_dep_is_tgish" ]; then
151 # This dep is not for rewrite
152 return
155 if "$flatten"; then
156 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
157 dn="";
158 else
159 bn="$(basename "$_dep.diff")";
160 dn="$(dirname "$_dep.diff")/";
161 if [ "x$dn" = "x./" ]; then
162 dn="";
166 if [ -e "$playground/$_dep" ]; then
167 # We've already seen this dep
168 return
171 mkdir -p "$playground/$(dirname "$_dep")";
172 touch "$playground/$_dep";
174 if branch_empty "$_dep"; then
175 echo "Skip empty patch $_dep";
176 else
177 if "$numbered"; then
178 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
179 bn="$(printf "%04u-$bn" $number)";
180 echo "$number" >"$playground/^number";
183 echo "Exporting $_dep"
184 mkdir -p "$output/$dn";
185 $tg patch "$_dep" >"$output/$dn$bn"
186 echo "$dn$bn -p1" >>"$output/series"
190 linearize()
192 if test ! -f "$playground/^BASE"; then
193 head="$(git rev-parse --verify "$_dep")"
194 echo "$head" > "$playground/^BASE"
195 git checkout -q "$head"
196 return;
199 head=$(git rev-parse --verify HEAD)
201 if [ -z "$_dep_is_tgish" ]; then
202 # merge in $_dep unless already included
203 rev="$(git rev-parse --verify "$_dep")";
204 common="$(git merge-base --all HEAD "$_dep")";
205 if test "$rev" = "$common"; then
206 # already included, just skip
208 else
209 retmerge=0;
211 git merge -s recursive "$_dep" || retmerge="$?";
212 if test "x$retmerge" != "x0"; then
213 echo fix up the merge, commit and then exit;
214 #todo error handling
215 sh -i </dev/tty;
218 else
219 retmerge=0;
221 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
223 if test "x$retmerge" != "x0"; then
224 git rerere;
225 echo "fix up the merge and update the index. Don't commit!"
226 #todo error handling
227 sh -i </dev/tty;
230 result_tree=$(git write-tree)
231 # testing branch_empty might not always give the right answer.
232 # It can happen that the patch is non-empty but still after
233 # linearizing there is no change. So compare the trees.
234 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
235 echo "skip empty commit $_dep";
236 else
237 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
238 git update-ref HEAD $newcommit $head
239 echo "exported commit $_dep";
244 ## Machinery
246 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
247 [ -n "$output" ] ||
248 die "no target branch specified"
249 ! ref_exists "$output" ||
250 die "target branch '$output' already exists; first run: git branch -D $output"
252 elif [ "$driver" = "quilt" ]; then
253 [ -n "$output" ] ||
254 die "no target directory specified"
255 [ ! -e "$output" ] ||
256 die "target directory already exists: $output"
258 mkdir -p "$output"
262 driver()
264 case $_dep in refs/remotes/*) return;; esac
265 branch_needs_update >/dev/null
266 [ "$_ret" -eq 0 ] ||
267 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
269 $driver
272 # Call driver on all the branches - this will happen
273 # in topological order.
274 if [ -z "$branches" ]; then
275 recurse_deps driver "$name"
276 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
277 else
278 echo "$branches" | tr ',' '\n' | while read _dep; do
279 _dep_is_tgish=1
280 $driver
281 done
282 name="$(echo "$branches" | sed 's/.*,//')"
286 if [ "$driver" = "collapse" ]; then
287 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
289 depcount="$(cat "$playground/^ticker" | wc -l)"
290 echo "Exported topic branch $name (total $depcount topics) to branch $output"
292 elif [ "$driver" = "quilt" ]; then
293 depcount="$(cat "$output/series" | wc -l)"
294 echo "Exported topic branch $name (total $depcount topics) to directory $output"
296 elif [ "$driver" = "linearize" ]; then
297 git checkout -q -b $output
299 echo $name
300 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
301 echo "Warning: Exported result doesn't match";
302 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
303 #git diff $head HEAD;
308 # vim:noet