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