README: add minor updates and clarifications
[topgit/pro.git] / tg-export.sh
blob3b5576b664b2dbf4ae74a01a76710025577958ec
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=
20 ## Parse options
22 while [ -n "$1" ]; do
23 arg="$1"; shift
24 case "$arg" in
25 -a|--all)
26 allbranches=true;;
27 -b)
28 branches="$1"; shift;;
29 --force)
30 forcebranch=1;;
31 --flatten)
32 flatten=true;;
33 --binary)
34 binary=1;;
35 --numbered)
36 flatten=true
37 numbered=true;;
38 --strip*)
39 val=${arg#*=}
40 if [ "$val" = "--strip" ]; then
41 strip=true
42 stripval=9999
43 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
44 strip=true
45 stripval=$val
46 else
47 die "invalid parameter $arg"
48 fi;;
49 --quilt)
50 driver=quilt;;
51 --collapse)
52 driver=collapse;;
53 --linearize)
54 driver=linearize;;
55 -*)
56 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
57 exit 1;;
59 [ -z "$output" ] || die "output already specified ($output)"
60 output="$arg";;
61 esac
62 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 [ -z "$branches" ] && ! "$allbranches"; then
88 # this check is only needed when no branches have been passed
89 name="$(verify_topgit_branch HEAD)"
92 read -r nowsecs nowtzoff <<EOT
93 $(date '+%s %z')
94 EOT
95 playground="$(get_temp tg-export -d)"
98 ## Collapse driver
100 bump_timestamp()
102 nowsecs=$(( $nowsecs + 1 ))
105 create_tg_commit()
107 name="$1"
108 tree="$2"
109 parent="$3"
111 # Get commit message and authorship information
112 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
114 unset GIT_AUTHOR_NAME
115 unset GIT_AUTHOR_EMAIL
117 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
118 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
119 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
120 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
122 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
123 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
125 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
126 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
127 export GIT_AUTHOR_DATE
128 export GIT_COMMITTER_DATE
130 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
131 git stripspace |
132 git commit-tree "$tree" -p "$parent"
134 unset GIT_AUTHOR_NAME
135 unset GIT_AUTHOR_EMAIL
136 unset GIT_AUTHOR_DATE
137 unset GIT_COMMITTER_DATE
140 # collapsed_commit NAME
141 # Produce a collapsed commit of branch NAME.
142 collapsed_commit()
144 name="$1"
146 rm -f "$playground/^pre" "$playground/^post"
147 >"$playground/^body"
149 # Determine parent
150 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/top-bases/$name" -- >> "$playground/$name^parents"
151 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null | \
152 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)"
153 if [ "$(cat "$playground/$name^parents" 2> /dev/null | wc_l)" -gt 1 ]; then
154 # Produce a merge commit first
155 parent="$({
156 echo "TopGit-driven merge of branches:"
157 echo
158 cut -f 2 "$playground/$name^parents"
159 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
160 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
161 git commit-tree "$(pretty_tree "$name" -b)" \
162 $(for p in $parent; do echo -p $p; done))"
165 if branch_empty "$name"; then
166 echo "$parent"
167 else
168 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
171 echo "$name" >>"$playground/^ticker"
174 # collapse
175 # This will collapse a single branch, using information about
176 # previously collapsed branches stored in $playground.
177 collapse()
179 if [ -s "$playground/$_dep" ]; then
180 # We've already seen this dep
181 commit="$(cat "$playground/$_dep")"
183 elif [ -z "$_dep_is_tgish" ]; then
184 # This dep is not for rewrite
185 commit="$(git rev-parse --verify "refs/heads/$_dep" --)"
187 else
188 # First time hitting this dep; the common case
189 echo "Collapsing $_dep"
190 commit="$(collapsed_commit "$_dep")"
191 bump_timestamp
192 mkdir -p "$playground/$(dirname "$_dep")"
193 echo "$commit" >"$playground/$_dep"
196 # Propagate our work through the dependency chain
197 mkdir -p "$playground/$(dirname "$_name")"
198 echo "$commit $_dep" >>"$playground/$_name^parents"
202 ## Quilt driver
204 quilt()
206 if [ -z "$_dep_is_tgish" ]; then
207 # This dep is not for rewrite
208 return
211 _dep_tmp=$_dep
213 if "$strip"; then
214 i=$stripval
215 while [ "$i" -gt 0 ]; do
216 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
217 _dep_tmp=${_dep_tmp#*/}
218 i=$((i - 1))
219 done
222 bn="$(basename "$_dep_tmp.diff")"
223 dn="$(dirname "$_dep_tmp.diff")/"
224 [ "x$dn" = "x./" ] && dn=""
226 if "$flatten" && [ "$dn" ]; then
227 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
228 dn=""
231 unset _dep_tmp
233 if [ -e "$playground/$_dep" ]; then
234 # We've already seen this dep
235 return
238 mkdir -p "$playground/$(dirname "$_dep")"
239 touch "$playground/$_dep"
241 if branch_empty "$_dep"; then
242 echo "Skip empty patch $_dep"
243 else
244 if "$numbered"; then
245 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))"
246 bn="$(printf "%04u-$bn" $number)"
247 echo "$number" >"$playground/^number"
250 echo "Exporting $_dep"
251 mkdir -p "$output/$dn"
252 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
253 echo "$dn$bn -p1" >>"$output/series"
257 linearize()
259 if test ! -f "$playground/^BASE"; then
260 if [ -n "$_dep_is_tgish" ]; then
261 head="$(git rev-parse --verify "refs/top-bases/$_dep" --)"
262 else
263 head="$(git rev-parse --verify "refs/heads/$_dep" --)"
265 echo "$head" > "$playground/^BASE"
266 git checkout -q "$head"
267 [ -n "$_dep_is_tgish" ] || return 0
270 head=$(git rev-parse --verify HEAD --)
272 if [ -z "$_dep_is_tgish" ]; then
273 # merge in $_dep unless already included
274 rev="$(git rev-parse --verify "$_dep" --)"
275 common="$(git merge-base --all HEAD "$_dep" || :)"
276 if test "$rev" = "$common"; then
277 # already included, just skip
279 else
280 retmerge=0
282 git merge -m "tgexport: merge $_dep into base" -s recursive "$_dep^0" || retmerge="$?"
283 if test "x$retmerge" != "x0"; then
284 echo "fix up the merge, commit and then exit."
285 #todo error handling
286 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
289 else
290 retmerge=0
292 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "$_dep")" || retmerge="$?"
294 if test "x$retmerge" != "x0"; then
295 git rerere
296 echo "fix up the merge, update the index and then exit. Don't commit!"
297 #todo error handling
298 "${SHELL:-@SHELL_PATH@}" -i </dev/tty
299 git rerere
302 result_tree=$(git write-tree)
303 # testing branch_empty might not always give the right answer.
304 # It can happen that the patch is non-empty but still after
305 # linearizing there is no change. So compare the trees.
306 if test "x$result_tree" = "x$(git rev-parse --verify $head^{tree} --)"; then
307 echo "skip empty commit $_dep"
308 else
309 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
310 bump_timestamp
311 git update-ref HEAD $newcommit $head
312 echo "exported commit $_dep"
317 ## Machinery
319 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
320 [ -n "$output" ] ||
321 die "no target branch specified"
322 if ! ref_exists "refs/heads/$output"; then
324 elif [ -z "$forcebranch" ]; then
325 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
326 else
327 checkout_opt=-B
330 elif [ "$driver" = "quilt" ]; then
331 [ -n "$output" ] ||
332 die "no target directory specified"
333 [ ! -e "$output" ] ||
334 die "target directory already exists: $output"
336 mkdir -p "$output"
340 driver()
342 # FIXME should we abort on missing dependency?
343 [ -z "$_dep_missing" ] || return 0
345 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
347 case $_dep in refs/remotes/*) return;; esac
348 branch_needs_update >/dev/null
349 [ "$_ret" -eq 0 ] ||
350 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
352 $driver
355 # Call driver on all the branches - this will happen
356 # in topological order.
357 if "$allbranches" ; then
358 _dep_is_tgish=1
359 non_annihilated_branches |
360 while read _dep; do
361 driver
362 done
363 elif [ -z "$branches" ]; then
364 recurse_deps driver "$name"
365 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
366 else
367 echo "$branches" | tr ',' '\n' | while read _dep; do
368 _dep_is_tgish=1
369 $driver
370 done
371 name="$(echo "$branches" | sed 's/.*,//')"
375 if [ "$driver" = "collapse" ]; then
376 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
377 [ -n "$forcebranch" ] || cmd="$cmd \"\""
378 eval "$cmd"
380 depcount="$(cat "$playground/^ticker" | wc_l)"
381 echo "Exported topic branch $name (total $depcount topics) to branch $output"
383 elif [ "$driver" = "quilt" ]; then
384 depcount="$(cat "$output/series" | wc_l)"
385 echo "Exported topic branch $name (total $depcount topics) to directory $output"
387 elif [ "$driver" = "linearize" ]; then
388 git checkout -q $checkout_opt $output
390 echo $name
391 if test $(git rev-parse --verify "$(pretty_tree $name)^{tree}" --) != $(git rev-parse --verify "HEAD^{tree}" --); then
392 echo "Warning: Exported result doesn't match"
393 echo "tg-head=$(git rev-parse --verify "refs/heads/$name" --), exported=$(git rev-parse --verify "HEAD" --)"
394 #git diff $head HEAD
399 # vim:noet