Skip to content

Commit

Permalink
Add _lp_git_commits_off_remote() function
Browse files Browse the repository at this point in the history
I got this down from 4 git calls to 1. The trick is both the
"@{upstream}" directive, the "--left-right" flag, and the
"<commit>...<commit>" notation.
If there is no upstream tracking branch, return 2.
  • Loading branch information
Rycieos committed Nov 25, 2020
1 parent fe9919f commit 309b443
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions liquidprompt
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,20 @@ _lp_git_stash_count() {
(( lp_vcs_stash_count ))
}

# Count commits behind and ahead on the remote tracking branch of the current
# local branch.
# return: false if no commits ahead or behind (1) or if there is no tracking
# branch (2).
# lp_vcs_commit_behind: commits behind the remote branch.
# lp_vcs_commit_ahead: commits ahead of the remote branch.
_lp_git_commits_off_remote() {
local counts
# The "@{upstream}" notation was added in Git 1.7.0, so this should work for everyone
counts="$(\git rev-list --count --left-right '@{upstream}...HEAD' 2>/dev/null)" || return 2
IFS=$' \t' read lp_vcs_commit_behind lp_vcs_commit_ahead <<<"$counts"
(( lp_vcs_commit_behind || lp_vcs_commit_ahead ))
}

_lp_git_untracked_files() {
lp_vcs_untracked_files="$(LC_ALL=C \git status --porcelain 2>/dev/null | \grep -c '^??')"
(( lp_vcs_untracked_files ))
Expand Down Expand Up @@ -1031,26 +1045,14 @@ _lp_git_branch_color() {
end="$LP_COLOR_COMMITS$LP_MARK_STASH$end"
fi

local remote
remote="$(\git config --get branch.${branch}.remote 2>/dev/null)"

local has_commit=""
local commit_ahead
local commit_behind
if [[ -n "$remote" ]]; then
local remote_branch
remote_branch="$(\git config --get branch.${branch}.merge)"
if [[ -n "$remote_branch" ]]; then
remote_branch=${remote_branch/refs\/heads/refs\/remotes\/$remote}
commit_ahead="$(\git rev-list --count $remote_branch..HEAD 2>/dev/null)"
commit_behind="$(\git rev-list --count HEAD..$remote_branch 2>/dev/null)"
if [[ "$commit_ahead" -ne "0" && "$commit_behind" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS}+$commit_ahead${NO_COL}/${LP_COLOR_COMMITS_BEHIND}-$commit_behind${NO_COL}"
elif [[ "$commit_ahead" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS}$commit_ahead${NO_COL}"
elif [[ "$commit_behind" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS_BEHIND}-$commit_behind${NO_COL}"
fi
if _lp_git_commits_off_remote; then
if [[ "$lp_vcs_commit_ahead" -ne "0" && "$lp_vcs_commit_behind" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS}+$lp_vcs_commit_ahead${NO_COL}/${LP_COLOR_COMMITS_BEHIND}-$lp_vcs_commit_behind${NO_COL}"
elif [[ "$lp_vcs_commit_ahead" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS}$lp_vcs_commit_ahead${NO_COL}"
elif [[ "$lp_vcs_commit_behind" -ne "0" ]]; then
has_commit="${LP_COLOR_COMMITS_BEHIND}-$lp_vcs_commit_behind${NO_COL}"
fi
fi

Expand Down

0 comments on commit 309b443

Please sign in to comment.