2013-11-01 93 views
1

我想通過流解析svn提交日誌。我有一個倉庫的工作,但如果我嘗試做兩個不同的倉庫,它不工作,因爲我打算。如果我在getLatest()中使用svn_parse,它會顯示兩次第一個回購消息。如果我按照以下方式執行操作,則會再次獲得第二個回購消息。我怎麼才能讓它按照我的意圖工作?SVN日誌解析通過流

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; 

回答

1

問題是我bufferstream是如何delcared,應該是

function parseStream(istream) { 
    var bufferstream = new BufferStream({encoding:'utf8', size:'flexible'}); 
    bufferstream.split('------------------------------------------------------------------------\n'); 
    bufferstream.on('split', function (chunk) { 
    bufferstream.emit('data', chunk) 
    }); 

    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'); 
    })); 
}