Remove *.sh 'local' bashism
[topgit/pro.git] / tg-export.sh
blob0655d941a570acb71c231185ea4c67992c3454b6
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
14 allbranches=false
17 ## Parse options
19 while [ -n "$1" ]; do
20 arg="$1"; shift
21 case "$arg" in
22 -a|--all)
23 allbranches=true;;
24 -b)
25 branches="$1"; shift;;
26 --flatten)
27 flatten=true;;
28 --numbered)
29 flatten=true;
30 numbered=true;;
31 --strip*)
32 val=${arg#*=}
33 if [ "$val" = "--strip" ]; then
34 strip=true
35 stripval=9999
36 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
37 strip=true
38 stripval=$val
39 else
40 die "invalid parameter $arg"
41 fi;;
42 --quilt)
43 driver=quilt;;
44 --collapse)
45 driver=collapse;;
46 --linearize)
47 driver=linearize;;
48 -*)
49 echo "Usage: tg [...] export ([--collapse] <newbranch> | [-a | --all | -b <branch1>...] --quilt <directory> | --linearize <newbranch>)" >&2
50 exit 1;;
52 [ -z "$output" ] || die "output already specified ($output)"
53 output="$arg";;
54 esac
55 done
59 [ -z "$branches" -o "$driver" = "quilt" ] ||
60 die "-b works only with the quilt driver"
62 [ "$driver" = "quilt" ] || ! "$numbered" ||
63 die "--numbered works only with the quilt driver";
65 [ "$driver" = "quilt" ] || ! "$flatten" ||
66 die "--flatten works only with the quilt driver"
68 [ "$driver" = "quilt" ] || ! "$strip" ||
69 die "--strip works only with the quilt driver"
71 [ "$driver" = "quilt" ] || ! "$allbranches" ||
72 die "--all works only with the quilt driver";
74 [ -z "$branches" ] || ! "$allbranches" ||
75 die "-b conflicts with the --all option";
77 if [ -z "$branches" ] && ! "$allbranches"; then
78 # this check is only needed when no branches have been passed
79 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
80 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
81 die "not on a TopGit-controlled branch"
85 playground="$(get_temp tg-export -d)"
88 ## Collapse driver
90 create_tg_commit()
92 name="$1"
93 tree="$2"
94 parent="$3"
96 # Get commit message and authorship information
97 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
99 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
100 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
101 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
102 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
104 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
105 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
106 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
108 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
109 git stripspace |
110 git commit-tree "$tree" -p "$parent"
113 # collapsed_commit NAME
114 # Produce a collapsed commit of branch NAME.
115 collapsed_commit()
117 name="$1"
119 rm -f "$playground/^pre" "$playground/^post"
120 >"$playground/^body"
122 # Determine parent
123 parent="$(cut -f 1 "$playground/$name^parents" | \
124 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)"
125 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
126 # Produce a merge commit first
127 parent="$({
128 echo "TopGit-driven merge of branches:"
129 echo
130 cut -f 2 "$playground/$name^parents"
131 } | git commit-tree "$(pretty_tree "$name" -b)" \
132 $(for p in $parent; do echo -p $p; done))"
135 if branch_empty "$name"; then
136 echo "$parent";
137 else
138 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
141 echo "$name" >>"$playground/^ticker"
144 # collapse
145 # This will collapse a single branch, using information about
146 # previously collapsed branches stored in $playground.
147 collapse()
149 if [ -s "$playground/$_dep" ]; then
150 # We've already seen this dep
151 commit="$(cat "$playground/$_dep")"
153 elif [ -z "$_dep_is_tgish" ]; then
154 # This dep is not for rewrite
155 commit="$(git rev-parse --verify "$_dep")"
157 else
158 # First time hitting this dep; the common case
159 echo "Collapsing $_dep"
160 commit="$(collapsed_commit "$_dep")"
161 mkdir -p "$playground/$(dirname "$_dep")"
162 echo "$commit" >"$playground/$_dep"
165 # Propagate our work through the dependency chain
166 mkdir -p "$playground/$(dirname "$_name")"
167 echo "$commit $_dep" >>"$playground/$_name^parents"
171 ## Quilt driver
173 quilt()
175 if [ -z "$_dep_is_tgish" ]; then
176 # This dep is not for rewrite
177 return
180 _dep_tmp=$_dep
182 if "$strip"; then
183 i=$stripval
184 while [ "$i" -gt 0 ]; do
185 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
186 _dep_tmp=${_dep_tmp#*/}
187 i=$((i - 1))
188 done
191 bn="$(basename "$_dep_tmp.diff")"
192 dn="$(dirname "$_dep_tmp.diff")/"
193 [ "x$dn" = "x./" ] && dn=""
195 if "$flatten" && [ "$dn" ]; then
196 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
197 dn=""
200 unset _dep_tmp
202 if [ -e "$playground/$_dep" ]; then
203 # We've already seen this dep
204 return
207 mkdir -p "$playground/$(dirname "$_dep")";
208 touch "$playground/$_dep";
210 if branch_empty "$_dep"; then
211 echo "Skip empty patch $_dep";
212 else
213 if "$numbered"; then
214 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
215 bn="$(printf "%04u-$bn" $number)";
216 echo "$number" >"$playground/^number";
219 echo "Exporting $_dep"
220 mkdir -p "$output/$dn";
221 $tg patch "$_dep" >"$output/$dn$bn"
222 echo "$dn$bn -p1" >>"$output/series"
226 linearize()
228 if test ! -f "$playground/^BASE"; then
229 head="$(git rev-parse --verify "$_dep")"
230 echo "$head" > "$playground/^BASE"
231 git checkout -q "$head"
232 return;
235 head=$(git rev-parse --verify HEAD)
237 if [ -z "$_dep_is_tgish" ]; then
238 # merge in $_dep unless already included
239 rev="$(git rev-parse --verify "$_dep")";
240 common="$(git merge-base --all HEAD "$_dep")";
241 if test "$rev" = "$common"; then
242 # already included, just skip
244 else
245 retmerge=0;
247 git merge -s recursive "$_dep" || retmerge="$?";
248 if test "x$retmerge" != "x0"; then
249 echo fix up the merge, commit and then exit;
250 #todo error handling
251 sh -i </dev/tty;
254 else
255 retmerge=0;
257 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
259 if test "x$retmerge" != "x0"; then
260 git rerere;
261 echo "fix up the merge and update the index. Don't commit!"
262 #todo error handling
263 sh -i </dev/tty;
266 result_tree=$(git write-tree)
267 # testing branch_empty might not always give the right answer.
268 # It can happen that the patch is non-empty but still after
269 # linearizing there is no change. So compare the trees.
270 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
271 echo "skip empty commit $_dep";
272 else
273 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
274 git update-ref HEAD $newcommit $head
275 echo "exported commit $_dep";
280 ## Machinery
282 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
283 [ -n "$output" ] ||
284 die "no target branch specified"
285 ! ref_exists "$output" ||
286 die "target branch '$output' already exists; first run: git branch -D $output"
288 elif [ "$driver" = "quilt" ]; then
289 [ -n "$output" ] ||
290 die "no target directory specified"
291 [ ! -e "$output" ] ||
292 die "target directory already exists: $output"
294 mkdir -p "$output"
298 driver()
300 # FIXME should we abort on missing dependency?
301 [ -z "$_dep_missing" ] || return 0
303 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
305 case $_dep in refs/remotes/*) return;; esac
306 branch_needs_update >/dev/null
307 [ "$_ret" -eq 0 ] ||
308 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
310 $driver
313 # Call driver on all the branches - this will happen
314 # in topological order.
315 if "$allbranches" ; then
316 _dep_is_tgish=1
317 non_annihilated_branches |
318 while read _dep; do
319 driver
320 done
321 elif [ -z "$branches" ]; then
322 recurse_deps driver "$name"
323 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
324 else
325 echo "$branches" | tr ',' '\n' | while read _dep; do
326 _dep_is_tgish=1
327 $driver
328 done
329 name="$(echo "$branches" | sed 's/.*,//')"
333 if [ "$driver" = "collapse" ]; then
334 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
336 depcount="$(cat "$playground/^ticker" | wc -l)"
337 echo "Exported topic branch $name (total $depcount topics) to branch $output"
339 elif [ "$driver" = "quilt" ]; then
340 depcount="$(cat "$output/series" | wc -l)"
341 echo "Exported topic branch $name (total $depcount topics) to directory $output"
343 elif [ "$driver" = "linearize" ]; then
344 git checkout -q -b $output
346 echo $name
347 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
348 echo "Warning: Exported result doesn't match";
349 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
350 #git diff $head HEAD;
355 # vim:noet