0

I have followed the steps given in the accepted answer in this question - Prepend current git branch in terminal. But even after adding following code to .bashrc and restarting the laptop, I do not see the branch name in terminal. Am I missing something? Do I have to somehow specify in the code as to whats the name of the repo root folder?

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$(parse_git_branch) $"
Prim
  • 99
  • You have to cd into the directory (or a subdirectory) of the repository whose branch name you want to see. – hayd Feb 09 '17 at 16:14

1 Answers1

0

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)/" ) 
} 
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • git branch works if you're in a subdirectory of the repository... are you suggesting that you want this to show the branch name of sergrep no matter which directory/repository you are in? – hayd May 01 '16 at 21:02
  • @hayd yup, exactly that - regardless of the directory. And yes, i know about the git branch showing output in subdirectories. – Sergiy Kolodyazhnyy May 01 '16 at 21:04
  • made the suggested changes, still no luck. – Prim May 02 '16 at 10:02
  • @Serg I think this is a strange usecase. Showing the branch of the repo that the directory is contained in is usually what you want. – hayd May 02 '16 at 15:35
  • git branch will report only if the current working directory is in the repository you want to track. – hayd May 02 '16 at 15:35
  • @hayd i know that. My answer is aimed to address OP's question. Plus, what if you only want to track one specific repo ? – Sergiy Kolodyazhnyy May 02 '16 at 16:16
  • I'm not even sure that's right for the OP's use case, better they just cd to the project directory! – hayd May 02 '16 at 16:49