ninetydegrees: Art: self-portrait (Default)
ninetydegrees (90d)☕ ([personal profile] ninetydegrees) wrote in [site community profile] dw_dev_training2013-06-16 11:16 pm
Entry tags:

Very basic scripts for newbies

I have two scripts I use all the time and which have saved me much grief and hair pulling. I was told there were worth sharing so here I am doing just that. :)

The first one just checks which branch you're on both in dw-free and nonfree (it's basically git branch with a bow):

#!/bin/bash
echo "..."
echo "dw-free"
echo "..."
cd $LJHOME
git branch

echo "..."
echo "dw-nonfree"
echo "..."
cd $LJHOME/ext/dw-nonfree
git branch



See? There's nothing particularly smart about this but I run it every time I start working on something. This goes into ####-USERNAME/bin on your hack and is made executable with chmod ugo+x ~/bin/SCRIPTNAME

The second one just checkouts develop for me on both branches and brings me back to /dw. See alierak's comment for an actual working way of doing this. :)

You can of course modify them to better fit your workflow or use Jeshyr's omnibus script instead. If you have interesting scripts, do share them in the comments please!
alierak: (Default)

[personal profile] alierak 2013-06-16 11:05 pm (UTC)(link)
Note: using cd in a script does not normally change directory in the shell from which you run the script. In order to make it actually leave you in a different place than you started, you'd have to run it with ". scriptname" (dot space scriptname) to interpret it within the current shell instead of a subshell.

That said, while at YAPC I banged my head on git for a while when trying to patch EmailPost.pm and being completely unable to find it. Turns out my dwu script left me on the master branch, which is apparently a bad place to try to get anything done. I switched around the order of branch updates in my script so it leaves me on the develop branch.
alierak: (Default)

[personal profile] alierak 2013-06-17 09:53 pm (UTC)(link)
There's not really anything you can do to write the script differently. I'm saying it would have to be run differently (at least, not in the usual way) in order to leave you in a different directory than you started in.

Suppose the second script is called "dev" and you've put it in your bin directory, made it executable with chmod, etc., so you can run it just by typing "dev". Now try this sequence of commands:

cd /tmp
pwd
dev
pwd


It should report that you're in /tmp both times. It doesn't leave you in $LJHOME because "dev" isn't actually running "cd" commands in your shell -- it's running them in a new, separate process that has its own idea of the current directory. The shebang line at the beginning of the script is what tells the OS what interpreter to run, not necessarily the same as your login shell, and the contents of the script are sent to the new process as standard input. It does whatever it does and then exits. If makes changes to the filesystem like creating files or switching git branches, obviously those will stick around. But anything it does with setting variables, changing its idea of the current directory, etc., that are essentially the local state of a shell? Those just go away when it exits.

So there are a couple of ways to make those sorts of changes happen in your login shell instead. One is to run the script by typing ". dev" or "source dev" instead of just "dev". Instead of starting a new process, using the shebang line, etc., this just tells your shell to read the file and treat it as input, as though you'd typed it in as a sequence of commands.

The other way to do this sort of thing is, instead of writing a script, you can write a shell function or alias so that typing "dev" runs a predefined sequence of commands in your shell, but doesn't involve reading them from a file called "dev". For example, if you add this function to your .bash_aliases:

dev() {
cd $LJHOME
git checkout develop
cd $LJHOME/ext/dw-nonfree
git checkout develop
cd $LJHOME
}


Then either log out and log back in or "source .bash_aliases", and you should be able to just type "dev" and have it change branches *and* leave you in $LJHOME.
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)

[personal profile] sophie 2013-06-19 05:40 pm (UTC)(link)
If it helps, the dwu script on the wiki currently is designed to bring you back to the same spot that you were in before you ran the script!