blob: 96c2e529bfee77f67a587543de37c1eb6d553f8a [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
Benedikt Böhm4a864fb2010-01-26 12:59:27 +010021export GITFLOW_DIR=$(dirname "$0")
22export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
23export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
Benedikt Böhm350e7152010-01-26 13:05:05 +010024export ORIGIN=$(git config --get gitflow.origin || echo origin)
Benedikt Böhm427c5db2010-01-26 18:23:44 +010025export README=$(git config --get gitflow.readme || echo README)
Benedikt Böhm4a864fb2010-01-26 12:59:27 +010026
Benedikt Böhm00ccea62010-01-26 12:39:36 +010027warn() { echo "$@" >&2; }
28die() { warn "$@"; exit 1; }
Benedikt Böhme2fa4912010-01-26 14:41:20 +010029
30has() {
31 local item=$1; shift
32 echo " $@ " | grep -q " $item "
33}
Benedikt Böhm00ccea62010-01-26 12:39:36 +010034
35usage() {
Vincent Driessen186d2b52010-01-27 23:48:39 +010036 echo "usage: git flow <subcommand>"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010037 echo
Vincent Driessen186d2b52010-01-27 23:48:39 +010038 echo "Available subcommands are:"
39 echo " init Initialize a new git repo with support for the branching model."
40 echo " feature Manage your feature branches."
41 echo " release Manage your release branches."
42 echo " hotfix Manage your hotfix branches."
43 echo " support Manage your support branches."
Vincent Driessen3625f392010-01-28 00:13:26 +010044 echo " version Shows version information."
Benedikt Böhm7d703a82010-01-26 13:17:12 +010045 echo
Vincent Driessen186d2b52010-01-27 23:48:39 +010046 echo "Try 'git flow <subcommand> help' for details."
Benedikt Böhm00ccea62010-01-26 12:39:36 +010047}
48
49main() {
Benedikt Böhm427c5db2010-01-26 18:23:44 +010050 if [ $# -lt 1 ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010051 usage
52 exit 1
53 fi
54
Benedikt Böhm00ccea62010-01-26 12:39:36 +010055 # sanity checks
Vincent Driessen186d2b52010-01-27 23:48:39 +010056 SUBCOMMAND="$1"; shift
Benedikt Böhm427c5db2010-01-26 18:23:44 +010057
Vincent Driessen186d2b52010-01-27 23:48:39 +010058 if [ ! -e "$GITFLOW_DIR/git-flow-$SUBCOMMAND" ]; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010059 usage
60 exit 1
61 fi
62
Vincent Driessen2ba9a4d2010-01-27 12:51:07 +010063 if ! git rev-parse --git-dir >/dev/null; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010064 die "Not a git repository"
65 fi
66
67 # get all available branches
68 LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
69 REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
70 ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
71
72 # run command
Vincent Driessen186d2b52010-01-27 23:48:39 +010073 . "$GITFLOW_DIR/git-flow-$SUBCOMMAND"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010074
Vincent Driessen186d2b52010-01-27 23:48:39 +010075 if ! typeset -f sub_main >/dev/null; then
Benedikt Böhm00ccea62010-01-26 12:39:36 +010076 usage
77 exit 1
78 fi
79
Benedikt Böhm00ccea62010-01-26 12:39:36 +010080 # run command
Vincent Driessen186d2b52010-01-27 23:48:39 +010081 sub_main "$@"
Benedikt Böhm00ccea62010-01-26 12:39:36 +010082}
83
Vincent Driessen186d2b52010-01-27 23:48:39 +010084sub_main() {
85 SUBACTION="${1:-default}"; shift
86 if ! typeset -f cmd_$SUBACTION 2>&1 >/dev/null; then
87 warn "Unknown subcommand: '$1'"
88 usage
89 exit 1
Benedikt Böhm427c5db2010-01-26 18:23:44 +010090 fi
91
Vincent Driessen186d2b52010-01-27 23:48:39 +010092 # run the specified action
Vincent Driessenb866b012010-01-28 01:01:53 +010093 # if the subcommand declares a setup() function, call that first
94 if typeset -f setup >/dev/null; then
95 setup
96 fi
Vincent Driessen186d2b52010-01-27 23:48:39 +010097 cmd_$SUBACTION "$@"
Benedikt Böhm427c5db2010-01-26 18:23:44 +010098}
99
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100100gitflow_check_clean_working_tree() {
101 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
102 die "Working tree contains unstaged changes. Aborting ..."
103 fi
104 if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
105 die "Index contains uncommited changes. Aborting ..."
106 fi
107}
108
109gitflow_require_local_branch() {
110 if ! has $1 $LOCAL_BRANCHES; then
111 die "Local branch '$1' does not exist and is required."
112 fi
113}
114
115gitflow_require_remote_branch() {
116 if ! has $1 $REMOTE_BRANCHES; then
117 die "Remote branch '$1' does not exist and is required."
118 fi
119}
120
121gitflow_require_branch() {
122 if ! has $1 $ALL_BRANCHES; then
123 die "Branch '$1' does not exist and is required."
124 fi
125}
126
127gitflow_require_branch_absent() {
128 if has $1 $ALL_BRANCHES; then
129 die "Branch '$1' already exists. Pick another name."
130 fi
131}
132
133#
134# gitflow_test_branches_equal()
135#
136# Tests whether branches and their "origin" counterparts have diverged and need
137# merging first. It returns error codes to provide more detail, like so:
138#
139# 0 Branch heads point to the same commit
140# 1 First given branch needs fast-forwarding
141# 2 Second given branch needs fast-forwarding
142# 3 Branch needs a real merge
143#
144gitflow_test_branches_equal() {
145 commit1=$(git rev-parse "$1")
146 commit2=$(git rev-parse "$2")
147 if [ "$commit1" != "$commit2" ]; then
148 base=$(git merge-base "$commit1" "$commit2")
149 if [ "$commit1" = "$base" ]; then
150 return 1
151 elif [ "$commit2" = "$base" ]; then
152 return 2
153 else
154 return 3
155 fi
156 else
157 return 0
158 fi
159}
160
161gitflow_require_branches_equal() {
162 gitflow_require_local_branch "$1"
163 gitflow_require_remote_branch "$2"
164 gitflow_test_branches_equal "$1" "$2"
165 status=$?
166 if [ $status -gt 0 ]; then
167 warn "Branches '$1' and '$2' have diverged."
168 if [ $status -eq 1 ]; then
169 die "And branch '$1' may be fast-forwarded."
170 elif [ $status -eq 2 ]; then
171 # Warn here, since there is no harm in being ahead
172 warn "And local branch '$1' is ahead of '$2'."
173 else
174 die "Branches need merging first."
175 fi
176 fi
177}
178
179main "$@"