Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Last active August 29, 2015 14:02
Show Gist options
  • Save igniteflow/502a044e1e9da6740ab1 to your computer and use it in GitHub Desktop.
Save igniteflow/502a044e1e9da6740ab1 to your computer and use it in GitHub Desktop.
Setting up a local git server for testing and experimenting with hooks

First create the user and folder structure:

adduser git
su git
mkdir -p ~/repos/myrepo.git
git --bare init ~/repos/myrepo.git

Now our remote is ready to add to our local repository. In the local repo:

git remote add local git@localhost:/repos/myrepo.git
git push local master

Now our git remote has been pushed to. Next we add a post-receive hook that we can do cool stuff with like Jenkins integration

su git

Add the following to: ~/repos/myrepo.git/hooks/post-receive

#!/bin/bash
  
while read oldrev newrev refname
do
    echo ${oldrev} ${newrev} ${refname}
done

or the same in in Python

#!/usr/bin/env python
import sys

for line in sys.stdin:
    old_ref, new_ref, refname = line.replace('\n', '').split(' ')
    print(old_ref, new_ref, refname)

and make it executable:

chmod +rx ~/repos/myrepo.git/hooks/post-receive

Now when you push a new ref to the local remote you'll see the args echoed out

 {master} ~/Projects/myrepo$ git push local release
Total 0 (delta 0), reused 0 (delta 0)
remote: 0000000000000000000000000000000000000000 b18e7e9f4ed2c8f58e4767d941134293bd0c9b28 refs/heads/release
To git@localhost:/repos/myrepo.git
 * [new branch]      release -> release
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment