git branch
will report only if the current working directory is the repository you want to track.
For instance:
$> pwd
/home/xieerqi
$> git branch
fatal: Not a git repository (or any of the parent directories): .git
$> cd sergrep
$> git branch
* master
You want to add a cd
call to that function that will navigate to that directory. Better yet, put the brackets around that command, so that the command is executed in subshell, so your current working directory is not affected. For me, the function can be written as so:
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/* \(.*\)/(\1)/' )
}
And here's how that works in action:
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)/" )
}