Permalink
Browse files

Reorder functions: Super-common functions go first, then all git-spec…

…ific

functions, then gitflow-specific functions and finally, assertions to use
in gitflow subcommands.
  • Loading branch information...
1 parent 7832d6e commit 55c15539fa56d21ee7eb99c7b8303ee29ec8a3b4 @nvie committed Feb 22, 2010
Showing with 80 additions and 72 deletions.
  1. +80 −72 gitflow-common
View
@@ -47,6 +47,83 @@ git_remote_branches() { git branch -r | sed 's/^[* ] //'; }
git_all_branches() { ( git branch; git branch -r) | sed 's/^[* ] //'; }
git_all_tags() { git tag; }
+git_current_branch() {
+ git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
+}
+
+git_is_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
+}
+
+git_repo_is_headless() {
+ ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1
+}
+
+git_local_branch_exists() {
+ has $1 $(git_local_branches)
+}
+
+git_branch_exists() {
+ has $1 $(git_all_branches)
+}
+
+git_tag_exists() {
+ has $1 $(git_all_tags)
+}
+
+#
+# git_compare_branches()
+#
+# 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
+# 4 There is no merge base, i.e. the branches have no common ancestors
+#
+git_compare_branches() {
+ local commit1=$(git rev-parse "$1")
+ local commit2=$(git rev-parse "$2")
+ if [ "$commit1" != "$commit2" ]; then
+ local base=$(git merge-base "$commit1" "$commit2")
+ if [ $? -ne 0 ]; then
+ return 4
+ elif [ "$commit1" = "$base" ]; then
+ return 1
+ elif [ "$commit2" = "$base" ]; then
+ return 2
+ else
+ return 3
+ fi
+ else
+ return 0
+ fi
+}
+
+#
+# git_is_branch_merged_into()
+#
+# Checks whether branch $1 is succesfully merged into $2
+#
+git_is_branch_merged_into() {
+ local subject=$1
+ local base=$2
+ local all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
+ has $base $all_merges
+}
+
+#
+# gitflow specific common functionality
+#
+
# check if this repo has been inited for gitflow
gitflow_has_master_configured() {
local master=$(git config --get gitflow.branch.master)
@@ -131,19 +208,9 @@ gitflow_resolve_nameprefix() {
fi
}
-git_current_branch() {
- git branch | grep '^\* ' | grep -v 'no branch' | sed 's/^* //g'
-}
-
-git_is_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
-}
+#
+# Assertions for use in git-flow subcommands
+#
require_git_repo() {
if ! git rev-parse --git-dir >/dev/null 2>&1; then
@@ -157,10 +224,6 @@ require_gitflow_initialized() {
fi
}
-git_repo_is_headless() {
- ! git rev-parse --quiet --verify HEAD >/dev/null 2>&1
-}
-
require_clean_working_tree() {
git_is_clean_working_tree
local result=$?
@@ -172,18 +235,6 @@ require_clean_working_tree() {
fi
}
-git_local_branch_exists() {
- has $1 $(git_local_branches)
-}
-
-git_branch_exists() {
- has $1 $(git_all_branches)
-}
-
-git_tag_exists() {
- has $1 $(git_all_tags)
-}
-
require_local_branch() {
if ! git_local_branch_exists; then
die "fatal: Local branch '$1' does not exist and is required."
@@ -214,37 +265,6 @@ require_tag_absent() {
fi
}
-#
-# git_compare_branches()
-#
-# 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
-# 4 There is no merge base, i.e. the branches have no common ancestors
-#
-git_compare_branches() {
- local commit1=$(git rev-parse "$1")
- local commit2=$(git rev-parse "$2")
- if [ "$commit1" != "$commit2" ]; then
- local base=$(git merge-base "$commit1" "$commit2")
- if [ $? -ne 0 ]; then
- return 4
- elif [ "$commit1" = "$base" ]; then
- return 1
- elif [ "$commit2" = "$base" ]; then
- return 2
- else
- return 3
- fi
- else
- return 0
- fi
-}
-
require_branches_equal() {
require_local_branch "$1"
require_remote_branch "$2"
@@ -262,15 +282,3 @@ require_branches_equal() {
fi
fi
}
-
-#
-# git_is_branch_merged_into()
-#
-# Checks whether branch $1 is succesfully merged into $2
-#
-git_is_branch_merged_into() {
- local subject=$1
- local base=$2
- local all_merges=$(git branch --contains $subject | sed 's/^[* ] //')
- has $base $all_merges
-}

0 comments on commit 55c1553

Please sign in to comment.