tg-export.sh: contravene collapse collisions
[topgit/pro.git] / tg-export.sh
blob974d9255e8364af2a6413e681250eb8b5deb6a8c
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^commit" ]; then
203 # We've already seen this dep
204 commit="$(cat "$playground/$_dep^commit")"
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 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
214 commit="$(collapsed_commit "$_dep")"
215 bump_timestamp
216 echol "$commit" >"$playground/$_dep^commit"
219 # Propagate our work through the dependency chain
220 test -d "$playground/${_name%/*}" || mkdir -p "$playground/${_name%/*}"
221 echo "$commit $_dep" >>"$playground/$_name^parents"
225 ## Quilt driver
227 quilt()
229 if [ -z "$_dep_is_tgish" ]; then
230 # This dep is not for rewrite
231 return
234 _dep_tmp=$_dep
236 if "$strip"; then
237 i=$stripval
238 while [ "$i" -gt 0 ]; do
239 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
240 _dep_tmp=${_dep_tmp#*/}
241 i=$((i - 1))
242 done
245 dn="${_dep_tmp%/}.diff"
246 case "$dn" in */*);;*) dn="./$dn"; esac
247 bn="${dn##*/}"
248 dn="${dn%/*}/"
249 [ "x$dn" = "x./" ] && dn=""
251 if "$flatten" && [ "$dn" ]; then
252 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
253 dn=""
256 unset _dep_tmp
258 if [ -e "$playground/$_dep^commit" ]; then
259 # We've already seen this dep
260 return
263 test -d "$playground/${_dep%/*}" || mkdir -p "$playground/${_dep%/*}"
264 >>"$playground/$_dep^commit"
266 if branch_empty "$_dep"; then
267 echo "Skip empty patch $_dep"
268 else
269 if "$numbered"; then
270 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
271 bn="$(printf "%04u-$bn" $number)"
272 echo "$number" >"$playground/^number"
275 echo "Exporting $_dep"
276 mkdir -p "$output/$dn"
277 tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
278 echol "$dn$bn -p1" >>"$output/series"
282 linearize()
284 if test ! -f "$playground/^BASE"; then
285 if [ -n "$_dep_is_tgish" ]; then
286 head="$(git rev-parse --verify "refs/$topbases/$_dep^0" --)"
287 else
288 head="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
290 echol "$head" > "$playground/^BASE"
291 git checkout -q $iowopt "$head"
292 [ -n "$_dep_is_tgish" ] || return 0
295 head=$(git rev-parse --verify HEAD --)
297 if [ -z "$_dep_is_tgish" ]; then
298 # merge in $_dep unless already included
299 rev="$(git rev-parse --verify "refs/heads/$_dep^0" --)"
300 common="$(git merge-base --all HEAD "$rev")" || :
301 if test "$rev" = "$common"; then
302 # already included, just skip
304 else
305 retmerge=0
307 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "refs/heads/$_dep^0" || retmerge="$?"
308 if test "x$retmerge" != "x0"; then
309 echo "fix up the merge, commit and then exit."
310 #todo error handling
311 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
314 else
315 retmerge=0
317 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
319 if test "x$retmerge" != "x0"; then
320 git rerere
321 echo "fix up the merge, update the index and then exit. Don't commit!"
322 #todo error handling
323 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
324 git rerere
327 result_tree=$(git write-tree)
328 # testing branch_empty might not always give the right answer.
329 # It can happen that the patch is non-empty but still after
330 # linearizing there is no change. So compare the trees.
331 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
332 echo "skip empty commit $_dep"
333 else
334 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
335 bump_timestamp
336 git update-ref HEAD $newcommit $head
337 echo "exported commit $_dep"
342 ## Machinery
344 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
345 [ -n "$output" ] ||
346 die "no target branch specified"
347 if ! ref_exists "refs/heads/$output"; then
349 elif [ -z "$forcebranch" ]; then
350 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
351 else
352 checkout_opt=-B
354 ensure_ident_available
356 elif [ "$driver" = "quilt" ]; then
357 [ -n "$output" ] ||
358 die "no target directory specified"
359 [ ! -e "$output" ] ||
360 die "target directory already exists: $output"
362 mkdir -p "$output"
366 driver()
368 # FIXME should we abort on missing dependency?
369 [ -z "$_dep_missing" ] || return 0
371 [ -z "$_dep_is_tgish" ] || [ -z "$_dep_annihilated" ] || return 0
373 case $_dep in ":"*) return; esac
374 branch_needs_update >/dev/null
375 [ "$_ret" -eq 0 ] ||
376 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
378 $driver
381 # Call driver on all the branches - this will happen
382 # in topological order.
383 if "$allbranches" ; then
384 _dep_is_tgish=1
385 non_annihilated_branches |
386 while read _dep; do
387 driver
388 done
389 test $? -eq 0
390 elif [ -z "$branches" ]; then
391 recurse_deps driver "$name"
392 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
393 test $? -eq 0
394 else
395 while read _dep && [ -n "$_dep" ]; do
396 _dep_is_tgish=1
397 $driver
398 done <<-EOT
399 $(sed 'y/ /\n/' <<-LIST
400 $branches
401 LIST
404 name="$branches"
405 case "$branches" in *" "*) pl="es"; esac
409 if [ "$driver" = "collapse" ]; then
410 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name^commit")"'
411 [ -n "$forcebranch" ] || cmd="$cmd \"\""
412 eval "$cmd"
414 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
415 echo "Exported topic branch $name (total $depcount topics) to branch $output"
417 elif [ "$driver" = "quilt" ]; then
418 depcount=$(( $(cat "$output/series" | wc -l) ))
419 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
421 elif [ "$driver" = "linearize" ]; then
422 git checkout -q $iowopt $checkout_opt $output
424 echol "$name"
425 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
426 echo "Warning: Exported result doesn't match"
427 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
428 #git diff $head HEAD