【问题标题】:Outputting dirty state of git repo in command line issue在命令行问题中输出 git repo 的脏状态
【发布时间】:2012-09-20 15:29:13
【问题描述】:

如果我在命令行中使用 git repo 运行它

git status 2> /dev/null | grep -iw 'modified'

我得到的输出

#   modified:   .rvmrc

所以我的假设是,如果我将它插入到 if 语句中,我将得到正确的执行代码行

但是当我创建函数作为 .bash_profile 的一部分时,这是我拥有的代码:

## Prompt ##
path="$white\w$reset"
folder_name="$white\W$reset"
git_branch="\$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/$red(\1)$reset/')"
rvm="$green(\$(~/.rvm/bin/rvm-prompt i v p))$reset"

# Checks if working tree is dirty
function _git_st {
  if [[ $(git status 2> /dev/null | grep -iw 'modified')  ]]; then
    echo "[x]"
  fi
}
git_st="$yellow`_git_st`$reset"

PS1="$rvm $folder_name $git_branch $git_st \$ "

X 没有回显....我有点迷茫,不知道我做错了什么。

这是我得到的输出:

(ruby-1.9.2-p290) folder-git (master)  $ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   .rvmrc
#
no changes added to commit (use "git add" and/or "git commit -a")
(ruby-1.9.2-p290) folder-git (master)  $ 

【问题讨论】:

  • 顺便说一句,您的_git_st 函数比它需要的要复杂一些。 if git status 2&gt; /dev/null | grep -qiw 'modified'; then 应该是一样的,不需要捕获输出并检查它是否为空。
  • 谢谢@chepner,我明天试试。

标签: bash .bash-profile


【解决方案1】:

这是因为您使用变量git_st 来设置PS1,该变量将在设置PS1 时进行评估。您可以尝试调用函数_git_st 而不是使用git_st,即尝试以下行:
PS1="$rvm $folder_name $git_branch \$(_git_st) \$ "
此外,您可能有兴趣知道 git 的较新版本提供的功能以及提供此类实用程序的 bash 完成。如果可用,您可以查看__git_ps1 函数。
希望这会有所帮助!
P.S.:Here 是这样的帖子,它可能会提供一些使用 __git_ps1 的指针。 This 是 Google 的快速搜索结果。

【讨论】:

  • 感谢@another.anon.coward 指出我的缺陷,稍后会尝试。
  • 就是这样,它的工作。太感谢了。我稍后会检查链接。
猜你喜欢
  • 2021-11-20
  • 2012-12-21
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-07
  • 2021-06-06
  • 2016-04-27
  • 1970-01-01
相关资源
最近更新 更多