Skip to content

Instantly share code, notes, and snippets.

@RyanHirsch
Created November 1, 2013 10:41
Show Gist options
  • Save RyanHirsch/7263696 to your computer and use it in GitHub Desktop.
Save RyanHirsch/7263696 to your computer and use it in GitHub Desktop.
SVN Parser via Streams
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');
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;
@RyanHirsch
Copy link
Author

If I use svn_parse in getLatest(), it shows me the first repo message twice. If I do it as implemented above, I get the second repo message twice. How can I get this to work as I intend?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment