Skip to content

Commit

Permalink
Extraced method that applies commitObj to provObj
Browse files Browse the repository at this point in the history
  • Loading branch information
vladistan committed Aug 26, 2014
1 parent 3cfbd85 commit 5246fe4
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 103 deletions.
193 changes: 98 additions & 95 deletions lib/git2provConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,99 @@ function getCommitObj(line) {
return commitObj;
}

function updateProvObj(provObject, urlprefix, commitObj){

var commitEntity = commitObj.entity + "_" + commitObj.id;


// Add the commit activity to the activities object
provObject.activities[urlprefix + ":" + commitObj.id] = {"prov:label" : commitObj.subject};// No need to add the parents as well, since we will eventually loop over them anyway
provObject.starts[urlprefix + ":" + commitObj.id + "_start"] ={
"prov:activity" : urlprefix + ":" + commitObj.id, "prov:time" : commitObj.author_date};
provObject.ends[urlprefix + ":" + commitObj.id + "_end"] = {
"prov:activity" : urlprefix + ":" + commitObj.id, "prov:time" : commitObj.commiter_date};
// Add the commit entities (files) to the entities, specializations and derivations object
provObject.entities[urlprefix + ":" + commitEntity] = {};
provObject.specializations[urlprefix + ":" + commitEntity + "_spec"] = {
"prov:generalEntity" : urlprefix + ":" + commitObj.entity,
"prov:specificEntity" : urlprefix + ":" + commitEntity};
switch(commitObj.modification_type){
case "D":
// The file was deleted in this commit
provObject.invalidations[urlprefix + ":" + commitEntity + "_inv"] = {
"prov:entity": urlprefix + ":" + commitEntity,
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:time": commitObj.author_date
};
break;
case "A":
// The file was added in this commit
provObject.generations[urlprefix + ":" + commitEntity + "_gen"] = {
"prov:entity": urlprefix + ":" + commitEntity,
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:time": commitObj.author_date
};
break;
default:
// The file was modified in this commit
var generation = urlprefix + ":" + commitEntity + "_gen";
provObject.generations[generation] = {
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:entity": urlprefix + ":" + commitEntity,
"prov:time": commitObj.author_date
};

commitObj.parents.forEach(function(parent){
if(parent !== ""){
var parentEntity = commitObj.entity + "_commit-" + parent;
var usage = urlprefix + ":" + parentEntity + "_" + commitObj.id + "_use";
provObject.usages[usage] = {
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:entity": urlprefix + ":" + parentEntity,
"prov:time": commitObj.author_date
};
provObject.derivations[urlprefix + ":" + commitEntity + "_" + parent + "_der"] = {
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:generatedEntity": urlprefix + ":" + commitEntity,
"prov:usedEntity": urlprefix + ":" + parentEntity,
"prov:generation": generation,
"prov:usage": usage
};
provObject.communications[urlprefix + ":" + commitObj.id + "_" + parent + "_comm"] = {
"prov:informant": urlprefix + ":" + "commit-" + parent,
"prov:informed": urlprefix + ":" + commitObj.id
};
}
});
break;
}
// Add the agents to the stack of agents
provObject.agents[urlprefix + ":" + commitObj.author] = {"prov:label" : commitObj.author_label};
// The file is definitly attributed to the author
provObject.attributions[urlprefix + ":" + commitObj.entity + "_" + commitObj.id + "_" + commitObj.author + "_attr"] = {
"prov:entity" : urlprefix + ":" + commitEntity,
"prov:agent" : urlprefix + ":" + commitObj.author,
"prov:type" : "authorship"
};
// And he/she is definitely associated with the commit activity
provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.author + "_assoc"] = {
"prov:activity" : urlprefix + ":" + commitObj.id,
"prov:agent" : urlprefix + ":" + commitObj.author,
"prov:role" : "author"
};
provObject.agents[urlprefix + ":" + commitObj.commiter] = {"prov:label" : commitObj.commiter_label};
// We can't say that the file was attributed to the committer, but we can associate the commit activity with him/her
if(provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.commiter + "_assoc"]){
provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.commiter + "_assoc"]["prov:role"] += ", committer";
} else {
provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.commiter + "_assoc"] = {
"prov:activity" : urlprefix + ":" + commitObj.id,
"prov:agent" : urlprefix + ":" + commitObj.commiter,
"prov:role" : "committer"
};
}
}

/* We convert git concepts to PROV concepts as follows:
commit c with subject s ---> activity(c, [prov:label="s"])
Expand All @@ -107,8 +200,8 @@ function getCommitObj(line) {
file f_c deleted in commit c ---> wasInvalidatedBy(f_c, c, authordate)
*/
function convertRepositoryToProv(repositoryPath, serialization, requestUrl, options, callback) {
// set the corresponding variables according to the options
var commitHash = options['shortHashes']?"%h":"%H";
// set the corresponding variables according to the options
var commitHash = options['shortHashes']?"%h":"%H";
var parentHash = options['shortHashes']?"%p":"%P";
var ignore = options['ignore']?options['ignore']:[];
// determine a QName for the bundle
Expand Down Expand Up @@ -145,98 +238,7 @@ function convertRepositoryToProv(repositoryPath, serialization, requestUrl, opti
// For some reason, git log appends empty lines here and there. Let's fitler them out.
lines = lines.filter(function(element, index, array) { return element !== ""; });
lines.forEach(function(line){

var commitObj = getCommitObj(line);
var commitEntity = commitObj.entity + "_" + commitObj.id;


// Add the commit activity to the activities object
provObject.activities[urlprefix + ":" + commitObj.id] = {"prov:label" : commitObj.subject};// No need to add the parents as well, since we will eventually loop over them anyway
provObject.starts[urlprefix + ":" + commitObj.id + "_start"] ={
"prov:activity" : urlprefix + ":" + commitObj.id, "prov:time" : commitObj.author_date};
provObject.ends[urlprefix + ":" + commitObj.id + "_end"] = {
"prov:activity" : urlprefix + ":" + commitObj.id, "prov:time" : commitObj.commiter_date};
// Add the commit entities (files) to the entities, specializations and derivations object
provObject.entities[urlprefix + ":" + commitEntity] = {};
provObject.specializations[urlprefix + ":" + commitEntity + "_spec"] = {
"prov:generalEntity" : urlprefix + ":" + commitObj.entity,
"prov:specificEntity" : urlprefix + ":" + commitEntity};
switch(commitObj.modification_type){
case "D":
// The file was deleted in this commit
provObject.invalidations[urlprefix + ":" + commitEntity + "_inv"] = {
"prov:entity": urlprefix + ":" + commitEntity,
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:time": commitObj.author_date
};
break;
case "A":
// The file was added in this commit
provObject.generations[urlprefix + ":" + commitEntity + "_gen"] = {
"prov:entity": urlprefix + ":" + commitEntity,
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:time": commitObj.author_date
};
break;
default:
// The file was modified in this commit
var generation = urlprefix + ":" + commitEntity + "_gen";
provObject.generations[generation] = {
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:entity": urlprefix + ":" + commitEntity,
"prov:time": commitObj.author_date
};

commitObj.parents.forEach(function(parent){
if(parent !== ""){
var parentEntity = commitObj.entity + "_commit-" + parent;
var usage = urlprefix + ":" + parentEntity + "_" + commitObj.id + "_use";
provObject.usages[usage] = {
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:entity": urlprefix + ":" + parentEntity,
"prov:time": commitObj.author_date
};
provObject.derivations[urlprefix + ":" + commitEntity + "_" + parent + "_der"] = {
"prov:activity": urlprefix + ":" + commitObj.id,
"prov:generatedEntity": urlprefix + ":" + commitEntity,
"prov:usedEntity": urlprefix + ":" + parentEntity,
"prov:generation": generation,
"prov:usage": usage
};
provObject.communications[urlprefix + ":" + commitObj.id + "_" + parent + "_comm"] = {
"prov:informant": urlprefix + ":" + "commit-" + parent,
"prov:informed": urlprefix + ":" + commitObj.id
};
}
});
break;
}
// Add the agents to the stack of agents
provObject.agents[urlprefix + ":" + commitObj.author] = {"prov:label" : commitObj.author_label};
// The file is definitly attributed to the author
provObject.attributions[urlprefix + ":" + commitObj.entity + "_" + commitObj.id + "_" + commitObj.author + "_attr"] = {
"prov:entity" : urlprefix + ":" + commitEntity,
"prov:agent" : urlprefix + ":" + commitObj.author,
"prov:type" : "authorship"
};
// And he/she is definitely associated with the commit activity
provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.author + "_assoc"] = {
"prov:activity" : urlprefix + ":" + commitObj.id,
"prov:agent" : urlprefix + ":" + commitObj.author,
"prov:role" : "author"
};
provObject.agents[urlprefix + ":" + commitObj.commiter] = {"prov:label" : commitObj.commiter_label};
// We can't say that the file was attributed to the committer, but we can associate the commit activity with him/her
if(provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.commiter + "_assoc"]){
provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.commiter + "_assoc"]["prov:role"] += ", committer";
} else {
provObject.associations[urlprefix + ":" + commitObj.id + "_" + commitObj.commiter + "_assoc"] = {
"prov:activity" : urlprefix + ":" + commitObj.id,
"prov:agent" : urlprefix + ":" + commitObj.commiter,
"prov:role" : "committer"
};
}
});
updateProvObj(provObject, urlprefix, getCommitObj(line)); });
if (logCmds.length) {
executeLogCmd(logCmds.pop());
}
Expand All @@ -250,4 +252,5 @@ function convertRepositoryToProv(repositoryPath, serialization, requestUrl, opti

exports.convert = convert;
exports.getProvObject = getProvObject;
exports.getCommitObj = getCommitObj;
exports.getCommitObj = getCommitObj;
exports.updateProvObj = updateProvObj;
59 changes: 51 additions & 8 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ var serializers = require('../lib/provSerializer');

describe('Unit', function(){

describe('ProvSerializer ShouldBeAdded', function() {
var log_line1 = "Mac-open-c,9ff4ed4636c15aa04c262f9e89c52d451a2c1e6d,21fd9dde98de5b31f4ca89fe0347b44fd2a1997a," +
"jill,2014-08-14 09:42:05 -0400,vlad,2014-08-14 11:50:51 -0400,Modernized and harmonized the code,D";
var log_line2 = "Mac-open-c,e01161193fe24e3c8f08a6d80c27ea33dac0c162,x y,bob,2014-08-12 20:06:41 -0400," +
"john,2014-08-12 20:06:41 -0400,Initial import,A";


describe('ProvSerializer ShouldBeAdded', function() {
var testColl = { 'a': 1, 'd': 23, 'bill': 9 };
var ignoreBlock = "bob;bill,dope";
it('Should allow to add property for which we have entry', function() {
Expand Down Expand Up @@ -408,23 +414,58 @@ describe('Unit', function(){

describe('ProvObject', function() {



it('should be constructed and have all necessary sections', function(){
var p = git2provCvt.getProvObject([],"http:/x");
assert.equal(p.bundle, "http:/x:provenance");
assert.deepEqual(p.entities, {});
});

describe('Update based on commit obj', function(){

var cm1 = git2provCvt.getCommitObj(log_line1);
var cm2 = git2provCvt.getCommitObj(log_line2);

var pObj = git2provCvt.getProvObject([],"http://y");

before(function(){
git2provCvt.updateProvObj(pObj,"http://y",cm1);
git2provCvt.updateProvObj(pObj,"http://y",cm2);
});

it('Should contain 2 commits in the activity section', function(){
assert.deepEqual(Object.keys(pObj.activities).sort(),[
"http://y:commit-9ff4ed4636c15aa04c262f9e89c52d451a2c1e6d",
"http://y:commit-e01161193fe24e3c8f08a6d80c27ea33dac0c162"
]);
});

it('Should contain 2 files in the entities section', function(){
assert.deepEqual(Object.keys(pObj.entities).sort(),[
"http://y:file-Mac-open-c_commit-9ff4ed4636c15aa04c262f9e89c52d451a2c1e6d",
"http://y:file-Mac-open-c_commit-e01161193fe24e3c8f08a6d80c27ea33dac0c162"
]);
});

it('Should contain 2 starts in the starts section', function(){
assert.deepEqual(Object.keys(pObj.starts).sort(),[
"http://y:commit-9ff4ed4636c15aa04c262f9e89c52d451a2c1e6d_start",
"http://y:commit-e01161193fe24e3c8f08a6d80c27ea33dac0c162_start"
]);
});



});

});

describe('Commit Obj', function(){

var line1 = "Mac-open-c,9ff4ed4636c15aa04c262f9e89c52d451a2c1e6d,21fd9dde98de5b31f4ca89fe0347b44fd2a1997a," +
"jill,2014-08-14 09:42:05 -0400,vlad,2014-08-14 11:50:51 -0400,Modernized and harmonized the code,D";
var line2 = "Mac-open-c,e01161193fe24e3c8f08a6d80c27ea33dac0c162,x y,bob,2014-08-12 20:06:41 -0400," +
"john,2014-08-12 20:06:41 -0400,Initial import,A";

var obj1 = git2provCvt.getCommitObj(line1);
var obj2 = git2provCvt.getCommitObj(line2);
var obj1 = git2provCvt.getCommitObj(log_line1);
var obj2 = git2provCvt.getCommitObj(log_line2);

it("should contain entity id", function() {
assert.equal(obj1.entity, "file-Mac-open-c");
Expand Down Expand Up @@ -475,7 +516,7 @@ describe('Unit', function(){
assert.equal(obj1.subject, "Modernized and harmonized the code");
assert.equal(obj2.subject, "Initial import");
});

it("should contain mod type", function() {
assert.equal(obj1.modification_type, "D");
assert.equal(obj2.modification_type, "A");
Expand All @@ -487,6 +528,8 @@ describe('Unit', function(){
});




});


Expand Down

0 comments on commit 5246fe4

Please sign in to comment.