Skip to content

Commit

Permalink
Put all common functions into separate file gitflow-common.
Browse files Browse the repository at this point in the history
Renamed shFlags.sh -> gitflow-shFlags
  • Loading branch information
nvie committed Feb 5, 2010
1 parent 5474e46 commit c3607ac
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 121 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ SCRIPT_FILES+=git-flow-hotfix
SCRIPT_FILES+=git-flow-release
SCRIPT_FILES+=git-flow-support
SCRIPT_FILES+=git-flow-version
SCRIPT_FILES+=shFlags.sh
SCRIPT_FILES+=gitflow-common
SCRIPT_FILES+=gitflow-shFlags

all:
@echo "usage: make install"
Expand Down
125 changes: 5 additions & 120 deletions git-flow
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,13 @@ if [ "$DEBUG" = "yes" ]; then
set -x
fi

export GIT_DIR=$(git rev-parse --git-dir)
export GITFLOW_DIR=$(dirname "$0")
export GIT_DIR=$(git rev-parse --git-dir)
export MASTER_BRANCH=$(git config --get gitflow.branch.master || echo master)
export DEVELOP_BRANCH=$(git config --get gitflow.branch.develop || echo develop)
export ORIGIN=$(git config --get gitflow.origin || echo origin)
export README=$(git config --get gitflow.readme || echo README)

warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }

has() {
local item=$1; shift
echo " $@ " | grep -q " $item "
}

usage() {
echo "usage: git flow <subcommand>"
echo
Expand All @@ -53,11 +45,12 @@ main() {
exit 1
fi

# load common functionality
. "$GITFLOW_DIR/gitflow-common"

# use the shFlags project to parse the command line arguments
. "$GITFLOW_DIR/shFlags.sh"
. "$GITFLOW_DIR/gitflow-shFlags"
FLAGS_PARENT="git flow"
#DEFINE_boolean quiet 0 'run without output' q
#DEFINE_boolean verbose 0 'run verbose (more output)' v
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"

Expand All @@ -73,11 +66,6 @@ main() {
die "Not a git repository"
fi

# get all available branches
LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"

# run command
. "$GITFLOW_DIR/git-flow-$SUBCOMMAND"
FLAGS_PARENT="git flow $SUBCOMMAND"
Expand All @@ -99,109 +87,6 @@ main() {
cmd_$SUBACTION "$@"
}

gitflow_test_clean_working_tree() {
if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
return 1
elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
return 2
else
return 0
fi
}

gitflow_require_clean_working_tree() {
gitflow_test_clean_working_tree
result=$?
if [ $result -eq 1 ]; then
die "fatal: Working tree contains unstaged changes. Aborting."
fi
if [ $result -eq 2 ]; then
die "fatal: Index contains uncommited changes. Aborting."
fi
}

gitflow_require_local_branch() {
if ! has $1 $LOCAL_BRANCHES; then
die "fatal: Local branch '$1' does not exist and is required."
fi
}

gitflow_require_remote_branch() {
if ! has $1 $REMOTE_BRANCHES; then
die "Remote branch '$1' does not exist and is required."
fi
}

gitflow_require_branch() {
if ! has $1 $ALL_BRANCHES; then
die "Branch '$1' does not exist and is required."
fi
}

gitflow_require_branch_absent() {
if has $1 $ALL_BRANCHES; then
die "Branch '$1' already exists. Pick another name."
fi
}

#
# gitflow_test_branches_equal()
#
# Tests whether branches and their "origin" counterparts have diverged and need
# merging first. It returns error codes to provide more detail, like so:
#
# 0 Branch heads point to the same commit
# 1 First given branch needs fast-forwarding
# 2 Second given branch needs fast-forwarding
# 3 Branch needs a real merge
#
gitflow_test_branches_equal() {
commit1=$(git rev-parse "$1")
commit2=$(git rev-parse "$2")
if [ "$commit1" != "$commit2" ]; then
base=$(git merge-base "$commit1" "$commit2")
if [ "$commit1" = "$base" ]; then
return 1
elif [ "$commit2" = "$base" ]; then
return 2
else
return 3
fi
else
return 0
fi
}

gitflow_require_branches_equal() {
gitflow_require_local_branch "$1"
gitflow_require_remote_branch "$2"
gitflow_test_branches_equal "$1" "$2"
status=$?
if [ $status -gt 0 ]; then
warn "Branches '$1' and '$2' have diverged."
if [ $status -eq 1 ]; then
die "And branch '$1' may be fast-forwarded."
elif [ $status -eq 2 ]; then
# Warn here, since there is no harm in being ahead
warn "And local branch '$1' is ahead of '$2'."
else
die "Branches need merging first."
fi
fi
}

#
# gitflow_is_branch_merged_into()
#
# Checks whether branch $1 is succesfully merged into $2
#
gitflow_is_branch_merged_into() {
local SUBJECT=$1
local BASE=$2
ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //')
has $BASE $ALL_MERGES
}

# helper functions for common reuse
max() { if [ "$1" -gt "$2" ]; then echo "$1"; else echo "$2"; fi; }

Expand Down
148 changes: 148 additions & 0 deletions gitflow-common
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
#
# git-flow -- A collection of Git extensions to provide high-level
# repository operations for Vincent Driessen's branching model.
#
# Original blog post presenting this model is found at:
# http://nvie.com/archives/323
#
# Feel free to contribute to this project at:
# http://github.com/nvie/gitflow
#
# Copyright (c) 2010 by Vincent Driessen
# Copyright (c) 2010 by Benedikt Böhm
#

# shell output
warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }

# set logic
has() {
local item=$1; shift
echo " $@ " | grep -q " $item "
}

# basic math
min() { [ "$1" -le "$2" ] && echo "$1" || echo "$2"; }
max() { [ "$1" -ge "$2" ] && echo "$1" || echo "$2"; }

# basic string matching
startswith() { [ "$1" != "${1#$2}" ]; }
endswith() { [ "$1" != "${1#$2}" ]; }

# convenience functions for checking shFlags flags
flag() { eval FLAG='$FLAGS_'$1; [ $FLAG -eq $FLAGS_TRUE ]; }
noflag() { eval FLAG='$FLAGS_'$1; [ $FLAG -ne $FLAGS_TRUE ]; }

#
# Git specific common functionality
#

# get all available branches
LOCAL_BRANCHES=$(git branch | sed 's/^[* ] //')
REMOTE_BRANCHES=$(git branch -r | sed 's/^[* ] //')
ALL_BRANCHES="$LOCAL_BRANCHES $REMOTE_BRANCHES"

gitflow_test_clean_working_tree() {
if ! git diff --no-ext-diff --ignore-submodules --quiet --exit-code; then
return 1
elif ! git diff-index --cached --quiet --ignore-submodules HEAD --; then
return 2
else
return 0
fi
}

gitflow_require_clean_working_tree() {
gitflow_test_clean_working_tree
result=$?
if [ $result -eq 1 ]; then
die "fatal: Working tree contains unstaged changes. Aborting."
fi
if [ $result -eq 2 ]; then
die "fatal: Index contains uncommited changes. Aborting."
fi
}

gitflow_require_local_branch() {
if ! has $1 $LOCAL_BRANCHES; then
die "fatal: Local branch '$1' does not exist and is required."
fi
}

gitflow_require_remote_branch() {
if ! has $1 $REMOTE_BRANCHES; then
die "Remote branch '$1' does not exist and is required."
fi
}

gitflow_require_branch() {
if ! has $1 $ALL_BRANCHES; then
die "Branch '$1' does not exist and is required."
fi
}

gitflow_require_branch_absent() {
if has $1 $ALL_BRANCHES; then
die "Branch '$1' already exists. Pick another name."
fi
}

#
# gitflow_test_branches_equal()
#
# Tests whether branches and their "origin" counterparts have diverged and need
# merging first. It returns error codes to provide more detail, like so:
#
# 0 Branch heads point to the same commit
# 1 First given branch needs fast-forwarding
# 2 Second given branch needs fast-forwarding
# 3 Branch needs a real merge
#
gitflow_test_branches_equal() {
commit1=$(git rev-parse "$1")
commit2=$(git rev-parse "$2")
if [ "$commit1" != "$commit2" ]; then
base=$(git merge-base "$commit1" "$commit2")
if [ "$commit1" = "$base" ]; then
return 1
elif [ "$commit2" = "$base" ]; then
return 2
else
return 3
fi
else
return 0
fi
}

gitflow_require_branches_equal() {
gitflow_require_local_branch "$1"
gitflow_require_remote_branch "$2"
gitflow_test_branches_equal "$1" "$2"
status=$?
if [ $status -gt 0 ]; then
warn "Branches '$1' and '$2' have diverged."
if [ $status -eq 1 ]; then
die "And branch '$1' may be fast-forwarded."
elif [ $status -eq 2 ]; then
# Warn here, since there is no harm in being ahead
warn "And local branch '$1' is ahead of '$2'."
else
die "Branches need merging first."
fi
fi
}

#
# gitflow_is_branch_merged_into()
#
# Checks whether branch $1 is succesfully merged into $2
#
gitflow_is_branch_merged_into() {
local SUBJECT=$1
local BASE=$2
ALL_MERGES=$(git branch --contains $SUBJECT | sed 's/^[* ] //')
has $BASE $ALL_MERGES
}

File renamed without changes.

0 comments on commit c3607ac

Please sign in to comment.