Skip to content
Newer
Older
100755 158 lines (137 sloc) 3.63 KB
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
1 #!/bin/bash
093a147 @nvie Added header comments to all files.
authored Jan 21, 2010
2 #
3 # gitflow -- A collection of Git wrapper scripts to provide high-level
4 # repository operations for Vincent Driessen's branching model:
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
5 #
093a147 @nvie Added header comments to all files.
authored Jan 21, 2010
6 # Original blog post presenting this model is found at:
7 # http://nvie.com/archives/323
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
8 #
093a147 @nvie Added header comments to all files.
authored Jan 21, 2010
9 # Feel free to contribute to this project at:
10 # http://github.com/nvie/gitflow
11 #
12 # Copyright (c) 2010 by Vincent Driessen
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
13 # Copyright (c) 2010 by Benedikt Böhm
093a147 @nvie Added header comments to all files.
authored Jan 21, 2010
14 #
61ade55 @nvie Add initial beginning to the gitflow subcommand infrastructure.
authored Jan 21, 2010
15
e5eaff9 @hollow add bash debug support
hollow authored Jan 26, 2010
16 # enable debug mode
17 if [ "$DEBUG" = "yes" ]; then
18 set -x
19 fi
20
4a864fb @hollow make master and develop branch names configurable
hollow authored Jan 26, 2010
21 export GITFLOW_DIR=$(dirname "$0")
22 export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
23 export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
350e715 @hollow make origin configurable
hollow authored Jan 26, 2010
24 export ORIGIN=$(git config --get gitflow.origin || echo origin)
4a864fb @hollow make master and develop branch names configurable
hollow authored Jan 26, 2010
25
a0434ca @nvie Add warn() function and redefine die() in terms of warn.
authored Jan 25, 2010
26 warn() { echo "$@" >&2; }
27 die() { warn "$@"; exit 1; }
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
28 has() { [[ " ${*:2} " == *" $1 "* ]]; }
29
30 usage() {
7d703a8 @hollow Merge branch 'develop' of git://github.com/nvie/gitflow into develop
hollow authored Jan 26, 2010
31 . "$GITFLOW_DIR/git-flow-version"
32 echo "gitflow, version $GITFLOW_VERSION"
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
33 echo
7d703a8 @hollow Merge branch 'develop' of git://github.com/nvie/gitflow into develop
hollow authored Jan 26, 2010
34 echo "usage: git flow <cmd> <type> <args>"
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
35 echo
7d703a8 @hollow Merge branch 'develop' of git://github.com/nvie/gitflow into develop
hollow authored Jan 26, 2010
36 echo "<type> can be any of: feature, release, hotfix, support"
37 echo
38 echo "Try 'git flow help <type>' for details."
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
39 }
40
41 main() {
42 if [ $# -lt 2 ]; then
43 usage
44 exit 1
45 fi
46
47 # 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
74 # run command
75 cmd_$ACTION "$@"
76 }
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
77
61ade55 @nvie Add initial beginning to the gitflow subcommand infrastructure.
authored Jan 21, 2010
78 gitflow_check_clean_working_tree() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
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 ..."
a1bc871 @nvie Implemented the gitflow_check_clean_working_tree() check for dirty di…
authored Jan 25, 2010
84 fi
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
85 }
86
87 gitflow_require_local_branch() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
88 if ! has $1 $LOCAL_BRANCHES; then
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
89 die "Local branch '$1' does not exist and is required."
90 fi
91 }
92
93 gitflow_require_remote_branch() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
94 if ! has $1 $REMOTE_BRANCHES; then
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
95 die "Remote branch '$1' does not exist and is required."
96 fi
97 }
98
99 gitflow_require_branch() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
100 if ! has $1 $ALL_BRANCHES; then
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
101 die "Branch '$1' does not exist and is required."
102 fi
61ade55 @nvie Add initial beginning to the gitflow subcommand infrastructure.
authored Jan 21, 2010
103 }
104
6c9e804 @nvie Add function gitflow_require_branch_absent(), to test for the *absenc…
authored Jan 25, 2010
105 gitflow_require_branch_absent() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
106 if has $1 $ALL_BRANCHES; then
ec2c895 @nvie Add extra line to error message for clarification.
authored Jan 25, 2010
107 die "Branch '$1' already exists. Pick another name."
6c9e804 @nvie Add function gitflow_require_branch_absent(), to test for the *absenc…
authored Jan 25, 2010
108 fi
109 }
110
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
111 #
112 # gitflow_test_branches_equal()
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
113 #
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
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:
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
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
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
121 #
122 gitflow_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
139 gitflow_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
51fa95d @nvie Only mention (warn) when develop is ahead of origin/develop. This is …
authored Jan 26, 2010
149 # Warn here, since there is no harm in being ahead
150 warn "And local branch '$1' is ahead of '$2'."
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
151 else
152 die "Branches need merging first."
153 fi
154 fi
155 }
156
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
157 main "$@"
Something went wrong with that request. Please try again.