chiark / gitweb /
dea24d9d6fa024ba3da1103a584991e7d9f4c7ec
[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         -*)
31                 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
32                 exit 1;;
33         *)
34                 [ -z "$output" ] || die "output already specified ($output)"
35                 output="$arg";;
36         esac
37 done
38
39
40
41 [ -z "$branches" -o "$driver" = "quilt" ] ||
42         die "-b works only with the quilt driver"
43
44 [ "$driver" = "quilt" ] || ! "$numbered" ||
45         die "--numbered works only with the quilt driver";
46
47 [ "$driver" = "quilt" ] || ! "$flatten" ||
48         die "--flatten works only with the quilt driver"
49
50 if [ -z "$branches" ]; then
51         # this check is only needed when no branches have been passed
52         name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
53         base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
54                 die "not on a TopGit-controlled branch"
55 fi
56
57
58 playground="$(mktemp -d -t tg-export.XXXXXX)"
59 trap 'rm -rf "$playground"' EXIT
60
61
62 ## Collapse driver
63
64 # pretty_tree NAME
65 # Output tree ID of a cleaned-up tree without tg's artifacts.
66 pretty_tree()
67 {
68         (export GIT_INDEX_FILE="$playground/^index"
69          git read-tree "$1"
70          git update-index --force-remove ".topmsg" ".topdeps"
71          git write-tree)
72 }
73
74 create_tg_commit()
75 {
76         name="$1"
77         tree="$2"
78         parent="$3"
79
80         # Get commit message and authorship information
81         git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
82
83         GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
84         GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
85         GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
86         SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
87
88         test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
89         test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
90         test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
91
92         (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
93         git stripspace |
94         git commit-tree "$tree" -p "$parent"
95 }
96
97 # collapsed_commit NAME
98 # Produce a collapsed commit of branch NAME.
99 collapsed_commit()
100 {
101         name="$1"
102
103         rm -f "$playground/^pre" "$playground/^post"
104         >"$playground/^body"
105
106         # Determine parent
107         parent="$(cut -f 1 "$playground/$name^parents")"
108         if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
109                 # Produce a merge commit first
110                 parent="$({
111                         echo "TopGit-driven merge of branches:"
112                         echo
113                         cut -f 2 "$playground/$name^parents"
114                 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
115                         $(for p in $parent; do echo -p $p; done))"
116         fi
117
118         if branch_empty "$name"; then
119                 echo "$parent";
120         else
121                 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
122         fi;
123
124         echo "$name" >>"$playground/^ticker"
125 }
126
127 # collapse
128 # This will collapse a single branch, using information about
129 # previously collapsed branches stored in $playground.
130 collapse()
131 {
132         if [ -s "$playground/$_dep" ]; then
133                 # We've already seen this dep
134                 commit="$(cat "$playground/$_dep")"
135
136         elif [ -z "$_dep_is_tgish" ]; then
137                 # This dep is not for rewrite
138                 commit="$(git rev-parse --verify "$_dep")"
139
140         else
141                 # First time hitting this dep; the common case
142                 echo "Collapsing $_dep"
143                 commit="$(collapsed_commit "$_dep")"
144                 mkdir -p "$playground/$(dirname "$_dep")"
145                 echo "$commit" >"$playground/$_dep"
146         fi
147
148         # Propagate our work through the dependency chain
149         mkdir -p "$playground/$(dirname "$_name")"
150         echo "$commit   $_dep" >>"$playground/$_name^parents"
151 }
152
153
154 ## Quilt driver
155
156 quilt()
157 {
158         if [ -z "$_dep_is_tgish" ]; then
159                 # This dep is not for rewrite
160                 return
161         fi
162
163         if "$flatten"; then
164                 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
165                 dn="";
166         else
167                 bn="$(basename "$_dep.diff")";
168                 dn="$(dirname "$_dep.diff")/";
169                 if [ "x$dn" = "x./" ]; then
170                         dn="";
171                 fi;
172         fi;
173
174         if [ -e "$playground/$_dep" ]; then
175                 # We've already seen this dep
176                 return
177         fi
178
179         mkdir -p "$playground/$(dirname "$_dep")";
180         touch "$playground/$_dep";
181
182         if branch_empty "$_dep"; then
183                 echo "Skip empty patch $_dep";
184         else
185                 if "$numbered"; then
186                         number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
187                         bn="$number-$bn";
188                         echo "$number" >"$playground/^number";
189                 fi;
190
191                 echo "Exporting $_dep"
192                 mkdir -p "$output/$dn";
193                 $tg patch "$_dep" >"$output/$dn$bn"
194                 echo "$dn$bn -p1" >>"$output/series"
195         fi
196 }
197
198
199 ## Machinery
200
201 if [ "$driver" = "collapse" ]; then
202         [ -n "$output" ] ||
203                 die "no target branch specified"
204         ! ref_exists "$output"  ||
205                 die "target branch '$output' already exists; first run: git branch -D $output"
206
207 elif [ "$driver" = "quilt" ]; then
208         [ -n "$output" ] ||
209                 die "no target directory specified"
210         [ ! -e "$output" ] ||
211                 die "target directory already exists: $output"
212
213         mkdir -p "$output"
214 fi
215
216
217 driver()
218 {
219         case $_dep in refs/remotes/*) return;; esac
220         branch_needs_update >/dev/null
221         [ "$_ret" -eq 0 ] ||
222                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
223
224         $driver
225 }
226
227 # Call driver on all the branches - this will happen
228 # in topological order.
229 if [ -z "$branches" ]; then
230         recurse_deps driver "$name"
231         (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
232 else
233         echo "$branches" | tr ',' '\n' | while read _dep; do
234                 _dep_is_tgish=1
235                 $driver
236         done
237         name="$(echo "$branches" | sed 's/.*,//')"
238 fi
239
240
241 if [ "$driver" = "collapse" ]; then
242         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
243
244         depcount="$(cat "$playground/^ticker" | wc -l)"
245         echo "Exported topic branch $name (total $depcount topics) to branch $output"
246
247 elif [ "$driver" = "quilt" ]; then
248         depcount="$(cat "$output/series" | wc -l)"
249         echo "Exported topic branch $name (total $depcount topics) to directory $output"
250 fi
251
252 # vim:noet