diff --git a/src/js/git/index.js b/src/js/git/index.js index b9e2d694..3047c6af 100644 --- a/src/js/git/index.js +++ b/src/js/git/index.js @@ -210,7 +210,7 @@ GitEngine.prototype.exportTree = function() { _.each(this.branchCollection.toJSON(), function(branch) { branch.target = branch.target.get('id'); - branch.visBranch = undefined; + delete branch.visBranch; totalExport.branches[branch.id] = branch; }); @@ -218,7 +218,7 @@ GitEngine.prototype.exportTree = function() { _.each(this.commitCollection.toJSON(), function(commit) { // clear out the fields that reference objects and create circular structure _.each(Commit.prototype.constants.circularFields, function(field) { - commit[field] = undefined; + delete commit[field]; }, this); // convert parents @@ -239,7 +239,7 @@ GitEngine.prototype.exportTree = function() { }, this); var HEAD = this.HEAD.toJSON(); - HEAD.lastTarget = HEAD.lastLastTarget = HEAD.visBranch = HEAD.visTag =undefined; + HEAD.lastTarget = HEAD.lastLastTarget = HEAD.visBranch = HEAD.visTag = undefined; HEAD.target = HEAD.target.get('id'); totalExport.HEAD = HEAD; @@ -291,23 +291,23 @@ GitEngine.prototype.instantiateFromTree = function(tree) { var createdSoFar = {}; _.each(tree.commits, function(commitJSON) { - var commit = this.getOrMakeRecursive(tree, createdSoFar, commitJSON.id); + var commit = this.getOrMakeRecursive(tree, createdSoFar, commitJSON.id, this.gitVisuals); this.commitCollection.add(commit); }, this); _.each(tree.branches, function(branchJSON) { - var branch = this.getOrMakeRecursive(tree, createdSoFar, branchJSON.id); + var branch = this.getOrMakeRecursive(tree, createdSoFar, branchJSON.id, this.gitVisuals); this.branchCollection.add(branch, {silent: true}); }, this); _.each(tree.tags, function(tagJSON) { - var tag = this.getOrMakeRecursive(tree, createdSoFar, tagJSON.id); + var tag = this.getOrMakeRecursive(tree, createdSoFar, tagJSON.id, this.gitVisuals); this.tagCollection.add(tag, {silent: true}); }, this); - var HEAD = this.getOrMakeRecursive(tree, createdSoFar, tree.HEAD.id); + var HEAD = this.getOrMakeRecursive(tree, createdSoFar, tree.HEAD.id, this.gitVisuals); this.HEAD = HEAD; this.rootCommit = createdSoFar['C0']; @@ -482,7 +482,8 @@ GitEngine.prototype.setLocalToTrackRemote = function(localBranch, remoteBranch) GitEngine.prototype.getOrMakeRecursive = function( tree, createdSoFar, - objID + objID, + gitVisuals ) { if (createdSoFar[objID]) { // base case diff --git a/src/js/graph/index.js b/src/js/graph/index.js index 3040ee02..06f206a0 100644 --- a/src/js/graph/index.js +++ b/src/js/graph/index.js @@ -1,6 +1,94 @@ var _ = require('underscore'); var Graph = { + getOrMakeRecursive: function( + tree, + createdSoFar, + objID, + gitVisuals + ) { + if (createdSoFar[objID]) { + // base case + return createdSoFar[objID]; + } + + var getType = function(tree, id) { + if (tree.commits[id]) { + return 'commit'; + } else if (tree.branches[id]) { + return 'branch'; + } else if (id == 'HEAD') { + return 'HEAD'; + } else if (tree.tags[id]) { + return 'tag'; + } + throw new Error("bad type for " + id); + }; + + // figure out what type + var type = getType(tree, objID); + + if (type == 'HEAD') { + var headJSON = tree.HEAD; + var HEAD = new Ref(_.extend( + tree.HEAD, + { + target: this.getOrMakeRecursive(tree, createdSoFar, headJSON.target) + } + )); + createdSoFar[objID] = HEAD; + return HEAD; + } + + if (type == 'branch') { + var branchJSON = tree.branches[objID]; + + var branch = new Branch(_.extend( + tree.branches[objID], + { + target: this.getOrMakeRecursive(tree, createdSoFar, branchJSON.target) + } + )); + createdSoFar[objID] = branch; + return branch; + } + + if (type == 'tag') { + var tagJSON = tree.tags[objID]; + + var tag = new Tag(_.extend( + tree.tags[objID], + { + target: this.getOrMakeRecursive(tree, createdSoFar, tagJSON.target) + } + )); + createdSoFar[objID] = tag; + return tag; + } + + if (type == 'commit') { + // for commits, we need to grab all the parents + var commitJSON = tree.commits[objID]; + + var parentObjs = []; + _.each(commitJSON.parents, function(parentID) { + parentObjs.push(this.getOrMakeRecursive(tree, createdSoFar, parentID)); + }, this); + + var commit = new Commit(_.extend( + commitJSON, + { + parents: parentObjs, + gitVisuals: this.gitVisuals + } + )); + createdSoFar[objID] = commit; + return commit; + } + + throw new Error('ruh rho!! unsupported type for ' + objID); + }, + descendSortDepth: function(objects) { return objects.sort(function(oA, oB) { return oB.depth - oA.depth;