chiark / gitweb /
90f7c6a6dfcf739a6f89061e4e89f33aa810a292
[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="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
195                         bn="$number-$bn";
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                         git merge -s recursive "$_dep";
226                         retmerge="$?";
227                         if test "x$retmerge" != "x0"; then
228                                 echo fix up the merge, commit and then exit;
229                                 #todo error handling
230                                 sh -i
231                         fi;
232                 fi;
233         else
234                 git merge-recursive "$(pretty_tree "refs/top-bases/$_dep")" -- HEAD "$(pretty_tree "refs/heads/$_dep")";
235                 retmerge="$?";
236
237                 if test "x$retmerge" != "x0"; then
238                         echo "fix up the merge and update the index.  Don't commit!"
239                         #todo error handling
240                         sh -i
241                 fi
242
243                 result_tree=$(git write-tree)
244                 # testing branch_empty might not always give the right answer.
245                 # It can happen that the patch is non-empty but still after
246                 # linearizing there is no change.  So compare the trees.
247                 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
248                         echo "skip empty commit $_dep";
249                 else
250                         newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
251                         git update-ref HEAD $newcommit $head
252                         echo "exported commit $_dep";
253                 fi
254         fi
255 }
256
257 ## Machinery
258
259 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
260         [ -n "$output" ] ||
261                 die "no target branch specified"
262         ! ref_exists "$output"  ||
263                 die "target branch '$output' already exists; first run: git branch -D $output"
264
265 elif [ "$driver" = "quilt" ]; then
266         [ -n "$output" ] ||
267                 die "no target directory specified"
268         [ ! -e "$output" ] ||
269                 die "target directory already exists: $output"
270
271         mkdir -p "$output"
272 fi
273
274
275 driver()
276 {
277         case $_dep in refs/remotes/*) return;; esac
278         branch_needs_update >/dev/null
279         [ "$_ret" -eq 0 ] ||
280                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
281
282         $driver
283 }
284
285 # Call driver on all the branches - this will happen
286 # in topological order.
287 echo "$branches" | tr , '\n' | while read name; do
288         recurse_deps driver "$name"
289         (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
290 done
291
292 if [ "$driver" = "collapse" ]; then
293         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
294
295         depcount="$(cat "$playground/^ticker" | wc -l)"
296         echo "Exported topic branch $name (total $depcount topics) to branch $output"
297
298 elif [ "$driver" = "quilt" ]; then
299         depcount="$(cat "$output/series" | wc -l)"
300         echo "Exported topic branch $name (total $depcount topics) to directory $output"
301
302 elif [ "$driver" = "linearize" ]; then
303         git checkout -q -b $output
304
305         echo $name
306         if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
307                 echo "Warning: Exported result doesn't match";
308                 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
309                 #git diff $head HEAD;
310         fi;
311
312 fi
313
314 # vim:noet