e344f4c0662bde84e720e723ab4a445df03e5100

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Merge pull request #4420 from apjanke/git-move-current_branch-to-core

Git: move current_branch() to core

Diff

This diff is truncated to protect this page.

  1diff --git a/lib/git.zsh b/lib/git.zsh
  2index 1e203c7c61e758ecbb89be2fd3684421791018aa..51e323a757e8b836cf9dee27a17d384563c39c02 100644
  3--- a/lib/git.zsh
  4+++ b/lib/git.zsh
  5@@ -1,5 +1,6 @@
  6-# get the name of the branch we are on
  7+# Outputs current branch info in prompt format
  8 function git_prompt_info() {
  9+  local ref
 10   if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]; then
 11     ref=$(command git symbolic-ref HEAD 2> /dev/null) || \
 12     ref=$(command git rev-parse --short HEAD 2> /dev/null) || return 0
 13@@ -7,9 +8,8 @@ function git_prompt_info() {
 14   fi
 15 }
 16 
 17-
 18 # Checks if working tree is dirty
 19-parse_git_dirty() {
 20+function parse_git_dirty() {
 21   local STATUS=''
 22   local FLAGS
 23   FLAGS=('--porcelain')
 24@@ -29,32 +29,26 @@ parse_git_dirty() {
 25   fi
 26 }
 27 
 28-# get the difference between the local and remote branches
 29-git_remote_status() {
 30+# Gets the difference between the local and remote branches
 31+function git_remote_status() {
 32+    local remote ahead behind git_remote_status git_remote_status_detailed
 33     remote=${$(command git rev-parse --verify ${hook_com[branch]}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
 34-    if [[ -n ${remote} ]] ; then
 35+    if [[ -n ${remote} ]]; then
 36         ahead=$(command git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
 37         behind=$(command git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
 38 
 39-        if [ $ahead -eq 0 ] && [ $behind -eq 0 ]
 40-        then
 41-          	git_remote_status="$ZSH_THEME_GIT_PROMPT_EQUAL_REMOTE"
 42-        elif [ $ahead -gt 0 ] && [ $behind -eq 0 ]
 43-        then
 44+        if [[ $ahead -gt 0 ]] && [[ $behind -eq 0 ]]; then
 45             git_remote_status="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE"
 46             git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}"
 47-        elif [ $behind -gt 0 ] && [ $ahead -eq 0 ] 
 48-        then
 49+        elif [[ $behind -gt 0 ]] && [[ $ahead -eq 0 ]]; then
 50             git_remote_status="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE"
 51             git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
 52-        elif [ $ahead -gt 0 ] && [ $behind -gt 0 ]
 53-        then
 54+        elif [[ $ahead -gt 0 ]] && [[ $behind -gt 0 ]]; then
 55             git_remote_status="$ZSH_THEME_GIT_PROMPT_DIVERGED_REMOTE"
 56             git_remote_status_detailed="$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_AHEAD_REMOTE$((ahead))%{$reset_color%}$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE_COLOR$ZSH_THEME_GIT_PROMPT_BEHIND_REMOTE$((behind))%{$reset_color%}"
 57         fi
 58 
 59-        if [ $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]
 60-        then
 61+        if [[ -n $ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_DETAILED ]]; then
 62             git_remote_status="$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_PREFIX$remote$git_remote_status_detailed$ZSH_THEME_GIT_PROMPT_REMOTE_STATUS_SUFFIX"
 63         fi
 64 
 65@@ -62,31 +56,47 @@ git_remote_status() {
 66     fi
 67 }
 68 
 69+# Outputs the name of the current branch
 70+# Usage example: git pull origin $(git_current_branch)
 71+# Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
 72+# it's not a symbolic ref, but in a Git repo.
 73+function git_current_branch() {
 74+  local ref
 75+  ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
 76+  local ret=$?
 77+  if [[ $ret != 0 ]]; then
 78+    [[ $ret == 128 ]] && return  # no git repo.
 79+    ref=$(command git rev-parse --short HEAD 2> /dev/null) || return
 80+  fi
 81+  echo ${ref#refs/heads/}
 82+}
 83+
 84+
 85 # Gets the number of commits ahead from remote
 86 function git_commits_ahead() {
 87   if $(echo "$(command git log @{upstream}..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
 88-    COMMITS=$(command git log @{upstream}..HEAD | grep '^commit' | wc -l | tr -d ' ')
 89+    local COMMITS=$(command git log @{upstream}..HEAD | grep '^commit' | wc -l | tr -d ' ')
 90     echo "$ZSH_THEME_GIT_COMMITS_AHEAD_PREFIX$COMMITS$ZSH_THEME_GIT_COMMITS_AHEAD_SUFFIX"
 91   fi
 92 }
 93 
 94 # Outputs if current branch is ahead of remote
 95 function git_prompt_ahead() {
 96-  if [[ -n "$(command git rev-list origin/$(current_branch)..HEAD 2> /dev/null)" ]]; then
 97+  if [[ -n "$(command git rev-list origin/$(git_current_branch)..HEAD 2> /dev/null)" ]]; then
 98     echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
 99   fi
100 }
101 
102 # Outputs if current branch is behind remote
103 function git_prompt_behind() {
104-  if [[ -n "$(command git rev-list HEAD..origin/$(current_branch) 2> /dev/null)" ]]; then
105diff --git a/plugins/git/git.plugin.zsh b/plugins/git/git.plugin.zsh
106index d78b82df30ec7be41ee8e42f23a6f1e17338831a..b851fb97da21d524c8f13543b773ab704ff02e2c 100644
107--- a/plugins/git/git.plugin.zsh
108+++ b/plugins/git/git.plugin.zsh
109@@ -6,19 +6,12 @@ zstyle -s ":vcs_info:git:*:-all-" "command" _omz_git_git_cmd
110 # Functions
111 #
112 
113-# The current branch name
114-# Usage example: git pull origin $(current_branch)
115-# Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
116-# it's not a symbolic ref, but in a Git repo.
117+# The name of the current branch
118+# Back-compatibility wrapper for when this function was defined here in
119+# the plugin, before being pulled in to core lib/git.zsh as git_current_branch()
120+# to fix the core -> git plugin dependency.
121 function current_branch() {
122-  local ref
123-  ref=$($_omz_git_git_cmd symbolic-ref --quiet HEAD 2> /dev/null)
124-  local ret=$?
125-  if [[ $ret != 0 ]]; then
126-    [[ $ret == 128 ]] && return  # no git repo.
127-    ref=$($_omz_git_git_cmd rev-parse --short HEAD 2> /dev/null) || return
128-  fi
129-  echo ${ref#refs/heads/}
130+  git_current_branch
131 }
132 # The list of remotes
133 function current_repository() {
134@@ -99,7 +92,7 @@ alias gfo='git fetch origin'
135 alias gg='git gui citool'
136 alias gga='git gui citool --amend'
137 ggf() {
138-[[ "$#" != 1 ]] && local b="$(current_branch)"
139+[[ "$#" != 1 ]] && local b="$(git_current_branch)"
140 git push --force origin "${b:=$1}"
141 }
142 compdef _git ggf=git-checkout
143@@ -107,23 +100,23 @@ ggl() {
144 if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
145 git pull origin "${*}"
146 else
147-[[ "$#" == 0 ]] && local b="$(current_branch)"
148+[[ "$#" == 0 ]] && local b="$(git_current_branch)"
149 git pull origin "${b:=$1}"
150 fi
151 }
152 compdef _git ggl=git-checkout
153-alias ggpull='git pull origin $(current_branch)'
154+alias ggpull='git pull origin $(git_current_branch)'
155 compdef _git ggpull=git-checkout
156 ggp() {
157 if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
158 git push origin "${*}"
159 else
160-[[ "$#" == 0 ]] && local b="$(current_branch)"
161+[[ "$#" == 0 ]] && local b="$(git_current_branch)"
162 git push origin "${b:=$1}"
163 fi
164 }
165 compdef _git ggp=git-checkout
166-alias ggpush='git push origin $(current_branch)'
167+alias ggpush='git push origin $(git_current_branch)'
168 compdef _git ggpush=git-checkout
169 ggpnp() {
170 if [[ "$#" == 0 ]]; then
171@@ -133,9 +126,9 @@ ggl "${*}" && ggp "${*}"
172 fi
173 }
174 compdef _git ggpnp=git-checkout
175-alias ggsup='git branch --set-upstream-to=origin/$(current_branch)'
176+alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
177 ggu() {
178-[[ "$#" != 1 ]] && local b="$(current_branch)"
179+[[ "$#" != 1 ]] && local b="$(git_current_branch)"
180 git pull --rebase origin "${b:=$1}"
181 }
182 compdef _git ggu=git-checkout
183diff --git a/themes/eastwood.zsh-theme b/themes/eastwood.zsh-theme
184index 1b284cdc27e8aa9aedcab9bb78e460d38c015d39..88134f8e63d21ec34cdc52a4f6103e592ea237af 100644
185--- a/themes/eastwood.zsh-theme
186+++ b/themes/eastwood.zsh-theme
187@@ -14,9 +14,9 @@ ZSH_THEME_GIT_PROMPT_CLEAN=""
188 
189 # Customized git status, oh-my-zsh currently does not allow render dirty status before branch
190 git_custom_status() {
191-  local cb=$(current_branch)
192+  local cb=$(git_current_branch)
193   if [ -n "$cb" ]; then
194-    echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
195+    echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX$(git_current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
196   fi
197 }
198 
199diff --git a/themes/gallois.zsh-theme b/themes/gallois.zsh-theme
200index d383ed5830d168409c82c67b8b17c8be7fe1f538..515325e3820bcec513b53cea382dbe99c138cd68 100644
201--- a/themes/gallois.zsh-theme
202+++ b/themes/gallois.zsh-theme
203@@ -1,3 +1,5 @@
204+# Depends on the git plugin for work_in_progress()
205+
206 ZSH_THEME_GIT_PROMPT_PREFIX="%{$reset_color%}%{$fg[green]%}["
207 ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%}"
208 ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}*%{$reset_color%}"
209@@ -5,9 +7,9 @@ ZSH_THEME_GIT_PROMPT_CLEAN=""
210 
211 #Customized git status, oh-my-zsh currently does not allow render dirty status before branch
212 git_custom_status() {
213-  local cb=$(current_branch)
214+  local cb=$(git_current_branch)
215   if [ -n "$cb" ]; then
216-    echo "$(parse_git_dirty)%{$fg_bold[yellow]%}$(work_in_progress)%{$reset_color%}$ZSH_THEME_GIT_PROMPT_PREFIX$(current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
217+    echo "$(parse_git_dirty)%{$fg_bold[yellow]%}$(work_in_progress)%{$reset_color%}$ZSH_THEME_GIT_PROMPT_PREFIX$(git_current_branch)$ZSH_THEME_GIT_PROMPT_SUFFIX"
218   fi
219 }
220 
221diff --git a/themes/josh.zsh-theme b/themes/josh.zsh-theme
222index 12dfe4069018153d4ceb6c87b3dc8ca79de5de3c..c8f0f4dd78b5d158a10907c68514ebcfbf9452c9 100644
223--- a/themes/josh.zsh-theme
224+++ b/themes/josh.zsh-theme
225@@ -9,7 +9,7 @@ function josh_prompt {
226   (( spare_width = ${COLUMNS} ))
227   prompt=" "
228 
229-  branch=$(current_branch)
230+  branch=$(git_current_branch)
231   ruby_version=$(rvm_prompt_info || rbenv_prompt_info)
232   path_size=${#PWD}
233   branch_size=${#branch}
234@@ -31,7 +31,7 @@ function josh_prompt {
235     prompt=" $prompt"
236   done
237   
238-  prompt="%{%F{green}%}$PWD$prompt%{%F{red}%}$(rvm_prompt_info || rbenv_prompt_info)%{$reset_color%} $(current_branch)"
239+  prompt="%{%F{green}%}$PWD$prompt%{%F{red}%}$(rvm_prompt_info || rbenv_prompt_info)%{$reset_color%} $(git_current_branch)"
240   
241   echo $prompt
242 }
243diff --git a/themes/juanghurtado.zsh-theme b/themes/juanghurtado.zsh-theme
244index bc470e8382b9c1a14f799d78f747378648fedba1..dbdfde7b889fb605e972dc0b147b5e5157416082 100644
245--- a/themes/juanghurtado.zsh-theme
246+++ b/themes/juanghurtado.zsh-theme
247@@ -1,5 +1,3 @@
248-# Needs Git plugin for current_branch method
249-
250 # Color shortcuts
251 RED=$fg[red]
252 YELLOW=$fg[yellow]
253@@ -40,4 +38,4 @@ ZSH_THEME_GIT_PROMPT_SHA_AFTER="%{$WHITE%}]"
254 PROMPT='
255 %{$GREEN_BOLD%}%n@%m%{$WHITE%}:%{$YELLOW%}%~%u$(parse_git_dirty)$(git_prompt_ahead)%{$RESET_COLOR%}
256 %{$BLUE%}>%{$RESET_COLOR%} '
257-RPROMPT='%{$GREEN_BOLD%}$(current_branch)$(git_prompt_short_sha)$(git_prompt_status)%{$RESET_COLOR%}'
258+RPROMPT='%{$GREEN_BOLD%}$(git_current_branch)$(git_prompt_short_sha)$(git_prompt_status)%{$RESET_COLOR%}'
259diff --git a/themes/mortalscumbag.zsh-theme b/themes/mortalscumbag.zsh-theme
260index ccce4197ac29e5c1ac85d5dc8b4280fa567e973b..55ece97609c05305695f9dacabf38a77dd0ad63f 100644
261--- a/themes/mortalscumbag.zsh-theme
262+++ b/themes/mortalscumbag.zsh-theme
263@@ -5,7 +5,7 @@ function my_git_prompt() {
264   STATUS=""
265 
266   # is branch ahead?
267-  if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
268+  if $(echo "$(git log origin/$(git_current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
269     STATUS="$STATUS$ZSH_THEME_GIT_PROMPT_AHEAD"
270   fi
271 
272@@ -37,7 +37,7 @@ function my_git_prompt() {
273 }
274 
275 function my_current_branch() {
276-  echo $(current_branch || echo "(no branch)")
277+  echo $(git_current_branch || echo "(no branch)")
278 }
279 
280 function ssh_connection() {
281diff --git a/themes/peepcode.zsh-theme b/themes/peepcode.zsh-theme
282index ca2a8862fcc213d4fe708aa7d4417f726f6b174d..96e4f11923731c01a65dcb143d7e2320858d7078 100644
283--- a/themes/peepcode.zsh-theme
284+++ b/themes/peepcode.zsh-theme
285@@ -28,7 +28,7 @@ git_dirty() {
286 }
287 
288 git_prompt() {
289-  local cb=$(current_branch)
290+  local cb=$(git_current_branch)
291   if [ -n "$cb" ]; then
292     local repo_path=$(git_repo_path)
293     echo " %{$fg_bold[grey]%}$cb %{$fg[white]%}$(git_commit_id)%{$reset_color%}$(git_mode)$(git_dirty)"
294diff --git a/themes/sunrise.zsh-theme b/themes/sunrise.zsh-theme
295index 28befd01b5059b7973a16225fe00223c9e3914c0..57d2649c6723478ba2d8ae9100f3ec3695cc4fb7 100644
296--- a/themes/sunrise.zsh-theme
297+++ b/themes/sunrise.zsh-theme
298@@ -1,6 +1,5 @@
299 # Sunrise theme for oh-my-zsh
300 # Intended to be used with Solarized: http://ethanschoonover.com/solarized
301-# (Needs Git plugin for current_branch method)
302 
303 # Color shortcuts
304 R=$fg_no_bold[red]