blob: 5be05f422579c271e88054e1303f7b9122bf3e1a [file] [log] [blame]
Benedikt Böhme2fa4912010-01-26 14:41:20 +01001#!/bin/sh
Benedikt Böhm00ccea62010-01-26 12:39:36 +01002#
Vincent Driessen6c2d30b2010-01-26 22:18:36 +01003# git-flow -- A collection of Git extensions to provide high-level
4# repository operations for Vincent Driessen's branching model.
Benedikt Böhm00ccea62010-01-26 12:39:36 +01005#
6# Original blog post presenting this model is found at:
7# http://nvie.com/archives/323
8#
9# Feel free to contribute to this project at:
10# http://github.com/nvie/gitflow
11#
12# Copyright (c) 2010 by Vincent Driessen
13# Copyright (c) 2010 by Benedikt Böhm
14#
15
Benedikt Böhme5eaff92010-01-26 12:44:55 +010016# enable debug mode
17if [ "$DEBUG" = "yes" ]; then
18 set -x
19fi
20
Vincent Driessen58995b52010-01-28 12:39:06 +010021export GIT_DIR=$(git rev-parse --git-dir)
Benedikt Böhm4a864fb2010-01-26 12:59:27 +010022export GITFLOW_DIR=$(dirname "$0")
23export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
24export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
Benedikt Böhm350e7152010-01-26 13:05:05 +010025export ORIGIN=$(git config --get gitflow.origin || echo origin)
Benedikt Böhm427c5db2010-01-26 18:23:44 +010026export README=$(git config --get gitflow.readme || echo README)
Benedikt Böhm4a864fb2010-01-26 12:59:27 +010027
Benedikt Böhm00ccea62010-01-26 12:39:36 +010028warn() { echo "$@" >&2; }
29die() { warn "$@"; exit 1; }
Benedikt Böhme2fa4912010-01-26 14:41:20 +010030
31has() {
32 local item=$1; shift
33 echo " $@ " | grep -q " $item "
34}
Benedikt Böhm00ccea62010-01-26 12:39:36 +010035
36usage() {
Vincent Driessen186d2b52010-01-27 23:48:39 +010037 echo "usage: git flow <subcommand>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010038 echo
Vincent Driessen186d2b52010-01-27 23:48:39 +010039 echo "Available subcommands are:"
40 echo " init Initialize a new git repo with support for the branching model."
41 echo " feature Manage your feature branches."
42 echo " release Manage your release branches."
43 echo " hotfix Manage your hotfix branches."
44 echo " support Manage your support branches."
Vincent Driessen3625f392010-01-28 00:13:26 +010045 echo " version Shows version information."
Benedikt Böhm7d703a82010-01-26 13:17:12 +010046 echo
Vincent Driessen186d2b52010-01-27 23:48:39 +010047 echo "Try 'git flow <subcommand> help' for details."
Benedikt Böhm00ccea62010-01-26 12:39:36 +010048}
49
50main() {
Benedikt Böhm427c5db2010-01-26 18:23:44 +010051 if [ $# -lt 1 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010052 usage
53 exit 1
54 fi
55
Benedikt Böhm00ccea62010-01-26 12:39:36 +010056 # sanity checks
Vincent Driessen186d2b52010-01-27 23:48:39 +010057 SUBCOMMAND="$1"; shift
Benedikt Böhm427c5db2010-01-26 18:23:44 +010058
Vincent Driessen186d2b52010-01-27 23:48:39 +010059 if [ ! -e "$GITFLOW_DIR/git-flow-$SUBCOMMAND" ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010060 usage
61 exit 1
62 fi
63
Vincent Driessen2ba9a4d2010-01-27 12:51:07 +010064 if ! git rev-parse --git-dir >/dev/null; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010065 die "Not a git repository"
66 fi
67
68 # get all available branches
69 LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
70 REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
71 ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
72
73 # run command
Vincent Driessen186d2b52010-01-27 23:48:39 +010074 . "$GITFLOW_DIR/git-flow-$SUBCOMMAND"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010075
Vincent Driessen186d2b52010-01-27 23:48:39 +010076 SUBACTION="${1:-default}"; shift
77 if ! typeset -f cmd_$SUBACTION 2>&1 >/dev/null; then
78 warn "Unknown subcommand: '$1'"
79 usage
80 exit 1
Benedikt Böhm427c5db2010-01-26 18:23:44 +010081 fi
82
Vincent Driessen186d2b52010-01-27 23:48:39 +010083 # run the specified action
84 cmd_$SUBACTION "$@"
Benedikt Böhm427c5db2010-01-26 18:23:44 +010085}
86
Vincent Driessen62345d52010-01-29 10:27:01 +010087gitflow_test_clean_working_tree() {
Benedikt Böhm00ccea62010-01-26 12:39:36 +010088 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
Vincent Driessen62345d52010-01-29 10:27:01 +010089 return 1
90 elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
91 return 2
92 else
93 return 0
94 fi
95}
96
97gitflow_require_clean_working_tree() {
98 gitflow_test_clean_working_tree
99 result=$?
100 if [ $result -eq 1 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100101 die "Working tree contains unstaged changes. Aborting ..."
102 fi
Vincent Driessen62345d52010-01-29 10:27:01 +0100103 if [ $result -eq 2 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100104 die "Index contains uncommited changes. Aborting ..."
105 fi
106}
107
108gitflow_require_local_branch() {
109 if ! has $1 $LOCAL_BRANCHES; then
110 die "Local branch '$1' does not exist and is required."
111 fi
112}
113
114gitflow_require_remote_branch() {
115 if ! has $1 $REMOTE_BRANCHES; then
116 die "Remote branch '$1' does not exist and is required."
117 fi
118}
119
120gitflow_require_branch() {
121 if ! has $1 $ALL_BRANCHES; then
122 die "Branch '$1' does not exist and is required."
123 fi
124}
125
126gitflow_require_branch_absent() {
127 if has $1 $ALL_BRANCHES; then
128 die "Branch '$1' already exists. Pick another name."
129 fi
130}
131
132#
133# gitflow_test_branches_equal()
134#
135# Tests whether branches and their "origin" counterparts have diverged and need
136# merging first. It returns error codes to provide more detail, like so:
137#
138# 0 Branch heads point to the same commit
139# 1 First given branch needs fast-forwarding
140# 2 Second given branch needs fast-forwarding
141# 3 Branch needs a real merge
142#
143gitflow_test_branches_equal() {
144 commit1=$(git rev-parse "$1")
145 commit2=$(git rev-parse "$2")
146 if [ "$commit1" != "$commit2" ]; then
147 base=$(git merge-base "$commit1" "$commit2")
148 if [ "$commit1" = "$base" ]; then
149 return 1
150 elif [ "$commit2" = "$base" ]; then
151 return 2
152 else
153 return 3
154 fi
155 else
156 return 0
157 fi
158}
159
160gitflow_require_branches_equal() {
161 gitflow_require_local_branch "$1"
162 gitflow_require_remote_branch "$2"
163 gitflow_test_branches_equal "$1" "$2"
164 status=$?
165 if [ $status -gt 0 ]; then
166 warn "Branches '$1' and '$2' have diverged."
167 if [ $status -eq 1 ]; then
168 die "And branch '$1' may be fast-forwarded."
169 elif [ $status -eq 2 ]; then
170 # Warn here, since there is no harm in being ahead
171 warn "And local branch '$1' is ahead of '$2'."
172 else
173 die "Branches need merging first."
174 fi
175 fi
176}
177
Vincent Driessen58995b52010-01-28 12:39:06 +0100178#
179# gitflow_is_branch_merged_into()
180#
181# Checks whether branch $1 is succesfully merged into $2
182#
183gitflow_is_branch_merged_into() {
184 local SUBJECT=$1
185 local BASE=$2
186 ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //')
187 has $BASE $ALL_MERGES
188}
189
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100190main "$@"