Git tip of the day

When working in git repositories, you (read: I) often want to go back to the repository top level directory, and used to do something like

cd ../../../../..

But found out that you with git rev-parse can add the parameter --show-cdup which tells you the path to the top level directory of the repository relative to the current directory.

git rev-parse --show-cdup

With git version 1.7.0+ you can use --show-toplevel which shows the absolute path to the top level directory of the repository.

git rev-parse --show-toplevel

So in my shell (bash or zsh) i have made an alias to easy cd into this directory, so edit and add this line to your .zshrc .bashrc or whatever

alias gitroot='cd $(git rev-parse --show-toplevel)'

And restart your shell, and cd into a git repository, and do a gitroot BOOM back to top level directory.

Configure postfix to deliver all mails locally

On my computer a lot of local web development is going on, and every one of those sites have access to send mails. Sometimes these mails is configured to be sent to an external valid customer email account. The external customer may not be very happy about getting hundreds of testing emails, so in this post i will tell you how to configure postfix to deliver all mail sent to an internal account.

It's quite simple to configure postfix to deliver all mails sent, locally.

First of all start by editing /etc/postfix/main.cf

sudo vim /etc/postfix/main.cf

Go to the very bottom of the file and add the following line

canonical_maps = regexp:/etc/postfix/canonical-redirect

Save and exit the file.

Create the following file /etc/postfix/canonical-redirect

sudo vim /etc/postfix/canonical-redirect

And paste the following line and replace YOUR_USER with your system username.

/^.*$/ YOUR_USER

Save and exit the file. Now you just need to restart postfix, and everything works.

sudo postfix reload

To check the mails delivered locally you can use shell command mail or check these links (1, 2)

Use PhantomJS for easy website screenshots

PhantomJS is a headless webkit browser, which means you can execute it from commandline and get it to do various tasks, such as headless website testing, screen capture, page automation and newwork monitoring.

In this blogpost i will show you how easy it is to take a screenshot of a website and save it to a preferred folder. I will use an Apple OSX computer for showing, but will easy be changed to ex. linux or Windows.

First of all you will need to start your terminal and download the package for OSX.

  cd ~/Desktop
  curl http://phantomjs.googlecode.com/files/phantomjs-1.8.1-macosx.zip
  unzip phantomjs-1.8.1-macosx.zip
  cd phantomjs-1.8.1-macosx

As an alternative you can install phantomjs with homebrew, but you will NOT get all the examples installed, and we need those examples for this to work.

Now you’re almost ready to go. Everything you will need is in the folder you’re in now. The phantomjs executable file is located in the bin/ folder, and a lot of examples is in the examples/ folder. I will not get into what every script can do, but only tell a bit about the examples/rasterize.js script.

To take a simple screenshot of an entire web page this command will do it for you:

bin/phantomjs examples/rasterize.js http://phantomjs.org/ ~/Desktop/phantomjs.png

And you will have a screenshot of the phantomjs.org website on your Desktop.

I have created a little script which will take a screenshot of whatever you have copied to your clipboard (with CMD+c), in this case it has to be a complete website url :o)

You will have to do these steps for using this script.

  cd ~/
  curl https://gist.github.com/raw/4572457/83473f4c6c21aa8bfe929d13e77044e6b75f44e1/screenshot.sh
  chmod +x screenshot.sh

Open the file in your preferred editor and change the variables PATH_TO_PHANTOMJS and SCREENSHOT_PATH to match your filestructure, and save the file.

You will be able to execute the file and a screenshot will be saved, and finder will open the folder.

~/screenshot.sh http://ramlev.dk

Or without an argument, and the latest entry in your clipboard will be used.

~/screenshot.sh