blob: 283c77940c2faddf02f078aabce8040372aa38ea [file] [log] [blame]
Benedikt Böhm00ccea62010-01-26 12:39:36 +01001#!/bin/bash
2#
3# gitflow -- A collection of Git wrapper scripts to provide high-level
4# repository operations for Vincent Driessen's branching model:
5#
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öhm4a864fb2010-01-26 12:59:27 +010025
Benedikt Böhm00ccea62010-01-26 12:39:36 +010026warn() { echo "$@" >&2; }
27die() { warn "$@"; exit 1; }
28has() { [[ " ${*:2} " == *" $1 "* ]]; }
29
30usage() {
31 echo "usage: git flow <cmd> <btype> <args>"
32 echo
33 echo "<btype> can be any of: feature, release, hotfix, support"
34 echo
35 echo "Try 'git flow help <btype>' for details."
36}
37
38main() {
39 if [ $# -lt 2 ]; then
40 usage
41 exit 1
42 fi
43
Benedikt Böhm00ccea62010-01-26 12:39:36 +010044 # sanity checks
45 ACTION="$1"
46 BTYPE="$2"
47 shift 2
48
49 if [ ! -e "$GITFLOW_DIR/git-flow-$BTYPE" ]; then
50 usage
51 exit 1
52 fi
53
54 if ! git rev-parse --git-dir &>/dev/null; then
55 die "Not a git repository"
56 fi
57
58 # get all available branches
59 LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
60 REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
61 ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
62
63 # run command
64 . "$GITFLOW_DIR/git-flow-$BTYPE"
65
66 if ! declare -f cmd_$ACTION >/dev/null; then
67 usage
68 exit 1
69 fi
70
Benedikt Böhm00ccea62010-01-26 12:39:36 +010071 # run command
72 cmd_$ACTION "$@"
73}
74
75gitflow_check_clean_working_tree() {
76 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
77 die "Working tree contains unstaged changes. Aborting ..."
78 fi
79 if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
80 die "Index contains uncommited changes. Aborting ..."
81 fi
82}
83
84gitflow_require_local_branch() {
85 if ! has $1 $LOCAL_BRANCHES; then
86 die "Local branch '$1' does not exist and is required."
87 fi
88}
89
90gitflow_require_remote_branch() {
91 if ! has $1 $REMOTE_BRANCHES; then
92 die "Remote branch '$1' does not exist and is required."
93 fi
94}
95
96gitflow_require_branch() {
97 if ! has $1 $ALL_BRANCHES; then
98 die "Branch '$1' does not exist and is required."
99 fi
100}
101
102gitflow_require_branch_absent() {
103 if has $1 $ALL_BRANCHES; then
104 die "Branch '$1' already exists. Pick another name."
105 fi
106}
107
108#
109# gitflow_test_branches_equal()
110#
111# Tests whether branches and their "origin" counterparts have diverged and need
112# merging first. It returns error codes to provide more detail, like so:
113#
114# 0 Branch heads point to the same commit
115# 1 First given branch needs fast-forwarding
116# 2 Second given branch needs fast-forwarding
117# 3 Branch needs a real merge
118#
119gitflow_test_branches_equal() {
120 commit1=$(git rev-parse "$1")
121 commit2=$(git rev-parse "$2")
122 if [ "$commit1" != "$commit2" ]; then
123 base=$(git merge-base "$commit1" "$commit2")
124 if [ "$commit1" = "$base" ]; then
125 return 1
126 elif [ "$commit2" = "$base" ]; then
127 return 2
128 else
129 return 3
130 fi
131 else
132 return 0
133 fi
134}
135
136gitflow_require_branches_equal() {
137 gitflow_require_local_branch "$1"
138 gitflow_require_remote_branch "$2"
139 gitflow_test_branches_equal "$1" "$2"
140 status=$?
141 if [ $status -gt 0 ]; then
142 warn "Branches '$1' and '$2' have diverged."
143 if [ $status -eq 1 ]; then
144 die "And branch '$1' may be fast-forwarded."
145 elif [ $status -eq 2 ]; then
146 # Warn here, since there is no harm in being ahead
147 warn "And local branch '$1' is ahead of '$2'."
148 else
149 die "Branches need merging first."
150 fi
151 fi
152}
153
154main "$@"