Jacopo De Simoi <wilderjds@users.noreply.github.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date
Message
Fix git_commits_{ahead,before} when no upstream branch is defined (#6658)
If @{u} is not defined, git rev-list will give an error; redirect
to stderr the error and deal with this case in what follows.
Diff
1diff --git a/lib/git.zsh b/lib/git.zsh
2index 9b0f6e36feecd17883dbc95552e605b4f2ea8a62..b55b762d7f88c80301fc72b3c460b78669f83049 100644
3--- a/lib/git.zsh
4+++ b/lib/git.zsh
5@@ -77,8 +77,8 @@ function git_current_branch() {
6 # Gets the number of commits ahead from remote
7 function git_commits_ahead() {
8 if command git rev-parse --git-dir &>/dev/null; then
9- local commits="$(git rev-list --count @{upstream}..HEAD)"
10- if [[ "$commits" != 0 ]]; then
11+ local commits="$(git rev-list --count @{upstream}..HEAD 2>/dev/null)"
12+ if [[ -n "$commits" && "$commits" != 0 ]]; then
13 echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$commits$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
14 fi
15 fi
16@@ -87,8 +87,8 @@ function git_commits_ahead() {
17 # Gets the number of commits behind remote
18 function git_commits_behind() {
19 if command git rev-parse --git-dir &>/dev/null; then
20- local commits="$(git rev-list --count HEAD..@{upstream})"
21- if [[ "$commits" != 0 ]]; then
22+ local commits="$(git rev-list --count HEAD..@{upstream} 2>/dev/null)"
23+ if [[ -n "$commits" && "$commits" != 0 ]]; then
24 echo "$ZSH_THEME_GIT_COMMITS_BEHIND_PREFIX$commits$ZSH_THEME_GIT_COMMITS_BEHIND_SUFFIX"
25 fi
26 fi