Skip to content
Newer
Older
100755 150 lines (129 sloc) 3.31 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
a0434ca @nvie Add warn() function and redefine die() in terms of warn.
authored Jan 25, 2010
16 warn() { echo "$@" >&2; }
17 die() { warn "$@"; exit 1; }
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
18 has() { [[ " ${*:2} " == *" $1 "* ]]; }
19
20 usage() {
21 echo "usage: git flow <cmd> <btype> <args>"
22 echo
23 echo "<btype> can be any of: feature, release, hotfix, support"
24 echo
25 echo "Try 'git flow help <btype>' for details."
26 }
27
28 main() {
29 if [ $# -lt 2 ]; then
30 usage
31 exit 1
32 fi
33
34 export GITFLOW_DIR=$(dirname "$0")
35
36 # sanity checks
37 ACTION="$1"
38 BTYPE="$2"
39 shift 2
40
41 if [ ! -e "$GITFLOW_DIR/git-flow-$BTYPE" ]; then
42 usage
43 exit 1
44 fi
45
46 if ! git rev-parse --git-dir &>/dev/null; then
47 die "Not a git repository"
48 fi
49
50 # get all available branches
51 LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
52 REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
53 ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
54
55 # run command
56 . "$GITFLOW_DIR/git-flow-$BTYPE"
57
58 if ! declare -f cmd_$ACTION >/dev/null; then
59 usage
60 exit 1
61 fi
62
63 # fail if a command failes
64 set -e
65
66 # run command
67 cmd_$ACTION "$@"
68 }
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
69
61ade55 @nvie Add initial beginning to the gitflow subcommand infrastructure.
authored Jan 21, 2010
70 gitflow_check_clean_working_tree() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
71 if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
72 die "Working tree contains unstaged changes. Aborting ..."
73 fi
74 if ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
75 die "Index contains uncommited changes. Aborting ..."
a1bc871 @nvie Implemented the gitflow_check_clean_working_tree() check for dirty di…
authored Jan 25, 2010
76 fi
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
77 }
78
79 gitflow_require_local_branch() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
80 if ! has $1 $LOCAL_BRANCHES; then
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
81 die "Local branch '$1' does not exist and is required."
82 fi
83 }
84
85 gitflow_require_remote_branch() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
86 if ! has $1 $REMOTE_BRANCHES; then
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
87 die "Remote branch '$1' does not exist and is required."
88 fi
89 }
90
91 gitflow_require_branch() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
92 if ! has $1 $ALL_BRANCHES; then
4f1cc33 @nvie Added functions for assuring branches are in place before doing the a…
authored Jan 25, 2010
93 die "Branch '$1' does not exist and is required."
94 fi
61ade55 @nvie Add initial beginning to the gitflow subcommand infrastructure.
authored Jan 21, 2010
95 }
96
6c9e804 @nvie Add function gitflow_require_branch_absent(), to test for the *absenc…
authored Jan 25, 2010
97 gitflow_require_branch_absent() {
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
98 if has $1 $ALL_BRANCHES; then
ec2c895 @nvie Add extra line to error message for clarification.
authored Jan 25, 2010
99 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
100 fi
101 }
102
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
103 #
104 # gitflow_test_branches_equal()
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
105 #
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
106 # Tests whether branches and their "origin" counterparts have diverged and need
107 # merging first. It returns error codes to provide more detail, like so:
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
108 #
109 # 0 Branch heads point to the same commit
110 # 1 First given branch needs fast-forwarding
111 # 2 Second given branch needs fast-forwarding
112 # 3 Branch needs a real merge
afee9fd @nvie Add function gitflow_require_branches_equal() to test whether local a…
authored Jan 25, 2010
113 #
114 gitflow_test_branches_equal() {
115 commit1=$(git rev-parse "$1")
116 commit2=$(git rev-parse "$2")
117 if [ "$commit1" != "$commit2" ]; then
118 base=$(git merge-base "$commit1" "$commit2")
119 if [ "$commit1" = "$base" ]; then
120 return 1
121 elif [ "$commit2" = "$base" ]; then
122 return 2
123 else
124 return 3
125 fi
126 else
127 return 0
128 fi
129 }
130
131 gitflow_require_branches_equal() {
132 gitflow_require_local_branch "$1"
133 gitflow_require_remote_branch "$2"
134 gitflow_test_branches_equal "$1" "$2"
135 status=$?
136 if [ $status -gt 0 ]; then
137 warn "Branches '$1' and '$2' have diverged."
138 if [ $status -eq 1 ]; then
139 die "And branch '$1' may be fast-forwarded."
140 elif [ $status -eq 2 ]; then
51fa95d @nvie Only mention (warn) when develop is ahead of origin/develop. This is …
authored Jan 26, 2010
141 # Warn here, since there is no harm in being ahead
142 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
143 else
144 die "Branches need merging first."
145 fi
146 fi
147 }
148
00ccea6 @hollow refactor the whole thing
hollow authored Jan 26, 2010
149 main "$@"
Something went wrong with that request. Please try again.