tg-create.sh: Introduce topgit.{to,cc,bcc} config options
[topgit.git] / tg-create.sh
blobd196e4f5fa56bbba8de8034a9c5366c478879f49
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz> 2008
4 # GPLv2
6 deps= # List of dependent branches
7 restarted= # Set to 1 if we are picking up in the middle of base setup
8 merge= # List of branches to be merged; subset of $deps
9 name=
12 ## Parse options
14 while [ -n "$1" ]; do
15 arg="$1"; shift
16 case "$arg" in
17 -*)
18 echo "Usage: tg create [-d DEPS...] NAME" >&2
19 exit 1;;
21 if [ -z "$name" ]; then
22 name="$arg"
23 else
24 deps="$deps $arg"
25 fi;;
26 esac
27 done
30 ## Auto-guess dependencies
32 deps="${deps# }"
33 if [ -z "$deps" ]; then
34 head="$(git symbolic-ref HEAD)"
35 bname="${head#refs/top-bases/}"
36 if [ "$bname" != "$head" -a -s "$git_dir/top-deps" -a -s "$git_dir/top-merge" ]; then
37 # We are on a base branch now; resume merge!
38 deps="$(cat "$git_dir/top-deps")"
39 merge="$(cat "$git_dir/top-merge")"
40 name="$bname"
41 restarted=1
42 info "Resuming $name setup..."
43 else
44 # The common case
45 [ -z "$name" ] && die "no branch name given"
46 deps="${head#refs/heads/}"
47 [ "$deps" != "$head" ] || die "refusing to auto-depend on non-head ref ($head)"
48 info "Automatically marking dependency on $deps"
52 [ -n "$merge" -o -n "$restarted" ] || merge="$deps "
54 for d in $deps; do
55 git rev-parse --verify "$d" >/dev/null 2>&1 ||
56 die "unknown branch dependency '$d'"
57 done
58 ! git rev-parse --verify "$name" >/dev/null 2>&1 ||
59 die "branch '$name' already exists"
61 # Clean up any stale stuff
62 rm -f "$git_dir/top-deps" "$git_dir/top-merge"
65 ## Create base
67 if [ -n "$merge" ]; then
68 # Unshift the first item from the to-merge list
69 branch="${merge%% *}"
70 merge="${merge#* }"
71 info "Creating $name base from $branch..."
72 switch_to_base "$name" "$branch"
76 ## Merge other dependencies into the base
78 while [ -n "$merge" ]; do
79 # Unshift the first item from the to-merge list
80 branch="${merge%% *}"
81 merge="${merge#* }"
82 info "Merging $name base with $branch..."
84 if ! git merge "$branch"; then
85 info "Please commit merge resolution and call: tg create"
86 info "It is also safe to abort this operation using \`git reset --hard\`"
87 info "but please remember you are on the base branch now;"
88 info "you will want to switch to a different branch."
89 echo "$deps" >"$git_dir/top-deps"
90 echo "$merge" >"$git_dir/top-merge"
91 exit 2
93 done
96 ## Set up the topic branch
98 git checkout -b "$name"
100 echo "$deps" | sed 's/ /\n/g' >"$root_dir/.topdeps"
101 git add "$root_dir/.topdeps"
103 author="$(git var GIT_AUTHOR_IDENT)"
104 author_addr="${author%> *}>"
106 echo "From: $author_addr"
107 ! header="$(git config topgit.to)" || echo "To: $header"
108 ! header="$(git config topgit.cc)" || echo "Cc: $header"
109 ! header="$(git config topgit.bcc)" || echo "Bcc: $header"
110 echo "Subject: [PATCH] $1"
111 echo
112 cat <<EOT
113 <patch description>
115 Signed-off-by: $author_addr
117 } >"$root_dir/.topmsg"
118 git add "$root_dir/.topmsg"
122 info "Topic branch $name set up. Please fill .topmsg now and make initial commit."
123 info "To abort: git rm -f .top* && git checkout ${deps%% *} && tg delete $name"