gnomake: suppress unwanted gratuitous "Entering directory ..." lines
[topgit/pro.git] / tg-export.sh
blobbcf6f418d6bf939a1028977646759b9d7bc41523
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) 2008 Petr Baudis <pasky@suse.cz>
4 # Copyright (C) 2015-2017 Kyle J. McKay <mackyle@gmail.com>
5 # All rights reserved
6 # GPLv2
8 name=
9 branches=
10 forcebranch=
11 checkout_opt=-b
12 output=
13 driver=collapse
14 flatten=false
15 numbered=false
16 strip=false
17 stripval=0
18 allbranches=false
19 binary=
20 pl=
22 ## Parse options
24 while [ -n "$1" ]; do
25 arg="$1"; shift
26 case "$arg" in
27 -a|--all)
28 allbranches=true;;
29 -b)
30 branches="${branches:+$branches }$1"; shift;;
31 --force)
32 forcebranch=1;;
33 --flatten)
34 flatten=true;;
35 --binary)
36 binary=1;;
37 --numbered)
38 flatten=true
39 numbered=true;;
40 --strip*)
41 val=${arg#*=}
42 if [ "$val" = "--strip" ]; then
43 strip=true
44 stripval=9999
45 elif [ -n "$val" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; then
46 strip=true
47 stripval=$val
48 else
49 die "invalid parameter $arg"
50 fi;;
51 --quilt)
52 driver=quilt;;
53 --collapse)
54 driver=collapse;;
55 --linearize)
56 driver=linearize;;
57 -*)
58 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
59 exit 1;;
61 [ -z "$output" ] || die "output already specified ($output)"
62 output="$arg";;
63 esac
64 done
66 [ -z "$branches" -o "$driver" = "quilt" ] ||
67 die "-b works only with the quilt driver"
69 [ "$driver" = "quilt" ] || ! "$numbered" ||
70 die "--numbered works only with the quilt driver"
72 [ "$driver" = "quilt" ] || ! "$flatten" ||
73 die "--flatten works only with the quilt driver"
75 [ "$driver" = "quilt" ] || [ -z "$binary" ] ||
76 die "--binary works only with the quilt driver"
78 [ "$driver" = "quilt" ] || ! "$strip" ||
79 die "--strip works only with the quilt driver"
81 [ "$driver" = "quilt" ] || ! "$allbranches" ||
82 die "--all works only with the quilt driver"
84 [ -z "$branches" ] || ! "$allbranches" ||
85 die "-b conflicts with the --all option"
87 if [ "$driver" = "linearize" ]; then
88 setup_git_dir_is_bare
89 if [ -n "$git_dir_is_bare" ]; then
90 fatal 'export --linearize does not work on a bare repository...yet!'
91 ensure_work_tree
95 if [ -z "$branches" ] && ! "$allbranches"; then
96 # this check is only needed when no branches have been passed
97 name="$(verify_topgit_branch HEAD)"
100 if [ -n "$branches" ]; then
101 oldbranches="$branches"
102 branches=
103 while read bname && [ -n "$bname" ]; do
104 branches="${branches:+$branches }$(verify_topgit_branch "$bname")"
105 done <<-EOT
106 $(sed 'y/ /\n/' <<-LIST
107 $oldbranches
108 LIST
111 unset oldbranches bname
114 read -r nowsecs nowtzoff <<EOT
115 $(date '+%s %z')
117 playground="$(get_temp tg-export -d)"
120 ## Collapse driver
122 bump_timestamp()
124 nowsecs=$(( $nowsecs + 1 ))
127 create_tg_commit()
129 name="$1"
130 tree="$2"
131 parent="$3"
133 # Get commit message and authorship information
134 git cat-file blob "$name:.topmsg" 2>/dev/null |
135 git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
137 unset GIT_AUTHOR_NAME
138 unset GIT_AUTHOR_EMAIL
140 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
141 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
142 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
143 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
145 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
146 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
148 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
149 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
150 export GIT_AUTHOR_DATE
151 export GIT_COMMITTER_DATE
153 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
154 git stripspace |
155 git commit-tree "$tree" -p "$parent"
157 unset GIT_AUTHOR_NAME
158 unset GIT_AUTHOR_EMAIL
159 unset GIT_AUTHOR_DATE
160 unset GIT_COMMITTER_DATE
163 # collapsed_commit NAME
164 # Produce a collapsed commit of branch NAME.
165 collapsed_commit()
167 name="$1"
169 rm -f "$playground/^pre" "$playground/^post"
170 >"$playground/^body"
172 # Determine parent
173 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name^0" -- >> "$playground/$name^parents"
174 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
175 while read -r p; do git rev-parse --quiet --verify "$p^0" -- || :; done)"
176 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
177 # Produce a merge commit first
178 parent="$({
179 echo "TopGit-driven merge of branches:"
180 echo
181 cut -f 2 "$playground/$name^parents"
182 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
183 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
184 git commit-tree "$(pretty_tree "$name" -b)" \
185 $(for p in $parent; do echo "-p $p"; done))"
188 if branch_empty "$name"; then
189 echol "$parent"
190 else
191 create_tg_commit "$name" "$(pretty_tree "$name")" "$parent"
194 echol "$name" >>"$playground/^ticker"
197 # collapse
198 # This will collapse a single branch, using information about
199 # previously collapsed branches stored in $playground.
200 collapse()
202 if [ -s "$playground/$_dep" ]; then
203 # We've already seen this dep
204 commit="$(cat "$playground/$_dep")"
206 elif [ -z "$_dep_is_tgish" ]; then
207 # This dep is not for rewrite
208 commit="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
210 else
211 # First time hitting this dep; the common case
212 echo "Collapsing $_dep"
213 commit="$(collapsed_commit "$_dep")"
214 bump_timestamp
215 _depdn="${_dep%/}"
216 case "$_depdn" in */*);;*) _depdn="./$_depdn"; esac
217 mkdir -p "$playground/${_depdn%/*}"
218 echol "$commit" >"$playground/$_dep"
221 # Propagate our work through the dependency chain
222 _namedn="${_name%/}"
223 case "$_namedn" in */*);;*) _namedn="./$_namedn"; esac
224 mkdir -p "$playground/${_namedn%/*}"
225 echo "$commit $_dep" >>"$playground/$_name^parents"
229 ## Quilt driver
231 quilt()
233 if [ -z "$_dep_is_tgish" ]; then
234 # This dep is not for rewrite
235 return
238 _dep_tmp=$_dep
240 if "$strip"; then
241 i=$stripval
242 while [ "$i" -gt 0 ]; do
243 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
244 _dep_tmp=${_dep_tmp#*/}
245 i=$((i - 1))
246 done
249 dn="${_dep_tmp%/}.diff"
250 case "$dn" in */*);;*) dn="./$dn"; esac
251 bn="${dn##*/}"
252 dn="${dn%/*}/"
253 [ "x$dn" = "x./" ] && dn=""
255 if "$flatten" && [ "$dn" ]; then
256 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
257 dn=""
260 unset _dep_tmp
262 if [ -e "$playground/$_dep" ]; then
263 # We've already seen this dep
264 return
267 _depdn="${_dep%/}"
268 case "$_depdn" in */*);;*) _depdn="./$_depdn"; esac
269 mkdir -p "$playground/${_depdn%/*}"
270 >>"$playground/$_dep"
272 if branch_empty "$_dep"; then
273 echo "Skip empty patch $_dep"
274 else
275 if "$numbered"; then
276 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
277 bn="$(printf "%04u-$bn" $number)"
278 echo "$number" >"$playground/^number"
281 echo "Exporting $_dep"
282 mkdir -p "$output/$dn"
283 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
284 echol "$dn$bn -p1" >>"$output/series"
288 linearize()
290 if test ! -f "$playground/^BASE"; then
291 if [ -n "$_dep_is_tgish" ]; then
292 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
293 else
294 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
296 echol "$head" > "$playground/^BASE"
297 git checkout -q $iowopt "$head"
298 [ -n "$_dep_is_tgish" ] || return 0
301 head=$(git rev-parse --verify HEAD --)
303 if [ -z "$_dep_is_tgish" ]; then
304 # merge in $_dep unless already included
305 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
306 common="$(git merge-base --all HEAD "$rev")" || :
307 if test "$rev" = "$common"; then
308 # already included, just skip
310 else
311 retmerge=0
313 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
314 if test "x$retmerge" != "x0"; then
315 echo "fix up the merge, commit and then exit."
316 #todo error handling
317 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
320 else
321 retmerge=0
323 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
325 if test "x$retmerge" != "x0"; then
326 git rerere
327 echo "fix up the merge, update the index and then exit. Don't commit!"
328 #todo error handling
329 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
330 git rerere
333 result_tree=$(git write-tree)
334 # testing branch_empty might not always give the right answer.
335 # It can happen that the patch is non-empty but still after
336 # linearizing there is no change. So compare the trees.
337 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
338 echo "skip empty commit $_dep"
339 else
340 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
341 bump_timestamp
342 git update-ref HEAD $newcommit $head
343 echo "exported commit $_dep"
348 ## Machinery
350 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
351 [ -n "$output" ] ||
352 die "no target branch specified"
353 if ! ref_exists "refs/heads/$output"; then
355 elif [ -z "$forcebranch" ]; then
356 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
357 else
358 checkout_opt=-B
360 ensure_ident_available
362 elif [ "$driver" = "quilt" ]; then
363 [ -n "$output" ] ||
364 die "no target directory specified"
365 [ ! -e "$output" ] ||
366 die "target directory already exists: $output"
368 mkdir -p "$output"
372 driver()
374 # FIXME should we abort on missing dependency?
375 [ -z "$_dep_missing" ] || return 0
377 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
379 case $_dep in refs/remotes/*) return;; esac
380 branch_needs_update >/dev/null
381 [ "$_ret" -eq 0 ] ||
382 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
384 $driver
387 # Call driver on all the branches - this will happen
388 # in topological order.
389 if "$allbranches" ; then
390 _dep_is_tgish=1
391 non_annihilated_branches |
392 while read _dep; do
393 driver
394 done
395 test $? -eq 0
396 elif [ -z "$branches" ]; then
397 recurse_deps driver "$name"
398 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
399 test $? -eq 0
400 else
401 while read _dep && [ -n "$_dep" ]; do
402 _dep_is_tgish=1
403 $driver
404 done <<-EOT
405 $(sed 'y/ /\n/' <<-LIST
406 $branches
407 LIST
410 name="$branches"
411 case "$branches" in *" "*) pl="es"; esac
415 if [ "$driver" = "collapse" ]; then
416 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
417 [ -n "$forcebranch" ] || cmd="$cmd \"\""
418 eval "$cmd"
420 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
421 echo "Exported topic branch $name (total $depcount topics) to branch $output"
423 elif [ "$driver" = "quilt" ]; then
424 depcount=$(( $(cat "$output/series" | wc -l) ))
425 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
427 elif [ "$driver" = "linearize" ]; then
428 git checkout -q $iowopt $checkout_opt $output
430 echol "$name"
431 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
432 echo "Warning: Exported result doesn't match"
433 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
434 #git diff $head HEAD