blob: d7cee2d397d65160c60a7384c3e501fe7a90a577 [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öhm00ccea62010-01-26 12:39:36 +010021warn() { echo "$@" >&2; }
22die() { warn "$@"; exit 1; }
23has() { [[ " ${*:2} " == *" $1 "* ]]; }
24
25usage() {
26 echo "usage: git flow <cmd> <btype> <args>"
27 echo
28 echo "<btype> can be any of: feature, release, hotfix, support"
29 echo
30 echo "Try 'git flow help <btype>' for details."
31}
32
33main() {
34 if [ $# -lt 2 ]; then
35 usage
36 exit 1
37 fi
38
39 export GITFLOW_DIR=$(dirname "$0")
40
41 # sanity checks
42 ACTION="$1"
43 BTYPE="$2"
44 shift 2
45
46 if [ ! -e "$GITFLOW_DIR/git-flow-$BTYPE" ]; then
47 usage
48 exit 1
49 fi
50
51 if ! git rev-parse --git-dir &>/dev/null; then
52 die "Not a git repository"
53 fi
54
55 # get all available branches
56 LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
57 REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
58 ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
59
60 # run command
61 . "$GITFLOW_DIR/git-flow-$BTYPE"
62
63 if ! declare -f cmd_$ACTION >/dev/null; then
64 usage
65 exit 1
66 fi
67
68 # fail if a command failes
69 set -e
70
71 # 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 "$@"