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