tg-revert.sh: convert any top-bases in TOPGIT REFS
[topgit/pro.git] / tg-export.sh
blob3273657b8cc5eaf695f356ba56f1db5a8a9536af
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 forcebranch=
9 checkout_opt=-b
10 output=
11 driver=collapse
12 flatten=false
13 numbered=false
14 strip=false
15 stripval=0
16 allbranches=false
17 binary=
18 pl=
21 ## Parse options
23 while [ -n "$1" ]; do
24 arg="$1"; shift
25 case "$arg" in
26 -a|--all)
27 allbranches=true;;
28 -b)
29 branches="${branches:+$branches }$1"; shift;;
30 --force)
31 forcebranch=1;;
32 --flatten)
33 flatten=true;;
34 --binary)
35 binary=1;;
36 --numbered)
37 flatten=true
38 numbered=true;;
39 --strip*)
40 val=${arg#*=}
41 if [ "$val" = "--strip" ]; then
42 strip=true
43 stripval=9999
44 elif [ -n "$val" -a "x$(echol "$val" | sed -e 's/[0-9]//g')" = "x" ]; then
45 strip=true
46 stripval=$val
47 else
48 die "invalid parameter $arg"
49 fi;;
50 --quilt)
51 driver=quilt;;
52 --collapse)
53 driver=collapse;;
54 --linearize)
55 driver=linearize;;
56 -*)
57 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
58 exit 1;;
60 [ -z "$output" ] || die "output already specified ($output)"
61 output="$arg";;
62 esac
63 done
67 [ -z "$branches" -o "$driver" = "quilt" ] ||
68 die "-b works only with the quilt driver"
70 [ "$driver" = "quilt" ] || ! "$numbered" ||
71 die "--numbered works only with the quilt driver"
73 [ "$driver" = "quilt" ] || ! "$flatten" ||
74 die "--flatten works only with the quilt driver"
76 [ "$driver" = "quilt" ] || [ -z "$binary" ] ||
77 die "--binary works only with the quilt driver"
79 [ "$driver" = "quilt" ] || ! "$strip" ||
80 die "--strip works only with the quilt driver"
82 [ "$driver" = "quilt" ] || ! "$allbranches" ||
83 die "--all works only with the quilt driver"
85 [ -z "$branches" ] || ! "$allbranches" ||
86 die "-b conflicts with the --all option"
88 if [ -z "$branches" ] && ! "$allbranches"; then
89 # this check is only needed when no branches have been passed
90 name="$(verify_topgit_branch HEAD)"
93 if [ -n "$branches" ]; then
94 oldbranches="$branches"
95 branches=
96 while read bname && [ -n "$bname" ]; do
97 branches="${branches:+$branches }$(verify_topgit_branch "$bname")"
98 done <<-EOT
99 $(sed 'y/ /\n/' <<-LIST
100 $oldbranches
101 LIST
104 unset oldbranches bname
107 read -r nowsecs nowtzoff <<EOT
108 $(date '+%s %z')
110 playground="$(get_temp tg-export -d)"
113 ## Collapse driver
115 bump_timestamp()
117 nowsecs=$(( $nowsecs + 1 ))
120 create_tg_commit()
122 name="$1"
123 tree="$2"
124 parent="$3"
126 # Get commit message and authorship information
127 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
129 unset GIT_AUTHOR_NAME
130 unset GIT_AUTHOR_EMAIL
132 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
133 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
134 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
135 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
137 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
138 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
140 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
141 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
142 export GIT_AUTHOR_DATE
143 export GIT_COMMITTER_DATE
145 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
146 git stripspace |
147 git commit-tree "$tree" -p "$parent"
149 unset GIT_AUTHOR_NAME
150 unset GIT_AUTHOR_EMAIL
151 unset GIT_AUTHOR_DATE
152 unset GIT_COMMITTER_DATE
155 # collapsed_commit NAME
156 # Produce a collapsed commit of branch NAME.
157 collapsed_commit()
159 name="$1"
161 rm -f "$playground/^pre" "$playground/^post"
162 >"$playground/^body"
164 # Determine parent
165 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/$topbases/$name" -- >> "$playground/$name^parents"
166 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null |
167 while read p; do [ "$(git cat-file -t "$p" 2> /dev/null)" = tag ] && git cat-file tag "$p" | head -1 | cut -d' ' -f2 || echol "$p"; done)"
168 if [ $(( $(cat "$playground/$name^parents" 2>/dev/null | wc -l) )) -gt 1 ]; then
169 # Produce a merge commit first
170 parent="$({
171 echo "TopGit-driven merge of branches:"
172 echo
173 cut -f 2 "$playground/$name^parents"
174 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
175 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
176 git commit-tree "$(pretty_tree "$name" -b)" \
177 $(for p in $parent; do echo "-p $p"; done))"
180 if branch_empty "$name"; then
181 echol "$parent"
182 else
183 create_tg_commit "$name" "$(pretty_tree "$name")" "$parent"
186 echol "$name" >>"$playground/^ticker"
189 # collapse
190 # This will collapse a single branch, using information about
191 # previously collapsed branches stored in $playground.
192 collapse()
194 if [ -s "$playground/$_dep" ]; then
195 # We've already seen this dep
196 commit="$(cat "$playground/$_dep")"
198 elif [ -z "$_dep_is_tgish" ]; then
199 # This dep is not for rewrite
200 commit="$(git rev-parse --verify "refs/heads/$_dep" --)"
202 else
203 # First time hitting this dep; the common case
204 echo "Collapsing $_dep"
205 commit="$(collapsed_commit "$_dep")"
206 bump_timestamp
207 mkdir -p "$playground/$(dirname "$_dep")"
208 echol "$commit" >"$playground/$_dep"
211 # Propagate our work through the dependency chain
212 mkdir -p "$playground/$(dirname "$_name")"
213 echo "$commit $_dep" >>"$playground/$_name^parents"
217 ## Quilt driver
219 quilt()
221 if [ -z "$_dep_is_tgish" ]; then
222 # This dep is not for rewrite
223 return
226 _dep_tmp=$_dep
228 if "$strip"; then
229 i=$stripval
230 while [ "$i" -gt 0 ]; do
231 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
232 _dep_tmp=${_dep_tmp#*/}
233 i=$((i - 1))
234 done
237 bn="$(basename "$_dep_tmp.diff")"
238 dn="$(dirname "$_dep_tmp.diff")/"
239 [ "x$dn" = "x./" ] && dn=""
241 if "$flatten" && [ "$dn" ]; then
242 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
243 dn=""
246 unset _dep_tmp
248 if [ -e "$playground/$_dep" ]; then
249 # We've already seen this dep
250 return
253 mkdir -p "$playground/$(dirname "$_dep")"
254 touch "$playground/$_dep"
256 if branch_empty "$_dep"; then
257 echo "Skip empty patch $_dep"
258 else
259 if "$numbered"; then
260 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
261 bn="$(printf "%04u-$bn" $number)"
262 echo "$number" >"$playground/^number"
265 echo "Exporting $_dep"
266 mkdir -p "$output/$dn"
267 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
268 echol "$dn$bn -p1" >>"$output/series"
272 linearize()
274 if test ! -f "$playground/^BASE"; then
275 if [ -n "$_dep_is_tgish" ]; then
276 head="$(git rev-parse --verify "refs/$topbases/$_dep" --)"
277 else
278 head="$(git rev-parse --verify "refs/heads/$_dep" --)"
280 echol "$head" > "$playground/^BASE"
281 git checkout -q "$head"
282 [ -n "$_dep_is_tgish" ] || return 0
285 head=$(git rev-parse --verify HEAD --)
287 if [ -z "$_dep_is_tgish" ]; then
288 # merge in $_dep unless already included
289 rev="$(git rev-parse --verify "$_dep" --)"
290 common="$(git merge-base --all HEAD "$_dep" || :)"
291 if test "$rev" = "$common"; then
292 # already included, just skip
294 else
295 retmerge=0
297 git merge $auhopt -m "tgexport: merge $_dep into base" -s recursive "$_dep^0" || retmerge="$?"
298 if test "x$retmerge" != "x0"; then
299 echo "fix up the merge, commit and then exit."
300 #todo error handling
301 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
304 else
305 retmerge=0
307 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
309 if test "x$retmerge" != "x0"; then
310 git rerere
311 echo "fix up the merge, update the index and then exit. Don't commit!"
312 #todo error handling
313 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
314 git rerere
317 result_tree=$(git write-tree)
318 # testing branch_empty might not always give the right answer.
319 # It can happen that the patch is non-empty but still after
320 # linearizing there is no change. So compare the trees.
321 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
322 echo "skip empty commit $_dep"
323 else
324 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
325 bump_timestamp
326 git update-ref HEAD $newcommit $head
327 echo "exported commit $_dep"
332 ## Machinery
334 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
335 [ -n "$output" ] ||
336 die "no target branch specified"
337 if ! ref_exists "refs/heads/$output"; then
339 elif [ -z "$forcebranch" ]; then
340 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
341 else
342 checkout_opt=-B
344 ensure_ident_available
346 elif [ "$driver" = "quilt" ]; then
347 [ -n "$output" ] ||
348 die "no target directory specified"
349 [ ! -e "$output" ] ||
350 die "target directory already exists: $output"
352 mkdir -p "$output"
356 driver()
358 # FIXME should we abort on missing dependency?
359 [ -z "$_dep_missing" ] || return 0
361 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
363 case $_dep in refs/remotes/*) return;; esac
364 branch_needs_update >/dev/null
365 [ "$_ret" -eq 0 ] ||
366 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
368 $driver
371 # Call driver on all the branches - this will happen
372 # in topological order.
373 if "$allbranches" ; then
374 _dep_is_tgish=1
375 non_annihilated_branches |
376 while read _dep; do
377 driver
378 done
379 test $? -eq 0
380 elif [ -z "$branches" ]; then
381 recurse_deps driver "$name"
382 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
383 test $? -eq 0
384 else
385 while read _dep && [ -n "$_dep" ]; do
386 _dep_is_tgish=1
387 $driver
388 done <<-EOT
389 $(sed 'y/ /\n/' <<-LIST
390 $branches
391 LIST
394 name="$branches"
395 case "$branches" in *" "*) pl="es"; esac
399 if [ "$driver" = "collapse" ]; then
400 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
401 [ -n "$forcebranch" ] || cmd="$cmd \"\""
402 eval "$cmd"
404 depcount=$(( $(cat "$playground/^ticker" | wc -l) ))
405 echo "Exported topic branch $name (total $depcount topics) to branch $output"
407 elif [ "$driver" = "quilt" ]; then
408 depcount=$(( $(cat "$output/series" | wc -l) ))
409 echo "Exported topic branch$pl $name (total $depcount topics) to directory $output"
411 elif [ "$driver" = "linearize" ]; then
412 git checkout -q $checkout_opt $output
414 echol "$name"
415 if test $(git rev-parse --verify "$(pretty_tree "$name")^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
416 echo "Warning: Exported result doesn't match"
417 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
418 #git diff $head HEAD
423 # vim:noet