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