Skip to content

Commit

Permalink
refactored a lot, most of the code is gone
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 6, 2013
1 parent d0c49f9 commit ae5d612
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 59 deletions.
30 changes: 4 additions & 26 deletions hooks/pre-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

'use strict';

var common = require(__dirname + '/pre-common');
var run = require(__dirname + '/pre-common');
var child = require('child_process');
var label = 'pre-commit:';
var label = 'pre-commit';

// exits if there are no changes
// exits if there are no changes to commit
function haveChangesToCommit(cb) {
child.exec('git status --porcelain', function changes(err, status) {
if (err) {
Expand All @@ -26,26 +26,4 @@ function haveChangesToCommit(cb) {
});
}

haveChangesToCommit(function () {
common.getGitRoot(run);
});

function run(root) {
if (!root) {
console.error('');
console.error(label, 'Failed to find git root. Cannot run the tests.');
console.error('');
return process.exit(1);
}
console.log('git root', root);

var tasks = common.getTasks(root, 'pre-commit');
if (!tasks || !tasks.length) {
console.log('');
console.log(label, 'Nothing to run. Bailing out.');
console.log('');
return;
}

common.runner(tasks);
}
run(label, haveChangesToCommit);
47 changes: 41 additions & 6 deletions hooks/pre-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,44 @@ function runner(run) {
});
}

module.exports = {
getGitRoot: getGitRoot,
failure: failure,
runner: runner,
getTasks: getTasks
};
function runAtRoot(root, label, check) {
if (!root) {
console.error('');
console.error(label, 'Failed to find git root. Cannot run the tests.');
console.error('');
return process.exit(1);
}
if (typeof label !== 'string') {
throw new Error('Expected string label (pre-commit, pre-push)');
}
if (typeof check !== 'function') {
throw new Error('Expected check changes function');
}

var tasks = getTasks(root, label);
if (!tasks || !tasks.length) {
console.log('');
console.log(label, 'Nothing to run. Bailing out.');
console.log('');
return;
}

check(function () {
runner(tasks);
});
}

function run(label, check) {
if (typeof label !== 'string') {
throw new Error('Expected string label (pre-commit, pre-push)');
}
if (typeof check !== 'function') {
throw new Error('Expected check changes function');
}

getGitRoot(function (root) {
runAtRoot(root, label, check);
});
}

module.exports = run;
31 changes: 4 additions & 27 deletions hooks/pre-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
'use strict';

var child = require('child_process');
var common = require(__dirname + '/pre-common');
var label = 'pre-push:';
// console.log('pre-push in', process.cwd());
var run = require(__dirname + '/pre-common');
var label = 'pre-push';

function haveCommitsToPush(cb) {
// todo: find if there are commits to push
// todo: find if there are commits to push for any remote name / any branch
child.exec('git log origin/master..HEAD', function (err, stdout) {
if (err) {
console.error(label, 'Failed to check for commits. Cannot run the tests.');
Expand All @@ -27,26 +26,4 @@ function haveCommitsToPush(cb) {
});
}

haveCommitsToPush(function () {
common.getGitRoot(run);
});

function run(root) {
if (!root) {
console.error('');
console.error(label, 'Failed to find git root. Cannot run the tests.');
console.error('');
return process.exit(1);
}
console.log('git root', root);

var tasks = common.getTasks(root, 'pre-push');
if (!tasks || !tasks.length) {
console.log('');
console.log(label, 'Nothing to run. Bailing out.');
console.log('');
return;
}

common.runner(tasks);
}
run(label, haveCommitsToPush);

0 comments on commit ae5d612

Please sign in to comment.