Skip to content

Commit

Permalink
Don't store remote and local branch names in shell variables, but query
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
nvie committed Feb 20, 2010
1 parent b3e89fe commit 21c7aa9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
4 changes: 2 additions & 2 deletions git-flow-feature
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions git-flow-hotfix
Expand Up @@ -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
Expand Down Expand Up @@ -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" ] || \
Expand Down
4 changes: 2 additions & 2 deletions git-flow-release
Expand Up @@ -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
Expand Down Expand Up @@ -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" ] || \
Expand Down
2 changes: 1 addition & 1 deletion git-flow-support
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions gitflow-common
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
} }


# #
Expand Down

0 comments on commit 21c7aa9

Please sign in to comment.