-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
194 lines (161 loc) · 5.42 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
var spawn = require("child_process").spawn;
var fs = require("fs");
var sgf = function (options = {}, callback) {
const typeOfOptions = typeof options;
let filter;
if (typeOfOptions === 'function') {
callback = options;
filter = undefined;
} else if (typeOfOptions === 'string') {
filter = options;
} else {
filter = options.filter;
}
if (typeof callback === 'undefined') {
return new Promise(function (resolve, reject) {
_sgf(filter, function (err, head) {
if (err)
return reject(err);
resolve(head);
}, options);
});
} else {
_sgf(filter, callback, options);
}
};
function _sgf (filter, callback, options) {
if (typeof filter === 'undefined')
filter = 'ACDMRTUXB';
sgf.getHead(function(err, head) {
if (err) {
callback(err);
} else {
var command = "git -c core.quotepath=false diff-index --cached --name-status";
if (options.relative) {
command += " --relative";
}
if (filter.indexOf('R') !== -1) {
command += " -M";
}
command += " --diff-filter=" + filter + " " + head;
run(command, function(err, stdout, stderr) {
if (err || stderr) {
callback(err || new Error(stderr));
} else {
callback(null, stdoutToResultsObject(stdout));
}
});
}
});
};
sgf.cwd = process.cwd();
sgf.debug = false;
sgf.includeContent = false;
// sgf.firstHead = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
sgf.getHead = function(callback) {
run("git rev-parse --verify HEAD", function(err, stdout, stderr) {
if (err && err.message.indexOf("fatal: Needed a single revision")!==-1) {
// callback(null, sgf.firstHead);
run("git hash-object -t tree /dev/null", function(err, stdout, stderr) {
if (err || stderr) {
callback(err || new Error("STDERR: " + stderr));
} else {
stdout = stdout.replace("\n", "");
callback(null, stdout);
}
});
} else if (err || stderr) {
callback(err || new Error("STDERR: " + stderr));
} else {
stdout = stdout.replace("\n", "");
callback(null, stdout);
}
});
}
sgf.readFile = function(filename, options, callback) {
fs.readFile(sgf.cwd + "/" + filename, options, callback);
}
module.exports = sgf;
/** ======================================== HELPERS ======================================== **/
var run = function(command, callback) {
if (sgf.debug) {
console.log("RUNNING: " + command);
}
var bits = command.split(" ");
var args = bits.slice(1);
var cmd = spawn(bits[0], args, {
cwd: module.exports.cwd
});
var stdout = "";
var stderr = "";
cmd.stdout.on('data', function(data){
stdout+=data.toString();
});
cmd.stderr.on('data', function(data){
stderr+=data.toString();
});
cmd.on("close", function(code){
var err = null;
if(code!==0){
err = new Error(stderr);
}
callback(err,stdout,stderr);
});
}
var codeToStatus = function(code) {
/* =======================================================================================================
** PER docs at https://git-scm.com/docs/git-diff-index
** Possible status letters are:
** A: addition of a file
** C: copy of a file into a new one
** D: deletion of a file
** M: modification of the contents or mode of a file
** R: renaming of a file
** T: change in the type of the file
** U: file is unmerged (you must complete the merge before it can be committed)
** X: "unknown" change type (most probably a bug, please report it)
**
** Status letters C and R are always followed by a score
** (denoting the percentage of similarity between the source and target of the move or copy).
** Status letter M may be followed by a score (denoting the percentage of dissimilarity) for file rewrites.
** ======================================================================================================= */
var map = {
"A": "Added",
"C": "Copied",
"D": "Deleted",
"M": "Modified",
"R": "Renamed",
"T": "Type-Change",
"U": "Unmerged",
"X": "Unknown",
"B": "Broken"
}
return map[code.charAt(0)];
}
var stdoutToResultsObject = function(stdout) {
var results = [];
var lines = stdout.split("\n");
var iLines = lines.length;
var files_with_errors = 0;
while (iLines--) {
var line = lines[iLines];
if (line != "") {
var parts = line.split("\t");
var result = {
filename: parts[2] || parts[1],
status: codeToStatus(parts[0])
}
if (sgf.includeContent) {
try {
result.content = fs.readFileSync(sgf.cwd + "/" + result.filename, {
encoding: "utf8"
});
} catch (err) {
result.err = err;
}
}
results.push(result);
}
}
return results;
}