1 var exec = require('sync-exec');
3 var glob = require("glob");
6 'bower_components/**/*',
11 var pkg = require('../../package');
12 // Change to \{{ hook }} once that variable is available.
13 // @see https://github.com/wecodemore/grunt-githooks/pull/40
14 var hooks = pkg.githooks && pkg.githooks['{{ task }}'];
16 var ret, hook, files, commands, staged, matchAll;
17 var filesMatched = [];
19 // Iterate over all hook definitions.
20 for (var h in hooks) {
22 commands = hook.commands || [];
23 staged = hook.staged === void 0 ? false : !!hook.staged;
24 matchAll = hook.matchAll === void 0 ? true : !!hook.matchAll;
26 // Iterate over all files in a hook definition.
28 // Expand all file paths using glob (for pattern matching).
29 if (typeof hook.files === 'string') {
30 files = glob.sync(hook.files, globOptions) || [];
32 if (Array.isArray(hook.files)) {
34 for (var f = 0; f < hook.files.length; f++) {
35 files = [].concat(files, glob.sync(hook.files[f], globOptions) || []);
39 // All files must either be staged or modified for the entire
40 // hook definition command(s) to be executed.
44 // Only continue if file has been staged or modified.
45 ret = exec((staged ? 'git diff --name-only --cached ' + file : 'git diff HEAD@{1} --stat -- ' + file));
47 if (exit === 0 && ret.stdout !== '') {
48 filesMatched.push(file);
50 else if (matchAll && (exit > 0 || ret.stdout === '')) {
51 console.log(ret.stdout);
59 // Iterate over all commands that should be executed for matched files.
60 if (filesMatched.length) {
61 if (typeof commands === 'string') {
62 commands = [commands];
64 for (var c in commands) {
65 var command = commands[c];
66 console.log("\033[4;35mgit: {{ task }}\033[0;35m\n>> " + command + "\033[0m\n");
68 console.log(ret.stdout);
76 // Display any errors.
77 if (exit > 0 && ret && ret.stderr) {
78 console.log(ret.stderr);