Getting set up to code… again

I previously wrote about getting my Snow Leopard installation set up to run Git. I’m going to do the same thing here, except with a fresh Mountain Lion installation.

Install Xcode

Since we’ve done this before, I know Homebrew will yell at me if I don’t have Command Line Tools for Xcode installed. Go to the App Store, download the latest Xcode (since we’re on the latest OS). Once that’s downloaded and installed, open Xcode and go to Xcode -> Preferences and open the Downloads tab. Now install Command Line Tools.

Install Homebrew

Here we go! Pop this into Terminal:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Install Git

Big surprise here.

brew install git

Set Up Git

I followed the instructions at Github for the rest (including setting up your SSH keys!). After that, you should be good to go!

Setting up my Windows box

In my last post I set up my iMac with Git and MacVim. (Which you should read because I didn’t want to repeat a lot of those steps here.) Well, it’s not easy for me to code on the move with my iMac, so it’s time to set up my laptop which runs Vista! (I know I know, I should use Windows 7…)

Installing gVim

ANYWAY, first I want to install gVim. Following this guide, I went with the default options on the installer except I additionally checked “Create .bat files for command line use”. This way I can access Vim and gVim from the command line.

Installing Git and using Github

I then followed the Git installation guide for Windows Github wrote up. The only changes I made were a) checking “Git Bash Here” and “Git GUI Here” for installing those context menu entries (right click menu), and b) choosing “Run Git from the Windows Command Prompt” because, you know, that can be useful down the road. :P

Following the guide, I ran into the same setup steps I took on my iMac. When I had to add my Github API to the Git config through Git Bash, I did a quick Google search to figure out you can press “Insert” to paste whatever is on the clipboard right into Git Bash. Since the Tumblr template repository already exists, we can just do the following from the root:

$ git clone git@github.com:bergren2/codeendcode.git

This clones the repo into the folder codeendcode in my root, which is excellent. Sadly, though, I had to type in my SSH key again. :/ This wasn’t a problem on the Mac because they stored the passphrase for us with Keychain. But hey, we’re on Windows now. Let’s fix that!

Setting up ssh-agent

Edit: I made another attempt at setting up ssh-agent which you should check out below. (“Setting up ssh-agent… again!”) I think it’s a better approach!

Using this UCLA guide as a reference, I recognized I had to get bash to use ssh-agent when I booted it up for the first time. To fix that I just added the following to ~/.bashrc:

export SSH_AUTH_SOCK=/tmp/.ssh-socket
    ssh-add -l 2>&1 >/dev/null
if [ $? = 2 ]; then
    # Exit status 2 means couldn't connect to ssh-agent; start one now
    ssh-agent -a $SSH_AUTH_SOCK >/tmp/.ssh-script
    . /tmp/.ssh-script
    echo $SSH_AGENT_PID >/tmp/.ssh-agent-pid
fi
function kill-agent {
    pid=`cat /tmp/.ssh-agent-pid`
    kill $pid
}

I did this using Vim, but you might want to try out the shiny gVim we installed earlier so you have an easier time using copy and paste. :) Next I added an environment variable by going to Control Panel > System > Advanced Settings > Environment Variables. There I added a variable with the name SSH_AUTH_SOCK and value /tmp/.ssh-socket. After that you’ll want to reopen Git Bash and do the following:

$ ssh-add ~/.ssh/id_rsa

You’ll be asked to enter your passphrase one last time and then tadaa, you’re good to go!

Setting up ssh-agent… again!

Apparently Github had a guide too on using ssh-agent, which I decided to follow instead. So now my ~/.bashrc instead contains the following:

 SSH_ENV="$HOME/.ssh/environment"

# start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."
    # spawn ssh-agent
    ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
    echo succeeded
    chmod 600 "$SSH_ENV"
    . "$SSH_ENV" > /dev/null
    ssh-add
}

# test for identities
function test_identities {
    # test whether standard identities have been added to the agent already
    ssh-add -l | grep "The agent has no identities" > /dev/null
    if [ $? -eq 0 ]; then
        ssh-add
        # $SSH_AUTH_SOCK broken so we start a new proper agent
        if [ $? -eq 2 ];then
            start_agent
        fi
    fi
}

# check for running ssh-agent with proper $SSH_AGENT_PID
if [ -n "$SSH_AGENT_PID" ]; then
    ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
    test_identities
    fi
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
else
    if [ -f "$SSH_ENV" ]; then
    . "$SSH_ENV" > /dev/null
    fi
    ps -ef | grep "$SSH_AGENT_PID" | grep -v grep | grep ssh-agent > /dev/null
    if [ $? -eq 0 ]; then
        test_identities
    else
        start_agent
    fi
fi

During my switch, though, I ran into an issue where ssh-agent had problems using the correct port because it was being used by… ssh-agent. 0_o Not a problem! I simply looked up the other ssh-agent’s PID using ps and then called kill <PID> to put it out of its misery.

Beginning work on the Tumblr template

Install Homebrew

First thing I wanted to do was install Homebrew. So I pasted the following into Terminal:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

The rest of Homebrew’s installation instructions can be found here but the only really important part was above. Trying to to anything else in the Terminal tells me what I need to do next — install Xcode.

Install Xcode 3.2.6

I haven’t made the move to Lion yet, so it looks like it’s an older version for me. :/ Since the Apple website is hard to navigate for finding any version but the latest and greatest (supposedly), you can grab 3.2.6 here (you’ll need an Apple Developers ID to get it, FYI).

If you’re looking to install a different version of Xcode, you can check them out here.

Install Git (and MacVim)

A simple brew git does the job. Since I’m also a fan of MacVim (which I can go into later), I also did brew macvim. Don’t forget to link up the app with brew linkapps! Anytime you see me do mvim blahblah it means I’m using MacVim to edit the file blahblah.

For setting up Git, I followed these instructions since I’m on a Mac. There are also similar instructions for Windows and Linux. Then I did the following to set up the repository with Github (you should sign up for an account!), which is pretty standard.

$ mkdir codeendcode
$ cd codeendcode
$ git init
$ touch README
$ mvim README
$ git add README
$ git commit -m 'first commit, yo'
$ git remote add origin git@github.com:bergren2/codeendcode.git
$ git push -u origin master

My commit message was really bland, so I decided to add some color to Terminal:

$ git config color.ui true

Of course, having to type out git commit reminded me I need to define my aliases.

$ git config --global alias.s status
$ git config --global alias.ci commit
$ git config --global alias.co checkout

And last but not least, I want to globally ignore some files from being committed, such as Vim .swp files and a few Mac folder config files. (You can check out some guidelines here.) My ~/.gitignore_global looks like this, for now:

# Vim #
#######
*.swp
*.swo

# OS generated files #
######################
.DS_Store*
ehthumbs.db
Icon?
Thumbs.db

And I simply ran git config --global core.excludesfile ~/.gitignore_global to put it into effect.

So now we’ve got a template?

So I put the current template on Github for a starting point. Ideally we develop the template using something like Thimble, which is outdated. D: I’m sure we can look into some cool tools for this later, but really I’d just like to get to know Tumblr custom themes before we go too crazy with this stuff. :P I can’t wait to make this Tumblr look less boring!