Ignore vim swp files
[topgit/fonseca.git] / tg-export.sh
blob654b38bf50805c182f67f189ce4cf5cfecda3fc0
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 name=
7 output=
8 driver=collapse
11 ## Parse options
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;;
24 [ -z "$output" ] || die "output already specified ($output)"
25 output="$arg";;
26 esac
27 done
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"
35 playground="$(mktemp -d -t tg-export.XXXXXX)"
36 trap 'rm -rf "$playground"' EXIT
39 ## Collapse driver
41 # Trusty Cogito code:
42 load_author()
44 if [ -z "$GIT_AUTHOR_NAME" ] && echo "$1" | grep -q '^[^< ]'; then
45 export GIT_AUTHOR_NAME="$(echo "$1" | sed 's/ *<.*//')"
47 if [ -z "$GIT_AUTHOR_EMAIL" ] && echo "$1" | grep -q '<.*>'; then
48 export GIT_AUTHOR_EMAIL="$(echo "$1" | sed 's/.*<\(.*\)>.*/\1/')"
52 # pretty_tree NAME
53 # Output tree ID of a cleaned-up tree without tg's artifacts.
54 pretty_tree()
56 (export GIT_INDEX_FILE="$playground/^index"
57 git read-tree "$1"
58 git update-index --force-remove ".topmsg" ".topdeps"
59 git write-tree)
62 # collapsed_commit NAME
63 # Produce a collapsed commit of branch NAME.
64 collapsed_commit()
66 name="$1"
68 rm -f "$playground/^pre" "$playground/^post"
69 >"$playground/^body"
71 # Get commit message and authorship information
72 git cat-file blob "$name:.topmsg" >"$playground/^msg"
73 while read line; do
74 if [ -z "$line" ]; then
75 # end of header
76 cat >"$playground/^body"
77 break
79 case "$line" in
80 From:*) load_author "${line#From: }";;
81 Subject:*) echo "${line#Subject: }" >>"$playground/^pre";;
82 *) echo "$line" >>"$playground/^post";;
83 esac
84 done <"$playground/^msg"
86 # Determine parent
87 parent="$(cut -f 1 "$playground/$name^parents")"
88 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
89 # Produce a merge commit first
90 parent="$({
91 echo "TopGit-driven merge of branches:"
92 echo
93 cut -f 2 "$playground/$name^parents"
94 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
95 $(for p in $parent; do echo -p $p; done))"
99 if [ -s "$playground/^pre" ]; then
100 cat "$playground/^pre"
101 echo
103 cat "$playground/^body"
104 [ ! -s "$playground/^post" ] || cat "$playground/^post"
105 } | git commit-tree "$(pretty_tree "$name")" -p "$parent"
107 echo "$name" >>"$playground/^ticker"
110 # collapse
111 # This will collapse a single branch, using information about
112 # previously collapsed branches stored in $playground.
113 collapse()
115 if [ -s "$playground/$_dep" ]; then
116 # We've already seen this dep
117 commit="$(cat "$playground/$_dep")"
119 elif [ -z "$_dep_is_tgish" ]; then
120 # This dep is not for rewrite
121 commit="$(git rev-parse --verify "$_dep")"
123 else
124 # First time hitting this dep; the common case
125 echo "Collapsing $_dep"
126 commit="$(collapsed_commit "$_dep")"
127 mkdir -p "$playground/$(dirname "$_dep")"
128 echo "$commit" >"$playground/$_dep"
131 # Propagate our work through the dependency chain
132 mkdir -p "$playground/$(dirname "$_name")"
133 echo "$commit $_dep" >>"$playground/$_name^parents"
137 ## Quilt driver
139 quilt()
141 if [ -z "$_dep_is_tgish" ]; then
142 # This dep is not for rewrite
143 return
146 filename="$output/$_dep.diff"
147 if [ -e "$filename" ]; then
148 # We've already seen this dep
149 return
152 echo "Exporting $_dep"
153 mkdir -p "$(dirname "$filename")"
154 $tg patch "$_dep" >"$filename"
155 echo "$_dep.diff -p1" >>"$output/series"
159 ## Machinery
161 if [ "$driver" = "collapse" ]; then
162 [ -n "$output" ] ||
163 die "no target branch specified"
164 ! ref_exists "$output" ||
165 die "target branch '$output' already exists; first run: git branch -D $output"
167 elif [ "$driver" = "quilt" ]; then
168 [ -n "$output" ] ||
169 die "no target directory specified"
170 [ ! -e "$output" ] ||
171 die "target directory already exists: $output"
173 mkdir -p "$output"
177 driver()
179 branch_needs_update >/dev/null
180 [ "$_ret" -eq 0 ] ||
181 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
183 $driver
186 # Call driver on all the branches - this will happen
187 # in topological order.
188 recurse_deps driver "$name"
189 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
192 if [ "$driver" = "collapse" ]; then
193 git update-ref "refs/heads/$output" "$(cat "$playground/$name")" ""
195 depcount="$(cat "$playground/^ticker" | wc -l)"
196 echo "Exported topic branch $name (total $depcount topics) to branch $output"
198 elif [ "$driver" = "quilt" ]; then
199 depcount="$(cat "$output/series" | wc -l)"
200 echo "Exported topic branch $name (total $depcount topics) to directory $output"