blob: 8e2fa78978ab73502109612a840d1476fb283ab2 [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 Driessen48386442010-01-29 10:30:40 +010087gitflow_require_clean_working_tree() {
Benedikt Böhm00ccea62010-01-26 12:39:36 +010088 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
89 die "Working tree contains unstaged changes. Aborting ..."
90 fi
91 if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
92 die "Index contains uncommited changes. Aborting ..."
93 fi
94}
95
96gitflow_require_local_branch() {
97 if ! has $1 $LOCAL_BRANCHES; then
98 die "Local branch '$1' does not exist and is required."
99 fi
100}
101
102gitflow_require_remote_branch() {
103 if ! has $1 $REMOTE_BRANCHES; then
104 die "Remote branch '$1' does not exist and is required."
105 fi
106}
107
108gitflow_require_branch() {
109 if ! has $1 $ALL_BRANCHES; then
110 die "Branch '$1' does not exist and is required."
111 fi
112}
113
114gitflow_require_branch_absent() {
115 if has $1 $ALL_BRANCHES; then
116 die "Branch '$1' already exists. Pick another name."
117 fi
118}
119
120#
121# gitflow_test_branches_equal()
122#
123# Tests whether branches and their "origin" counterparts have diverged and need
124# merging first. It returns error codes to provide more detail, like so:
125#
126# 0 Branch heads point to the same commit
127# 1 First given branch needs fast-forwarding
128# 2 Second given branch needs fast-forwarding
129# 3 Branch needs a real merge
130#
131gitflow_test_branches_equal() {
132 commit1=$(git rev-parse "$1")
133 commit2=$(git rev-parse "$2")
134 if [ "$commit1" != "$commit2" ]; then
135 base=$(git merge-base "$commit1" "$commit2")
136 if [ "$commit1" = "$base" ]; then
137 return 1
138 elif [ "$commit2" = "$base" ]; then
139 return 2
140 else
141 return 3
142 fi
143 else
144 return 0
145 fi
146}
147
148gitflow_require_branches_equal() {
149 gitflow_require_local_branch "$1"
150 gitflow_require_remote_branch "$2"
151 gitflow_test_branches_equal "$1" "$2"
152 status=$?
153 if [ $status -gt 0 ]; then
154 warn "Branches '$1' and '$2' have diverged."
155 if [ $status -eq 1 ]; then
156 die "And branch '$1' may be fast-forwarded."
157 elif [ $status -eq 2 ]; then
158 # Warn here, since there is no harm in being ahead
159 warn "And local branch '$1' is ahead of '$2'."
160 else
161 die "Branches need merging first."
162 fi
163 fi
164}
165
Vincent Driessen58995b52010-01-28 12:39:06 +0100166#
167# gitflow_is_branch_merged_into()
168#
169# Checks whether branch $1 is succesfully merged into $2
170#
171gitflow_is_branch_merged_into() {
172 local SUBJECT=$1
173 local BASE=$2
174 ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //')
175 has $BASE $ALL_MERGES
176}
177
Benedikt Böhm00ccea62010-01-26 12:39:36 +0100178main "$@"