From 43ac2a587f9b0b218875e3f1aa5a68ed4f1f8f19 Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Thu, 12 Feb 2015 16:53:08 +0000 Subject: find branch names and commits ahead or behind --- git-base.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'git-base.sh') diff --git a/git-base.sh b/git-base.sh index 39e7367..9ffda78 100755 --- a/git-base.sh +++ b/git-base.sh @@ -1,5 +1,6 @@ dot_git="" cwd="" +remote="" in_current_dir() { local wd="$(pwd)" @@ -96,3 +97,56 @@ fetch() { git fetch debug_print $debug "Finished fetch" } + +branch_name() { + if is_repo; then + local localBranch="$(git symbolic-ref --short HEAD)" + echo $localBranch + fi +} + +is_tracking_remote() { + if [[ -n "$(remote_branch_name)" ]]; then + return 0 + else + return 1 + fi +} + +remote_branch_name() { + if is_repo; then + local remoteBranch="$(git for-each-ref --format='%(upstream:short)' | grep "$(branch_name)")" + if [[ -n $remoteBranch ]]; then + echo $remoteBranch + return 0 + else + return 1 + fi + fi +} + +commits_behind_of_remote() { + if is_tracking_remote; then + set -- + set -- $(git rev-list --left-right --count $(remote_branch_name)...HEAD) + behind=$1 + ahead=$2 + set -- + echo $behind + else + echo "0" + fi +} + +commits_ahead_of_remote() { + if is_tracking_remote; then + set -- + set -- $(git rev-list --left-right --count $(remote_branch_name)...HEAD) + behind=$1 + ahead=$2 + set -- + echo $ahead + else + echo "0" + fi +} -- cgit v1.2.3