Install and Use Git Locally

Git is a powerful, flexible version control simple, one I find more intuitive than Subversion. Once again, I use MacPorts to install Git, and then I supply some global configuration settings.

markf$ sudo port install git-core
markf$ git config --global user.name "Mark Fenoglio"
markf$ git config --global user.email markf@istarelworkshop.com
markf$ git config --global color.diff auto
markf$ git config --global color.status auto
markf$ git config --global color.branch auto

The first two Git configuration commands set important defaults for identifying the author of changes to the repository. The last three commands define what colors to use when Git presents information in the console.

Create the Initial Git Repository

markf$ cd ~/Sites/iw
markf$ git init
Initialized empty Git repository in /Users/markf/Sites/iw/.git/

Ignoring Mac OS X Finder Detritus

One nuisance in Mac OS X when working with version control systems is (normally hidden) .DS_Store files created by the Finder. Git allows you to globally ignore files.

markf$ git config --global core.excludesfile ~/.gitignore
markf$ echo .DS_Store >> ~/.gitignore

Exclude Files from Version Control

In addition to being able to ignore certain files regardless of context, you can also ignore files for a specific Git repository. One common example of this is configuration files where the values of certain parameters will be different between development (local) and production (remote) environments.

markf$ cd ~/Sites/iw
markf$ vi .git/info/exclude
conf/ApplicationConstants.php
httpd.conf
install/conf/sh.conf
install/php/config.php

Initial Repository

Now that the appropriate files will not be part of the repository, I can create its initial version.

markf$ git add .
markf$ git commit -m "[install] initial repository"

The . in the first command is a wildcard that means add all non-ignored files to the current batch of changes. The -m switch on the commit command is the message to be included as the changes are committed to the repository.

Secure Login

Logging in to a remote server via secure shell is safe and effective. Using public and private keys eliminates the need to type in a password every time you connect to the remote server. This makes such work both more secure and more convenient.

Create the private and public keys

A simple unix command generates the keys, and the output also includes a key fingerprint and an ascii "randomart" image (Neither is included here for security reasons.) I left the passphrase empty. (It prompts twice for it.)

markf$ cd ~/.ssh
markf$ ssh-keygen -t dsa

Put the public key on the remote server

At this point, I have created a private (~/.ssh/id_dsa) and public (~/.ssh/id_dsa.pub) key. I want to copy the public key to the remote server.

markf$ ssh user@www.istarelworkshop.com
user$ mkdir .ssh && cd .ssh
user$ vi authorized_keys

Paste the contents of the local public key into ~/.ssh/authorized_keys on the remote server. Note: if the .ssh directory already exists on the remote server, you may only need to append the contents of ~/.ssh/id_dsa.pub to ~/.ssh/authorized_keys.

Shell Script Application Installation

An application is virtually certain to change design quite drastically from initial conception to launch. A well-conceived shell script that combines the application database schema with the Istarel Workshop Application Framework's object-relational mapping (ORM) tool makes it quite simple to cleanly handle change.

Listing: /iw/install/install.sh

#!/bin/bash
. conf/sh.conf

echo "Deleting database..."
dropdb -U $DATABASE_USER $DATABASE_NAME

echo "Creating database..."
createdb -U $DATABASE_USER --encoding UNICODE --template template0 $DATABASE_NAME

echo "Applying schema..."
psql -q -U $DATABASE_USER -f sql/schema.sql -d $DATABASE_NAME

echo "Import redirects..."
psql -q -U $DATABASE_USER -f sql/redirects.sql -d $DATABASE_NAME

echo "Import application data..."
psql -q -U $DATABASE_USER -f sql/projects.sql -d $DATABASE_NAME
psql -q -U $DATABASE_USER -f sql/categories.sql -d $DATABASE_NAME
psql -q -U $DATABASE_USER -f sql/articles.sql -d $DATABASE_NAME

echo "Creating model object classes..."
$FRAMEWORK_DIR/install/orm_install -n Istarel Workshop -v 3.0 -w $WEBROOT_DIR 
    -a $PROJECT_DIR --all-relations --with-foreign-keys

The last command invokes the framework ORM tool, which parses the PostgreSQL database and constructs a hierarchy of PHP model object and factory classes to interact with the database.

The configuration file referenced in the installation script defines the executable paths and application-related constants.

Listing: /iw/install/conf/sh.conf

#!/bin/sh

DATABASE_NAME=iw
DATABASE_USER=iwuser
PHP=/opt/local/bin/php
PSQL=/opt/local/lib/postgresql84/bin/psql
WEBROOT_DIR=/Users/markf/Sites
PROJECT_DIR=/iw
FRAMEWORK_DIR=/Users/markf/Sites/fw