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? -- *** E-Mail is NOT a SECURE channel *** James B. Byrne mailto:ByrneJB at Harte-Lyne.ca Harte & Lyne Limited http://www.harte-lyne.ca 9 Brockley Drive vox: +1 905 561 1241 Hamilton, Ontario fax: +1 905 561 0757 Canada L8E 3C3
James B. Byrne ha scritto:> 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? >I did a simple test: cd /tmp touch .git export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"; if [ -f .git ] ; then echo WARNING: .git DIRECTORY ; fi ' and now every time I cd on that directory and every time I issue a command (on that directory) I get the warning. Of course you can do something more elegant (for example change the color of the whole prompt...) Don't know the impact on performance... hope this helps. -- Regards Lorenzo
On Wed, Aug 05, 2009 at 09:49:04AM -0400, James B. Byrne wrote:> 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?Untested, and it depends very much on your shell, but create a function cdr() { cd $1 || exit if [ -x ./.myscript ] then ./.myscript fi } Now if you use "cdr" (for Change Directory & Run) it should do what you want (runs a file called ".myscript" in the new directory if it exists) This _might_ work... cd() { command cd $1 || exit if [ -x ./.myscript ] then ./.myscript fi } OK, I just tested the later under ksh88 and it worked. You'd need to test with bash/ksh93/whatever-your-shell-is $ cat x1/.myscript echo hello $ cd x1 hello $ mkdir x0 $ cd x0 $ cd .. hello $ As you can see, everytime I enter the "x1" directory it runs .myscript -- rgds Stephen
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. -- Les Mikesell lesmikesell at gmail.com
On: Wed, 5 Aug 2009 10:15:47 -0400, Stephen Harris <lists at spuddy.org> wrote:> > Untested, and it depends very much on your shell, but create a > function > > cdr() > { > cd $1 || exit > if [ -x ./.myscript ] > then > ./.myscript > fi > } > > Now if you use "cdr" (for Change Directory & Run) it should do what > you want (runs a file called ".myscript" in the new directory if > it exists) > > > This _might_ work... > > cd() > { > command cd $1 || exit > if [ -x ./.myscript ] > then > ./.myscript > fi > } > > OK, I just tested the later under ksh88 and it worked. You'd need > to > test with bash/ksh93/whatever-your-shell-is > > $ cat x1/.myscript > echo hello > $ cd x1 > hello > $ mkdir x0 > $ cd x0 > $ cd .. > hello > $ > > As you can see, everytime I enter the "x1" directory it runs > .myscript > > -- > > rgds > Stephen >****************** >Thank You. I usually run in the CentOS default (bash) shell. I will give this a try and report back. Many thanks, -- *** E-Mail is NOT a SECURE channel *** James B. Byrne mailto:ByrneJB at Harte-Lyne.ca Harte & Lyne Limited http://www.harte-lyne.ca 9 Brockley Drive vox: +1 905 561 1241 Hamilton, Ontario fax: +1 905 561 0757 Canada L8E 3C3