1 of 11

Getting "Hooked" with Git

Jeff Amaral and Aaron Froehlich

2 of 11

Git Intro

Git from 40,000 feet

  • Fully distributed SCM
  • .git
  • Every cloned repo contains full history (Drupal)

3 of 11

.git

.git

├── HEAD

├── config

├── description

├── hooks

│ ├── applypatch-msg.sample

│ ├── commit-msg.sample

│ ├── post-update.sample

│ ├── pre-applypatch.sample

│ ├── pre-commit.sample

│ ├── pre-push.sample

│ ├── pre-rebase.sample

│ ├── prepare-commit-msg.sample

│ └── update.sample

├── index

├── info

│ └── exclude

├── logs

│ ├── HEAD

│ └── refs

│ ├── heads

│ │ └── master

│ └── remotes

│ └── origin

│ └── HEAD

├── objects

│ ├── 00

│ │ └── 1d890215

└── refs

├── heads

│ └── master

├── remotes

│ └── origin

│ └── HEAD

└── tags

4 of 11

What's a hook?

  • Similar to Drupal hook, in that they are invoked based on an action
  • Hooks are not shared
  • “Client-side” hooks and “Server-side” hooks
    • read more

5 of 11

Kinds of hooks

.git

└── hooks

├── applypatch-msg.sample

├── commit-msg.sample

├── post-update.sample

├── pre-applypatch.sample

├── pre-commit.sample

├── pre-push.sample

├── pre-rebase.sample

├── prepare-commit-msg.sample

└── update.sample

6 of 11

Examples: pre-commit

Ensure that there's a commit message

Check for code errors with PHP CS

https://gist.github.com/eethann/0abd3cb74c4e69aeff14

7 of 11

Example: post-checkout

Drupal specific: check for branch change, and archive branch data

https://gist.github.com/jeffam/17b94bf1a1dee26ce88f

8 of 11

Example: post-receive

Runs on a bare repo (what's that?) and is great for simple deployments.

#!/bin/sh

# post-receive hook that checks out the latest tag to the live web root and the head of the master branch to dev web root

LATESTTAG=`git --git-dir=/path/to/repo.git rev-list --tags --max-count=1`

LATESTTAGNAME=`git --git-dir=/path/to/repo.git describe --tags $LATESTTAG`

git --work-tree=/srv/www/example.org --git-dir=/path/to/repo.git checkout -fq $LATESTTAG

git --work-tree=/srv/www/dev.example.org --git-dir=/path/to/repo.git checkout -fq master

echo "Updated test site to master"

echo "Updated live site to $LATESTTAGNAME"

9 of 11

Example: post-receive

For more complicated post-receive processes that take a lot of time, use a task queue.

https://gist.github.com/jeffam/53069312ffbfc252531a

10 of 11

Example: post-checkout

11 of 11

Example: Post-commit

Proclaimed “The single most useful piece of software known to humankind”

Have some fun with your team

Save the moment for posterity