tg mail -s SEND_EMAIL_ARGS: Pass arguments to git send-email
[topgit.git] / tg-export.sh
blob6098c1a8b4580914aee5143cafaf8b035237e376
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 output=
8 driver=collapse
11 ## Parse options
13 while [ -n "$1" ]; do
14 arg="$1"; shift
15 case "$arg" in
16 --quilt)
17 driver=quilt;;
18 --collapse)
19 driver=collapse;;
20 -*)
21 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | --quilt DIRECTORY)" >&2
22 exit 1;;
24 [ -z "$output" ] || die "output already specified ($output)"
25 output="$arg";;
26 esac
27 done
30 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
31 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
32 die "not on a TopGit-controlled branch"
35 playground="$(mktemp -d -t tg-export.XXXXXX)"
36 trap 'rm -rf "$playground"' EXIT
39 ## Collapse driver
41 # pretty_tree NAME
42 # Output tree ID of a cleaned-up tree without tg's artifacts.
43 pretty_tree()
45 (export GIT_INDEX_FILE="$playground/^index"
46 git read-tree "$1"
47 git update-index --force-remove ".topmsg" ".topdeps"
48 git write-tree)
51 # collapsed_commit NAME
52 # Produce a collapsed commit of branch NAME.
53 collapsed_commit()
55 name="$1"
57 rm -f "$playground/^pre" "$playground/^post"
58 >"$playground/^body"
60 # Get commit message and authorship information
61 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
63 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
64 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
65 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
66 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
68 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
69 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
70 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
72 # Determine parent
73 parent="$(cut -f 1 "$playground/$name^parents")"
74 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
75 # Produce a merge commit first
76 parent="$({
77 echo "TopGit-driven merge of branches:"
78 echo
79 cut -f 2 "$playground/$name^parents"
80 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
81 $(for p in $parent; do echo -p $p; done))"
84 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
85 git stripspace |
86 git commit-tree "$(pretty_tree "$name")" -p "$parent"
88 echo "$name" >>"$playground/^ticker"
91 # collapse
92 # This will collapse a single branch, using information about
93 # previously collapsed branches stored in $playground.
94 collapse()
96 if [ -s "$playground/$_dep" ]; then
97 # We've already seen this dep
98 commit="$(cat "$playground/$_dep")"
100 elif [ -z "$_dep_is_tgish" ]; then
101 # This dep is not for rewrite
102 commit="$(git rev-parse --verify "$_dep")"
104 else
105 # First time hitting this dep; the common case
106 echo "Collapsing $_dep"
107 commit="$(collapsed_commit "$_dep")"
108 mkdir -p "$playground/$(dirname "$_dep")"
109 echo "$commit" >"$playground/$_dep"
112 # Propagate our work through the dependency chain
113 mkdir -p "$playground/$(dirname "$_name")"
114 echo "$commit $_dep" >>"$playground/$_name^parents"
118 ## Quilt driver
120 quilt()
122 if [ -z "$_dep_is_tgish" ]; then
123 # This dep is not for rewrite
124 return
127 filename="$output/$_dep.diff"
128 if [ -e "$filename" ]; then
129 # We've already seen this dep
130 return
133 echo "Exporting $_dep"
134 mkdir -p "$(dirname "$filename")"
135 $tg patch "$_dep" >"$filename"
136 echo "$_dep.diff -p1" >>"$output/series"
140 ## Machinery
142 if [ "$driver" = "collapse" ]; then
143 [ -n "$output" ] ||
144 die "no target branch specified"
145 ! ref_exists "$output" ||
146 die "target branch '$output' already exists; first run: git branch -D $output"
148 elif [ "$driver" = "quilt" ]; then
149 [ -n "$output" ] ||
150 die "no target directory specified"
151 [ ! -e "$output" ] ||
152 die "target directory already exists: $output"
154 mkdir -p "$output"
158 driver()
160 case $_dep in refs/remotes/*) return;; esac
161 branch_needs_update >/dev/null
162 [ "$_ret" -eq 0 ] ||
163 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
165 $driver
168 # Call driver on all the branches - this will happen
169 # in topological order.
170 recurse_deps driver "$name"
171 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
174 if [ "$driver" = "collapse" ]; then
175 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
177 depcount="$(cat "$playground/^ticker" | wc -l)"
178 echo "Exported topic branch $name (total $depcount topics) to branch $output"
180 elif [ "$driver" = "quilt" ]; then
181 depcount="$(cat "$output/series" | wc -l)"
182 echo "Exported topic branch $name (total $depcount topics) to directory $output"