【问题标题】:Showing git status on Mac terminal not working with custom bash profile在 Mac 终端上显示 git 状态不使用自定义 bash 配置文件
【发布时间】:2017-05-29 02:57:50
【问题描述】:

我想在终端上的 git repo 中显示我当前的分支状态(如 * 表示已修改),如下所示。但是,即使我切换到 git repo,它也总是显示以下内容。

ᴾᴋᴹɴ iFeelNewsBot master wrong... ↪ 

当我在终端中打开一个新选项卡时,它会显示当前 git 分支的正确文本渲染,可以进一步查看。它应该是什么样子

ᴾᴋᴹɴ iFeelNewsBot master * ↪ 

我的自定义 bash 配置文件代码如下

# user name and color
USER_NAME='mr.universe';
TRAINER_TITLE='ᴾᴋᴹɴ'
USER_NAME_COLOR='\[\033[00m\]';
END_COLOR='\e[0m';

# \W = current working path
# \u = user
function parse_git_dirty {
  gitStatus=$(git status 2> /dev/null | tail -n1)
  clean="nothing to commit, working directory clean"
  if [[ -d "./.git" ]]
  then
    [[ $gitStatus != $clean ]] && echo "*" || echo "="
  elif [[ ! -d "./.git" ]]
  then
    echo "wrong...."
  else
    echo "STOP"
  fi
}

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

export PS1="\[\033[40;02m\]$TRAINER_TITLE \W \[\033[36;02m\]\[\033[29;00m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2) $(parse_git_dirty) ↪ "

【问题讨论】:

  • 看起来parse_git_branch 函数从未使用过。

标签: git bash macos github


【解决方案1】:

您假设 git 存储库中的每个目录都包含 .git,但只有存储库的顶级目录中包含 .git 目录。所以第一个if 失败,elif 检查.git 目录是否不存在。看起来结果是真的,它打印“错误”。

【讨论】:

    【解决方案2】:

    你可以检查你是否是inside a working tree of a git repo:

    inside_git_repo="$(git -C $PWD rev-parse --is-inside-work-tree 2>/dev/null)"
    
    if [ "$inside_git_repo" ]; then
      echo "inside git repo"
    else
      echo "not in git repo"
    fi
    

    这比寻找.git 文件夹更可靠。

    【讨论】:

    • 显示 "inside git repo" 有效,但是当我切换到新的非 git 目录时,"not in git repo" 在我打开新标签或刷新终端之前不会显示
    • 切换到一个新的分支,你的意思是git checkout aNewBranch吗? “刷新终端”是什么意思?
    • 我的错误我的意思是 cd 到一个新的非 git 目录。我的意思是关闭并重新打开我的终端。
    • 如果你cd到一个非git目录,按理说脚本显示“non in git repo”,不是吗?
    • 它没有,而是显示"not in git repo",直到我在我的mac终端中打开一个新标签或有趣地运行这个命令source ~/.bash_profile
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2013-05-08
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 2017-07-24
    相关资源
    最近更新 更多