Commit 21c7aa9c authored by Vincent Driessen's avatar Vincent Driessen

Don't store remote and local branch names in shell variables, but query

them live using git commands instead. This avoids git commands being
issued by subcommands that do not necessarily require an existing Git repo
to be initialized (i.e. git-flow init).
parent b3e89fe5
...@@ -35,7 +35,7 @@ cmd_list() { ...@@ -35,7 +35,7 @@ cmd_list() {
local feature_branches local feature_branches
local current_branch local current_branch
local short_names local short_names
feature_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX") feature_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
if [ -z "$feature_branches" ]; then if [ -z "$feature_branches" ]; then
warn "No feature branches exist." warn "No feature branches exist."
exit 0 exit 0
...@@ -231,7 +231,7 @@ cmd_finish() { ...@@ -231,7 +231,7 @@ cmd_finish() {
git fetch -q "$ORIGIN" "$BRANCH" git fetch -q "$ORIGIN" "$BRANCH"
fi fi
if has "$ORIGIN/$BRANCH" "$REMOTE_BRANCHES"; then if has "$ORIGIN/$BRANCH" "$(gitflow_remote_branches)"; then
gitflow_require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH" gitflow_require_branches_equal "$BRANCH" "$ORIGIN/$BRANCH"
fi fi
gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH" gitflow_require_branches_equal "$DEVELOP_BRANCH" "$ORIGIN/$DEVELOP_BRANCH"
......
...@@ -32,7 +32,7 @@ cmd_list() { ...@@ -32,7 +32,7 @@ cmd_list() {
local hotfix_branches local hotfix_branches
local current_branch local current_branch
local short_names local short_names
hotfix_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX") hotfix_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
if [ -z "$hotfix_branches" ]; then if [ -z "$hotfix_branches" ]; then
warn "No hotfix branches exist." warn "No hotfix branches exist."
exit 0 exit 0
...@@ -113,7 +113,7 @@ require_base_is_on_master() { ...@@ -113,7 +113,7 @@ require_base_is_on_master() {
} }
require_no_existing_hotfix_branches() { require_no_existing_hotfix_branches() {
local hotfix_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX") local hotfix_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
local first_branch=$(echo ${hotfix_branches} | head -n1) local first_branch=$(echo ${hotfix_branches} | head -n1)
first_branch=${first_branch#$PREFIX} first_branch=${first_branch#$PREFIX}
[ -z "$hotfix_branches" ] || \ [ -z "$hotfix_branches" ] || \
......
...@@ -43,7 +43,7 @@ cmd_list() { ...@@ -43,7 +43,7 @@ cmd_list() {
local release_branches local release_branches
local current_branch local current_branch
local short_names local short_names
release_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX") release_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
if [ -z "$release_branches" ]; then if [ -z "$release_branches" ]; then
warn "No release branches exist." warn "No release branches exist."
exit 0 exit 0
...@@ -119,7 +119,7 @@ require_base_is_on_develop() { ...@@ -119,7 +119,7 @@ require_base_is_on_develop() {
} }
require_no_existing_release_branches() { require_no_existing_release_branches() {
local release_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX") local release_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
local first_branch=$(echo ${release_branches} | head -n1) local first_branch=$(echo ${release_branches} | head -n1)
first_branch=${first_branch#$PREFIX} first_branch=${first_branch#$PREFIX}
[ -z "$release_branches" ] || \ [ -z "$release_branches" ] || \
......
...@@ -34,7 +34,7 @@ cmd_list() { ...@@ -34,7 +34,7 @@ cmd_list() {
local support_branches local support_branches
local current_branch local current_branch
local short_names local short_names
support_branches=$(echo "$LOCAL_BRANCHES" | grep "^$PREFIX") support_branches=$(echo "$(gitflow_local_branches)" | grep "^$PREFIX")
if [ -z "$support_branches" ]; then if [ -z "$support_branches" ]; then
warn "No support branches exist." warn "No support branches exist."
exit 0 exit 0
......
...@@ -39,10 +39,10 @@ noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; } ...@@ -39,10 +39,10 @@ noflag() { local FLAG; eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }
# #
# get all available branches # get all available branches
LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //') gitflow_local_branches() { git branch | sed 's/^[* ] //'; }
REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //') gitflow_remote_branches() { git branch -r | sed 's/^[* ] //'; }
ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES" gitflow_all_branches() { gitflow_local_branches; gitflow_remote_branches; }
ALL_TAGS=$(git tag) gitflow_all_tags() { git tag; }
# #
# resolve_nameprefix # resolve_nameprefix
...@@ -51,8 +51,8 @@ ALL_TAGS=$(git tag) ...@@ -51,8 +51,8 @@ ALL_TAGS=$(git tag)
# $1 = name prefix to resolve # $1 = name prefix to resolve
# $2 = branch prefix to use # $2 = branch prefix to use
# #
# Searches branch names from LOCAL_BRANCHES to look for a unique branch # Searches branch names from gitflow_local_branches() to look for a unique
# name whose name starts with the given name prefix. # branch name whose name starts with the given name prefix.
# #
# There are multiple exit codes possible: # There are multiple exit codes possible:
# 0: The unambiguous full name of the branch is written to stdout # 0: The unambiguous full name of the branch is written to stdout
...@@ -67,12 +67,12 @@ resolve_nameprefix() { ...@@ -67,12 +67,12 @@ resolve_nameprefix() {
local num_matches local num_matches
# first, check if there is a perfect match # first, check if there is a perfect match
if has "$LOCAL_BRANCHES" "$prefix$name"; then if has "$(gitflow_local_branches)" "$prefix$name"; then
echo "$name" echo "$name"
return 0 return 0
fi fi
matches=$(echo "$LOCAL_BRANCHES" | grep "^$prefix$name") matches=$(echo "$(gitflow_local_branches)" | grep "^$prefix$name")
num_matches=$(echo "$matches" | wc -l) num_matches=$(echo "$matches" | wc -l)
if [ -z "$matches" ]; then if [ -z "$matches" ]; then
# no prefix match, so take it literally # no prefix match, so take it literally
...@@ -119,37 +119,37 @@ gitflow_require_clean_working_tree() { ...@@ -119,37 +119,37 @@ gitflow_require_clean_working_tree() {
} }
gitflow_require_local_branch() { gitflow_require_local_branch() {
if ! has $1 $LOCAL_BRANCHES; then if ! has $1 $(gitflow_local_branches); then
die "fatal: Local branch '$1' does not exist and is required." die "fatal: Local branch '$1' does not exist and is required."
fi fi
} }
gitflow_require_remote_branch() { gitflow_require_remote_branch() {
if ! has $1 $REMOTE_BRANCHES; then if ! has $1 $(gitflow_remote_branches); then
die "Remote branch '$1' does not exist and is required." die "Remote branch '$1' does not exist and is required."
fi fi
} }
gitflow_require_branch() { gitflow_require_branch() {
if ! has $1 $ALL_BRANCHES; then if ! has $1 $(gitflow_all_branches); then
die "Branch '$1' does not exist and is required." die "Branch '$1' does not exist and is required."
fi fi
} }
gitflow_require_branch_absent() { gitflow_require_branch_absent() {
if has $1 $ALL_BRANCHES; then if has $1 $(gitflow_all_branches); then
die "Branch '$1' already exists. Pick another name." die "Branch '$1' already exists. Pick another name."
fi fi
} }
gitflow_require_tag_absent() { gitflow_require_tag_absent() {
if has $1 $ALL_TAGS; then if has $1 $(gitflow_all_tags); then
die "Tag '$1' already exists. Pick another name." die "Tag '$1' already exists. Pick another name."
fi fi
} }
gitflow_tag_exists() { gitflow_tag_exists() {
has $1 $ALL_TAGS has $1 $(gitflow_all_tags)
} }
# #
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment