Skip to content

Commit

Permalink
Added rimraf to rm -rf compiled directory on post_receive
Browse files Browse the repository at this point in the history
  • Loading branch information
gseguin committed Jan 4, 2012
1 parent 15a529b commit 322bd3f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,8 @@
"dependencies": {
"express": "2.5.2",
"request": "latest",
"requirejs": "latest"
"requirejs": "latest",
"rimraf": "latest"
},
"scripts": {
"start": "REPO_BASE_DIR=/home/node/repositories WORK_BASE_DIR=/home/node/src node server.js"
Expand Down
54 changes: 40 additions & 14 deletions server.js
Expand Up @@ -4,6 +4,7 @@ var express = require( 'express' ),
app = express.createServer(),
crypto = require( 'crypto' ),
fs = require( 'fs' ),
rimraf = require( 'rimraf' ),
exec = require( 'child_process' ).exec,
path = require( 'path' ),
regexp = require( './lib/regexp' ),
Expand Down Expand Up @@ -43,7 +44,7 @@ function fetch( repo, callback ) {
function checkout( repo, tag, callback ){
var wsDir = workBaseDir + "/" + repo + "." + tag;

fs.mkdir( dstDir, function () {
fs.mkdir( wsDir, function () {
exec( "git --work-tree=" + wsDir + " checkout -f " + tag,
{
encoding: 'utf8',
Expand Down Expand Up @@ -74,17 +75,20 @@ app.get( '/:repo/fetch', function ( req, res ) {

app.post( '/post_receive', function ( req, res ) {
var payload = req.body.payload,
onFetch = function ( error, stdout, stderr ) {
if ( error !== null ) {
res.send( error, 500 );
} else {
res.send( stdout );
}
},
fetchIfExists = function( candidates ) {
path.exists( candidates.shift() , function( exists ) {
repo, repoDir, tag,
fetchIfExists = function( candidates, callback ) {
var repo = candidates.shift();
path.exists( repo , function( exists ) {
if ( exists ) {
fetch( repoDir, onFetch );
fetch( repo,
function ( error, stdout, stderr ) {
if ( error !== null ) {
res.send( error, 500 );
} else {
callback( repo );
}
}
);
} else {
if ( candidates.length ) {
fetchIfExists( candidates );
Expand All @@ -98,10 +102,32 @@ app.post( '/post_receive', function ( req, res ) {
if ( payload ) {
try {
payload = JSON.parse( payload );
repo = payload.repository.name;
tag = payload.ref.split( "/" ).pop();

if ( payload.repository && payload.repository.name ) {
var repoDir = repoBaseDir + "/" + payload.repository.name;
fetchIfExists( [ repoDir, repoDir + ".git" ] );
repoDir = repoBaseDir + "/" + repo;
fetchIfExists( [ repoDir, repoDir + ".git" ],
function( dir ) {
checkout( repo, tag, function() {
var workspace = workBaseDir + "/" + repo + "." + tag,
compiled = workspace + "/compiled";
rimraf( compiled, function( err ) {
if ( err ) {
res.send( err, 500 );
} else {
fs.mkdir( compiled, function( err ) {
if ( err ) {
res.send( err, 500 );
} else {
res.send( "OK" );
}
});
}
});
})
}
);
} else {
res.send( "Wrong door!", 404 );
}
Expand All @@ -114,7 +140,7 @@ app.post( '/post_receive', function ( req, res ) {
});

app.get( '/:repo/:tag/checkout', function ( req, res ) {
var wsDir = workBaseDir + "/" + req.params.repo + "." + req.params.tag;
var wsDir = workBaseDir + "/" + req.params.repo + "." + req.params.tag;

fs.mkdir( dstDir, function () {
exec( "git --work-tree=" + wsDir + " checkout -f " + req.params.tag,
Expand Down

0 comments on commit 322bd3f

Please sign in to comment.