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