tg-patch.sh: allow git diff-tree options to be passed through
[topgit/pro.git] / tg-export.sh
blobca5ed660854f68c4e68de92d1aee5e2b8a4d7b06
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="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
90 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
91 die "not on a TopGit-controlled branch"
94 read -r nowsecs nowtzoff <<EOT
95 $(date '+%s %z')
96 EOT
97 playground="$(get_temp tg-export -d)"
100 ## Collapse driver
102 bump_timestamp()
104 nowsecs=$(( $nowsecs + 1 ))
107 create_tg_commit()
109 name="$1"
110 tree="$2"
111 parent="$3"
113 # Get commit message and authorship information
114 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
116 unset GIT_AUTHOR_NAME
117 unset GIT_AUTHOR_EMAIL
119 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
120 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
121 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
122 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
124 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
125 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
127 GIT_COMMITTER_DATE="$nowsecs $nowtzoff"
128 : ${GIT_AUTHOR_DATE:=$GIT_COMMITTER_DATE}
129 export GIT_AUTHOR_DATE
130 export GIT_COMMITTER_DATE
132 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
133 git stripspace |
134 git commit-tree "$tree" -p "$parent"
136 unset GIT_AUTHOR_NAME
137 unset GIT_AUTHOR_EMAIL
138 unset GIT_AUTHOR_DATE
139 unset GIT_COMMITTER_DATE
142 # collapsed_commit NAME
143 # Produce a collapsed commit of branch NAME.
144 collapsed_commit()
146 name="$1"
148 rm -f "$playground/^pre" "$playground/^post"
149 >"$playground/^body"
151 # Determine parent
152 [ -s "$playground/$name^parents" ] || git rev-parse --verify "refs/top-bases/$name" >> "$playground/$name^parents"
153 parent="$(cut -f 1 "$playground/$name^parents" 2> /dev/null | \
154 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)"
155 if [ "$(cat "$playground/$name^parents" 2> /dev/null | wc_l)" -gt 1 ]; then
156 # Produce a merge commit first
157 parent="$({
158 echo "TopGit-driven merge of branches:"
159 echo
160 cut -f 2 "$playground/$name^parents"
161 } | GIT_AUTHOR_DATE="$nowsecs $nowtzoff" \
162 GIT_COMMITTER_DATE="$nowsecs $nowtzoff" \
163 git commit-tree "$(pretty_tree "$name" -b)" \
164 $(for p in $parent; do echo -p $p; done))"
167 if branch_empty "$name"; then
168 echo "$parent";
169 else
170 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
173 echo "$name" >>"$playground/^ticker"
176 # collapse
177 # This will collapse a single branch, using information about
178 # previously collapsed branches stored in $playground.
179 collapse()
181 if [ -s "$playground/$_dep" ]; then
182 # We've already seen this dep
183 commit="$(cat "$playground/$_dep")"
185 elif [ -z "$_dep_is_tgish" ]; then
186 # This dep is not for rewrite
187 commit="$(git rev-parse --verify "$_dep")"
189 else
190 # First time hitting this dep; the common case
191 echo "Collapsing $_dep"
192 commit="$(collapsed_commit "$_dep")"
193 bump_timestamp
194 mkdir -p "$playground/$(dirname "$_dep")"
195 echo "$commit" >"$playground/$_dep"
198 # Propagate our work through the dependency chain
199 mkdir -p "$playground/$(dirname "$_name")"
200 echo "$commit $_dep" >>"$playground/$_name^parents"
204 ## Quilt driver
206 quilt()
208 if [ -z "$_dep_is_tgish" ]; then
209 # This dep is not for rewrite
210 return
213 _dep_tmp=$_dep
215 if "$strip"; then
216 i=$stripval
217 while [ "$i" -gt 0 ]; do
218 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
219 _dep_tmp=${_dep_tmp#*/}
220 i=$((i - 1))
221 done
224 bn="$(basename "$_dep_tmp.diff")"
225 dn="$(dirname "$_dep_tmp.diff")/"
226 [ "x$dn" = "x./" ] && dn=""
228 if "$flatten" && [ "$dn" ]; then
229 bn="$(echo "$_dep_tmp.diff" | sed -e 's#_#__#g' -e 's#/#_#g')"
230 dn=""
233 unset _dep_tmp
235 if [ -e "$playground/$_dep" ]; then
236 # We've already seen this dep
237 return
240 mkdir -p "$playground/$(dirname "$_dep")";
241 touch "$playground/$_dep";
243 if branch_empty "$_dep"; then
244 echo "Skip empty patch $_dep";
245 else
246 if "$numbered"; then
247 number="$(echo $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
248 bn="$(printf "%04u-$bn" $number)";
249 echo "$number" >"$playground/^number";
252 echo "Exporting $_dep"
253 mkdir -p "$output/$dn";
254 $tg patch ${binary:+--binary} "$_dep" >"$output/$dn$bn"
255 echo "$dn$bn -p1" >>"$output/series"
259 linearize()
261 if test ! -f "$playground/^BASE"; then
262 if [ -n "$_dep_is_tgish" ]; then
263 head="$(git rev-parse --verify "refs/top-bases/$_dep")"
264 else
265 head="$(git rev-parse --verify "refs/heads/$_dep")"
267 echo "$head" > "$playground/^BASE"
268 git checkout -q "$head"
269 [ -n "$_dep_is_tgish" ] || return 0;
272 head=$(git rev-parse --verify HEAD)
274 if [ -z "$_dep_is_tgish" ]; then
275 # merge in $_dep unless already included
276 rev="$(git rev-parse --verify "$_dep")";
277 common="$(git merge-base --all HEAD "$_dep")";
278 if test "$rev" = "$common"; then
279 # already included, just skip
281 else
282 retmerge=0;
284 git merge -s recursive "$_dep" || retmerge="$?";
285 if test "x$retmerge" != "x0"; then
286 echo "fix up the merge, commit and then exit."
287 #todo error handling
288 "${SHELL:-/bin/sh}" -i </dev/tty;
291 else
292 retmerge=0;
294 git merge-recursive "$(pretty_tree "$_dep" -b)" -- HEAD "$(pretty_tree "refs/heads/$_dep")" || retmerge="$?";
296 if test "x$retmerge" != "x0"; then
297 git rerere;
298 echo "fix up the merge, update the index and then exit. Don't commit!"
299 #todo error handling
300 "${SHELL:-/bin/sh}" -i </dev/tty;
303 result_tree=$(git write-tree)
304 # testing branch_empty might not always give the right answer.
305 # It can happen that the patch is non-empty but still after
306 # linearizing there is no change. So compare the trees.
307 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
308 echo "skip empty commit $_dep";
309 else
310 newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
311 bump_timestamp
312 git update-ref HEAD $newcommit $head
313 echo "exported commit $_dep";
318 ## Machinery
320 if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
321 [ -n "$output" ] ||
322 die "no target branch specified"
323 if ! ref_exists $output; then
325 elif [ -z "$forcebranch" ]; then
326 die "target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
327 else
328 checkout_opt=-B
331 elif [ "$driver" = "quilt" ]; then
332 [ -n "$output" ] ||
333 die "no target directory specified"
334 [ ! -e "$output" ] ||
335 die "target directory already exists: $output"
337 mkdir -p "$output"
341 driver()
343 # FIXME should we abort on missing dependency?
344 [ -z "$_dep_missing" ] || return 0
346 [ -z "$_dep_is_tgish" ] || ! branch_annihilated "$_dep" || return 0
348 case $_dep in refs/remotes/*) return;; esac
349 branch_needs_update >/dev/null
350 [ "$_ret" -eq 0 ] ||
351 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
353 $driver
356 # Call driver on all the branches - this will happen
357 # in topological order.
358 if "$allbranches" ; then
359 _dep_is_tgish=1
360 non_annihilated_branches |
361 while read _dep; do
362 driver
363 done
364 elif [ -z "$branches" ]; then
365 recurse_deps driver "$name"
366 (_ret=0; _dep="$name"; _name=; _dep_is_tgish=1; _dep_missing=; driver)
367 else
368 echo "$branches" | tr ',' '\n' | while read _dep; do
369 _dep_is_tgish=1
370 $driver
371 done
372 name="$(echo "$branches" | sed 's/.*,//')"
376 if [ "$driver" = "collapse" ]; then
377 cmd='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
378 [ -n "$forcebranch" ] || cmd="$cmd \"\""
379 eval "$cmd"
381 depcount="$(cat "$playground/^ticker" | wc_l)"
382 echo "Exported topic branch $name (total $depcount topics) to branch $output"
384 elif [ "$driver" = "quilt" ]; then
385 depcount="$(cat "$output/series" | wc_l)"
386 echo "Exported topic branch $name (total $depcount topics) to directory $output"
388 elif [ "$driver" = "linearize" ]; then
389 git checkout -q $checkout_opt $output
391 echo $name
392 if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
393 echo "Warning: Exported result doesn't match";
394 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
395 #git diff $head HEAD;
400 # vim:noet