README: lever -> level (spotted by jikos)
[topgit/fonseca.git] / tg-export.sh
blob659e15ad76b156e421d49581ab9fccd482001f2e
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 branches=
8 output=
9 driver=collapse
12 ## Parse options
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;;
27 [ -z "$output" ] || die "output already specified ($output)"
28 output="$arg";;
29 esac
30 done
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"
37 [ -z "$branches" -o "$driver" = "quilt" ] ||
38 die "-b works only with the quilt driver"
41 playground="$(mktemp -d -t tg-export.XXXXXX)"
42 trap 'rm -rf "$playground"' EXIT
45 ## Collapse driver
47 # pretty_tree NAME
48 # Output tree ID of a cleaned-up tree without tg's artifacts.
49 pretty_tree()
51 (export GIT_INDEX_FILE="$playground/^index"
52 git read-tree "$1"
53 git update-index --force-remove ".topmsg" ".topdeps"
54 git write-tree)
57 # collapsed_commit NAME
58 # Produce a collapsed commit of branch NAME.
59 collapsed_commit()
61 name="$1"
63 rm -f "$playground/^pre" "$playground/^post"
64 >"$playground/^body"
66 # Get commit message and authorship information
67 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
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")"
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
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))"
90 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
91 git stripspace |
92 git commit-tree "$(pretty_tree "$name")" -p "$parent"
94 echo "$name" >>"$playground/^ticker"
97 # collapse
98 # This will collapse a single branch, using information about
99 # previously collapsed branches stored in $playground.
100 collapse()
102 if [ -s "$playground/$_dep" ]; then
103 # We've already seen this dep
104 commit="$(cat "$playground/$_dep")"
106 elif [ -z "$_dep_is_tgish" ]; then
107 # This dep is not for rewrite
108 commit="$(git rev-parse --verify "$_dep")"
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"
118 # Propagate our work through the dependency chain
119 mkdir -p "$playground/$(dirname "$_name")"
120 echo "$commit $_dep" >>"$playground/$_name^parents"
124 ## Quilt driver
126 quilt()
128 if [ -z "$_dep_is_tgish" ]; then
129 # This dep is not for rewrite
130 return
133 filename="$output/$_dep.diff"
134 if [ -e "$filename" ]; then
135 # We've already seen this dep
136 return
139 echo "Exporting $_dep"
140 mkdir -p "$(dirname "$filename")"
141 $tg patch "$_dep" >"$filename"
142 echo "$_dep.diff -p1" >>"$output/series"
146 ## Machinery
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"
154 elif [ "$driver" = "quilt" ]; then
155 [ -n "$output" ] ||
156 die "no target directory specified"
157 [ ! -e "$output" ] ||
158 die "target directory already exists: $output"
160 mkdir -p "$output"
164 driver()
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"
171 $driver
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/.*,//')"
188 if [ "$driver" = "collapse" ]; then
189 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
191 depcount="$(cat "$playground/^ticker" | wc -l)"
192 echo "Exported topic branch $name (total $depcount topics) to branch $output"
194 elif [ "$driver" = "quilt" ]; then
195 depcount="$(cat "$output/series" | wc -l)"
196 echo "Exported topic branch $name (total $depcount topics) to directory $output"