chiark / gitweb /
8e98f4695c825752428fdf2bd2b947488c6c99c4
[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 branches=
8 output=
9 driver=collapse
10
11
12 ## Parse options
13
14 while [ -n "$1" ]; do
15         arg="$1"; shift
16         case "$arg" in
17         -b)
18                 branches="$1"; shift;;
19         --quilt)
20                 driver=quilt;;
21         --collapse)
22                 driver=collapse;;
23         -*)
24                 echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
25                 exit 1;;
26         *)
27                 [ -z "$output" ] || die "output already specified ($output)"
28                 output="$arg";;
29         esac
30 done
31
32
33
34 [ -z "$branches" -o "$driver" = "quilt" ] ||
35         die "-b works only with the quilt driver"
36
37 if [ -z "$branches" ]; then
38         # this check is only needed when no branches have been passed
39         name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
40         base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
41                 die "not on a TopGit-controlled branch"
42         branches="$name"
43 else
44         name="${branches##*,}" # the last of the comma-separated items
45 fi
46 # $name holds the current branch
47 # $branches holds the comma-separated list of branches
48 # $name is equal to the last of the list of branches
49
50
51 playground="$(mktemp -d -t tg-export.XXXXXX)"
52 trap 'rm -rf "$playground"' EXIT
53
54
55 ## Collapse driver
56
57 # pretty_tree NAME
58 # Output tree ID of a cleaned-up tree without tg's artifacts.
59 pretty_tree()
60 {
61         (export GIT_INDEX_FILE="$playground/^index"
62          git read-tree "$1"
63          git update-index --force-remove ".topmsg" ".topdeps"
64          git write-tree)
65 }
66
67 # collapsed_commit NAME
68 # Produce a collapsed commit of branch NAME.
69 collapsed_commit()
70 {
71         local name; name="$1"
72
73         rm -f "$playground/^pre" "$playground/^post"
74         >"$playground/^body"
75
76         # Get commit message and authorship information
77         git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
78
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")"
83
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
87
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))"
98         fi
99
100         (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
101         git stripspace |
102         git commit-tree "$(pretty_tree "$name")" -p "$parent"
103
104         echo "$name" >>"$playground/^ticker"
105 }
106
107 # collapse
108 # This will collapse a single branch, using information about
109 # previously collapsed branches stored in $playground.
110 collapse()
111 {
112         if [ -s "$playground/$_dep" ]; then
113                 # We've already seen this dep
114                 commit="$(cat "$playground/$_dep")"
115
116         elif [ -z "$_dep_is_tgish" ]; then
117                 # This dep is not for rewrite
118                 commit="$(git rev-parse --verify "$_dep")"
119
120         else
121                 # First time hitting this dep; the common case
122                 echo "Collapsing $_dep"
123                 commit="$(collapsed_commit "$_dep")"
124                 mkdir -p "$playground/$(dirname "$_dep")"
125                 echo "$commit" >"$playground/$_dep"
126         fi
127
128         # Propagate our work through the dependency chain
129         mkdir -p "$playground/$(dirname "$_name")"
130         echo "$commit   $_dep" >>"$playground/$_name^parents"
131 }
132
133
134 ## Quilt driver
135
136 quilt()
137 {
138         if [ -z "$_dep_is_tgish" ]; then
139                 # This dep is not for rewrite
140                 return
141         fi
142
143         filename="$output/$_dep.diff"
144         if [ -e "$filename" ]; then
145                 # We've already seen this dep
146                 return
147         fi
148
149         echo "Exporting $_dep"
150         mkdir -p "$(dirname "$filename")"
151         $tg patch "$_dep" >"$filename"
152         echo "$_dep.diff -p1" >>"$output/series"
153 }
154
155
156 ## Machinery
157
158 if [ "$driver" = "collapse" ]; then
159         [ -n "$output" ] ||
160                 die "no target branch specified"
161         ! ref_exists "$output"  ||
162                 die "target branch '$output' already exists; first run: git branch -D $output"
163
164 elif [ "$driver" = "quilt" ]; then
165         [ -n "$output" ] ||
166                 die "no target directory specified"
167         [ ! -e "$output" ] ||
168                 die "target directory already exists: $output"
169
170         mkdir -p "$output"
171 fi
172
173
174 driver()
175 {
176         case $_dep in refs/remotes/*) return;; esac
177         branch_needs_update >/dev/null
178         [ "$_ret" -eq 0 ] ||
179                 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
180
181         $driver
182 }
183
184 # Call driver on all the branches - this will happen
185 # in topological order.
186 echo "$branches" | tr , '\n' | while read name; do
187         recurse_deps driver "$name"
188         (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
189 done
190
191 if [ "$driver" = "collapse" ]; then
192         git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
193
194         depcount="$(cat "$playground/^ticker" | wc -l)"
195         echo "Exported topic branch $name (total $depcount topics) to branch $output"
196
197 elif [ "$driver" = "quilt" ]; then
198         depcount="$(cat "$output/series" | wc -l)"
199         echo "Exported topic branch $name (total $depcount topics) to directory $output"
200 fi