mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-28 08:50:06 +02:00
levels file, for GAMEIFICATIONNNnnn
This commit is contained in:
parent
103ea281ff
commit
018a404bc6
3 changed files with 170 additions and 1 deletions
81
src/git.js
81
src/git.js
|
@ -81,6 +81,86 @@ GitEngine.prototype.exportTree = function() {
|
||||||
return totalExport;
|
return totalExport;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GitEngine.prototype.compareBranchesWithinTrees = function(treeA, treeB, branches) {
|
||||||
|
var result = true;
|
||||||
|
_.each(branches, function(branchName) {
|
||||||
|
result = result && this.compareBranchWithinTrees(treeA, treeB, branchName);
|
||||||
|
}, this);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
GitEngine.prototype.compareBranchWithinTrees = function(treeA, treeB, branchName) {
|
||||||
|
treeA = this.convertTreeSafe(treeA);
|
||||||
|
treeB = this.convertTreeSafe(treeB);
|
||||||
|
|
||||||
|
this.stripTreeFields([treeA, treeB]);
|
||||||
|
|
||||||
|
// we need a recursive comparison function to bubble up the branch
|
||||||
|
var recurseCompare = function(commitA, commitB) {
|
||||||
|
// this is the short-circuit base case
|
||||||
|
var result = _.isEqual(commitA, commitB);
|
||||||
|
if (!result) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// we loop through each parent ID. we sort the parent ID's beforehand
|
||||||
|
// so the index lookup is valid
|
||||||
|
_.each(commitA.parents, function(pAid, index) {
|
||||||
|
var pBid = commitB.parents[index];
|
||||||
|
|
||||||
|
var childA = treeA.commits[pAid];
|
||||||
|
var childB = treeB.commits[pBid];
|
||||||
|
|
||||||
|
result = result && recurseCompare(childA, childB);
|
||||||
|
}, this);
|
||||||
|
// if each of our children recursively are equal, we are good
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
var branchA = treeA.branches[branchName];
|
||||||
|
var branchB = treeB.branches[branchName];
|
||||||
|
|
||||||
|
return _.isEqual(branchA, branchB) &&
|
||||||
|
recurseCompare(treeA.commits[branchA.target], treeB.commits[branchB.target]);
|
||||||
|
};
|
||||||
|
|
||||||
|
GitEngine.prototype.convertTreeSafe = function(tree) {
|
||||||
|
if (typeof tree == 'string') {
|
||||||
|
return JSON.parse(unescape(tree));
|
||||||
|
}
|
||||||
|
return tree;
|
||||||
|
};
|
||||||
|
|
||||||
|
GitEngine.prototype.stripTreeFields = function(trees) {
|
||||||
|
var stripFields = ['createTime', 'author', 'commitMessage'];
|
||||||
|
var sortFields = ['children', 'parents'];
|
||||||
|
|
||||||
|
_.each(trees, function(tree) {
|
||||||
|
_.each(tree.commits, function(commit) {
|
||||||
|
_.each(stripFields, function(field) {
|
||||||
|
commit[field] = undefined;
|
||||||
|
});
|
||||||
|
_.each(sortFields, function(field) {
|
||||||
|
if (commit[field]) {
|
||||||
|
commit[field] = commit[field].sort();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
GitEngine.prototype.compareTrees = function(treeA, treeB) {
|
||||||
|
treeA = this.convertTreeSafe(treeA);
|
||||||
|
treeB = this.convertTreeSafe(treeB);
|
||||||
|
|
||||||
|
// now we need to strip out the fields we don't care about, aka things
|
||||||
|
// like createTime, message, author
|
||||||
|
this.stripTreeFields([treeA, treeB]);
|
||||||
|
|
||||||
|
return _.isEqual(treeA, treeB);
|
||||||
|
};
|
||||||
|
|
||||||
GitEngine.prototype.printTree = function() {
|
GitEngine.prototype.printTree = function() {
|
||||||
var str = escape(JSON.stringify(this.exportTree()));
|
var str = escape(JSON.stringify(this.exportTree()));
|
||||||
return str;
|
return str;
|
||||||
|
@ -92,7 +172,6 @@ GitEngine.prototype.printAndCopyTree = function() {
|
||||||
|
|
||||||
GitEngine.prototype.loadTree = function(tree) {
|
GitEngine.prototype.loadTree = function(tree) {
|
||||||
// first clear everything
|
// first clear everything
|
||||||
// debugger
|
|
||||||
this.removeAll();
|
this.removeAll();
|
||||||
|
|
||||||
this.instantiateFromTree(tree);
|
this.instantiateFromTree(tree);
|
||||||
|
|
|
@ -144,5 +144,8 @@
|
||||||
<script src="tree.js"></script>
|
<script src="tree.js"></script>
|
||||||
<script src="animationFactory.js"></script>
|
<script src="animationFactory.js"></script>
|
||||||
|
|
||||||
|
<!-- levels -->
|
||||||
|
<script src="levels.js"></script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
87
src/levels.js
Normal file
87
src/levels.js
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
// static class...
|
||||||
|
function LevelEngine() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
LevelEngine.prototype.compareBranchesWithinTrees = function(treeA, treeB, branches) {
|
||||||
|
var result = true;
|
||||||
|
_.each(branches, function(branchName) {
|
||||||
|
result = result && this.compareBranchWithinTrees(treeA, treeB, branchName);
|
||||||
|
}, this);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
LevelEngine.prototype.compareBranchWithinTrees = function(treeA, treeB, branchName) {
|
||||||
|
treeA = this.convertTreeSafe(treeA);
|
||||||
|
treeB = this.convertTreeSafe(treeB);
|
||||||
|
|
||||||
|
this.stripTreeFields([treeA, treeB]);
|
||||||
|
|
||||||
|
// we need a recursive comparison function to bubble up the branch
|
||||||
|
var recurseCompare = function(commitA, commitB) {
|
||||||
|
// this is the short-circuit base case
|
||||||
|
var result = _.isEqual(commitA, commitB);
|
||||||
|
if (!result) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// we loop through each parent ID. we sort the parent ID's beforehand
|
||||||
|
// so the index lookup is valid
|
||||||
|
_.each(commitA.parents, function(pAid, index) {
|
||||||
|
var pBid = commitB.parents[index];
|
||||||
|
|
||||||
|
var childA = treeA.commits[pAid];
|
||||||
|
var childB = treeB.commits[pBid];
|
||||||
|
|
||||||
|
result = result && recurseCompare(childA, childB);
|
||||||
|
}, this);
|
||||||
|
// if each of our children recursively are equal, we are good
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
var branchA = treeA.branches[branchName];
|
||||||
|
var branchB = treeB.branches[branchName];
|
||||||
|
|
||||||
|
return _.isEqual(branchA, branchB) &&
|
||||||
|
recurseCompare(treeA.commits[branchA.target], treeB.commits[branchB.target]);
|
||||||
|
};
|
||||||
|
|
||||||
|
LevelEngine.prototype.convertTreeSafe = function(tree) {
|
||||||
|
if (typeof tree == 'string') {
|
||||||
|
return JSON.parse(unescape(tree));
|
||||||
|
}
|
||||||
|
return tree;
|
||||||
|
};
|
||||||
|
|
||||||
|
LevelEngine.prototype.stripTreeFields = function(trees) {
|
||||||
|
var stripFields = ['createTime', 'author', 'commitMessage'];
|
||||||
|
var sortFields = ['children', 'parents'];
|
||||||
|
|
||||||
|
_.each(trees, function(tree) {
|
||||||
|
_.each(tree.commits, function(commit) {
|
||||||
|
_.each(stripFields, function(field) {
|
||||||
|
commit[field] = undefined;
|
||||||
|
});
|
||||||
|
_.each(sortFields, function(field) {
|
||||||
|
if (commit[field]) {
|
||||||
|
commit[field] = commit[field].sort();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
LevelEngine.prototype.compareTrees = function(treeA, treeB) {
|
||||||
|
treeA = this.convertTreeSafe(treeA);
|
||||||
|
treeB = this.convertTreeSafe(treeB);
|
||||||
|
|
||||||
|
// now we need to strip out the fields we don't care about, aka things
|
||||||
|
// like createTime, message, author
|
||||||
|
this.stripTreeFields([treeA, treeB]);
|
||||||
|
|
||||||
|
return _.isEqual(treeA, treeB);
|
||||||
|
};
|
||||||
|
|
||||||
|
var levelEngine = new LevelEngine();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue