Add function gitflow_require_branches_equal() to test whether local and remote branches have diverged.
diff --git a/gitflow-sh-setup b/gitflow-sh-setup
index 7fbef51..e1d5329 100755
--- a/gitflow-sh-setup
+++ b/gitflow-sh-setup
@@ -49,3 +49,51 @@
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")
+ short_base=$(git rev-parse --short "$base")
+
+ 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
+ die "And local branch '$1' is ahead of '$2'."
+ else
+ die "Branches need merging first."
+ fi
+ fi
+}
+
diff --git a/test-sh-setup b/test-sh-setup
index ad7126c..1c69056 100755
--- a/test-sh-setup
+++ b/test-sh-setup
@@ -6,4 +6,8 @@
gitflow_require_branch 'origin/develop'
gitflow_require_branch 'origin/master'
+gitflow_require_branches_equal 'master' 'origin/master'
+gitflow_require_branches_equal 'develop' 'origin/develop'
+gitflow_require_branches_equal 'rebase-not-merge' 'truemped/rebase-not-merge'
+
echo "All OK."