[TOPGIT] make creating a commit from a topgit branch a function
[topgit/tsort.git] / tg-export.sh
blobdea24d9d6fa024ba3da1103a584991e7d9f4c7ec
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 output=
9 driver=collapse
10 flatten=false
11 numbered=false
14 ## Parse options
16 while [ -n "$1" ]; do
17 arg="$1"; shift
18 case "$arg" in
19 -b)
20 branches="$1"; shift;;
21 --flatten)
22 flatten=true;;
23 --numbered)
24 flatten=true;
25 numbered=true;;
26 --quilt)
27 driver=quilt;;
28 --collapse)
29 driver=collapse;;
30 -*)
31 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
32 exit 1;;
34 [ -z "$output" ] || die "output already specified ($output)"
35 output="$arg";;
36 esac
37 done
41 [ -z "$branches" -o "$driver" = "quilt" ] ||
42 die "-b works only with the quilt driver"
44 [ "$driver" = "quilt" ] || ! "$numbered" ||
45 die "--numbered works only with the quilt driver";
47 [ "$driver" = "quilt" ] || ! "$flatten" ||
48 die "--flatten works only with the quilt driver"
50 if [ -z "$branches" ]; then
51 # this check is only needed when no branches have been passed
52 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
53 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
54 die "not on a TopGit-controlled branch"
58 playground="$(mktemp -d -t tg-export.XXXXXX)"
59 trap 'rm -rf "$playground"' EXIT
62 ## Collapse driver
64 # pretty_tree NAME
65 # Output tree ID of a cleaned-up tree without tg's artifacts.
66 pretty_tree()
68 (export GIT_INDEX_FILE="$playground/^index"
69 git read-tree "$1"
70 git update-index --force-remove ".topmsg" ".topdeps"
71 git write-tree)
74 create_tg_commit()
76 name="$1"
77 tree="$2"
78 parent="$3"
80 # Get commit message and authorship information
81 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
83 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
84 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
85 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
86 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
88 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
89 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
90 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
92 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
93 git stripspace |
94 git commit-tree "$tree" -p "$parent"
97 # collapsed_commit NAME
98 # Produce a collapsed commit of branch NAME.
99 collapsed_commit()
101 name="$1"
103 rm -f "$playground/^pre" "$playground/^post"
104 >"$playground/^body"
106 # Determine parent
107 parent="$(cut -f 1 "$playground/$name^parents")"
108 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
109 # Produce a merge commit first
110 parent="$({
111 echo "TopGit-driven merge of branches:"
112 echo
113 cut -f 2 "$playground/$name^parents"
114 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
115 $(for p in $parent; do echo -p $p; done))"
118 if branch_empty "$name"; then
119 echo "$parent";
120 else
121 create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
124 echo "$name" >>"$playground/^ticker"
127 # collapse
128 # This will collapse a single branch, using information about
129 # previously collapsed branches stored in $playground.
130 collapse()
132 if [ -s "$playground/$_dep" ]; then
133 # We've already seen this dep
134 commit="$(cat "$playground/$_dep")"
136 elif [ -z "$_dep_is_tgish" ]; then
137 # This dep is not for rewrite
138 commit="$(git rev-parse --verify "$_dep")"
140 else
141 # First time hitting this dep; the common case
142 echo "Collapsing $_dep"
143 commit="$(collapsed_commit "$_dep")"
144 mkdir -p "$playground/$(dirname "$_dep")"
145 echo "$commit" >"$playground/$_dep"
148 # Propagate our work through the dependency chain
149 mkdir -p "$playground/$(dirname "$_name")"
150 echo "$commit $_dep" >>"$playground/$_name^parents"
154 ## Quilt driver
156 quilt()
158 if [ -z "$_dep_is_tgish" ]; then
159 # This dep is not for rewrite
160 return
163 if "$flatten"; then
164 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
165 dn="";
166 else
167 bn="$(basename "$_dep.diff")";
168 dn="$(dirname "$_dep.diff")/";
169 if [ "x$dn" = "x./" ]; then
170 dn="";
174 if [ -e "$playground/$_dep" ]; then
175 # We've already seen this dep
176 return
179 mkdir -p "$playground/$(dirname "$_dep")";
180 touch "$playground/$_dep";
182 if branch_empty "$_dep"; then
183 echo "Skip empty patch $_dep";
184 else
185 if "$numbered"; then
186 number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
187 bn="$number-$bn";
188 echo "$number" >"$playground/^number";
191 echo "Exporting $_dep"
192 mkdir -p "$output/$dn";
193 $tg patch "$_dep" >"$output/$dn$bn"
194 echo "$dn$bn -p1" >>"$output/series"
199 ## Machinery
201 if [ "$driver" = "collapse" ]; then
202 [ -n "$output" ] ||
203 die "no target branch specified"
204 ! ref_exists "$output" ||
205 die "target branch '$output' already exists; first run: git branch -D $output"
207 elif [ "$driver" = "quilt" ]; then
208 [ -n "$output" ] ||
209 die "no target directory specified"
210 [ ! -e "$output" ] ||
211 die "target directory already exists: $output"
213 mkdir -p "$output"
217 driver()
219 case $_dep in refs/remotes/*) return;; esac
220 branch_needs_update >/dev/null
221 [ "$_ret" -eq 0 ] ||
222 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
224 $driver
227 # Call driver on all the branches - this will happen
228 # in topological order.
229 if [ -z "$branches" ]; then
230 recurse_deps driver "$name"
231 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
232 else
233 echo "$branches" | tr ',' '\n' | while read _dep; do
234 _dep_is_tgish=1
235 $driver
236 done
237 name="$(echo "$branches" | sed 's/.*,//')"
241 if [ "$driver" = "collapse" ]; then
242 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
244 depcount="$(cat "$playground/^ticker" | wc -l)"
245 echo "Exported topic branch $name (total $depcount topics) to branch $output"
247 elif [ "$driver" = "quilt" ]; then
248 depcount="$(cat "$output/series" | wc -l)"
249 echo "Exported topic branch $name (total $depcount topics) to directory $output"
252 # vim:noet