tg export (quilt): Implement numbering the patches
[topgit.git] / tg-export.sh
blob9e6940fcff23fc1a6bbd5ded2b9a7bf037ece54c
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 # collapsed_commit NAME
75 # Produce a collapsed commit of branch NAME.
76 collapsed_commit()
78 name="$1"
80 rm -f "$playground/^pre" "$playground/^post"
81 >"$playground/^body"
83 # Get commit message and authorship information
84 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
86 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
87 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
88 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
89 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
91 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
92 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
93 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
95 # Determine parent
96 parent="$(cut -f 1 "$playground/$name^parents")"
97 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
98 # Produce a merge commit first
99 parent="$({
100 echo "TopGit-driven merge of branches:"
101 echo
102 cut -f 2 "$playground/$name^parents"
103 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
104 $(for p in $parent; do echo -p $p; done))"
107 if branch_empty "$name"; then
108 echo "$parent";
109 else
110 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
111 git stripspace |
112 git commit-tree "$(pretty_tree "$name")" -p "$parent"
115 echo "$name" >>"$playground/^ticker"
118 # collapse
119 # This will collapse a single branch, using information about
120 # previously collapsed branches stored in $playground.
121 collapse()
123 if [ -s "$playground/$_dep" ]; then
124 # We've already seen this dep
125 commit="$(cat "$playground/$_dep")"
127 elif [ -z "$_dep_is_tgish" ]; then
128 # This dep is not for rewrite
129 commit="$(git rev-parse --verify "$_dep")"
131 else
132 # First time hitting this dep; the common case
133 echo "Collapsing $_dep"
134 commit="$(collapsed_commit "$_dep")"
135 mkdir -p "$playground/$(dirname "$_dep")"
136 echo "$commit" >"$playground/$_dep"
139 # Propagate our work through the dependency chain
140 mkdir -p "$playground/$(dirname "$_name")"
141 echo "$commit $_dep" >>"$playground/$_name^parents"
145 ## Quilt driver
147 quilt()
149 if [ -z "$_dep_is_tgish" ]; then
150 # This dep is not for rewrite
151 return
154 if "$flatten"; then
155 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
156 dn="";
157 else
158 bn="$(basename "$_dep.diff")";
159 dn="$(dirname "$_dep.diff")/";
160 if [ "x$dn" = "x./" ]; then
161 dn="";
165 if [ -e "$playground/$_dep" ]; then
166 # We've already seen this dep
167 return
170 mkdir -p "$playground/$(dirname "$_dep")";
171 touch "$playground/$_dep";
173 if branch_empty "$_dep"; then
174 echo "Skip empty patch $_dep";
175 else
176 if "$numbered"; then
177 number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
178 bn="$number-$bn";
179 echo "$number" >"$playground/^number";
182 echo "Exporting $_dep"
183 mkdir -p "$output/$dn";
184 $tg patch "$_dep" >"$output/$dn$bn"
185 echo "$dn$bn -p1" >>"$output/series"
190 ## Machinery
192 if [ "$driver" = "collapse" ]; then
193 [ -n "$output" ] ||
194 die "no target branch specified"
195 ! ref_exists "$output" ||
196 die "target branch '$output' already exists; first run: git branch -D $output"
198 elif [ "$driver" = "quilt" ]; then
199 [ -n "$output" ] ||
200 die "no target directory specified"
201 [ ! -e "$output" ] ||
202 die "target directory already exists: $output"
204 mkdir -p "$output"
208 driver()
210 case $_dep in refs/remotes/*) return;; esac
211 branch_needs_update >/dev/null
212 [ "$_ret" -eq 0 ] ||
213 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
215 $driver
218 # Call driver on all the branches - this will happen
219 # in topological order.
220 if [ -z "$branches" ]; then
221 recurse_deps driver "$name"
222 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
223 else
224 echo "$branches" | tr ',' '\n' | while read _dep; do
225 _dep_is_tgish=1
226 $driver
227 done
228 name="$(echo "$branches" | sed 's/.*,//')"
232 if [ "$driver" = "collapse" ]; then
233 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
235 depcount="$(cat "$playground/^ticker" | wc -l)"
236 echo "Exported topic branch $name (total $depcount topics) to branch $output"
238 elif [ "$driver" = "quilt" ]; then
239 depcount="$(cat "$output/series" | wc -l)"
240 echo "Exported topic branch $name (total $depcount topics) to directory $output"
243 # vim:noet