-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
git-flow
executable file
·179 lines (156 loc) · 4.24 KB
/
git-flow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
#
# git-flow -- A collection of Git extensions to provide high-level
# repository operations for Vincent Driessen's branching model.
#
# Original blog post presenting this model is found at:
# http://nvie.com/archives/323
#
# Feel free to contribute to this project at:
# http://github.com/nvie/gitflow
#
# Copyright (c) 2010 by Vincent Driessen
# Copyright (c) 2010 by Benedikt Böhm
#
# enable debug mode
if [ "$DEBUG" = "yes" ]; then
set -x
fi
export GITFLOW_DIR=$(dirname "$0")
export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
export ORIGIN=$(git config --get gitflow.origin || echo origin)
export README=$(git config --get gitflow.readme || echo README)
warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }
has() {
local item=$1; shift
echo " $@ " | grep -q " $item "
}
usage() {
echo "usage: git flow <subcommand>"
echo
echo "Available subcommands are:"
echo " init Initialize a new git repo with support for the branching model."
echo " feature Manage your feature branches."
echo " release Manage your release branches."
echo " hotfix Manage your hotfix branches."
echo " support Manage your support branches."
echo " version Shows version information."
echo
echo "Try 'git flow <subcommand> help' for details."
}
main() {
if [ $# -lt 1 ]; then
usage
exit 1
fi
# sanity checks
SUBCOMMAND="$1"; shift
if [ ! -e "$GITFLOW_DIR/git-flow-$SUBCOMMAND" ]; then
usage
exit 1
fi
if ! git rev-parse --git-dir >/dev/null; then
die "Not a git repository"
fi
# get all available branches
LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
# run command
. "$GITFLOW_DIR/git-flow-$SUBCOMMAND"
if ! typeset -f sub_main >/dev/null; then
usage
exit 1
fi
# run command
sub_main "$@"
}
sub_main() {
SUBACTION="${1:-default}"; shift
if ! typeset -f cmd_$SUBACTION 2>&1 >/dev/null; then
warn "Unknown subcommand: '$1'"
usage
exit 1
fi
# run the specified action
# if the subcommand declares a setup() function, call that first
if typeset -f setup >/dev/null; then
setup
fi
cmd_$SUBACTION "$@"
}
gitflow_check_clean_working_tree() {
if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
die "Working tree contains unstaged changes. Aborting ..."
fi
if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
die "Index contains uncommited changes. Aborting ..."
fi
}
gitflow_require_local_branch() {
if ! has $1 $LOCAL_BRANCHES; then
die "Local branch '$1' does not exist and is required."
fi
}
gitflow_require_remote_branch() {
if ! has $1 $REMOTE_BRANCHES; then
die "Remote branch '$1' does not exist and is required."
fi
}
gitflow_require_branch() {
if ! has $1 $ALL_BRANCHES; then
die "Branch '$1' does not exist and is required."
fi
}
gitflow_require_branch_absent() {
if has $1 $ALL_BRANCHES; then
die "Branch '$1' already exists. Pick another name."
fi
}
#
# gitflow_test_branches_equal()
#
# Tests whether branches and their "origin" counterparts have diverged and need
# merging first. It returns error codes to provide more detail, like so:
#
# 0 Branch heads point to the same commit
# 1 First given branch needs fast-forwarding
# 2 Second given branch needs fast-forwarding
# 3 Branch needs a real merge
#
gitflow_test_branches_equal() {
commit1=$(git rev-parse "$1")
commit2=$(git rev-parse "$2")
if [ "$commit1" != "$commit2" ]; then
base=$(git merge-base "$commit1" "$commit2")
if [ "$commit1" = "$base" ]; then
return 1
elif [ "$commit2" = "$base" ]; then
return 2
else
return 3
fi
else
return 0
fi
}
gitflow_require_branches_equal() {
gitflow_require_local_branch "$1"
gitflow_require_remote_branch "$2"
gitflow_test_branches_equal "$1" "$2"
status=$?
if [ $status -gt 0 ]; then
warn "Branches '$1' and '$2' have diverged."
if [ $status -eq 1 ]; then
die "And branch '$1' may be fast-forwarded."
elif [ $status -eq 2 ]; then
# Warn here, since there is no harm in being ahead
warn "And local branch '$1' is ahead of '$2'."
else
die "Branches need merging first."
fi
fi
}
main "$@"