git branch
informará somente se o diretório de trabalho atual for o repositório que você deseja rastrear.
Por exemplo:
$> pwd
/home/xieerqi
$> git branch
fatal: Not a git repository (or any of the parent directories): .git
$> cd sergrep
$> git branch
* master
Você deseja adicionar uma chamada cd
a essa função que navegará para esse diretório. Melhor ainda, coloque os parênteses em torno desse comando, de forma que o comando seja executado em subshell, para que seu diretório de trabalho atual não seja afetado. Para mim, a função pode ser escrita assim:
parse_git_branch(){
# navigate in sub shell to my git repository
# and execute git branch
( cd /home/xieerqi/sergrep; git branch 2> /dev/null | \
sed -e '/^[^*]/d' -e 's/* \(.*\)/()/' )
}
E aqui está como isso funciona em ação:
DIR:/xieerqi|14:24|skolodya@ubuntu:
$ source ~/.mkshrc
DIR:/xieerqi|14:24|skolodya@ubuntu:
$ PS1="$(parse_git_branch)$PS1"
(master)DIR:/xieerqi|14:24|skolodya@ubuntu:
$ echo HELLO ASKUBUNTU
HELLO ASKUBUNTU
(master)DIR:/xieerqi|14:24|skolodya@ubuntu:
$
(master)DIR:/xieerqi|14:24|skolodya@ubuntu:
$ typeset -f parse_git_branch
parse_git_branch() {
( cd /home/xieerqi/sergrep
git branch 2>/dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/(\1)/" )
}