46e63340eef243f25890e2178931919125c58aae

Author
whoami <atommixz@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(branch): show mercurial bookmarks if used (#9948)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

  1diff --git a/plugins/branch/README.md b/plugins/branch/README.md
  2index 56ab8da4b4c8e1a1989c6d03c28138260e3c73f4..a15dd22df2aeb1f0f756713791003165b29c45c4 100644
  3--- a/plugins/branch/README.md
  4+++ b/plugins/branch/README.md
  5@@ -1,31 +1,47 @@
  6-# Branch
  7+# Branch plugin
  8 
  9-Displays the current Git or Mercurial branch fast.
 10+This plugin displays the current Git or Mercurial branch, fast. If in a Mercurial repository,
 11+also display the current bookmark, if present.
 12+
 13+To use it, add `branch` to the plugins array in your zshrc file:
 14+
 15+```zsh
 16+plugins=(... branch)
 17+```
 18 
 19 ## Speed test
 20 
 21-### Mercurial
 22+- `hg branch`:
 23 
 24-```shell
 25-$ time hg branch
 26-0.11s user 0.14s system 70% cpu 0.355 total
 27-```
 28+  ```console
 29+  $ time hg branch
 30+  0.11s user 0.14s system 70% cpu 0.355 total
 31+  ```
 32 
 33-### Branch plugin
 34+- branch plugin:
 35 
 36-```shell
 37-$ time zsh /tmp/branch_prompt_info_test.zsh
 38-0.00s user 0.01s system 78% cpu 0.014 total
 39-```
 40+  ```console
 41+  $ time zsh /tmp/branch_prompt_info_test.zsh
 42+  0.00s user 0.01s system 78% cpu 0.014 total
 43+  ```
 44 
 45 ## Usage
 46 
 47-Edit your theme file (eg.: `~/.oh-my-zsh/theme/robbyrussell.zsh-theme`)
 48-adding `$(branch_prompt_info)` in your prompt like this:
 49+Copy your theme to `$ZSH_CUSTOM/themes/` and modify it to add `$(branch_prompt_info)` in your prompt.
 50+This example is for the `robbyrussell` theme:
 51 
 52 ```diff
 53-- PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'
 54-+ PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)$(branch_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}'
 55+diff --git a/themes/robbyrussell.zsh-theme b/themes/robbyrussell.zsh-theme
 56+index 2fd5f2cd..9d89a464 100644
 57+--- a/themes/robbyrussell.zsh-theme
 58++++ b/themes/robbyrussell.zsh-theme
 59+@@ -1,5 +1,5 @@
 60+ PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
 61+-PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
 62++PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(branch_prompt_info)'
 63+ 
 64+ ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
 65+ ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
 66 ```
 67 
 68 ## Maintainer
 69diff --git a/plugins/branch/branch.plugin.zsh b/plugins/branch/branch.plugin.zsh
 70index 2e5659bdf5b5e7da839c502503b20a7786d26f88..dd5871fdc3b3ff7c7d037bdb67c2e79fecdec9e6 100644
 71--- a/plugins/branch/branch.plugin.zsh
 72+++ b/plugins/branch/branch.plugin.zsh
 73@@ -3,29 +3,33 @@
 74 # Oct 2, 2015
 75 
 76 function branch_prompt_info() {
 77-  # Defines path as current directory
 78-  local current_dir=$PWD
 79-  # While current path is not root path
 80-  while [[ $current_dir != '/' ]]
 81-  do
 82-    # Git repository
 83-    if [[ -d "${current_dir}/.git" ]]
 84-    then
 85-      echo '±' ${"$(<"$current_dir/.git/HEAD")"##*/}
 86-      return;
 87+  # Start checking in current working directory
 88+  local branch="" dir="$PWD"
 89+  while [[ "$dir" != '/' ]]; do
 90+    # Found .git directory
 91+    if [[ -d "${dir}/.git" ]]; then
 92+      branch="${"$(<"${dir}/.git/HEAD")"##*/}"
 93+      echo '±' "${branch:gs/%/%%}"
 94+      return
 95     fi
 96-    # Mercurial repository
 97-    if [[ -d "${current_dir}/.hg" ]]
 98-    then
 99-      if [[ -f "$current_dir/.hg/branch" ]]
100-      then
101-        echo '☿' $(<"$current_dir/.hg/branch")
102+
103+    # Found .hg directory
104+    if [[ -d "${dir}/.hg" ]]; then
105+      if [[ -f "${dir}/.hg/branch" ]]; then
106+        branch="$(<"${dir}/.hg/branch")"
107       else
108-        echo '☿ default'
109+        branch="default"
110+      fi
111+
112+      if [[ -f "${dir}/.hg/bookmarks.current" ]]; then
113+        branch="${branch}/$(<"${dir}/.hg/bookmarks.current")"
114       fi
115-      return;
116+
117+      echo '☿' "${branch:gs/%/%%}"
118+      return
119     fi
120-    # Defines path as parent directory and keeps looking for :)
121-    current_dir="${current_dir:h}"
122+
123+    # Check parent directory
124+    dir="${dir:h}"
125   done
126 }