From e7d9b76b9851b2dccca9a7df654820151aee1234 Mon Sep 17 00:00:00 2001 From: Peter Cottle Date: Mon, 23 Dec 2013 10:15:16 -0800 Subject: [PATCH] moved another function to graph --- src/js/git/commands.js | 3 ++- src/js/git/index.js | 52 +++++++++--------------------------------- src/js/graph/index.js | 31 +++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/js/git/commands.js b/src/js/git/commands.js index 975bfbd3..7823bf3c 100644 --- a/src/js/git/commands.js +++ b/src/js/git/commands.js @@ -1,6 +1,7 @@ var _ = require('underscore'); var intl = require('../intl'); +var Graph = require('../graph'); var Errors = require('../util/errors'); var CommandProcessError = Errors.CommandProcessError; var GitError = Errors.GitError; @@ -177,7 +178,7 @@ var commandConfig = { command.validateArgBounds(generalArgs, 1, Number.MAX_VALUE); - var set = engine.getUpstreamSet('HEAD'); + var set = Graph.getUpstreamSet(engine, 'HEAD'); // first resolve all the refs (as an error check) var toCherrypick = _.map(generalArgs, function(arg) { var commit = engine.getCommitFromRef(arg); diff --git a/src/js/git/index.js b/src/js/git/index.js index c7bf862f..d2be67d4 100644 --- a/src/js/git/index.js +++ b/src/js/git/index.js @@ -27,12 +27,6 @@ function catchShortCircuit(err) { } } -function invariant(truthy, reason) { - if (!truthy) { - throw new Error(reason); - } -} - function GitEngine(options) { this.rootCommit = null; this.refs = {}; @@ -174,7 +168,7 @@ GitEngine.prototype.exportTreeForBranch = function(branchName) { // is not connected to branchname var tree = this.exportTree(); // get the upstream set - var set = this.getUpstreamSet(branchName); + var set = Graph.getUpstreamSet(this, branchName); // now loop through and delete commits var commitsToLoop = tree.commits; tree.commits = {}; @@ -934,7 +928,7 @@ GitEngine.prototype.checkUpstreamOfSource = function( // target. Hence target should be strictly upstream of source // lets first get the upstream set from source's dest branch - var upstream = source.getUpstreamSet(sourceBranch); + var upstream = Graph.getUpstreamSet(source, sourceBranch); var targetLocationID = target.getCommitFromRef(targetBranch).get('id'); if (!upstream[targetLocationID]) { @@ -954,7 +948,7 @@ GitEngine.prototype.getTargetGraphDifference = function( options = options || {}; sourceBranch = source.resolveID(sourceBranch); - var targetSet = target.getUpstreamSet(targetBranch); + var targetSet = Graph.getUpstreamSet(target, targetBranch); var sourceStartCommit = source.getCommitFromRef(sourceBranch); var sourceTree = source.exportTree(); @@ -1707,7 +1701,7 @@ GitEngine.prototype.pruneTreeAndPlay = function() { GitEngine.prototype.pruneTree = function() { var set = this.getUpstreamBranchSet(); // dont prune commits that HEAD depends on - var headSet = this.getUpstreamSet('HEAD'); + var headSet = Graph.getUpstreamSet(this, 'HEAD'); _.each(headSet, function(val, commitID) { set[commitID] = true; }); @@ -1802,7 +1796,7 @@ GitEngine.prototype.getUpstreamCollectionSet = function(collection) { }; GitEngine.prototype.getUpstreamHeadSet = function() { - var set = this.getUpstreamSet('HEAD'); + var set = Graph.getUpstreamSet(this, 'HEAD'); var including = this.getCommitFromRef('HEAD').get('id'); set[including] = true; @@ -1950,7 +1944,7 @@ GitEngine.prototype.hgRebase = function(destination, base) { // we need everything BELOW ourselves... var downstream = this.getDownstreamSet(base); // and we need to go upwards to the stop set - var stopSet = this.getUpstreamSet(destination); + var stopSet = Graph.getUpstreamSet(this, destination); var upstream = this.getUpstreamDiffSetFromSet(stopSet, base); // and NOWWWwwww get all the descendants of this set @@ -2032,7 +2026,7 @@ GitEngine.prototype.rebase = function(targetSource, currentLocation, options) { // then we BFS from currentLocation, using the downstream set as our stopping point. // we need to BFS because we need to include all commits below // pop these commits on top of targetSource and modify their ids with quotes - var stopSet = this.getUpstreamSet(targetSource); + var stopSet = Graph.getUpstreamSet(this, targetSource); var toRebaseRough = this.getUpstreamDiffFromSet(stopSet, currentLocation); return this.rebaseFinish(toRebaseRough, stopSet, targetSource, currentLocation, options); }; @@ -2064,7 +2058,7 @@ GitEngine.prototype.rebaseInteractive = function(targetSource, currentLocation, } // now get the stop set - var stopSet = this.getUpstreamSet(targetSource); + var stopSet = Graph.getUpstreamSet(this, targetSource); var toRebaseRough = []; // standard BFS @@ -2567,7 +2561,7 @@ GitEngine.prototype.status = function() { GitEngine.prototype.logWithout = function(ref, omitBranch) { // slice off the ^branch omitBranch = omitBranch.slice(1); - this.log(ref, this.getUpstreamSet(omitBranch)); + this.log(ref, Graph.getUpstreamSet(this, omitBranch)); }; GitEngine.prototype.log = function(ref, omitSet) { @@ -2612,7 +2606,7 @@ GitEngine.prototype.getCommonAncestor = function(ancestor, cousin) { throw new Error('Dont use common ancestor if we are upstream!'); } - var upstreamSet = this.getUpstreamSet(ancestor); + var upstreamSet = Graph.getUpstreamSet(this, ancestor); // now BFS off of cousin until you find something var queue = [this.getCommitFromRef(cousin)]; @@ -2631,7 +2625,7 @@ GitEngine.prototype.isUpstreamOf = function(child, ancestor) { // basically just do a completely BFS search on ancestor to the root, then // check for membership of child in that set of explored nodes - var upstream = this.getUpstreamSet(ancestor); + var upstream = Graph.getUpstreamSet(this, ancestor); return upstream[child.get('id')] !== undefined; }; @@ -2658,29 +2652,6 @@ GitEngine.prototype.getDownstreamSet = function(ancestor) { return exploredSet; }; -GitEngine.prototype.getUpstreamSet = function(ancestor) { - var commit = this.getCommitFromRef(ancestor); - var ancestorID = commit.get('id'); - var queue = [commit]; - - var exploredSet = {}; - exploredSet[ancestorID] = true; - - var addToExplored = function(rent) { - exploredSet[rent.get('id')] = true; - queue.push(rent); - }; - - while (queue.length) { - var here = queue.pop(); - var rents = here.get('parents'); - - _.each(rents, addToExplored); - } - return exploredSet; -}; - - var Ref = Backbone.Model.extend({ initialize: function() { if (!this.get('target')) { @@ -2741,7 +2712,6 @@ var Branch = Ref.extend({ * * With that in mind, we change our branch model to support the following */ - setRemoteTrackingBranchID: function(id) { this.set('remoteTrackingBranchID', id); }, diff --git a/src/js/graph/index.js b/src/js/graph/index.js index 92f921ad..88774f2a 100644 --- a/src/js/graph/index.js +++ b/src/js/graph/index.js @@ -6,7 +6,14 @@ var Branch = Git.Branch; var Tag = Git.Tag; var Ref = Git.Ref; +function invariant(truthy, reason) { + if (!truthy) { + throw new Error(reason); + } +} + var Graph = { + getOrMakeRecursive: function( tree, createdSoFar, @@ -118,6 +125,30 @@ var Graph = { return result; }, + getUpstreamSet: function(engine, ancestor) { + invariant(typeof ancestor === 'string', 'pass in string'); + + var commit = engine.getCommitFromRef(ancestor); + var ancestorID = commit.get('id'); + var queue = [commit]; + + var exploredSet = {}; + exploredSet[ancestorID] = true; + + var addToExplored = function(rent) { + exploredSet[rent.get('id')] = true; + queue.push(rent); + }; + + while (queue.length) { + var here = queue.pop(); + var rents = here.get('parents'); + + _.each(rents, addToExplored); + } + return exploredSet; + }, + getUniqueObjects: function(objects) { var unique = {}; var result = [];