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