Created
December 17, 2012 22:57
-
-
Save kalley/4323203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var spawn = require('child_process').spawn; | |
var summary = []; | |
var errors = []; | |
var app = { | |
toProcess: 0,, | |
processed: 0, | |
readdir: function(err, files) { | |
files.forEach(function(filename, i) { | |
app.cd(filename, i, files.length); | |
}); | |
}, | |
cd: function(filename, i, total) { | |
fs.stat(filename, function(err, stats) { | |
if ( stats.isDirectory() ) { | |
if ( fs.existsSync(filename + '/.git') ) { | |
app.toProcess++; | |
app.updateRepos( __dirname + '/' + filename ); | |
} | |
} | |
}); | |
}, | |
updateRepos: function(workTree) { | |
var added = false; | |
var gitDir = workTree + '/.git'; | |
var fetch = spawn('git', ['--git-dir=' + gitDir, 'fetch']); | |
fetch.on('exit', function(code) { | |
if ( ! app.pushError(code, workTree + '(fetch) code: ' + code) ) { | |
return; | |
} | |
var branch = spawn('git', ['--git-dir=' + gitDir, '--work-tree=' + workTree, 'branch']); | |
var remote; | |
branch.stdout.on('data', function(data) { | |
data = data.toString(); | |
data = data.split(/\n/); | |
data.forEach(function(line) { | |
if ( line.match(/^\*/) ) { | |
remote = line.replace('* ', 'origin/'); | |
} | |
}); | |
}); | |
branch.on('exit', function(code) { | |
if ( ! app.pushError(code, workTree + '(branch) code: ' + code) ) { | |
return; | |
} | |
function runMerge() { | |
if ( ! remote ) { | |
setTimeout(runMerge, 100); | |
} else { | |
var merge = spawn('git', ['--git-dir=' + gitDir, '--work-tree=' + workTree, 'merge', remote]); | |
merge.stdout.on('data', function(data) { | |
data = data.toString(); | |
if ( ! added && ! data.match(/up-to-date/) ) { | |
added = true; | |
summary.push(workTree); | |
} | |
}); | |
merge.on('exit', function(code) { | |
app.pushError(code, workTree + '(merge) code: ' + code, true); | |
}); | |
} | |
} | |
runMerge(); | |
}); | |
}) | |
}, | |
pushError: function(code, message, finalProcess) { | |
if ( code !== 0 ) { | |
errors.push(message); | |
finalProcess || app.processed++; | |
} | |
if ( finalProcess ) { | |
app.processed++; | |
} | |
if(app.processed === app.toProcess) { | |
app.summarize(); | |
} | |
return code === 0; | |
}, | |
summarize: function() { | |
if ( ! summary.length && ! errors.length ) { | |
console.log('Everything is up-to-date!\n'); | |
} else { | |
if ( summary.length ) { | |
console.log('These repos were updated\n-------------------------------------\n' + summary.join('\n')); | |
} | |
if ( errors.length ) { | |
console.log('These repos had errors\n-------------------------------------\n' + errors.join('\n')); | |
} | |
} | |
process.exit(0); | |
} | |
}; | |
fs.readdir('.', app.readdir); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment