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