chiark / gitweb /
tg.sh: Runtime tg-* command check
[topgit.git] / tg-create.sh
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # (c) Petr Baudis <pasky@suse.cz>  2008
4 # GPLv2
5
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=
10
11
12 ## Parse options
13
14 while [ -n "$1" ]; do
15         arg="$1"; shift
16         case "$arg" in
17         -*)
18                 echo "Usage: tg create NAME [DEPS...]" >&2
19                 exit 1;;
20         *)
21                 if [ -z "$name" ]; then
22                         name="$arg"
23                 else
24                         deps="$deps $arg"
25                 fi;;
26         esac
27 done
28
29
30 ## Auto-guess dependencies
31
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"
49         fi
50 fi
51
52 [ -n "$merge" -o -n "$restarted" ] || merge="$deps "
53
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"
60
61 # Clean up any stale stuff
62 rm -f "$git_dir/top-deps" "$git_dir/top-merge"
63
64
65 ## Create base
66
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"
73 fi
74
75
76 ## Merge other dependencies into the base
77
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..."
83
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
92         fi
93 done
94
95
96 ## Set up the topic branch
97
98 git checkout -b "$name"
99
100 echo "$deps" | sed 's/ /\n/g' >"$root_dir/.topdeps"
101 git add "$root_dir/.topdeps"
102
103 author="$(git var GIT_AUTHOR_IDENT)"
104 author_addr="${author%> *}>"
105 {
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         ! subject_prefix="$(git config topgit.subjectprefix)" || subject_prefix="$subject_prefix "
111         echo "Subject: [${subject_prefix}PATCH] $name"
112         echo
113         cat <<EOT
114 <patch description>
115
116 Signed-off-by: $author_addr
117 EOT
118 } >"$root_dir/.topmsg"
119 git add "$root_dir/.topmsg"
120
121
122
123 info "Topic branch $name set up. Please fill .topmsg now and make initial commit."
124 info "To abort: git rm -f .top* && git checkout ${deps%% *} && tg delete $name"