tg export (quilt): Implement flattening patch paths
[topgit/kirr.git] / tg-export.sh
blobb367aaffcdf43630a6a9bb016c3818909b9f213e
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
13 ## Parse options
15 while [ -n "$1" ]; do
16 arg="$1"; shift
17 case "$arg" in
18 -b)
19 branches="$1"; shift;;
20 --flatten)
21 flatten=true;;
22 --quilt)
23 driver=quilt;;
24 --collapse)
25 driver=collapse;;
26 -*)
27 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
28 exit 1;;
30 [ -z "$output" ] || die "output already specified ($output)"
31 output="$arg";;
32 esac
33 done
37 [ -z "$branches" -o "$driver" = "quilt" ] ||
38 die "-b works only with the quilt driver"
40 [ "$driver" = "quilt" ] || ! "$flatten" ||
41 die "--flatten works only with the quilt driver"
43 if [ -z "$branches" ]; then
44 # this check is only needed when no branches have been passed
45 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
46 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
47 die "not on a TopGit-controlled branch"
51 playground="$(mktemp -d -t tg-export.XXXXXX)"
52 trap 'rm -rf "$playground"' EXIT
55 ## Collapse driver
57 # pretty_tree NAME
58 # Output tree ID of a cleaned-up tree without tg's artifacts.
59 pretty_tree()
61 (export GIT_INDEX_FILE="$playground/^index"
62 git read-tree "$1"
63 git update-index --force-remove ".topmsg" ".topdeps"
64 git write-tree)
67 # collapsed_commit NAME
68 # Produce a collapsed commit of branch NAME.
69 collapsed_commit()
71 name="$1"
73 rm -f "$playground/^pre" "$playground/^post"
74 >"$playground/^body"
76 # Get commit message and authorship information
77 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
79 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
80 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
81 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
82 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
84 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
85 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
86 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
88 # Determine parent
89 parent="$(cut -f 1 "$playground/$name^parents")"
90 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
91 # Produce a merge commit first
92 parent="$({
93 echo "TopGit-driven merge of branches:"
94 echo
95 cut -f 2 "$playground/$name^parents"
96 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
97 $(for p in $parent; do echo -p $p; done))"
100 if branch_empty "$name"; then
101 echo "$parent";
102 else
103 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
104 git stripspace |
105 git commit-tree "$(pretty_tree "$name")" -p "$parent"
108 echo "$name" >>"$playground/^ticker"
111 # collapse
112 # This will collapse a single branch, using information about
113 # previously collapsed branches stored in $playground.
114 collapse()
116 if [ -s "$playground/$_dep" ]; then
117 # We've already seen this dep
118 commit="$(cat "$playground/$_dep")"
120 elif [ -z "$_dep_is_tgish" ]; then
121 # This dep is not for rewrite
122 commit="$(git rev-parse --verify "$_dep")"
124 else
125 # First time hitting this dep; the common case
126 echo "Collapsing $_dep"
127 commit="$(collapsed_commit "$_dep")"
128 mkdir -p "$playground/$(dirname "$_dep")"
129 echo "$commit" >"$playground/$_dep"
132 # Propagate our work through the dependency chain
133 mkdir -p "$playground/$(dirname "$_name")"
134 echo "$commit $_dep" >>"$playground/$_name^parents"
138 ## Quilt driver
140 quilt()
142 if [ -z "$_dep_is_tgish" ]; then
143 # This dep is not for rewrite
144 return
147 if "$flatten"; then
148 bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
149 dn="";
150 else
151 bn="$(basename "$_dep.diff")";
152 dn="$(dirname "$_dep.diff")/";
153 if [ "x$dn" = "x./" ]; then
154 dn="";
158 filename="$output/$dn$bn";
159 if [ -e "$filename" ]; then
160 # We've already seen this dep
161 return
164 if branch_empty "$_dep"; then
165 echo "Skip empty patch $_dep";
166 else
167 echo "Exporting $_dep"
168 mkdir -p "$output/$dn";
169 $tg patch "$_dep" >"$filename"
170 echo "$dn$bn -p1" >>"$output/series"
175 ## Machinery
177 if [ "$driver" = "collapse" ]; then
178 [ -n "$output" ] ||
179 die "no target branch specified"
180 ! ref_exists "$output" ||
181 die "target branch '$output' already exists; first run: git branch -D $output"
183 elif [ "$driver" = "quilt" ]; then
184 [ -n "$output" ] ||
185 die "no target directory specified"
186 [ ! -e "$output" ] ||
187 die "target directory already exists: $output"
189 mkdir -p "$output"
193 driver()
195 case $_dep in refs/remotes/*) return;; esac
196 branch_needs_update >/dev/null
197 [ "$_ret" -eq 0 ] ||
198 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
200 $driver
203 # Call driver on all the branches - this will happen
204 # in topological order.
205 if [ -z "$branches" ]; then
206 recurse_deps driver "$name"
207 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
208 else
209 echo "$branches" | tr ',' '\n' | while read _dep; do
210 _dep_is_tgish=1
211 $driver
212 done
213 name="$(echo "$branches" | sed 's/.*,//')"
217 if [ "$driver" = "collapse" ]; then
218 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
220 depcount="$(cat "$playground/^ticker" | wc -l)"
221 echo "Exported topic branch $name (total $depcount topics) to branch $output"
223 elif [ "$driver" = "quilt" ]; then
224 depcount="$(cat "$output/series" | wc -l)"
225 echo "Exported topic branch $name (total $depcount topics) to directory $output"
228 # vim:noet