From 4f88708af94e82e7f8da427d7f3c6a876acbd71a Mon Sep 17 00:00:00 2001 From: Matt Hunter Date: Sun, 8 Mar 2026 18:52:47 -0400 Subject: Fix a bug which surfaced in commits_ahead/behind_remote If a remote branch is set, but doesn't exist (in the 'missingups' condition), these functions would output blank ("") causing an error when used in comparisons in color_local_commits(). It's arguable whether the POSIX reworks caused this bug or simply revealed it, since the previous code used bash's [[ ]] test syntax which seemed to tolerate this case. Restructure control flow so that any "error" in these functions yields a result of "0". The ahead/behind_master functions did not have this problem (the git-rev-list command failure is already handled), but rewrite them to match the style of the local functions. Additionally, the git-rev-list commands are updated to use the more straight-forward A..B syntax, dropping the use of --left-only or --right-only. This is more obvious code, and older git clients may return incorrect results using the previous syntax. Signed-off-by: Matt Hunter --- git-sonar | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/git-sonar b/git-sonar index 0cd53be..fa2237e 100755 --- a/git-sonar +++ b/git-sonar @@ -85,21 +85,17 @@ remote_branch_name() { } commits_behind_of_remote() { - remote_branch=${1:-"$(remote_branch_name)"} - if [ -n "$remote_branch" ]; then - git rev-list --left-only --count "${remote_branch}...HEAD" 2>/dev/null - else - printf '%s' "0" - fi + remote_branch="$1" + [ -n "$remote_branch" ] \ + && git rev-list --count "HEAD..${remote_branch}" 2>/dev/null \ + || printf '0\n' } commits_ahead_of_remote() { - remote_branch=${1:-"$(remote_branch_name)"} - if [ -n "$remote_branch" ]; then - git rev-list --right-only --count "${remote_branch}...HEAD" 2>/dev/null - else - printf '%s' "0" - fi + remote_branch="$1" + [ -n "$remote_branch" ] \ + && git rev-list --count "${remote_branch}..HEAD" 2>/dev/null \ + || printf '0\n' } determine_tracked_remote() { @@ -113,23 +109,21 @@ determine_tracked_remote() { } remote_behind_of_master() { - remote_branch=${1:-"$(remote_branch_name)"} - tracked_remote=$(determine_tracked_remote) - if [ -n "$remote_branch" ] && [ "$remote_branch" != "$tracked_remote" ]; then - git rev-list --left-only --count "${tracked_remote}...${remote_branch}" 2>/dev/null || printf '%s' "0" - else - printf '%s' "0" - fi + remote_branch="$1" + tracked_remote="$(determine_tracked_remote)" + [ -n "$remote_branch" ] \ + && [ "$remote_branch" != "$tracked_remote" ] \ + && git rev-list --count "${remote_branch}..${tracked_remote}" 2>/dev/null \ + || printf '0\n' } remote_ahead_of_master() { - remote_branch=${1:-"$(remote_branch_name)"} - tracked_remote=$(determine_tracked_remote) - if [ -n "$remote_branch" ] && [ "$remote_branch" != "$tracked_remote" ]; then - git rev-list --right-only --count "${tracked_remote}...${remote_branch}" 2>/dev/null || printf '%s' "0" - else - printf '%s' "0" - fi + remote_branch="$1" + tracked_remote="$(determine_tracked_remote)" + [ -n "$remote_branch" ] \ + && [ "$remote_branch" != "$tracked_remote" ] \ + && git rev-list --count "${tracked_remote}..${remote_branch}" 2>/dev/null \ + || printf '0\n' } # Diacritic marks for overlaying an arrow over A D C etc -- cgit v1.2.3