سؤال

أقوم بتشغيل الرموز التالية بشكل منفصل كوسيطي دون جدوى في .zshrc. هذا يقترحني أنه ليس لدي برنامج يسمى __git_ps1. ليس في macports.

#1

PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$

#2

PROMPT="$(__git_ps1 " (%s)")\$"$

#3

# Get the name of the branch we are on
git_prompt_info() {
  branch_prompt=$(__git_ps1)
  if [ -n "$branch_prompt" ]; then
    status_icon=$(git_status)
    echo $branch_prompt $status_icon
  fi
}

# Show character if changes are pending
git_status() {
  if current_git_status=$(git status | grep 'added to commit' 2> /dev/null); then
    echo "☠"
  fi
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='
%~%{$fg_bold[black]%}$(git_prompt_info)
→ %{$reset_color%}'

كيف يمكنك الحصول على مطالبة التي تظهر اسم فرع GIT؟

هل كانت مفيدة؟

المحلول

__git_ps1 هو من git-completion.bash. في ZSH، ربما عليك تقديم وظيفتك الخاصة لتحديد الدلائل الحالية فرع GIT. هناك عدد لا بأس به المشاركات بلوق عن جيت موجه ل ZSH.

تحتاج فقط:

  • وظيفة لتوفير اسم الفرع
  • تمكين استبدال المطالبة (الأمر)
  • أضف الوظيفة إلى موجه

علي سبيل المثال

git_prompt() {
 ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
 echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit

تحديث: استخدم الوحدة النمطية ZSH VCS_INFO بدلا من git_prompt ()

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' actionformats \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats       \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'

zstyle ':vcs_info:*' enable git cvs svn

# or use pre_cmd, see man zshcontrib
vcs_info_wrapper() {
  vcs_info
  if [ -n "$vcs_info_msg_0_" ]; then
    echo "%{$fg[grey]%}${vcs_info_msg_0_}%{$reset_color%}$del"
  fi
}
RPROMPT=$'$(vcs_info_wrapper)'

نصائح أخرى

فيما يلي موجه GIT ممتد ل ZSH: زاش جيت.

alt text

إجابة KO-DOS رائعة، لكنني أفضل مطالبة بدوامة أكثر قليلا من الشخص الذي يستخدمه. على وجه التحديد، أحب إشعارات الملفات المنظمة وغير المستقرة وغير المستخدمة في المطالبة بنفسها. باستخدام إجابته و ZSH VCS_INFO أمثلة، لقد توصلت إلى ما يلي:

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' stagedstr 'M' 
zstyle ':vcs_info:*' unstagedstr 'M' 
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats \
  '%F{5}[%F{2}%b%F{5}] %F{2}%c%F{3}%u%f'
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked
zstyle ':vcs_info:*' enable git 
+vi-git-untracked() {
  if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
  [[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
  hook_com[unstaged]+='%F{1}??%f'
fi
}


precmd () { vcs_info }
PROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '

هذا يخلق موجه الذي يحاكي الإخراج الملون git status -s (والتي يمكن تكوينها في الخاص بك .gitconfig ملف). ربما تكون الصورة مفيدة هنا:

prompt

مقارنة مع git status -s:

enter image description here

إذا كنت لا تحب الإخراج الملون، فهل تفضل بعض الطابع الآخر أو البناء الفوري، فقط تغيير stagedstr, unstagedstr, ، و hook_com[unstaged] القيم في التعليمات البرمجية أعلاه.

بدا الكثير من هذه الحلول بطيئا بالنسبة لي عند تقليل مفتاح الإرجاع، لذلك هنا خيار أساسي وسريع:

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '

ستحصل على موجه يشبه هذا:~/dev/project (feature-branch) $

أشكرك على الروابط!

أنا جعلت موجه التالي بناء عليهم

     # get the name of the branch we are on
     git_prompt_info() { 
         git branch | awk '/^\*/ { print $2 }'
     }
     get_git_dirty() { 
       git diff --quiet || echo '*'
     }

     autoload -U colors
     colors     
     setopt prompt_subst

     PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'                                           

     RPROMPT='%{$fg[green]%}%1(j.%j.)'        

من فضلك، اقتراح أي تحسينات.

أنا فقط redid منجم منذ أن لدينا أسماء فرعية طويلة في العمل. سيتم اقتطاع هذا المرء مع علامة القطع إذا كان أكثر من 35 حرفا.

parse_git_branch() {
    git_status="$(git status 2> /dev/null)"
    pattern="On branch ([^[:space:]]*)"
    if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then
        state="*"
    fi
    if [[ ${git_status} =~ ${pattern} ]]; then
      branch=${match[1]}
      branch_cut=${branch:0:35}
      if (( ${#branch} > ${#branch_cut} )); then
          echo "(${branch_cut}…${state})"
      else
          echo "(${branch}${state})"
      fi
    fi
}

setopt PROMPT_SUBST
PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$'

(أنا بالحرج في مدى فخور أنا من هذا.)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top