James B. Byrne wrote:
I have a little niggling situation that I would like to resolve programmatically. I use Git as my SCM and I have release branches which are sometimes patched. I find myself sometimes entering the working directory tree forgetting that I was last on a release branch and not on the master.
What I would like to do is to have a script run every time that I enter a directory, check for .git, and if it finds it then simply do a git-branch for me so that which I am on is forcefully pointed out to me before I proceed to do something foolish.
All I can come up with from searching wrt cd is details on why one cannot change the working directory of a running script and various kluges around this. I do not wish to change the pwd of the shell, I just want some way of testing for a certain file and running a specific command if it is found when I enter a working tree. If this requires testing every directory that I cd to then I can live with that. If instead one can put a script that runs only when one enters certain directories then I can live with that as well.
Is there any way to do this?
If you are talking about the command line, a shell function replacement for cd might work.
cd() { builtin cd $@ pwd # replace with your commands... }
You could put this in your .bashrc or export (-f) it from .bash_profile so subshells get it. I generally don't like surprises, so I'd probably name the funtion cdg or something else and use that when I wanted the special behavior.