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