forked from ecomfe/gitdiff-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (181 loc) · 8.32 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* @file gitdiff 消息解析器
* @author errorrik([email protected])
*/
(function (root) {
var STAT_START = 2;
var STAT_FILE_META = 3;
var STAT_HUNK = 5;
var parser = {
/**
* 解析 gitdiff 消息
*
* @param {string} source gitdiff消息内容
* @return {Object}
*/
parse: function (source) {
var infos = [];
var stat = STAT_START;
var currentInfo;
var currentHunk;
var changeOldLine;
var changeNewLine;
var lines = source.split('\n');
var linesLen = lines.length;
var i = 0;
while (i < linesLen) {
var line = lines[i];
if (line.indexOf('diff --git') === 0) {
// read file
currentInfo = {
hunks: [],
oldEndingNewLine: true,
newEndingNewLine: true
};
infos.push(currentInfo);
// 1. 如果oldPath是/dev/null就是add
// 2. 如果newPath是/dev/null就是delete
// 3. 如果有 rename from foo.js 这样的就是rename
// 4. 如果有 copy from foo.js 这样的就是copy
// 5. 其它情况是modify
var currentInfoType = null;
// read type and index
var simiLine;
simiLoop: while ((simiLine = lines[++i])) {
var spaceIndex = simiLine.indexOf(' ');
var infoType = spaceIndex > -1 ? simiLine.slice(0, spaceIndex) : infoType;
switch (infoType) {
case 'diff': // diff --git
i--;
break simiLoop;
case 'deleted':
case 'new':
var leftStr = simiLine.slice(spaceIndex + 1);
if (leftStr.indexOf('file mode') === 0) {
currentInfo[infoType === 'new' ? 'newMode' : 'oldMode'] = leftStr.slice(10);
}
break;
case 'similarity':
currentInfo.similarity = parseInt(simiLine.split(' ')[2], 10);
break;
case 'index':
var segs = simiLine.slice(spaceIndex + 1).split(' ');
var revs = segs[0].split('..');
currentInfo.oldRevision = revs[0];
currentInfo.newRevision = revs[1];
if (segs[1]) {
currentInfo.oldMode = currentInfo.newMode = segs[1];
}
break;
case 'copy':
case 'rename':
var infoStr = simiLine.slice(spaceIndex + 1);
if (infoStr.indexOf('from') === 0) {
currentInfo.oldPath = infoStr.slice(5);
}
else { // rename to
currentInfo.newPath = infoStr.slice(3);
}
currentInfoType = infoType;
break;
case '---':
var oldPath = simiLine.slice(spaceIndex + 1);
var newPath = lines[++i].slice(4); // next line must be "+++ xxx"
if (oldPath === '/dev/null') {
newPath = newPath.slice(2);
currentInfoType = 'add';
}
else if (newPath === '/dev/null') {
oldPath = oldPath.slice(2);
currentInfoType = 'delete';
} else {
currentInfoType = 'modify';
oldPath = oldPath.slice(2);
newPath = newPath.slice(2);
}
currentInfo.oldPath = oldPath;
currentInfo.newPath = newPath;
stat = STAT_HUNK;
break simiLoop;
}
}
currentInfo.type = currentInfoType || 'modify';
}
else if (line.indexOf('Binary') === 0) {
currentInfo.isBinary = true;
currentInfo.type = line.indexOf('/dev/null and') >= 0
? 'add'
: (line.indexOf('and /dev/null') >= 0 ? 'delete' : 'modify');
stat = STAT_START;
currentInfo = null;
}
else if (stat === STAT_HUNK) {
if (line.indexOf('@@') === 0) {
var match = /^@@\s+-([0-9]+)(,([0-9]+))?\s+\+([0-9]+)(,([0-9]+))?/.exec(line)
currentHunk = {
content: line,
oldStart: match[1] - 0,
newStart: match[4] - 0,
oldLines: match[3] - 0 || 1,
newLines: match[6] - 0 || 1,
changes: []
};
currentInfo.hunks.push(currentHunk);
changeOldLine = currentHunk.oldStart;
changeNewLine = currentHunk.newStart;
}
else {
var typeChar = line.slice(0, 1);
var change = {
content: line.slice(1)
};
switch (typeChar) {
case '+':
change.type = 'insert';
change.isInsert = true;
change.lineNumber = changeNewLine;
changeNewLine++;
break;
case '-':
change.type = 'delete';
change.isDelete = true;
change.lineNumber = changeOldLine;
changeOldLine++;
break;
case ' ':
change.type = 'normal';
change.isNormal = true;
change.oldLineNumber = changeOldLine;
change.newLineNumber = changeNewLine;
changeOldLine++;
changeNewLine++;
break;
case '\\': // Seems "no newline" is the only case starting with /
var lastChange = currentHunk.changes[currentHunk.changes.length - 1];
if (!lastChange.isDelete) {
currentInfo.newEndingNewLine = false;
}
if (!lastChange.isInsert) {
currentInfo.oldEndingNewLine = false;
}
}
change.type && currentHunk.changes.push(change);
}
}
i++;
}
return infos;
}
};
if (typeof exports === 'object' && typeof module === 'object') {
// For CommonJS
exports = module.exports = parser;
}
else if (typeof define === 'function' && define.amd) {
// For AMD
define('gitDiffParser', [], parser);
}
else {
root.gitDiffParser = parser;
}
})(this);