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