@@ -13,19 +13,21 @@ var pkg = require('../../package');
13
13
// @see https://github.com/wecodemore/grunt-githooks/pull/40
14
14
var hooks = pkg.githooks && pkg.githooks['{{ task }} '];
15
15
if (hooks) {
16
- var ret, hook, files, commands, staged;
16
+ var ret, hook, files, commands, staged, matchAll;
17
+ var filesMatched = [];
17
18
18
19
// Iterate over all hook definitions.
19
20
for (var h in hooks) {
20
21
hook = hooks[h];
21
- commands = hook.commands;
22
- staged = hook.staged;
22
+ commands = hook.commands || [];
23
+ staged = hook.staged === void 0 ? false : !!hook.staged;
24
+ matchAll = hook.matchAll === void 0 ? true : !!hook.matchAll;
23
25
24
26
// Iterate over all files in a hook definition.
25
27
if (hook.files) {
26
28
// Expand all file paths using glob (for pattern matching).
27
29
if (typeof hook.files === 'string') {
28
- files = glob.sync(hook.files[f] , globOptions) || [];
30
+ files = glob.sync(hook.files, globOptions) || [];
29
31
}
30
32
if (Array.isArray(hook.files)) {
31
33
files = [];
@@ -42,17 +44,20 @@ if (hooks) {
42
44
// Only continue if file has been staged or modified.
43
45
ret = exec((staged ? 'git diff --name-only --cached ' + file : 'git diff HEAD@{1} --stat -- ' + file));
44
46
exit = ret.status;
45
- if (exit > 0 || ret.stdout === '') {
47
+ if (exit === 0 && ret.stdout !== '') {
48
+ filesMatched.push(file);
49
+ }
50
+ else if (matchAll && (exit > 0 || ret.stdout === '')) {
46
51
console.log(ret.stdout);
47
- commands = false ;
52
+ filesMatched = [] ;
48
53
break;
49
54
}
50
55
}
51
56
}
52
57
}
53
58
54
- // Iterate over all commands that should be executed.
55
- if (commands ) {
59
+ // Iterate over all commands that should be executed for matched files .
60
+ if (filesMatched.length ) {
56
61
if (typeof commands === 'string') {
57
62
commands = [commands];
58
63
}
0 commit comments