Print help output when no command is given
[topgit/fonseca.git] / tg-export.sh
blob52af88dfd14e891627b145f63702b1212155a82c
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
34 [ -z "$branches" -o "$driver" = "quilt" ] ||
35 die "-b works only with the quilt driver"
37 if [ -z "$branches" ]; then
38 # this check is only needed when no branches have been passed
39 name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
40 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
41 die "not on a TopGit-controlled branch"
45 playground="$(mktemp -d -t tg-export.XXXXXX)"
46 trap 'rm -rf "$playground"' EXIT
49 ## Collapse driver
51 # pretty_tree NAME
52 # Output tree ID of a cleaned-up tree without tg's artifacts.
53 pretty_tree()
55 (export GIT_INDEX_FILE="$playground/^index"
56 git read-tree "$1"
57 git update-index --force-remove ".topmsg" ".topdeps"
58 git write-tree)
61 # collapsed_commit NAME
62 # Produce a collapsed commit of branch NAME.
63 collapsed_commit()
65 name="$1"
67 rm -f "$playground/^pre" "$playground/^post"
68 >"$playground/^body"
70 # Get commit message and authorship information
71 git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
73 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$playground/^info")"
74 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$playground/^info")"
75 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$playground/^info")"
76 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$playground/^info")"
78 test -n "$GIT_AUTHOR_NAME" && export GIT_AUTHOR_NAME
79 test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
80 test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
82 # Determine parent
83 parent="$(cut -f 1 "$playground/$name^parents")"
84 if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
85 # Produce a merge commit first
86 parent="$({
87 echo "TopGit-driven merge of branches:"
88 echo
89 cut -f 2 "$playground/$name^parents"
90 } | git commit-tree "$(pretty_tree "refs/top-bases/$name")" \
91 $(for p in $parent; do echo -p $p; done))"
94 (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
95 git stripspace |
96 git commit-tree "$(pretty_tree "$name")" -p "$parent"
98 echo "$name" >>"$playground/^ticker"
101 # collapse
102 # This will collapse a single branch, using information about
103 # previously collapsed branches stored in $playground.
104 collapse()
106 if [ -s "$playground/$_dep" ]; then
107 # We've already seen this dep
108 commit="$(cat "$playground/$_dep")"
110 elif [ -z "$_dep_is_tgish" ]; then
111 # This dep is not for rewrite
112 commit="$(git rev-parse --verify "$_dep")"
114 else
115 # First time hitting this dep; the common case
116 echo "Collapsing $_dep"
117 commit="$(collapsed_commit "$_dep")"
118 mkdir -p "$playground/$(dirname "$_dep")"
119 echo "$commit" >"$playground/$_dep"
122 # Propagate our work through the dependency chain
123 mkdir -p "$playground/$(dirname "$_name")"
124 echo "$commit $_dep" >>"$playground/$_name^parents"
128 ## Quilt driver
130 quilt()
132 if [ -z "$_dep_is_tgish" ]; then
133 # This dep is not for rewrite
134 return
137 filename="$output/$_dep.diff"
138 if [ -e "$filename" ]; then
139 # We've already seen this dep
140 return
143 echo "Exporting $_dep"
144 mkdir -p "$(dirname "$filename")"
145 $tg patch "$_dep" >"$filename"
146 echo "$_dep.diff -p1" >>"$output/series"
150 ## Machinery
152 if [ "$driver" = "collapse" ]; then
153 [ -n "$output" ] ||
154 die "no target branch specified"
155 ! ref_exists "$output" ||
156 die "target branch '$output' already exists; first run: git branch -D $output"
158 elif [ "$driver" = "quilt" ]; then
159 [ -n "$output" ] ||
160 die "no target directory specified"
161 [ ! -e "$output" ] ||
162 die "target directory already exists: $output"
164 mkdir -p "$output"
168 driver()
170 case $_dep in refs/remotes/*) return;; esac
171 branch_needs_update >/dev/null
172 [ "$_ret" -eq 0 ] ||
173 die "cancelling export of $_dep (-> $_name): branch not up-to-date"
175 $driver
178 # Call driver on all the branches - this will happen
179 # in topological order.
180 if [ -z "$branches" ]; then
181 recurse_deps driver "$name"
182 (_ret=0; _dep="$name"; _name=""; _dep_is_tgish=1; driver)
183 else
184 echo "$branches" | tr ',' '\n' | while read _dep; do
185 _dep_is_tgish=1
186 $driver
187 done
188 name="$(echo "$branches" | sed 's/.*,//')"
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"