我正在尝试通过流解析 svn 提交日志。我的东西适用于一个存储库,但如果我尝试做两个不同的存储库,它就不会按我的意愿工作。如果我在 getLatest() 中使用 svn_parse,它会向我显示第一条 repo 消息两次。如果我按照下面的方式进行操作,我会两次收到第二条 repo 消息。我怎样才能让它按我的意图工作?
var svn_parse = require('./svn_stream_parser'),
spawn = require('child_process').spawn;
function getLatest(repo) {
var sp = require('./svn_stream_parser');
var svn = spawn('svn', ['log', '-v', '-l', '1', repo]);
sp(svn.stdout).pipe(process.stdout);
}
getLatest('https://github.com/emberjs/ember.js');
getLatest('https://github.com/emberjs/starter-kit');
svn_stream_parser.js:
var BufferStream = require('bufferstream'),
through = require('through'),
_ = require('lodash');
var bufferstream = new BufferStream({encoding:'utf8', size:'flexible'});
bufferstream.split('------------------------------------------------------------------------\n');
bufferstream.on('split', function (chunk) {
bufferstream.emit('data', chunk)
});
function parseStream(istream) {
return istream.pipe(bufferstream)
.pipe(through(function(chunk) {
var str = chunk.toString();
if(str.trim().length === 0) return;
var commitObj = parseCommit(str);
this.queue(JSON.stringify(commitObj) + '\n');
}));
}
function parseCommit(commit) {
var commitSplit = commit.split('\n');
var summarySplit = commitSplit.shift().split('|');
var commitObject = {
/* repository: repositoryUrl,*/
revision: summarySplit[0].trim().substring(1),
author: summarySplit[1].trim(),
timestamp: summarySplit[2].trim(),
files: [],
messages: []
};
var idx = 0;
if(commitSplit[0].trim() === 'Changed paths:') {
// Start at 1 because 0 is expected to be an empty string
// inserted for formatting
for(idx = 1; idx < commitSplit.length; idx++) {
var fileInfo = commitSplit[idx].trim();
if(fileInfo.length === 0) {
break; // File change block is contiguous, empty line means the block is done
}
commitObject.files.push({
operation: fileInfo.substring(0,1),
path: fileInfo.substring(2)
});
}
}
// Parse the remaining lines, they should all be the commit message
_.forEach(commitSplit.slice(idx + 1), function(msg) {
msg = msg.trim();
if(msg.length > 0) {
commitObject.messages.push(msg);
}
});
return commitObject;
}
module.exports = parseStream;