chiark / gitweb /
4b0148c1d7770eb5cb6f1e9fda1a5bf1e327afd1
[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 create_tg_commit()
67 {
68         name="$1"
69         tree="$2"
70         parent="$3"
71
72         # Get commit message and authorship information
73         git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
74
75         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
76         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
77         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
78         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
79
80         test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
81         test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
82         test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
83
84         (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
85         git stripspace |
86         git commit-tree "$tree" -p "$parent"
87 }
88
89 # collapsed_commit NAME
90 # Produce a collapsed commit of branch NAME.
91 collapsed_commit()
92 {
93         name="$1"
94
95         rm -f "$playground/^pre" "$playground/^post"
96         >"$playground/^body"
97
98         # Determine parent
99         parent="$(cut -f 1 "$playground/$name^parents")"
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 "refs/top-bases/$name")" \
107                         $(for p in $parent; do echo -p $p; done))"
108         fi
109
110         if branch_empty "$name"; then
111                 echo "$parent";
112         else
113                 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
114         fi;
115
116         echo "$name" >>"$playground/^ticker"
117 }
118
119 # collapse
120 # This will collapse a single branch, using information about
121 # previously collapsed branches stored in $playground.
122 collapse()
123 {
124         if [ -s "$playground/$_dep" ]; then
125                 # We've already seen this dep
126                 commit="$(cat "$playground/$_dep")"
127
128         elif [ -z "$_dep_is_tgish" ]; then
129                 # This dep is not for rewrite
130                 commit="$(git rev-parse --verify "$_dep")"
131
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"
138         fi
139
140         # Propagate our work through the dependency chain
141         mkdir -p "$playground/$(dirname "$_name")"
142         echo "$commit   $_dep" >>"$playground/$_name^parents"
143 }
144
145
146 ## Quilt driver
147
148 quilt()
149 {
150         if [ -z "$_dep_is_tgish" ]; then
151                 # This dep is not for rewrite
152                 return
153         fi
154
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="";
163                 fi;
164         fi;
165
166         if [ -e "$playground/$_dep" ]; then
167                 # We've already seen this dep
168                 return
169         fi
170
171         mkdir -p "$playground/$(dirname "$_dep")";
172         touch "$playground/$_dep";
173
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";
181                 fi;
182
183                 echo "Exporting $_dep"
184                 mkdir -p "$output/$dn";
185                 $tg patch "$_dep" >"$output/$dn$bn"
186                 echo "$dn$bn -p1" >>"$output/series"
187         fi
188 }
189
190 linearize()
191 {
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;
197         fi;
198
199         head=$(git rev-parse --verify HEAD)
200
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
207                         :;
208                 else
209                         retmerge=0;
210
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;
216                         fi;
217                 fi;
218         else
219                 retmerge=0;
220
221                 git merge-recursive "$(pretty_tree "refs/top-bases/$_dep")" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
222
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;
228                 fi
229
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";
240                 fi
241         fi
242 }
243
244 ## Machinery
245
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"
251
252 elif [ "$driver" = "quilt" ]; then
253         [ -n "$output" ] ||
254                 die "no target directory specified"
255         [ ! -e "$output" ] ||
256                 die "target directory already exists: $output"
257
258         mkdir -p "$output"
259 fi
260
261
262 driver()
263 {
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"
268
269         $driver
270 }
271
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/.*,//')"
283 fi
284
285
286 if [ "$driver" = "collapse" ]; then
287         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
288
289         depcount="$(cat "$playground/^ticker" | wc -l)"
290         echo "Exported topic branch $name (total $depcount topics) to branch $output"
291
292 elif [ "$driver" = "quilt" ]; then
293         depcount="$(cat "$output/series" | wc -l)"
294         echo "Exported topic branch $name (total $depcount topics) to directory $output"
295
296 elif [ "$driver" = "linearize" ]; then
297         git checkout -q -b $output
298
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;
304         fi;
305
306 fi
307
308 # vim:noet