diff options
| author | Matt Hunter <m@lfurio.us> | 2026-03-08 18:52:47 -0400 |
|---|---|---|
| committer | Matt Hunter <m@lfurio.us> | 2026-03-30 21:56:09 -0400 |
| commit | 4f88708af94e82e7f8da427d7f3c6a876acbd71a (patch) | |
| tree | 362ad0338b3e544dfcf8c5af4c2dc35b9c695cc1 | |
| parent | 23b39889be1b58f17c945b54b6d180778a0f86ed (diff) | |
| download | git-sonar-4f88708af94e82e7f8da427d7f3c6a876acbd71a.tar.gz git-sonar-4f88708af94e82e7f8da427d7f3c6a876acbd71a.zip | |
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 <m@lfurio.us>
Diffstat (limited to '')
| -rwxr-xr-x | git-sonar | 46 |
1 files changed, 20 insertions, 26 deletions
@@ -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 |
