chiark / gitweb /
tg info: Show information about remote branches
[topgit.git] / tg-export.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
6 name=
7 output=
8 driver=collapse
9
10
11 ## Parse options
12
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;;
23         *)
24                 [ -z "$output" ] || die "output already specified ($output)"
25                 output="$arg";;
26         esac
27 done
28
29
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"
33
34
35 playground="$(mktemp -d -t tg-export.XXXXXX)"
36 trap 'rm -rf "$playground"' EXIT
37
38
39 ## Collapse driver
40
41 # Trusty Cogito code:
42 load_author()
43 {
44         if [ -z "$GIT_AUTHOR_NAME" ] && echo "$1" | grep -q '^[^< ]'; then
45                 export GIT_AUTHOR_NAME="$(echo "$1" | sed 's/ *<.*//')"
46         fi
47         if [ -z "$GIT_AUTHOR_EMAIL" ] && echo "$1" | grep -q '<.*>'; then
48                 export GIT_AUTHOR_EMAIL="$(echo "$1" | sed 's/.*<\(.*\)>.*/\1/')"
49         fi
50 }
51
52 # pretty_tree NAME
53 # Output tree ID of a cleaned-up tree without tg's artifacts.
54 pretty_tree()
55 {
56         (export GIT_INDEX_FILE="$playground/^index"
57          git read-tree "$1"
58          git update-index --force-remove ".topmsg" ".topdeps"
59          git write-tree)
60 }
61
62 # collapsed_commit NAME
63 # Produce a collapsed commit of branch NAME.
64 collapsed_commit()
65 {
66         name="$1"
67
68         rm -f "$playground/^pre" "$playground/^post"
69         >"$playground/^body"
70
71         # Get commit message and authorship information
72         git cat-file blob "$name:.topmsg" >"$playground/^msg"
73         while read line; do
74                 if [ -z "$line" ]; then
75                         # end of header
76                         cat >"$playground/^body"
77                         break
78                 fi
79                 case "$line" in
80                 From:*) load_author "${line#From: }";;
81                 Subject:*) echo "${line#Subject: }" >>"$playground/^pre";;
82                 *) echo "$line" >>"$playground/^post";;
83                 esac
84         done <"$playground/^msg"
85
86         # Determine parent
87         parent="$(cut -f 1 "$playground/$name^parents")"
88         if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
89                 # Produce a merge commit first
90                 parent="$({
91                         echo "TopGit-driven merge of branches:"
92                         echo
93                         cut -f 2 "$playground/$name^parents"
94                 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
95                         $(for p in $parent; do echo -p $p; done))"
96         fi
97
98         {
99                 if [ -s "$playground/^pre" ]; then
100                         cat "$playground/^pre"
101                         echo
102                 fi
103                 cat "$playground/^body"
104                 [ ! -s "$playground/^post" ] || cat "$playground/^post"
105         } | git commit-tree "$(pretty_tree "$name")" -p "$parent"
106
107         echo "$name" >>"$playground/^ticker"
108 }
109
110 # collapse
111 # This will collapse a single branch, using information about
112 # previously collapsed branches stored in $playground.
113 collapse()
114 {
115         if [ -s "$playground/$_dep" ]; then
116                 # We've already seen this dep
117                 commit="$(cat "$playground/$_dep")"
118
119         elif [ -z "$_dep_is_tgish" ]; then
120                 # This dep is not for rewrite
121                 commit="$(git rev-parse --verify "$_dep")"
122
123         else
124                 # First time hitting this dep; the common case
125                 echo "Collapsing $_dep"
126                 commit="$(collapsed_commit "$_dep")"
127                 mkdir -p "$playground/$(dirname "$_dep")"
128                 echo "$commit" >"$playground/$_dep"
129         fi
130
131         # Propagate our work through the dependency chain
132         mkdir -p "$playground/$(dirname "$_name")"
133         echo "$commit   $_dep" >>"$playground/$_name^parents"
134 }
135
136
137 ## Quilt driver
138
139 quilt()
140 {
141         if [ -z "$_dep_is_tgish" ]; then
142                 # This dep is not for rewrite
143                 return
144         fi
145
146         filename="$output/$_dep.diff"
147         if [ -e "$filename" ]; then
148                 # We've already seen this dep
149                 return
150         fi
151
152         echo "Exporting $_dep"
153         mkdir -p "$(dirname "$filename")"
154         tg patch "$_dep" >"$filename"
155         echo "$_dep.diff -p1" >>"$output/series"
156 }
157
158
159 ## Machinery
160
161 if [ "$driver" = "collapse" ]; then
162         [ -n "$output" ] ||
163                 die "no target branch specified"
164         ! ref_exists "$output"  ||
165                 die "target branch '$output' already exists; first run: git branch -D $output"
166
167 elif [ "$driver" = "quilt" ]; then
168         [ -n "$output" ] ||
169                 die "no target directory specified"
170         [ ! -e "$output" ] ||
171                 die "target directory already exists: $output"
172
173         mkdir -p "$output"
174 fi
175
176
177 driver()
178 {
179         branch_needs_update >/dev/null
180         [ "$_ret" -eq 0 ] ||
181                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
182
183         $driver
184 }
185
186 # Call driver on all the branches - this will happen
187 # in topological order.
188 recurse_deps driver "$name"
189 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
190
191
192 if [ "$driver" = "collapse" ]; then
193         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
194
195         depcount="$(cat "$playground/^ticker" | wc -l)"
196         echo "Exported topic branch $name (total $depcount topics) to branch $output"
197
198 elif [ "$driver" = "quilt" ]; then
199         depcount="$(cat "$output/series" | wc -l)"
200         echo "Exported topic branch $name (total $depcount topics) to directory $output"
201 fi