summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Allen <michael@michaelallen.io>2015-02-16 16:45:09 +0000
committerMichael Allen <michael@michaelallen.io>2015-02-16 16:45:09 +0000
commit17279c846cd8c12fa2e5db0b88914d58dbb47e35 (patch)
tree7f9be2df2a8e69ec6a0023d8c00000db51e3a7d0
parent43ac2a587f9b0b218875e3f1aa5a68ed4f1f8f19 (diff)
downloadgit-sonar-17279c846cd8c12fa2e5db0b88914d58dbb47e35.tar.gz
git-sonar-17279c846cd8c12fa2e5db0b88914d58dbb47e35.zip
functions to get the commit difference between the branch on the remote and the master
Diffstat (limited to '')
-rwxr-xr-xgit-base.sh26
-rwxr-xr-xtest-commits.sh74
2 files changed, 100 insertions, 0 deletions
diff --git a/git-base.sh b/git-base.sh
index 9ffda78..951ec58 100755
--- a/git-base.sh
+++ b/git-base.sh
@@ -150,3 +150,29 @@ commits_ahead_of_remote() {
echo "0"
fi
}
+
+remote_behind_of_master() {
+ if is_tracking_remote; then
+ set --
+ set -- $(git rev-list --left-right --count origin/master...$(remote_branch_name))
+ behind=$1
+ ahead=$2
+ set --
+ echo $behind
+ else
+ echo "0"
+ fi
+}
+
+remote_ahead_of_master() {
+ if is_tracking_remote; then
+ set --
+ set -- $(git rev-list --left-right --count origin/master...$(remote_branch_name))
+ behind=$1
+ ahead=$2
+ set --
+ echo $ahead
+ else
+ echo "0"
+ fi
+}
diff --git a/test-commits.sh b/test-commits.sh
index 003ebbf..28885e3 100755
--- a/test-commits.sh
+++ b/test-commits.sh
@@ -115,4 +115,78 @@ test_commits_ahead_with_remote() {
rm_tmp
}
+test_remote_ahead_master() {
+ cd_to_tmp "remote"
+ git init --quiet
+ touch README
+ git add .
+ git commit -m "initial commit" --quiet
+ remoteLocation="$(pwd)"
+
+ cd_to_tmp "new"
+ git init --quiet
+ git remote add origin $remoteLocation
+ git fetch origin --quiet
+ git checkout master --quiet
+
+ git checkout -b foo --quiet
+ git push --quiet -u origin foo >/dev/null
+
+ echo "foo" > foo
+ git add .
+ git commit -m "test commit" --quiet
+ assertEquals "0" "$(remote_ahead_of_master)"
+ git push --quiet
+ assertEquals "1" "$(remote_ahead_of_master)"
+
+ echo "bar" > bar
+ git add .
+ git commit -m "test commit" --quiet
+ assertEquals "1" "$(remote_ahead_of_master)"
+ git push --quiet
+ assertEquals "2" "$(remote_ahead_of_master)"
+
+ rm_tmp
+}
+
+test_remote_behind_master() {
+ cd_to_tmp "remote"
+ git init --bare --quiet
+ remoteLocation="$(pwd)"
+
+ cd_to_tmp "new"
+ git init --quiet
+ git remote add origin $remoteLocation
+ git fetch origin --quiet
+ git checkout -b master --quiet
+ touch README
+ git add README
+ git commit -m "initial commit" --quiet
+
+ git push --quiet -u origin master >/dev/null
+ git reset --quiet --hard HEAD
+
+ git checkout -b foo --quiet
+ git push --quiet -u origin foo >/dev/null
+
+ assertEquals "0" "$(remote_behind_of_master)"
+ git checkout master --quiet
+ echo "foo" > foo
+ git add .
+ git commit -m "test commit" --quiet
+ git push --quiet >/dev/null
+ git checkout foo --quiet
+ assertEquals "1" "$(remote_behind_of_master)"
+
+ git checkout master --quiet
+ echo "bar" > bar
+ git add .
+ git commit -m "test commit" --quiet
+ git push --quiet >/dev/null
+ git checkout foo --quiet
+ assertEquals "2" "$(remote_behind_of_master)"
+
+ rm_tmp
+}
+
. ./shunit/shunit2