tree compare refactored

This commit is contained in:
Peter Cottle 2013-06-08 12:37:44 -10:00
parent 89377b2956
commit 89fa08e149
5 changed files with 87 additions and 96 deletions

View file

@ -1,18 +1,17 @@
var _ = require('underscore');
// static class...
function TreeCompare() {
var TreeCompare = {};
}
TreeCompare.prototype.compareAllBranchesWithinTreesAndHEAD = function(treeA, treeB) {
// would love to have copy properties here.. :(
TreeCompare.compareAllBranchesWithinTreesAndHEAD = function(treeA, treeB) {
treeA = this.convertTreeSafe(treeA);
treeB = this.convertTreeSafe(treeB);
return treeA.HEAD.target == treeB.HEAD.target && this.compareAllBranchesWithinTrees(treeA, treeB);
};
TreeCompare.prototype.compareAllBranchesWithinTrees = function(treeA, treeB) {
TreeCompare.compareAllBranchesWithinTrees = function(treeA, treeB) {
treeA = this.convertTreeSafe(treeA);
treeB = this.convertTreeSafe(treeB);
@ -29,7 +28,7 @@ TreeCompare.prototype.compareAllBranchesWithinTrees = function(treeA, treeB) {
return result;
};
TreeCompare.prototype.compareBranchesWithinTrees = function(treeA, treeB, branches) {
TreeCompare.compareBranchesWithinTrees = function(treeA, treeB, branches) {
var result = true;
_.each(branches, function(branchName) {
result = result && this.compareBranchWithinTrees(treeA, treeB, branchName);
@ -38,7 +37,7 @@ TreeCompare.prototype.compareBranchesWithinTrees = function(treeA, treeB, branch
return result;
};
TreeCompare.prototype.compareBranchWithinTrees = function(treeA, treeB, branchName) {
TreeCompare.compareBranchWithinTrees = function(treeA, treeB, branchName) {
treeA = this.convertTreeSafe(treeA);
treeB = this.convertTreeSafe(treeB);
this.reduceTreeFields([treeA, treeB]);
@ -51,7 +50,7 @@ TreeCompare.prototype.compareBranchWithinTrees = function(treeA, treeB, branchNa
recurseCompare(treeA.commits[branchA.target], treeB.commits[branchB.target]);
};
TreeCompare.prototype.compareAllBranchesWithinTreesHashAgnostic = function(treeA, treeB) {
TreeCompare.compareAllBranchesWithinTreesHashAgnostic = function(treeA, treeB) {
treeA = this.convertTreeSafe(treeA);
treeB = this.convertTreeSafe(treeB);
this.reduceTreeFields([treeA, treeB]);
@ -67,7 +66,7 @@ TreeCompare.prototype.compareAllBranchesWithinTreesHashAgnostic = function(treeA
return this.compareBranchesWithinTreesHashAgnostic(treeA, treeB, branchNames);
};
TreeCompare.prototype.compareBranchesWithinTreesHashAgnostic = function(treeA, treeB, branches) {
TreeCompare.compareBranchesWithinTreesHashAgnostic = function(treeA, treeB, branches) {
// we can't DRY unfortunately here because we need a special _.isEqual function
// for both the recursive compare and the branch compare
treeA = this.convertTreeSafe(treeA);
@ -102,7 +101,7 @@ TreeCompare.prototype.compareBranchesWithinTreesHashAgnostic = function(treeA, t
return result;
};
TreeCompare.prototype.evalAsserts = function(tree, assertsPerBranch) {
TreeCompare.evalAsserts = function(tree, assertsPerBranch) {
var result = true;
_.each(assertsPerBranch, function(asserts, branchName) {
result = result && this.evalAssertsOnBranch(tree, branchName, asserts);
@ -112,7 +111,7 @@ TreeCompare.prototype.evalAsserts = function(tree, assertsPerBranch) {
return result;
};
TreeCompare.prototype.evalAssertsOnBranch = function(tree, branchName, asserts) {
TreeCompare.evalAssertsOnBranch = function(tree, branchName, asserts) {
tree = this.convertTreeSafe(tree);
// here is the outline:
@ -151,7 +150,7 @@ TreeCompare.prototype.evalAssertsOnBranch = function(tree, branchName, asserts)
return result;
};
TreeCompare.prototype.getNumHashes = function(ref) {
TreeCompare.getNumHashes = function(ref) {
var regexMap = [
[/^C(\d+)([']{0,3})$/, function(bits) {
if (!bits[2]) {
@ -175,7 +174,7 @@ TreeCompare.prototype.getNumHashes = function(ref) {
throw new Error('coudlnt parse ref ' + ref);
};
TreeCompare.prototype.getBaseRef = function(ref) {
TreeCompare.getBaseRef = function(ref) {
var idRegex = /^C(\d+)/;
var bits = idRegex.exec(ref);
if (!bits) { throw new Error('no regex matchy for ' + ref); }
@ -184,7 +183,7 @@ TreeCompare.prototype.getBaseRef = function(ref) {
return 'C' + bits[1];
};
TreeCompare.prototype.getRecurseCompareHashAgnostic = function(treeA, treeB) {
TreeCompare.getRecurseCompareHashAgnostic = function(treeA, treeB) {
// here we pass in a special comparison function to pass into the base
// recursive compare.
@ -210,7 +209,7 @@ TreeCompare.prototype.getRecurseCompareHashAgnostic = function(treeA, treeB) {
return this.getRecurseCompare(treeA, treeB, {isEqual: isEqual});
};
TreeCompare.prototype.getRecurseCompare = function(treeA, treeB, options) {
TreeCompare.getRecurseCompare = function(treeA, treeB, options) {
options = options || {};
// we need a recursive comparison function to bubble up the branch
@ -243,14 +242,14 @@ TreeCompare.prototype.getRecurseCompare = function(treeA, treeB, options) {
return recurseCompare;
};
TreeCompare.prototype.convertTreeSafe = function(tree) {
TreeCompare.convertTreeSafe = function(tree) {
if (typeof tree == 'string') {
return JSON.parse(unescape(tree));
}
return tree;
};
TreeCompare.prototype.reduceTreeFields = function(trees) {
TreeCompare.reduceTreeFields = function(trees) {
var commitSaveFields = [
'parents',
'id',
@ -296,7 +295,7 @@ TreeCompare.prototype.reduceTreeFields = function(trees) {
});
};
TreeCompare.prototype.compareTrees = function(treeA, treeB) {
TreeCompare.compareTrees = function(treeA, treeB) {
treeA = this.convertTreeSafe(treeA);
treeB = this.convertTreeSafe(treeB);