Poor man’s __git_ps1

Recently one of the remotely maintained servers I worked on regularly has started to exhibit slowdowns in bash. It started only occasionally, perhaps as a result of a high number of users or long-running script, but become a lot worse and resulted in a few seconds delay every time a new prompt was drawn. A few seconds may not sound a lot, but can be incredibly frustrating!

Upon a bit of an investigation I found that the culprit was the call to __git_ps1 in my $PS1 variable. Removing this made the bash prompt speedy again! Hooray! However, having the current branch on my screen immediately is infinitely useful and I can’t really live without it any more, so I carried on digging.

On this particular set of servers git has been replaced with a set of custom script that perform a lot of tasks. This means that calls to simple git commands run a LOT of other commands, logging and getting repo information among other things. I thought about replacing all the references to git in the git-prompt.sh file with the direct path to the binary, but thought it shouldn’t be too tough to just get the information myself using the information in the .git folder.

The result is this bash function.

It’s fairly primitive, it checks the current path for a .git directory, if it doesn’t find one, it goes up one level, until either finding one, or hitting the root. It printfs the result in the same way as the real __git_ps1 and handles the basic states like |MERGING although doesn’t detect a detached HEAD any other similar states, but serve the main purpose, now I can correctly see my current branch and avoid committing the wrong thing to the wrong place!

Feel free to share if this is useful and let me know if there are any minor improvements (short of using the full version!).

Leave a Reply

Your email address will not be published. Required fields are marked *