From 48f5934b702510f116d19956c14f85ab35ba85fa Mon Sep 17 00:00:00 2001 From: Michael Allen Date: Wed, 3 Jun 2015 15:52:13 +0100 Subject: fixed issue with branches that contain other branch names When a remote branch begins with or ends with the local branch name then the commits against diff functions can report the commits for the remote branch instead of your local branch, e.g.: I have local branch `foo` with 1 commit ahead of master and a remote branch `foobar` with 2 ahead of master. The prompt will report 2 commits instead of the true 1 commit because the `grep $branch_name` returns the `foobar` branch. Simple fix is to ensure we grep for the full `/$` so: branch_name = `foo` `origin/foo` matches `other/foo` matches `origin/foobar` doesn't match `origin/barfoo` doesn't match --- git-base.sh | 2 +- test-commits.sh | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/git-base.sh b/git-base.sh index 950422d..eb91fc8 100755 --- a/git-base.sh +++ b/git-base.sh @@ -122,7 +122,7 @@ readable_branch_name() { } remote_branch_name() { - local localRef="$(branch_name)" + local localRef="\/$(branch_name)$" if [[ -n "$localRef" ]]; then local remoteBranch="$(git for-each-ref --format='%(upstream:short)' refs/heads $localRef 2>/dev/null | grep $localRef)" if [[ -n $remoteBranch ]]; then diff --git a/test-commits.sh b/test-commits.sh index 340365d..01b07d3 100755 --- a/test-commits.sh +++ b/test-commits.sh @@ -189,6 +189,74 @@ test_remote_behind_master() { rm_tmp } +test_remote_branch_starts_with_local_branch_name() { + cd_to_tmp "remote" + git init --bare --quiet + remoteLocation="$(pwd)" + + cd_to_tmp "local" + 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 foobar --quiet + touch foobarfile + git add foobarfile + git commit -m "added foobar" --quiet + git push --quiet -u origin foobar >/dev/null + + git checkout -b foo --quiet + + assertEquals "0" "$(remote_ahead_of_master)" + assertEquals "0" "$(remote_behind_of_master)" + assertEquals "0" "$(commits_behind_of_remote)" + assertEquals "0" "$(commits_ahead_of_remote)" + + rm_tmp +} + +test_remote_branch_ends_with_local_branch_name() { + cd_to_tmp "remote" + git init --bare --quiet + remoteLocation="$(pwd)" + + cd_to_tmp "local" + 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 foobar --quiet + touch foobarfile + git add foobarfile + git commit -m "added foobar" --quiet + git push --quiet -u origin foobar >/dev/null + + git checkout -b bar --quiet + + assertEquals "0" "$(remote_ahead_of_master)" + assertEquals "0" "$(remote_behind_of_master)" + assertEquals "0" "$(commits_behind_of_remote)" + assertEquals "0" "$(commits_ahead_of_remote)" + + rm_tmp +} + test_dont_call_remote_branch_name() { cd_to_tmp "remote" git init --bare --quiet -- cgit v1.2.3