Skip to content

Commit c3607ac

Browse files
committed
Put all common functions into separate file gitflow-common.
Renamed shFlags.sh -> gitflow-shFlags
1 parent 5474e46 commit c3607ac

File tree

4 files changed

+155
-121
lines changed

4 files changed

+155
-121
lines changed

Diff for: Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ SCRIPT_FILES+=git-flow-hotfix
1010
SCRIPT_FILES+=git-flow-release
1111
SCRIPT_FILES+=git-flow-support
1212
SCRIPT_FILES+=git-flow-version
13-
SCRIPT_FILES+=shFlags.sh
13+
SCRIPT_FILES+=gitflow-common
14+
SCRIPT_FILES+=gitflow-shFlags
1415

1516
all:
1617
@echo "usage: make install"

Diff for: git-flow

+5-120
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,13 @@ if [ "$DEBUG" = "yes" ]; then
1818
set -x
1919
fi
2020

21-
export GIT_DIR=$(git rev-parse --git-dir)
2221
export GITFLOW_DIR=$(dirname "$0")
22+
export GIT_DIR=$(git rev-parse --git-dir)
2323
export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
2424
export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
2525
export ORIGIN=$(git config --get gitflow.origin || echo origin)
2626
export README=$(git config --get gitflow.readme || echo README)
2727

28-
warn() { echo "$@" >&2; }
29-
die() { warn "$@"; exit 1; }
30-
31-
has() {
32-
local item=$1; shift
33-
echo " $@ " | grep -q " $item "
34-
}
35-
3628
usage() {
3729
echo "usage: git flow <subcommand>"
3830
echo
@@ -53,11 +45,12 @@ main() {
5345
exit 1
5446
fi
5547

48+
# load common functionality
49+
. "$GITFLOW_DIR/gitflow-common"
50+
5651
# use the shFlags project to parse the command line arguments
57-
. "$GITFLOW_DIR/shFlags.sh"
52+
. "$GITFLOW_DIR/gitflow-shFlags"
5853
FLAGS_PARENT="git flow"
59-
#DEFINE_boolean quiet 0 'run without output' q
60-
#DEFINE_boolean verbose 0 'run verbose (more output)' v
6154
FLAGS "$@" || exit $?
6255
eval set -- "${FLAGS_ARGV}"
6356

@@ -73,11 +66,6 @@ main() {
7366
die "Not a git repository"
7467
fi
7568

76-
# get all available branches
77-
LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
78-
REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
79-
ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
80-
8169
# run command
8270
. "$GITFLOW_DIR/git-flow-$SUBCOMMAND"
8371
FLAGS_PARENT="git flow $SUBCOMMAND"
@@ -99,109 +87,6 @@ main() {
9987
cmd_$SUBACTION "$@"
10088
}
10189

102-
gitflow_test_clean_working_tree() {
103-
if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
104-
return 1
105-
elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
106-
return 2
107-
else
108-
return 0
109-
fi
110-
}
111-
112-
gitflow_require_clean_working_tree() {
113-
gitflow_test_clean_working_tree
114-
result=$?
115-
if [ $result -eq 1 ]; then
116-
die "fatal: Working tree contains unstaged changes. Aborting."
117-
fi
118-
if [ $result -eq 2 ]; then
119-
die "fatal: Index contains uncommited changes. Aborting."
120-
fi
121-
}
122-
123-
gitflow_require_local_branch() {
124-
if ! has $1 $LOCAL_BRANCHES; then
125-
die "fatal: Local branch '$1' does not exist and is required."
126-
fi
127-
}
128-
129-
gitflow_require_remote_branch() {
130-
if ! has $1 $REMOTE_BRANCHES; then
131-
die "Remote branch '$1' does not exist and is required."
132-
fi
133-
}
134-
135-
gitflow_require_branch() {
136-
if ! has $1 $ALL_BRANCHES; then
137-
die "Branch '$1' does not exist and is required."
138-
fi
139-
}
140-
141-
gitflow_require_branch_absent() {
142-
if has $1 $ALL_BRANCHES; then
143-
die "Branch '$1' already exists. Pick another name."
144-
fi
145-
}
146-
147-
#
148-
# gitflow_test_branches_equal()
149-
#
150-
# Tests whether branches and their "origin" counterparts have diverged and need
151-
# merging first. It returns error codes to provide more detail, like so:
152-
#
153-
# 0 Branch heads point to the same commit
154-
# 1 First given branch needs fast-forwarding
155-
# 2 Second given branch needs fast-forwarding
156-
# 3 Branch needs a real merge
157-
#
158-
gitflow_test_branches_equal() {
159-
commit1=$(git rev-parse "$1")
160-
commit2=$(git rev-parse "$2")
161-
if [ "$commit1" != "$commit2" ]; then
162-
base=$(git merge-base "$commit1" "$commit2")
163-
if [ "$commit1" = "$base" ]; then
164-
return 1
165-
elif [ "$commit2" = "$base" ]; then
166-
return 2
167-
else
168-
return 3
169-
fi
170-
else
171-
return 0
172-
fi
173-
}
174-
175-
gitflow_require_branches_equal() {
176-
gitflow_require_local_branch "$1"
177-
gitflow_require_remote_branch "$2"
178-
gitflow_test_branches_equal "$1" "$2"
179-
status=$?
180-
if [ $status -gt 0 ]; then
181-
warn "Branches '$1' and '$2' have diverged."
182-
if [ $status -eq 1 ]; then
183-
die "And branch '$1' may be fast-forwarded."
184-
elif [ $status -eq 2 ]; then
185-
# Warn here, since there is no harm in being ahead
186-
warn "And local branch '$1' is ahead of '$2'."
187-
else
188-
die "Branches need merging first."
189-
fi
190-
fi
191-
}
192-
193-
#
194-
# gitflow_is_branch_merged_into()
195-
#
196-
# Checks whether branch $1 is succesfully merged into $2
197-
#
198-
gitflow_is_branch_merged_into() {
199-
local SUBJECT=$1
200-
local BASE=$2
201-
ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //')
202-
has $BASE $ALL_MERGES
203-
}
204-
20590
# helper functions for common reuse
20691
max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; }
20792

Diff for: gitflow-common

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#
2+
# git-flow -- A collection of Git extensions to provide high-level
3+
# repository operations for Vincent Driessen's branching model.
4+
#
5+
# Original blog post presenting this model is found at:
6+
# http://nvie.com/archives/323
7+
#
8+
# Feel free to contribute to this project at:
9+
# http://github.com/nvie/gitflow
10+
#
11+
# Copyright (c) 2010 by Vincent Driessen
12+
# Copyright (c) 2010 by Benedikt Böhm
13+
#
14+
15+
# shell output
16+
warn() { echo "$@" >&2; }
17+
die() { warn "$@"; exit 1; }
18+
19+
# set logic
20+
has() {
21+
local item=$1; shift
22+
echo " $@ " | grep -q " $item "
23+
}
24+
25+
# basic math
26+
min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
27+
max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }
28+
29+
# basic string matching
30+
startswith() { [ "$1" != "${1#$2}" ]; }
31+
endswith() { [ "$1" != "${1#$2}" ]; }
32+
33+
# convenience functions for checking shFlags flags
34+
flag() { eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
35+
noflag() { eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
36+
37+
#
38+
# Git specific common functionality
39+
#
40+
41+
# get all available branches
42+
LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
43+
REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
44+
ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"
45+
46+
gitflow_test_clean_working_tree() {
47+
if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
48+
return 1
49+
elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
50+
return 2
51+
else
52+
return 0
53+
fi
54+
}
55+
56+
gitflow_require_clean_working_tree() {
57+
gitflow_test_clean_working_tree
58+
result=$?
59+
if [ $result -eq 1 ]; then
60+
die "fatal: Working tree contains unstaged changes. Aborting."
61+
fi
62+
if [ $result -eq 2 ]; then
63+
die "fatal: Index contains uncommited changes. Aborting."
64+
fi
65+
}
66+
67+
gitflow_require_local_branch() {
68+
if ! has $1 $LOCAL_BRANCHES; then
69+
die "fatal: Local branch '$1' does not exist and is required."
70+
fi
71+
}
72+
73+
gitflow_require_remote_branch() {
74+
if ! has $1 $REMOTE_BRANCHES; then
75+
die "Remote branch '$1' does not exist and is required."
76+
fi
77+
}
78+
79+
gitflow_require_branch() {
80+
if ! has $1 $ALL_BRANCHES; then
81+
die "Branch '$1' does not exist and is required."
82+
fi
83+
}
84+
85+
gitflow_require_branch_absent() {
86+
if has $1 $ALL_BRANCHES; then
87+
die "Branch '$1' already exists. Pick another name."
88+
fi
89+
}
90+
91+
#
92+
# gitflow_test_branches_equal()
93+
#
94+
# Tests whether branches and their "origin" counterparts have diverged and need
95+
# merging first. It returns error codes to provide more detail, like so:
96+
#
97+
# 0 Branch heads point to the same commit
98+
# 1 First given branch needs fast-forwarding
99+
# 2 Second given branch needs fast-forwarding
100+
# 3 Branch needs a real merge
101+
#
102+
gitflow_test_branches_equal() {
103+
commit1=$(git rev-parse "$1")
104+
commit2=$(git rev-parse "$2")
105+
if [ "$commit1" != "$commit2" ]; then
106+
base=$(git merge-base "$commit1" "$commit2")
107+
if [ "$commit1" = "$base" ]; then
108+
return 1
109+
elif [ "$commit2" = "$base" ]; then
110+
return 2
111+
else
112+
return 3
113+
fi
114+
else
115+
return 0
116+
fi
117+
}
118+
119+
gitflow_require_branches_equal() {
120+
gitflow_require_local_branch "$1"
121+
gitflow_require_remote_branch "$2"
122+
gitflow_test_branches_equal "$1" "$2"
123+
status=$?
124+
if [ $status -gt 0 ]; then
125+
warn "Branches '$1' and '$2' have diverged."
126+
if [ $status -eq 1 ]; then
127+
die "And branch '$1' may be fast-forwarded."
128+
elif [ $status -eq 2 ]; then
129+
# Warn here, since there is no harm in being ahead
130+
warn "And local branch '$1' is ahead of '$2'."
131+
else
132+
die "Branches need merging first."
133+
fi
134+
fi
135+
}
136+
137+
#
138+
# gitflow_is_branch_merged_into()
139+
#
140+
# Checks whether branch $1 is succesfully merged into $2
141+
#
142+
gitflow_is_branch_merged_into() {
143+
local SUBJECT=$1
144+
local BASE=$2
145+
ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //')
146+
has $BASE $ALL_MERGES
147+
}
148+

Diff for: shFlags.sh renamed to gitflow-shFlags

File renamed without changes.

0 commit comments

Comments
 (0)