Skip to content

Commit

Permalink
Merge branch 'feature/armstrong'
Browse files Browse the repository at this point in the history
  • Loading branch information
U-epsilon10\blecher committed Nov 4, 2015
2 parents efb77ce + 4371640 commit c3e6239
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 0 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name":"git2json",
"version":"0.0.1",
"description": "convert git log to json",
"author": "stephan blecher <stephan@blecher.at>",
"keywords": [ "git", "soap" ],
"bin": { "git-json": "src/main/cli.js" },
"main": "src/index.js",
"scripts": {
"test": "src/test/index.js",
"start": "src/main/server.js" },
"preferGlobal": true,
"private": true
}
37 changes: 37 additions & 0 deletions src/gitlogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Compatibility to browser js without node
if(module == undefined) { var exports = {}}


exports.retrieve = function(limit, callback) {
var exec = require('child_process').exec;
var git_params = {
'sha': 'H',
'ssha': 'h', // abbreviated hash
'parenthashes': 'P',
'authorname': 'an',
'authoremail': 'ae',
'authordate': 'at',
'committername': 'cn',
'committeremail': 'ce',
'committerdate': 'ct',
'encoding': 'e',
'subject': 's',
'ssubject': 'f',
'body': 'b',
'refnames': 'd'
};

var pattern = "";

// Build git log format.
Object.keys(git_params).forEach(function(key) {
var val = git_params[key];
pattern += "%H-"+key+" %"+val+ "/%H%n"
});

// Run git log and push output to callback - TODO: streaming
exec('git log --all -n '+limit+' --pretty="'+pattern+'"', function (error, stdout, stderr) {
callback(stdout)
});
};

6 changes: 6 additions & 0 deletions src/gitparser-plugin-parents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function (key, value) {
if(key == 'parenthashes') {
if(value.trim().length == 0) return []
else return value.trim().split(" ")
} else return value
}
9 changes: 9 additions & 0 deletions src/gitparser-plugin-refnames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

/* this parses the reflog entry of git %d in format " (head1, head2)" to an array. */
module.exports = function (key, value) {
if(key == "refnames") {
if(value == '') return [];
value = value.replace(/^ \(/,'').replace(/\)$/,'').split(", ")
}
return value;
}
37 changes: 37 additions & 0 deletions src/gitparser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Simple parser for the log created by the gitlogger module.
Entries are in format "HASH-key value/HASH". this is used to make the parser immune to any special characters in the commit message.
*/

module.exports = function(text, plugins) {
var commits = {}
var index = 0
var length = text.length
var HL=40

while(text.length > 10) {
// find hash
var hash = text.substring(0, HL);
var end = text.indexOf("/"+hash);
var kvsep = text.indexOf(" ");

var key = text.substring(HL+1, kvsep);
var value = text.substring(kvsep+1, end);

var commit = commits[hash];
if(commit == undefined) {
commit = commits[hash] = {};
}

// Apply any transformation plugins on the value
if(plugins) plugins.forEach(function(f) { value = f(key, value)});
commit[key] = value;

// move pointer to the next entry
var index = end + HL + 1;
text = text.substring(index).trim();
}

return commits
}

32 changes: 32 additions & 0 deletions src/gitprocessor-plugin-heads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* module to link heads to all parents */
module.exports = function(commits) {

Object.keys(commits).forEach(function(hash) {
var commit = commits[hash];
commit.inHeads = []
})

Object.keys(commits).forEach(function(hash) {
var commit = commits[hash];
if(commit.refnames.length > 0) {
assignHeads(commits, commit)
}
})
}

function assignHeads(commits, commit) {
var parents1 = commit.parenthashes.slice(0) // copy array

while(parents1.length > 0) {
var newParents = []
parents1.forEach(function(parentHash) {
var p = commits[parentHash]
if(p != undefined) {
p.inHeads.push(commit.commithash)
// add all grandparents to the newparents
newParents = newParents.concat(p.parenthashes)
}
});
parents1 = newParents
}
}
9 changes: 9 additions & 0 deletions src/gitprocessor-plugin-index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* module to assign index */
module.exports = function(commits) {

var index = 0;
Object.keys(commits).forEach(function(hash) {
var commit = commits[hash];
commit.index = index++;
})
}
20 changes: 20 additions & 0 deletions src/gitprocessor-plugin-relations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* module to link commits to each other */

module.exports = function(commits) {
Object.keys(commits).forEach(function(hash) {
var commit = commits[hash];
commit.parents = [];
commit.children = [];
});


Object.keys(commits).forEach(function(hash) {
var commit = commits[hash];
commit.parenthashes.forEach(function(parenthash) {
var parentCommit = commits[parenthash];
if(parentCommit != undefined) {
parentCommit.children.push(hash);
}
});
});
}
12 changes: 12 additions & 0 deletions src/gitprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
processor. only processes all processor plugins
*/
module.exports = function(commits, plugins) {
// Apply any processing plugins on the value
if(plugins) plugins.forEach(function(f) { value = f(commits)});

Object.keys(commits).forEach(function(hash) {
var commit = commits[hash];
})
};

34 changes: 34 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

var gitlogger = require('./gitlogger.js')
var gitparser = require('./gitparser.js')
var gitprocessor = require('./gitprocessor.js')
var defaultParserplugins = [
require('./gitparser-plugin-refnames.js'),
require('./gitparser-plugin-parents.js') ]

var defaultProcessorplugins = [
require('./gitprocessor-plugin-relations.js'),
require('./gitprocessor-plugin-heads.js'),
require('./gitprocessor-plugin-index.js') ]

module.exports = {
logger : gitlogger,
parser : gitparser,
processor : gitprocessor,
parserplugins : defaultParserplugins,
processorplugins : defaultProcessorplugins,

run : function(callback, maxCommits) {
var parserplugins = this.parserplugins;
var processorplugins = this.processorplugins;
var loggerCallback = function(text) {
var commits = gitparser(text, parserplugins)
gitprocessor(commits, processorplugins);

callback(commits);
}

gitlogger.retrieve(maxCommits || 300, loggerCallback)
}
}

8 changes: 8 additions & 0 deletions src/main/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node

var git2json = require("../index.js")

git2json.run(function(commits) {
console.log("%s", JSON.stringify(commits, null, 2));
})

11 changes: 11 additions & 0 deletions src/test/smoke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

var git2json = require('../index.js')

var loggerCallback = function(text) {
var commits = git2json.gitparser(text, git2json.defaultParserplugins)
git2json.gitprocessor(commits, git2json.defaultProcessorplugins);

console.log("commit: %s", JSON.stringify(commits, null, 2));
}

git2json.gitlogger.retrieve(30, loggerCallback)
35 changes: 35 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

- git logger -> load history into json model
{
'commit-hash': 'H',
'parent-hash': 'T',
'author-name': 'an',
'author-email': 'ae',
'author-date': 'at',
'committer-name': 'cn',
'committer-email': 'ce',
'committer-date': 'ct',
'encoding': 'e',
'subject': 's',
'sanitized-subject': 'f',
'body': 'b',
}




- git parser -> parse history and figure out branches, relations
- serverside


- renderer - render history
- render branches
- add button to checkout
- render commit
- plugin able. add button for merge,
- render relation



https://github.com/nickola/web-console
https://github.com/jcubic/jquery.terminal

0 comments on commit c3e6239

Please sign in to comment.