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