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