blob: 7a5c4cc348aedd3620382ccc2031faaf335c44c6 [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
Benedikt Böhm00ccea62010-01-26 12:39:36 +010068 # run command
69 cmd_$ACTION "$@"
70}
71
72gitflow_check_clean_working_tree() {
73 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
74 die "Working tree contains unstaged changes. Aborting ..."
75 fi
76 if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
77 die "Index contains uncommited changes. Aborting ..."
78 fi
79}
80
81gitflow_require_local_branch() {
82 if ! has $1 $LOCAL_BRANCHES; then
83 die "Local branch '$1' does not exist and is required."
84 fi
85}
86
87gitflow_require_remote_branch() {
88 if ! has $1 $REMOTE_BRANCHES; then
89 die "Remote branch '$1' does not exist and is required."
90 fi
91}
92
93gitflow_require_branch() {
94 if ! has $1 $ALL_BRANCHES; then
95 die "Branch '$1' does not exist and is required."
96 fi
97}
98
99gitflow_require_branch_absent() {
100 if has $1 $ALL_BRANCHES; then
101 die "Branch '$1' already exists. Pick another name."
102 fi
103}
104
105#
106# gitflow_test_branches_equal()
107#
108# Tests whether branches and their "origin" counterparts have diverged and need
109# merging first. It returns error codes to provide more detail, like so:
110#
111# 0 Branch heads point to the same commit
112# 1 First given branch needs fast-forwarding
113# 2 Second given branch needs fast-forwarding
114# 3 Branch needs a real merge
115#
116gitflow_test_branches_equal() {
117 commit1=$(git rev-parse "$1")
118 commit2=$(git rev-parse "$2")
119 if [ "$commit1" != "$commit2" ]; then
120 base=$(git merge-base "$commit1" "$commit2")
121 if [ "$commit1" = "$base" ]; then
122 return 1
123 elif [ "$commit2" = "$base" ]; then
124 return 2
125 else
126 return 3
127 fi
128 else
129 return 0
130 fi
131}
132
133gitflow_require_branches_equal() {
134 gitflow_require_local_branch "$1"
135 gitflow_require_remote_branch "$2"
136 gitflow_test_branches_equal "$1" "$2"
137 status=$?
138 if [ $status -gt 0 ]; then
139 warn "Branches '$1' and '$2' have diverged."
140 if [ $status -eq 1 ]; then
141 die "And branch '$1' may be fast-forwarded."
142 elif [ $status -eq 2 ]; then
143 # Warn here, since there is no harm in being ahead
144 warn "And local branch '$1' is ahead of '$2'."
145 else
146 die "Branches need merging first."
147 fi
148 fi
149}
150
151main "$@"