Archive for December, 2010

Git: committing only partial changes of a file

Posted on December 30th, 2010 in git | 1 Comment »

or better known as “staging patches”.

So you created some new functionality in a file but you have a lot of other code you’re not quite ready to commit yet. Answer: git add -p . It will interactively go though modified sections of your file asking if you want to “stage this hunk”. Answer y for yes, n for no, q for stop, then commit as usual. You can even split hunks if you want to get more fine-grained.

For more details check out this page.

Unix tidbits from Unix Power Tools, 3rd edition

Posted on December 26th, 2010 in unix | No Comments »

Just finished the 1000+ pager Unix Power Tools, Third Editionbook over Christmas break and found it a pretty good complete general introduction / refresher to Unix / Linux.

I originally passed the book up as it was last updated in 2002 but my interest was piqued skimming it and in the end I found that 98% of it’s contents still relevant today. It was like having lunch with a old-time unix guy – all the command line stuff just runs faster these days and he talks about screen instead of tmux, etc. Additionally, interesting history and tips were given on some unix programs (for ways of dropping out of a shell script faster since back in the day CPU cycles were rented).

Less known tidbits

Here are some quick notes I jotted down of some new things I learned:

pushd and popd is a great way of cd’ing to a far away path, and zipping yourself back where you were:

pushd # pop this directory on the "remember" stack
popd # go back to pop directory
dirs # see your pushd paths

I dabble in jailbreaking my phone occasionally and must download the latest jailbreak app off the torrents. This alerts the security professional in me as I have no idea what’s in the compiled code as anybody could have seeded it. The Jailbreak guys always provide an md5 verification hash on their site of their latest build and now I have the capability to validate them:

md5sum filename(s) > savedhash # create hash
md5sum -c savedhash # compare hash

Sometimes I need to modify the output of a program but also want to keep a copy of it’s unmodified output. Tee does that for me in one go.

echo 'hi there' | tee hi.txt | sed 's/hi/bye/'
# outputs 'by there' while saving original string to hi.txt

Cool way to fix command line typos:

cd bydirectory
-bash: cd: bydirectory: No such file or directory
# returns and executes: cd mydirectory

Can also do:

echo 'hi thereee'
# returns: hi thereee
# returns and executes: echo 'hi there'

Other short tidbits include:

sdiff <i>file1</i> <i>file2</i> # does side by side diff comparisons
split # split large files into smaller files
fmt # text formatter
man -k string # like M-x apropos in emacs

Lastly, put a -xv at the top of your shell scripts to debug them:

#! /bin/sh -xv

Suspend emacs to the background on a remote box

Posted on December 25th, 2010 in emacs | No Comments »

Though emacs has the ability to run a shell inside (three different ones actually), anything that requires more than just a “dumb terminal” doesn’t work as well as I’d like in these emulated terminals. My research in finding a solution has lead to some creative answers such as using tmux (thanks Takashi) which on its own solves and improves tons of daily workflow situations. I, however, will dedicate that topic in it’s entirety to another entry as it does add some optional complexity to your workflow; too much if you’re looking to solve just this one problem.

What I really want to be able to do is to be working in emacs on a remote machine (via ssh) with all my windows and buffers setup and to drop down to shell occasionally to look something up or run some other scripts and then move back to emacs at will. This is easy if you’re running emacs on your local terminal on OS X: just like any other process like top, use C-z to move emacs to a background process and type fg when you want to move it back to the foreground. All your current windows and buffers are resumed. On the two remote machines I’ve tested C-z just moved you to the previous line. I’ve tried unbinding that key combo, but all I get is a terminal beep. After some research and hanging around the #emacs chan (thanks insomniaSalt) the answer is to drop this in your .emacs file. Now C-z will work as it does on your local terminal:

(global-set-key "\C-z" 'suspend-emacs) ; Stops emacs and return to superior process 

Using make to concatenate JS files

Posted on December 21st, 2010 in bash, git | No Comments »

In a previous project I used a bash script in conjunction with a git hook to concatenate all my js files and increment build numbers in html file references (cache-kill) after every commit. This was convenient at first, but quickly got annoying as I wasn’t always committing js file changes. I eventually opted to just call the script manually before any deployment to qa or production, thinking I would automate it again one day to only be called when JS files were changed (git hooks probably provide this info).

Today I read up on make. I’ve always used make in the past to build versions of a program for my system but never how it actually worked. When ran, it essentially reads in a list of dependencies, and if the source files have modification times newer than the “built” file, it runs your script to rebuild them. Concatenating javascript files seems like the perfect opportunity for this. I’ll rewrite my bash script to use make, set it as a git hook, and should be able to forget it. Will let you know how it goes.

Expires header doesn’t work on the iPhone / iPad

Posted on December 17th, 2010 in Apache, iOS, iPhone | No Comments »

I’m optimizing the web views of an iPhone app in preparation for Christmas traffic and one of the ideas I had was to use small increments of Expires header to cache files client-side. I assumed this would be no problem as all my work with Safari on the iPhone has shown it a very capable browser—as good as any desktop one.

Turns out after some testing that using Expires Header isn’t possible— not because of cache size limitations as one would assume. It looks like the guys in Cupertino wanted to limit this ability on the iOS as every HTTP request made by an iOS device issues a pragma: no-cache directive. I’ve yet to understand why. Better fire up more memcache servers.

Here’s the raw headers if interested:

GET /expires/ HTTP/1.1
User-Agent: Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Cookie: __utma=244034822.428964265.1285289496.1290546798.1290723961.13; __utmz=244034822.1285289496.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=1.1229958817.1285277083.1288920090.1292540519.14; __utmc=1; __utmz=1.1285277083.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Pragma: no-cache
Connection: keep-alive