diff options
author | Michael Allen <michael@michaelallen.io> | 2015-05-07 13:39:04 +0100 |
---|---|---|
committer | Michael Allen <michael@michaelallen.io> | 2015-05-07 13:39:04 +0100 |
commit | 68fa32350114852188161c7ab96e5e493aaa6e12 (patch) | |
tree | ebed381af0fef12e9b0f67c7a6a798e1ddf0b622 | |
parent | 9bb6cd2e1057e820aba445ec2206df8de69bc961 (diff) | |
download | git-sonar-68fa32350114852188161c7ab96e5e493aaa6e12.tar.gz git-sonar-68fa32350114852188161c7ab96e5e493aaa6e12.zip |
cleanly support repos with no master to track
-rwxr-xr-x | git-base.sh | 16 | ||||
-rwxr-xr-x | test-commits.sh | 41 |
2 files changed, 53 insertions, 4 deletions
diff --git a/git-base.sh b/git-base.sh index f854ae3..950422d 100755 --- a/git-base.sh +++ b/git-base.sh @@ -167,11 +167,15 @@ remote_behind_of_master() { tracked_remote="origin/master" if [[ -n "$remote_branch" && "$remote_branch" != "$tracked_remote" ]]; then set -- - set -- $(git rev-list --left-right --count origin/master...${remote_branch}) + set -- $(git rev-list --left-right --count ${tracked_remote}...${remote_branch} 2>/dev/null) behind=$1 ahead=$2 set -- - echo $behind + if [[ -n "$behind" ]]; then + echo $behind + else + echo "0" + fi else echo "0" fi @@ -182,11 +186,15 @@ remote_ahead_of_master() { tracked_remote="origin/master" if [[ -n "$remote_branch" && "$remote_branch" != "$tracked_remote" ]]; then set -- - set -- $(git rev-list --left-right --count origin/master...${remote_branch}) + set -- $(git rev-list --left-right --count ${tracked_remote}...${remote_branch} 2>/dev/null) behind=$1 ahead=$2 set -- - echo $ahead + if [[ -n "$ahead" ]]; then + echo $ahead + else + echo "0" + fi else echo "0" fi diff --git a/test-commits.sh b/test-commits.sh index e1800aa..340365d 100755 --- a/test-commits.sh +++ b/test-commits.sh @@ -259,4 +259,45 @@ test_dont_remote_if_remote_is_master() { rm_tmp } +test_quiet_if_no_remote_master() { + cd_to_tmp "remote" + git init --quiet + touch README + git add . + git checkout -b foo --quiet + 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 foo --quiet + repoLocation="$(pwd)" + + remote_branch="$(remote_branch_name)" + + debug_output="$( + { + output="$( + remote_behind_of_master "$remote_branch"; + )" + } 2>&1 + echo "$output" + )" + + assertEquals "0" "$debug_output" + debug_output="$( + { + output="$( + remote_ahead_of_master "$remote_branch"; + )" + } 2>&1 + echo "$output" + )" + + assertEquals "0" "$debug_output" + + rm_tmp +} . ./shunit/shunit2 |