gitdiff-parser
Advanced tools
Comparing version 0.0.1 to 0.0.2
73
index.js
@@ -0,3 +1,6 @@ | ||
/** | ||
* @file gitdiff 消息解析器 | ||
* @author errorrik(errorrik@gmail.com) | ||
*/ | ||
(function (root) { | ||
@@ -12,2 +15,8 @@ var STAT_START = 2; | ||
var parser = { | ||
/** | ||
* 解析 gitdiff 消息 | ||
* | ||
* @param {string} source gitdiff消息内容 | ||
* @return {Object} | ||
*/ | ||
parse: function (source) { | ||
@@ -38,4 +47,13 @@ var infos = []; | ||
}; | ||
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 mode change | ||
@@ -58,24 +76,46 @@ var nextLine = lines[i + 1]; | ||
var simiLine; | ||
while ((simiLine = lines[++i])) { | ||
simiLoop: while ((simiLine = lines[++i])) { | ||
var segs = simiLine.split(' '); | ||
if (segs[0] === 'index') { | ||
var revs = segs[1].split('..'); | ||
currentInfo.oldRevision = revs[0]; | ||
currentInfo.newRevision = revs[1]; | ||
switch (segs[0]) { | ||
case 'diff': // diff --git | ||
i--; | ||
break simiLoop; | ||
if (segs[2]) { | ||
currentInfo.oldMode = currentInfo.newMode = segs[2]; | ||
} | ||
stat = STAT_HUNK; | ||
case 'index': | ||
var revs = segs[1].split('..'); | ||
currentInfo.oldRevision = revs[0]; | ||
currentInfo.newRevision = revs[1]; | ||
if (lines[i + 1].indexOf('---') === 0) { | ||
i += 2; | ||
} | ||
break; | ||
if (segs[2]) { | ||
currentInfo.oldMode = currentInfo.newMode = segs[2]; | ||
} | ||
stat = STAT_HUNK; | ||
var oldFileName = lines[i + 1]; | ||
if (oldFileName.indexOf('---') === 0) { | ||
var newFileName = lines[i + 2]; | ||
if (/\s\/dev\/null$/.test(oldFileName)) { | ||
currentInfo.oldPath = '/dev/null'; | ||
currentInfoType = 'add'; | ||
} | ||
else if (/\s\/dev\/null$/.test(newFileName)) { | ||
currentInfo.newPath = '/dev/null'; | ||
currentInfoType = 'delete'; | ||
} | ||
i += 2; | ||
} | ||
break simiLoop; | ||
} | ||
else if (!currentInfo.type) { | ||
currentInfo.type = segs[0]; | ||
if (!currentInfoType) { | ||
currentInfoType = segs[0]; | ||
} | ||
} | ||
currentInfo.type = currentInfoType || 'modify'; | ||
} | ||
@@ -91,2 +131,3 @@ else if (line.indexOf('Binary') === 0) { | ||
currentHunk = { | ||
content: line, | ||
oldStart: match[1] - 0, | ||
@@ -93,0 +134,0 @@ newStart: match[4] - 0, |
{ | ||
"name": "gitdiff-parser", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4604852
174