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
="$(verify_topgit_branch HEAD)"
92 read -r nowsecs nowtzoff
<<EOT
95 playground
="$(get_temp tg-export -d)"
102 nowsecs
=$
(( $nowsecs + 1 ))
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") |
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.
146 rm -f "$playground/^pre" "$playground/^post"
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
156 echo "TopGit-driven merge of branches
:"
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
168 create_tg_commit
"$name" "$(pretty_tree $name)" "$parent"
171 echo "$name" >>"$playground/^ticker"
175 # This will collapse a single branch, using information about
176 # previously collapsed branches stored in $playground.
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" --)"
188 # First time hitting this dep; the common case
189 echo "Collapsing $_dep"
190 commit
="$(collapsed_commit "$_dep")"
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"
206 if [ -z "$_dep_is_tgish" ]; then
207 # This dep is not for rewrite
215 while [ "$i" -gt 0 ]; do
216 [ "$_dep_tmp" = "${_dep_tmp#*/}" ] && break
217 _dep_tmp
=${_dep_tmp#*/}
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')"
233 if [ -e "$playground/$_dep" ]; then
234 # We've already seen this dep
238 mkdir
-p "$playground/$(dirname "$_dep")"
239 touch "$playground/$_dep"
241 if branch_empty
"$_dep"; then
242 echo "Skip empty patch $_dep"
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"
259 if test ! -f "$playground/^BASE"; then
260 if [ -n "$_dep_is_tgish" ]; then
261 head="$(git rev-parse --verify "refs
/top-bases
/$_dep" --)"
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
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."
286 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
292 git merge-recursive
"$(pretty_tree "$_dep" -b)" -- HEAD
"$(pretty_tree "$_dep")" || retmerge
="$?"
294 if test "x$retmerge" != "x0"; then
296 echo "fix up the merge, update the index and then exit. Don't commit!"
298 "${SHELL:-@SHELL_PATH@}" -i </dev
/tty
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"
309 newcommit
=$
(create_tg_commit
"$_dep" "$result_tree" HEAD
)
311 git update-ref HEAD
$newcommit $head
312 echo "exported commit $_dep"
319 if [ "$driver" = "collapse" ] ||
[ "$driver" = "linearize" ]; then
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"
330 elif [ "$driver" = "quilt" ]; then
332 die
"no target directory specified"
333 [ ! -e "$output" ] ||
334 die
"target directory already exists: $output"
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
350 die
"cancelling export of $_dep (-> $_name): branch not up-to-date"
355 # Call driver on all the branches - this will happen
356 # in topological order.
357 if "$allbranches" ; then
359 non_annihilated_branches |
363 elif [ -z "$branches" ]; then
364 recurse_deps driver
"$name"
365 (_ret
=0; _dep
="$name"; _name
=; _dep_is_tgish
=1; _dep_missing
=; driver
)
367 echo "$branches" |
tr ',' '\n' |
while read _dep
; do
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 \"\""
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
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
" --)"