简体   繁体   中英

Converting git log output into Json array not working(Node.js)

I am trying to obtain log of all the branches and then save it in Json file. My code currently doesn't output an array in my json file. How do I do this? I am very new to node.js and Javascript in general.

var sys = require('sys')
var exec = require('child_process').exec;
var branchesObj;
function puts(error, stdout, stderr) {
console.log(stdout)
var fs = require('fs');
console.log("Creating branches log ..."); 

function getBranches(stdout) {  

branchesObj = JSON.parse(branches);

branches = JSON.stringify(stdout.split(/\r?\n/));
var lines = branches.toString().split('commit');
var results = new Array();
lines.forEach(function(line) {
    var parts = line.split('/\r?\n/');
    results[parts[0]] = parts[1];
});

console.log(results);
fs.writeFile('Branches.json', results);
};
fs.writeFile('Branches.json', stdout);
}
exec("git branch--all", puts);

Currently this is what my JSON file looks like and it doesn't have the brackets around the array eg Branches = [ { }]:

* master
remotes/origin/38563
remotes/origin/53035
remotes/origin/AVA_SAMPSON
remotes/origin/AlexTMP

Here's what you want:

var exec = require('child_process').exec;
var fs = require('fs');

function puts(error, stdout, stderr) { 
   var branches = JSON.stringify(stdout.split(/\r?\n/).map(function(e) { return e.substring(2);}).filter(function(e) { return e; }));
   fs.writeFile('Branches.json', branches);
}
exec("git branch --all", puts);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM