From cad0d3c1b78c93b8f0846b6d32f6d02f3bfd89cb Mon Sep 17 00:00:00 2001 From: Alexander Lutay Date: Wed, 21 Oct 2015 16:46:42 +0200 Subject: [PATCH] MT#15803 Introducing the new function to provide current branch status Previously we guessed the status of current branch by parsing 'git status' output. Unfortunately we have to care about a corner cases in this way. In the same time we can easily consider current branch status comparing commits. Introducing the new function to provide current branch status: up-2-date, ahead, behind or diverged with origin. Change-Id: I4d09c8867069ccafc97e5d31638e8bc94caa58cd --- functions/main | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/functions/main b/functions/main index b5623858..81c33764 100644 --- a/functions/main +++ b/functions/main @@ -47,6 +47,34 @@ log_debug() { console_output "DEBUG: $*\n" fi } + +get_branch_status() { + log_debug "cd $NGCPCTL_MAIN" + cd "$NGCPCTL_MAIN" + + log_debug "git rev-parse HEAD" + local LOCAL=$(git rev-parse HEAD) + + log_debug "git rev-parse @{u}" + local REMOTE=$(git rev-parse @{u}) + + log_debug "git merge-base HEAD @{u}" + local BASE=$(git merge-base HEAD @{u}) + + if [ "$LOCAL" = "$REMOTE" ]; then + # Up-to-date + return 0 + elif [ "$LOCAL" = "$BASE" ]; then + # Need to pull + return 1 + elif [ "$REMOTE" = "$BASE" ]; then + # Need to push + return 2 + else + # Diverged + return 3 + fi +} ## }}} ## important variables we depend on to operate successfully {{{