-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (166 loc) · 4.97 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
195
// From https://github.com/mcwhittemore/staged-git-files/blob/master/index.js
var spawn = require('child_process').spawn
var execSync = require('child_process').execSync
var fs = require('fs')
var nps = require('path')
var decode = require('./decode')
var sgf = function (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
var filter = options.filter || 'ACDMRTUXB',
head = options.head,
relative = typeof options.relative === 'undefined' ? true : options.relative
function coreRun(head) {
run('git version', function (err, stdout, stderr) {
var s = ''
var useDecode = false
if (stdout && /git version ([^\s]+)/.test(stdout)) {
if (RegExp.$1.split('.')[0] > 1) {
s = '-c core.quotepath=false '
}
else {
// Compatible with git version 1
useDecode = true
}
}
var command = 'git ' + s + 'diff --name-status'
if (relative === true) {
command += ' --relative'
} else if (typeof relative === 'string') {
command += ' --relative=' + nps.join(sgf.cwd, 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, useDecode))
}
})
})
}
if (!head) {
sgf.getHead(function (err, head) {
if (err) {
callback(err)
} else {
coreRun(head)
}
})
}
else {
coreRun(head)
}
}
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)
} 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: sgf.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 (stderr && 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, useDecode) {
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 (useDecode) {
result.filename = decode(result.filename)
}
if (sgf.includeContent) {
try {
result.content = fs.readFileSync(sgf.cwd + '/' + result.filename, {
encoding: 'utf8'
})
} catch (err) {
result.err = err
}
}
results.push(result)
}
}
return results
}