Iterate on #698 to solve #697

This commit is contained in:
Peter Cottle 2020-04-27 19:32:03 -07:00
parent 270167189f
commit d5ba50c97d

View file

@ -78,82 +78,83 @@ TreeCompare.dispatchShallow = function(levelBlob, goalTreeString, treeToCompare)
}; };
// would love to have copy properties here.. :( // would love to have copy properties here.. :(
TreeCompare.compareAllBranchesWithinTreesAndHEAD = function(treeA, treeB) { TreeCompare.compareAllBranchesWithinTreesAndHEAD = function(treeToCompare, goalTree) {
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
// also compare tags!! for just one level // also compare tags!! for just one level
return treeA.HEAD.target === treeB.HEAD.target && return treeToCompare.HEAD.target === goalTree.HEAD.target &&
this.compareAllBranchesWithinTrees(treeA, treeB) && this.compareAllBranchesWithinTrees(treeToCompare, goalTree) &&
this.compareAllTagsWithinTrees(treeA, treeB); this.compareAllTagsWithinTrees(treeToCompare, goalTree);
}; };
TreeCompare.compareAllBranchesWithinTrees = function(treeA, treeB) { TreeCompare.compareAllBranchesWithinTrees = function(treeToCompare, goalTree) {
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
var allBranches = Object.assign( /**
{}, * Disclaimer / reminder!! We only care about branches in the goal tree;
treeA.branches, * if you have extra branches in your source tree thats ok. but that means
treeB.branches * the arguments here are important -- always call this function with
); * goalTree being the latter argument, since we will discard extra branches
* from treeToCompare (the first argument).
return Object.keys(allBranches).every(function(branch) { */
return this.compareBranchWithinTrees(treeA, treeB, branch); return Object.keys(goalTree.branches).every(function(branch) {
return this.compareBranchWithinTrees(treeToCompare, goalTree, branch);
}.bind(this)); }.bind(this));
}; };
TreeCompare.compareAllTagsWithinTrees = function(treeA, treeB) { TreeCompare.compareAllTagsWithinTrees = function(treeToCompare, goalTree) {
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
this.reduceTreeFields([treeA, treeB]); this.reduceTreeFields([treeToCompare, goalTree]);
return _.isEqual(treeA.tags, treeB.tags); return _.isEqual(treeToCompare.tags, goalTree.tags);
}; };
TreeCompare.compareBranchesWithinTrees = function(treeA, treeB, branches) { TreeCompare.compareBranchesWithinTrees = function(treeToCompare, goalTree, branches) {
var result = true; var result = true;
branches.forEach(function(branchName) { branches.forEach(function(branchName) {
result = result && this.compareBranchWithinTrees(treeA, treeB, branchName); result = result && this.compareBranchWithinTrees(treeToCompare, goalTree, branchName);
}, this); }, this);
return result; return result;
}; };
TreeCompare.compareBranchWithinTrees = function(treeA, treeB, branchName) { TreeCompare.compareBranchWithinTrees = function(treeToCompare, goalTree, branchName) {
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
this.reduceTreeFields([treeA, treeB]); this.reduceTreeFields([treeToCompare, goalTree]);
var recurseCompare = this.getRecurseCompare(treeA, treeB); var recurseCompare = this.getRecurseCompare(treeToCompare, goalTree);
var branchA = treeA.branches[branchName]; var branchA = treeToCompare.branches[branchName];
var branchB = treeB.branches[branchName]; var branchB = goalTree.branches[branchName];
return _.isEqual(branchA, branchB) && return _.isEqual(branchA, branchB) &&
recurseCompare(treeA.commits[branchA.target], treeB.commits[branchB.target]); recurseCompare(treeToCompare.commits[branchA.target], goalTree.commits[branchB.target]);
}; };
TreeCompare.compareAllBranchesWithinTreesHashAgnostic = function(treeA, treeB) { TreeCompare.compareAllBranchesWithinTreesHashAgnostic = function(treeToCompare, goalTree) {
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
this.reduceTreeFields([treeA, treeB]); this.reduceTreeFields([treeToCompare, goalTree]);
var allBranches = Object.assign( var allBranches = Object.assign(
{}, {},
treeA.branches, treeToCompare.branches,
treeB.branches goalTree.branches
); );
var branchNames = Object.keys(allBranches || {}); var branchNames = Object.keys(allBranches || {});
return this.compareBranchesWithinTreesHashAgnostic(treeA, treeB, branchNames); return this.compareBranchesWithinTreesHashAgnostic(treeToCompare, goalTree, branchNames);
}; };
TreeCompare.compareBranchesWithinTreesHashAgnostic = function(treeA, treeB, branches) { TreeCompare.compareBranchesWithinTreesHashAgnostic = function(treeToCompare, goalTree, branches) {
// we can't DRY unfortunately here because we need a special _.isEqual function // we can't DRY unfortunately here because we need a special _.isEqual function
// for both the recursive compare and the branch compare // for both the recursive compare and the branch compare
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
this.reduceTreeFields([treeA, treeB]); this.reduceTreeFields([treeToCompare, goalTree]);
// get a function to compare branch objects without hashes // get a function to compare branch objects without hashes
var compareBranchObjs = function(branchA, branchB) { var compareBranchObjs = function(branchA, branchB) {
@ -170,15 +171,15 @@ TreeCompare.compareBranchesWithinTreesHashAgnostic = function(treeA, treeB, bran
return _.isEqual(branchA, branchB); return _.isEqual(branchA, branchB);
}.bind(this); }.bind(this);
// and a function to compare recursively without worrying about hashes // and a function to compare recursively without worrying about hashes
var recurseCompare = this.getRecurseCompareHashAgnostic(treeA, treeB); var recurseCompare = this.getRecurseCompareHashAgnostic(treeToCompare, goalTree);
var result = true; var result = true;
branches.forEach(function(branchName) { branches.forEach(function(branchName) {
var branchA = treeA.branches[branchName]; var branchA = treeToCompare.branches[branchName];
var branchB = treeB.branches[branchName]; var branchB = goalTree.branches[branchName];
result = result && compareBranchObjs(branchA, branchB) && result = result && compareBranchObjs(branchA, branchB) &&
recurseCompare(treeA.commits[branchA.target], treeB.commits[branchB.target]); recurseCompare(treeToCompare.commits[branchA.target], goalTree.commits[branchB.target]);
}, this); }, this);
return result; return result;
}; };
@ -261,7 +262,7 @@ TreeCompare.getBaseRef = function(ref) {
return 'C' + bits[1]; return 'C' + bits[1];
}; };
TreeCompare.getRecurseCompareHashAgnostic = function(treeA, treeB) { TreeCompare.getRecurseCompareHashAgnostic = function(treeToCompare, goalTree) {
// here we pass in a special comparison function to pass into the base // here we pass in a special comparison function to pass into the base
// recursive compare. // recursive compare.
@ -284,10 +285,10 @@ TreeCompare.getRecurseCompareHashAgnostic = function(treeA, treeB) {
getStrippedCommitCopy(commitB) getStrippedCommitCopy(commitB)
); );
}; };
return this.getRecurseCompare(treeA, treeB, {isEqual: isEqual}); return this.getRecurseCompare(treeToCompare, goalTree, {isEqual: isEqual});
}; };
TreeCompare.getRecurseCompare = function(treeA, treeB, options) { TreeCompare.getRecurseCompare = function(treeToCompare, goalTree, options) {
options = options || {}; options = options || {};
// we need a recursive comparison function to bubble up the branch // we need a recursive comparison function to bubble up the branch
@ -307,10 +308,10 @@ TreeCompare.getRecurseCompare = function(treeA, treeB, options) {
var pAid = commitA.parents[index]; var pAid = commitA.parents[index];
var pBid = commitB.parents[index]; var pBid = commitB.parents[index];
// if treeA or treeB doesn't have this parent, // if treeToCompare or goalTree doesn't have this parent,
// then we get an undefined child which is fine when we pass into _.isEqual // then we get an undefined child which is fine when we pass into _.isEqual
var childA = treeA.commits[pAid]; var childA = treeToCompare.commits[pAid];
var childB = treeB.commits[pBid]; var childB = goalTree.commits[pBid];
result = result && recurseCompare(childA, childB); result = result && recurseCompare(childA, childB);
} }
@ -426,15 +427,15 @@ TreeCompare.reduceTreeFields = function(trees) {
}, this); }, this);
}; };
TreeCompare.compareTrees = function(treeA, treeB) { TreeCompare.compareTrees = function(treeToCompare, goalTree) {
treeA = this.convertTreeSafe(treeA); treeToCompare = this.convertTreeSafe(treeToCompare);
treeB = this.convertTreeSafe(treeB); goalTree = this.convertTreeSafe(goalTree);
// now we need to strip out the fields we don't care about, aka things // now we need to strip out the fields we don't care about, aka things
// like createTime, message, author // like createTime, message, author
this.reduceTreeFields([treeA, treeB]); this.reduceTreeFields([treeToCompare, goalTree]);
return _.isEqual(treeA, treeB); return _.isEqual(treeToCompare, goalTree);
}; };
module.exports = TreeCompare; module.exports = TreeCompare;