chiark / gitweb /
remove test for git send-email
[topgit.git] / tg-export.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6 name=
7 branches=
8 output=
9 driver=collapse
10 flatten=false
11 numbered=false
12
13
14 ## Parse options
15
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;;
35         *)
36                 [ -z "$output" ] || die "output already specified ($output)"
37                 output="$arg";;
38         esac
39 done
40
41
42
43 [ -z "$branches" -o "$driver" = "quilt" ] ||
44         die "-b works only with the quilt driver"
45
46 [ "$driver" = "quilt" ] || ! "$numbered" ||
47         die "--numbered works only with the quilt driver";
48
49 [ "$driver" = "quilt" ] || ! "$flatten" ||
50         die "--flatten works only with the quilt driver"
51
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"
57 fi
58
59
60 playground="$(mktemp -d -t tg-export.XXXXXX)"
61 trap 'rm -rf "$playground"' EXIT
62
63
64 ## Collapse driver
65
66 # pretty_tree NAME
67 # Output tree ID of a cleaned-up tree without tg's artifacts.
68 pretty_tree()
69 {
70         (export GIT_INDEX_FILE="$playground/^index"
71          git read-tree "$1"
72          git update-index --force-remove ".topmsg" ".topdeps"
73          git write-tree)
74 }
75
76 create_tg_commit()
77 {
78         name="$1"
79         tree="$2"
80         parent="$3"
81
82         # Get commit message and authorship information
83         git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
84
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")"
89
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
93
94         (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
95         git stripspace |
96         git commit-tree "$tree" -p "$parent"
97 }
98
99 # collapsed_commit NAME
100 # Produce a collapsed commit of branch NAME.
101 collapsed_commit()
102 {
103         name="$1"
104
105         rm -f "$playground/^pre" "$playground/^post"
106         >"$playground/^body"
107
108         # Determine parent
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
112                 parent="$({
113                         echo "TopGit-driven merge of branches:"
114                         echo
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))"
118         fi
119
120         if branch_empty "$name"; then
121                 echo "$parent";
122         else
123                 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
124         fi;
125
126         echo "$name" >>"$playground/^ticker"
127 }
128
129 # collapse
130 # This will collapse a single branch, using information about
131 # previously collapsed branches stored in $playground.
132 collapse()
133 {
134         if [ -s "$playground/$_dep" ]; then
135                 # We've already seen this dep
136                 commit="$(cat "$playground/$_dep")"
137
138         elif [ -z "$_dep_is_tgish" ]; then
139                 # This dep is not for rewrite
140                 commit="$(git rev-parse --verify "$_dep")"
141
142         else
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"
148         fi
149
150         # Propagate our work through the dependency chain
151         mkdir -p "$playground/$(dirname "$_name")"
152         echo "$commit   $_dep" >>"$playground/$_name^parents"
153 }
154
155
156 ## Quilt driver
157
158 quilt()
159 {
160         if [ -z "$_dep_is_tgish" ]; then
161                 # This dep is not for rewrite
162                 return
163         fi
164
165         if "$flatten"; then
166                 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
167                 dn="";
168         else
169                 bn="$(basename "$_dep.diff")";
170                 dn="$(dirname "$_dep.diff")/";
171                 if [ "x$dn" = "x./" ]; then
172                         dn="";
173                 fi;
174         fi;
175
176         if [ -e "$playground/$_dep" ]; then
177                 # We've already seen this dep
178                 return
179         fi
180
181         mkdir -p "$playground/$(dirname "$_dep")";
182         touch "$playground/$_dep";
183
184         if branch_empty "$_dep"; then
185                 echo "Skip empty patch $_dep";
186         else
187                 if "$numbered"; then
188                         number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
189                         bn="$number-$bn";
190                         echo "$number" >"$playground/^number";
191                 fi;
192
193                 echo "Exporting $_dep"
194                 mkdir -p "$output/$dn";
195                 $tg patch "$_dep" >"$output/$dn$bn"
196                 echo "$dn$bn -p1" >>"$output/series"
197         fi
198 }
199
200 linearize()
201 {
202         if test ! -f "$playground/^BASE"; then
203                 head="$(git rev-parse --verify "$_dep")"
204                 echo "$head" > "$playground/^BASE"
205                 git checkout -q "$head"
206                 return;
207         fi;
208
209         head=$(git rev-parse --verify HEAD)
210
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
217                         :;
218                 else
219                         retmerge=0;
220
221                         git merge -s recursive "$_dep" || retmerge="$?";
222                         if test "x$retmerge" != "x0"; then
223                                 echo fix up the merge, commit and then exit;
224                                 #todo error handling
225                                 sh -i </dev/tty;
226                         fi;
227                 fi;
228         else
229                 retmerge=0;
230
231                 git merge-recursive "$(pretty_tree "refs/top-bases/$_dep")" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
232
233                 if test "x$retmerge" != "x0"; then
234                         git rerere;
235                         echo "fix up the merge and update the index.  Don't commit!"
236                         #todo error handling
237                         sh -i </dev/tty;
238                 fi
239
240                 result_tree=$(git write-tree)
241                 # testing branch_empty might not always give the right answer.
242                 # It can happen that the patch is non-empty but still after
243                 # linearizing there is no change.  So compare the trees.
244                 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
245                         echo "skip empty commit $_dep";
246                 else
247                         newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
248                         git update-ref HEAD $newcommit $head
249                         echo "exported commit $_dep";
250                 fi
251         fi
252 }
253
254 ## Machinery
255
256 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
257         [ -n "$output" ] ||
258                 die "no target branch specified"
259         ! ref_exists "$output"  ||
260                 die "target branch '$output' already exists; first run: git branch -D $output"
261
262 elif [ "$driver" = "quilt" ]; then
263         [ -n "$output" ] ||
264                 die "no target directory specified"
265         [ ! -e "$output" ] ||
266                 die "target directory already exists: $output"
267
268         mkdir -p "$output"
269 fi
270
271
272 driver()
273 {
274         case $_dep in refs/remotes/*) return;; esac
275         branch_needs_update >/dev/null
276         [ "$_ret" -eq 0 ] ||
277                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
278
279         $driver
280 }
281
282 # Call driver on all the branches - this will happen
283 # in topological order.
284 if [ -z "$branches" ]; then
285         recurse_deps driver "$name"
286         (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
287 else
288         echo "$branches" | tr ',' '\n' | while read _dep; do
289                 _dep_is_tgish=1
290                 $driver
291         done
292         name="$(echo "$branches" | sed 's/.*,//')"
293 fi
294
295
296 if [ "$driver" = "collapse" ]; then
297         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
298
299         depcount="$(cat "$playground/^ticker" | wc -l)"
300         echo "Exported topic branch $name (total $depcount topics) to branch $output"
301
302 elif [ "$driver" = "quilt" ]; then
303         depcount="$(cat "$output/series" | wc -l)"
304         echo "Exported topic branch $name (total $depcount topics) to directory $output"
305
306 elif [ "$driver" = "linearize" ]; then
307         git checkout -q -b $output
308
309         echo $name
310         if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
311                 echo "Warning: Exported result doesn't match";
312                 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
313                 #git diff $head HEAD;
314         fi;
315
316 fi
317
318 # vim:noet