Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9619
  • 博文数量: 2
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-14 11:40
文章分类
文章存档

2013年(1)

2012年(1)

我的朋友

分类: LINUX

2012-12-14 11:46:29

当在一个项目中频繁使用多个分支时,可以使用 git status 命令查询自己现在正工作在哪个分支下面,不过难免有脑子发昏的时候,忘记自己在哪个分支下面,因而发生误操作之类的杯具。

那么把分支显示在 Shell 提示符中无疑方便了很多,再也不需要频繁的使用 git status 命令了…

实现原理很简单,大体就是查询当前目录下面的 Git 分支名称,然后嵌入到 PS1 变量中。那么,Git 分支名称可以通过下面的脚本轻松的获得:

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

把上面的脚本封装到函数中,修改 PS1 变量,嵌入函数… 大体是这样。但是这样也意味着一个问题,就是每次 shell 活动(比如切换目录,甚至只是敲下回车)都会执行一次 git … sed 命令,这样每次都启动2个进程,实在是有些不爽。

好在,可以使用另外一种方式来获取 Git 分支名称,在每个 Git 项目中,都有一个 .git 目录,这个目录下面有个叫做 HEAD 的文件,里面包含的当前分支的路径信息:

ref: refs/heads/BRANCH-NAME

我们只要读取这个文件,然后再和对应的路径互相匹配一下就知道正确地分支名称了。不要只是简单的从 HEAD 内容中拆分出最后的 BRANCH-NAME,因为它不一定是正确地。

以下是  的实现方式:

## Parses out the branch name from .git/HEAD: find_git_branch () { local dir=. head until [ "$dir" -ef / ]; do if [ -f "$dir/.git/HEAD" ]; then head=$(< "$dir/.git/HEAD") if [[ $head = ref:\ refs/heads/* ]]; then git_branch=" → ${head#*/*/}" elif [[ $head != '' ]]; then git_branch=" → (detached)" else git_branch=" → (unknow)" fi return fi dir="../$dir" done git_branch='' }

接下来,将这个函数加入到 PROMPT_COMMAND 中,保证 Bash 在创建 prompt 之前调用这个函数取得分支名称:

PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"

最后只要重新定义 PS1 变量即可:

# Here is bash color codes you can use black=$'\[\e[1;30m\]' red=$'\[\e[1;31m\]' green=$'\[\e[1;32m\]' yellow=$'\[\e[1;33m\]' blue=$'\[\e[1;34m\]' magenta=$'\[\e[1;35m\]' cyan=$'\[\e[1;36m\]' white=$'\[\e[1;37m\]' normal=$'\[\e[m\]'   PS1="$white[$magenta\u$white@$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"

以上的代码你可以放在 ~/.profile 或者 ~/.bash_profile 等文件中即可,我的系统是 Snow Leopard,PS1 定义在 /etc/bashrc 中,所以我直接修改的这个文件。

最终效果如下:

image


UPDATE – 2010/06/23:
如果你安装了随 Git 附送的 git-completion.sh 子命令自动完成脚本,也可以使用该脚本提供的方法:

export PS1="[\u@\h \W"'$(__git_ps1 " (%s)")'"]\$ "

Ubuntu 系统,请参考: /etc/bash_completion.d/git

修改.bashrc

  1. force_color_prompt=yes

  2. if [ -n "$force_color_prompt" ]; then
  3.     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  4.     # We have color support; assume it's compliant with Ecma-48
  5.     # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
  6.     # a case would tend to support setf rather than setaf.)
  7.     color_prompt=yes
  8.     else
  9.     color_prompt=
  10.     fi
  11. fi

  12. black=$'\[\e[1;30m\]'
  13. red=$'\[\e[1;31m\]'
  14. green=$'\[\e[1;32m\]'
  15. yellow=$'\[\e[1;33m\]'
  16. blue=$'\[\e[1;34m\]'
  17. magenta=$'\[\e[1;35m\]'
  18. cyan=$'\[\e[1;36m\]'
  19. white=$'\[\e[1;37m\]'
  20. normal=$'\[\e[m\]'
  21. if [ "$color_prompt" = yes ]; then
  22.     PS1="$blue[$magenta\u$blue@$green\h$blue:$cyan\w$red\$(__git_ps1 ' (%s)')$blue]\$ $normal"
  23.     #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  24. else
  25.     PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1 " (%s)")\$'
阅读(4411) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:shell中常用的特殊符号 $#${}$? $*

给主人留下些什么吧!~~