-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (86 loc) · 2.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*!
* is-dirty (https://github.com/jonschlinkert|jonschlinkert/is-dirty)
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var gitty = require('gitty');
var mm = require('micromatch');
module.exports = function isDirty(cwd, patterns, cb) {
if (typeof patterns === 'function') {
cb = patterns;
patterns = null;
}
if (typeof cb !== 'function') {
throw new TypeError('expected a callback function');
}
if (typeof cwd !== 'string') {
cwd = process.cwd();
}
var fp = path.resolve(cwd, '.git');
var repo = gitty(cwd);
fs.stat(fp, function(err, stats) {
if (err) {
cb(err);
return;
}
repo.status(function(err, status) {
if (err) {
cb(err);
return;
}
if (hasFiles(status)) {
status.matches = [];
if (patterns) {
cb(null, getMatches(cwd, patterns, status));
} else {
cb(null, status);
}
} else {
cb();
}
});
});
};
function hasFiles(status) {
var types = ['staged', 'unstaged', 'untracked'];
var len = types.length;
var idx = -1;
while (++idx < len) {
var type = types[idx];
if (status[type].length) {
return true;
}
}
return false;
}
function getMatches(cwd, patterns, status) {
if (status.staged.length) {
status.matches = mm(pluckFiles(status.staged), patterns);
}
if (status.unstaged.length) {
status.matches = status.matches.concat(mm(pluckFiles(status.unstaged), patterns));
}
if (status.untracked.length) {
status.matches = status.matches.concat(mm(status.untracked, patterns));
}
status.matches = status.matches.map(function(filename) {
return path.relative(cwd, path.resolve(cwd, filename));
});
return status;
}
function pluckFiles(arr) {
var res = [];
var len = arr.length;
var idx = -1;
while (++idx < len) {
var val = arr[idx];
if (val.status !== 'deleted') {
res.push(val.file);
}
}
return res;
}