blob: 5dd07d8baf643190809de0a9c00d904f57daf5de [file] [log] [blame]
Benedikt Böhm00ccea62010-01-26 12:39:36 +01001#
Vincent Driessen6c2d30b2010-01-26 22:18:36 +01002# git-flow -- A collection of Git extensions to provide high-level
3# repository operations for Vincent Driessen's branching model.
Benedikt Böhm00ccea62010-01-26 12:39:36 +01004#
5# Original blog post presenting this model is found at:
6# http://nvie.com/archives/323
7#
8# Feel free to contribute to this project at:
9# http://github.com/nvie/gitflow
10#
11# Copyright (c) 2010 by Vincent Driessen
12# Copyright (c) 2010 by Benedikt Böhm
13#
14
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010015VERSION_PREFIX=$(git config --get gitflow.prefix.versiontag)
16PREFIX=$(git config --get gitflow.prefix.support || echo support/)
Vincent Driessen2acfffd2010-01-29 12:37:22 +010017FLAG_FETCH=1
Benedikt Böhm49dd62b2010-01-28 00:51:15 +010018
Vincent Driessen3c337fb2010-02-04 11:30:18 +010019warn "note: The support subcommand is still very EXPERIMENTAL!"
20warn "note: DO NOT use it in a production situation."
21
Benedikt Böhm00ccea62010-01-26 12:39:36 +010022usage() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010023 echo "usage: git flow support [list] [-v]"
Vincent Driessen010252a2010-02-04 10:31:29 +010024 echo " git flow support start <version> <base>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010025}
26
Vincent Driessenb866b012010-01-28 01:01:53 +010027cmd_default() {
28 cmd_list "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010029}
30
Vincent Driessenb866b012010-01-28 01:01:53 +010031cmd_list() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010032 DEFINE_boolean verbose false 'verbose (more) output' v
33 parse_args "$@"
34
Vincent Driessen27592dd2010-02-06 14:45:39 +010035 typeset support_branches
36 typeset current_branch
37 typeset short_names
38 support_branches="$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX")"
39 if [ -z "$support_branches" ]; then
Vincent Driessen170dc742010-01-28 00:20:51 +010040 warn "No support branches exist."
41 exit 0
42 fi
Vincent Driessen27592dd2010-02-06 14:45:39 +010043 current_branch=$(git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g')
44 short_names=$(echo "$support_branches" | sed "s?^$PREFIX??g")
Vincent Driessen3c337fb2010-02-04 11:30:18 +010045
Vincent Driessen3c337fb2010-02-04 11:30:18 +010046 # determine column width first
Vincent Driessen27592dd2010-02-06 14:45:39 +010047 typeset -i width=0
48 typeset branch
49 for branch in $short_names; do
50 typeset -i len=$(($(echo "$branch" | wc -c) - 1))
Vincent Driessen3c337fb2010-02-04 11:30:18 +010051 width=$(max $width $len)
52 done
Vincent Driessen27592dd2010-02-06 14:45:39 +010053 width=width+3
Vincent Driessen3c337fb2010-02-04 11:30:18 +010054
Vincent Driessen27592dd2010-02-06 14:45:39 +010055 typeset branch
56 for branch in $short_names; do
57 typeset fullname="$PREFIX$branch"
58 typeset base=$(git merge-base "$fullname" "$MASTER_BRANCH")
59 typeset master_sha=$(git rev-parse "$MASTER_BRANCH")
60 typeset branch_sha=$(git rev-parse "$fullname")
61 if [ "$fullname" = "$current_branch" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +010062 printf "* "
63 else
64 printf " "
65 fi
66 if flag verbose; then
67 printf "%-${width}s" "$branch"
68 if [ "$branch_sha" = "$master_sha" ]; then
69 printf "(no commits yet)"
70 else
Vincent Driessen27592dd2010-02-06 14:45:39 +010071 typeset tagname=$(git name-rev --tags --no-undefined --name-only $base)
72 typeset nicename
Vincent Driessen3c337fb2010-02-04 11:30:18 +010073 if [ "$tagname" != "" ]; then
74 nicename="$tagname"
75 else
76 nicename="$(git rev-parse --short $base)"
77 fi
78 printf "(based on $nicename)"
79 fi
80 else
81 printf "%s" "$branch"
82 fi
83 echo
84 done
Vincent Driessen170dc742010-01-28 00:20:51 +010085}
86
Benedikt Böhm00ccea62010-01-26 12:39:36 +010087cmd_help() {
88 usage
89 exit 0
90}
91
Benedikt Böhm00ccea62010-01-26 12:39:36 +010092parse_args() {
Vincent Driessen3c337fb2010-02-04 11:30:18 +010093 # parse options
94 FLAGS "$@" || exit $?
95 eval set -- "${FLAGS_ARGV}"
96
97 # read arguments into global variables
Benedikt Böhm00ccea62010-01-26 12:39:36 +010098 VERSION="$1"
Vincent Driessen010252a2010-02-04 10:31:29 +010099 BASE="$2"
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100100 BRANCH=$PREFIX$VERSION
101}
102
103require_version_arg() {
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100104 if [ "$VERSION" = "" ]; then
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100105 warn "Missing argument <version>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100106 usage
107 exit 1
108 fi
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100109}
110
Vincent Driessen010252a2010-02-04 10:31:29 +0100111require_base_arg() {
112 if [ "$BASE" = "" ]; then
113 warn "Missing argument <base>"
114 usage
115 exit 1
116 fi
117}
118
119require_base_is_on_master() {
120 if ! git branch --contains "$BASE" 2>/dev/null \
121 | sed 's/[* ] //g' \
122 | grep -q "^$MASTER_BRANCH\$"; then
123 die "fatal: Given base '$BASE' is not a valid commit on '$MASTER_BRANCH'."
124 fi
125}
126
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100127cmd_start() {
128 parse_args "$@"
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100129 require_version_arg
Vincent Driessen010252a2010-02-04 10:31:29 +0100130 require_base_arg
131 require_base_is_on_master
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100132
133 # sanity checks
Vincent Driessen48386442010-01-29 10:30:40 +0100134 gitflow_require_clean_working_tree
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100135
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100136 # fetch remote changes
137 if [ $FLAG_FETCH -eq 1 ]; then
138 git fetch -q $ORIGIN $BASE
139 fi
Vincent Driessen010252a2010-02-04 10:31:29 +0100140 gitflow_require_branch_absent $BRANCH
Vincent Driessen2acfffd2010-01-29 12:37:22 +0100141
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100142 # create branch
Vincent Driessen3c337fb2010-02-04 11:30:18 +0100143 git checkout -b "$BRANCH" "$BASE"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100144
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100145 echo
146 echo "Summary of actions:"
Vincent Driessen010252a2010-02-04 10:31:29 +0100147 echo "- A new branch '$BRANCH' was created, based on '$BASE'"
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100148 echo "- You are now on branch '$BRANCH'"
149 echo
150}