2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
28 branches
="$1"; shift;;
40 if [ "$val" = "--strip" ]; then
43 elif [ -n "$val" -a "x$(echo $val | sed -e 's/[0-9]//g')" = "x" ]; then
47 die
"invalid parameter $arg"
56 echo "Usage: ${tgname:-tg} [...] export ([--collapse] <newbranch> [--force] | [-a | --all | -b <branch1>...] [--binary] --quilt <directory> | --linearize <newbranch> [--force])" >&2
59 [ -z "$output" ] || die
"output already specified ($output)"
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
97 playground
="$(get_temp tg-export -d)"
104 nowsecs
=$
(( $nowsecs + 1 ))
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") |
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.
148 rm -f "$playground/^pre" "$playground/^post"
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
158 echo "TopGit-driven merge of branches
:"
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
170 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
173 echo "$name" >>"$playground/^ticker"
177 # This will collapse a single branch, using information about
178 # previously collapsed branches stored in $playground.
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")"
190 # First time hitting this dep; the common case
191 echo "Collapsing $_dep"
192 commit
="$(collapsed_commit "$_dep")"
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"
208 if [ -z "$_dep_is_tgish" ]; then
209 # This dep is not for rewrite
217 while [ "$i" -gt 0 ]; do
218 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
219 _dep_tmp
=${_dep_tmp#*/}
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')"
235 if [ -e "$playground/$_dep" ]; then
236 # We've already seen this dep
240 mkdir
-p "$playground/$(dirname "$_dep")"
241 touch "$playground/$_dep"
243 if branch_empty
"$_dep"; then
244 echo "Skip empty patch $_dep"
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"
261 if test ! -f "$playground/^BASE"; then
262 if [ -n "$_dep_is_tgish" ]; then
263 head="$(git rev-parse --verify "refs
/top-bases
/$_dep")"
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
284 git merge
-s recursive
"$_dep" || retmerge
="$?"
285 if test "x$retmerge" != "x0"; then
286 echo "fix up the merge, commit and then exit."
288 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
294 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
296 if test "x$retmerge" != "x0"; then
298 echo "fix up the merge, update the index and then exit. Don't commit!"
300 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
304 result_tree
=$
(git write-tree
)
305 # testing branch_empty might not always give the right answer.
306 # It can happen that the patch is non-empty but still after
307 # linearizing there is no change. So compare the trees.
308 if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
309 echo "skip empty commit $_dep"
311 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
313 git update-ref HEAD
$newcommit $head
314 echo "exported commit $_dep"
321 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
323 die
"no target branch specified"
324 if ! ref_exists
"refs/heads/$output"; then
326 elif [ -z "$forcebranch" ]; then
327 die
"target branch '$output' already exists; first run: git$gitcdopt branch -D $output, or run $tgdisplay export with --force"
332 elif [ "$driver" = "quilt" ]; then
334 die
"no target directory specified"
335 [ ! -e "$output" ] ||
336 die
"target directory already exists: $output"
344 # FIXME should we abort on missing dependency?
345 [ -z "$_dep_missing" ] ||
return 0
347 [ -z "$_dep_is_tgish" ] ||
! branch_annihilated
"$_dep" ||
return 0
349 case $_dep in refs
/remotes
/*) return;; esac
350 branch_needs_update
>/dev
/null
352 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
357 # Call driver on all the branches - this will happen
358 # in topological order.
359 if "$allbranches" ; then
361 non_annihilated_branches |
365 elif [ -z "$branches" ]; then
366 recurse_deps driver
"$name"
367 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
369 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
373 name
="$(echo "$branches" | sed 's/.*,//')"
377 if [ "$driver" = "collapse" ]; then
378 cmd
='git update-ref "refs/heads/$output" "$(cat "$playground/$name")"'
379 [ -n "$forcebranch" ] || cmd
="$cmd \"\""
382 depcount
="$(cat "$playground/^ticker
" | wc_l)"
383 echo "Exported topic branch $name (total $depcount topics) to branch $output"
385 elif [ "$driver" = "quilt" ]; then
386 depcount
="$(cat "$output/series
" | wc_l)"
387 echo "Exported topic branch $name (total $depcount topics) to directory $output"
389 elif [ "$driver" = "linearize" ]; then
390 git checkout
-q $checkout_opt $output
393 if test $
(git rev-parse
"$(pretty_tree $name)^{tree}") != $
(git rev-parse
"HEAD^{tree}"); then
394 echo "Warning: Exported result doesn't match"
395 echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD
")"