--strip option for tg export
[topgit.git] / tg-export.sh
blob72320af05beaaa4ea5a7a7f71298628e227201b9
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 branches=
8 output=
9 driver=collapse
10 flatten=false
11 numbered=false
12 strip=false
13 stripval=0
16 ## Parse options
18 while [ -n "$1" ]; do
19 arg="$1"; shift
20 case "$arg" in
21 -b)
22 branches="$1"; shift;;
23 --flatten)
24 flatten=true;;
25 --numbered)
26 flatten=true;
27 numbered=true;;
28 --strip*)
29 val=${arg#*=}
30 if [ "$val" = "--strip" ]; then
31 strip=true
32 stripval=9999
33 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
34 strip=true
35 stripval=$val
36 else
37 die "invalid parameter $arg"
38 fi;;
39 --quilt)
40 driver=quilt;;
41 --collapse)
42 driver=collapse;;
43 --linearize)
44 driver=linearize;;
45 -*)
46 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY | --linearize NEWBRANCH)" >&2
47 exit 1;;
49 [ -z "$output" ] || die "output already specified ($output)"
50 output="$arg";;
51 esac
52 done
56 [ -z "$branches" -o "$driver" = "quilt" ] ||
57 die "-b works only with the quilt driver"
59 [ "$driver" = "quilt" ] || ! "$numbered" ||
60 die "--numbered works only with the quilt driver";
62 [ "$driver" = "quilt" ] || ! "$flatten" ||
63 die "--flatten works only with the quilt driver"
65 [ "$driver" = "quilt" ] || ! "$strip" ||
66 die "--strip works only with the quilt driver"
68 if [ -z "$branches" ]; then
69 # this check is only needed when no branches have been passed
70 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
71 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
72 die "not on a TopGit-controlled branch"
76 playground="$(get_temp tg-export -d)"
79 ## Collapse driver
81 create_tg_commit()
83 name="$1"
84 tree="$2"
85 parent="$3"
87 # Get commit message and authorship information
88 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
90 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
91 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
92 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
93 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
95 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
96 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
97 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
99 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
100 git stripspace |
101 git commit-tree "$tree" -p "$parent"
104 # collapsed_commit NAME
105 # Produce a collapsed commit of branch NAME.
106 collapsed_commit()
108 name="$1"
110 rm -f "$playground/^pre" "$playground/^post"
111 >"$playground/^body"
113 # Determine parent
114 parent="$(cut -f 1 "$playground/$name^parents" | \
115 while read p; do [ $(git cat-file -t $p 2> /dev/null) = tag ] && git cat-file tag $p | head -1 | cut -d' ' -f2 || echo $p; done)"
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 "$name" -b)" \
123 $(for p in $parent; do echo -p $p; done))"
126 if branch_empty "$name"; then
127 echo "$parent";
128 else
129 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
132 echo "$name" >>"$playground/^ticker"
135 # collapse
136 # This will collapse a single branch, using information about
137 # previously collapsed branches stored in $playground.
138 collapse()
140 if [ -s "$playground/$_dep" ]; then
141 # We've already seen this dep
142 commit="$(cat "$playground/$_dep")"
144 elif [ -z "$_dep_is_tgish" ]; then
145 # This dep is not for rewrite
146 commit="$(git rev-parse --verify "$_dep")"
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"
156 # Propagate our work through the dependency chain
157 mkdir -p "$playground/$(dirname "$_name")"
158 echo "$commit $_dep" >>"$playground/$_name^parents"
162 ## Quilt driver
164 quilt()
166 if [ -z "$_dep_is_tgish" ]; then
167 # This dep is not for rewrite
168 return
171 _dep_tmp=$_dep
173 if "$strip"; then
174 i=$stripval
175 while [ "$i" -gt 0 ]; do
176 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
177 _dep_tmp=${_dep_tmp#*/}
178 i=$((i - 1))
179 done
182 bn="$(basename "$_dep_tmp.diff")"
183 dn="$(dirname "$_dep_tmp.diff")/"
184 [ "x$dn" = "x./" ] && dn=""
186 if "$flatten" && [ "$dn" ]; then
187 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
188 dn=""
191 unset _dep_tmp
193 if [ -e "$playground/$_dep" ]; then
194 # We've already seen this dep
195 return
198 mkdir -p "$playground/$(dirname "$_dep")";
199 touch "$playground/$_dep";
201 if branch_empty "$_dep"; then
202 echo "Skip empty patch $_dep";
203 else
204 if "$numbered"; then
205 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
206 bn="$(printf "%04u-$bn" $number)";
207 echo "$number" >"$playground/^number";
210 echo "Exporting $_dep"
211 mkdir -p "$output/$dn";
212 $tg patch "$_dep" >"$output/$dn$bn"
213 echo "$dn$bn -p1" >>"$output/series"
217 linearize()
219 if test ! -f "$playground/^BASE"; then
220 head="$(git rev-parse --verify "$_dep")"
221 echo "$head" > "$playground/^BASE"
222 git checkout -q "$head"
223 return;
226 head=$(git rev-parse --verify HEAD)
228 if [ -z "$_dep_is_tgish" ]; then
229 # merge in $_dep unless already included
230 rev="$(git rev-parse --verify "$_dep")";
231 common="$(git merge-base --all HEAD "$_dep")";
232 if test "$rev" = "$common"; then
233 # already included, just skip
235 else
236 retmerge=0;
238 git merge -s recursive "$_dep" || retmerge="$?";
239 if test "x$retmerge" != "x0"; then
240 echo fix up the merge, commit and then exit;
241 #todo error handling
242 sh -i </dev/tty;
245 else
246 retmerge=0;
248 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
250 if test "x$retmerge" != "x0"; then
251 git rerere;
252 echo "fix up the merge and update the index. Don't commit!"
253 #todo error handling
254 sh -i </dev/tty;
257 result_tree=$(git write-tree)
258 # testing branch_empty might not always give the right answer.
259 # It can happen that the patch is non-empty but still after
260 # linearizing there is no change. So compare the trees.
261 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
262 echo "skip empty commit $_dep";
263 else
264 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
265 git update-ref HEAD $newcommit $head
266 echo "exported commit $_dep";
271 ## Machinery
273 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
274 [ -n "$output" ] ||
275 die "no target branch specified"
276 ! ref_exists "$output" ||
277 die "target branch '$output' already exists; first run: git branch -D $output"
279 elif [ "$driver" = "quilt" ]; then
280 [ -n "$output" ] ||
281 die "no target directory specified"
282 [ ! -e "$output" ] ||
283 die "target directory already exists: $output"
285 mkdir -p "$output"
289 driver()
291 # FIXME should we abort on missing dependency?
292 [ -z "$_dep_missing" ] || return 0
294 case $_dep in refs/remotes/*) return;; esac
295 branch_needs_update >/dev/null
296 [ "$_ret" -eq 0 ] ||
297 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
299 $driver
302 # Call driver on all the branches - this will happen
303 # in topological order.
304 if [ -z "$branches" ]; then
305 recurse_deps driver "$name"
306 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
307 else
308 echo "$branches" | tr ',' '\n' | while read _dep; do
309 _dep_is_tgish=1
310 $driver
311 done
312 name="$(echo "$branches" | sed 's/.*,//')"
316 if [ "$driver" = "collapse" ]; then
317 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
319 depcount="$(cat "$playground/^ticker" | wc -l)"
320 echo "Exported topic branch $name (total $depcount topics) to branch $output"
322 elif [ "$driver" = "quilt" ]; then
323 depcount="$(cat "$output/series" | wc -l)"
324 echo "Exported topic branch $name (total $depcount topics) to directory $output"
326 elif [ "$driver" = "linearize" ]; then
327 git checkout -q -b $output
329 echo $name
330 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
331 echo "Warning: Exported result doesn't match";
332 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
333 #git diff $head HEAD;
338 # vim:noet