mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-25 23:48:34 +02:00
moved another function to graph
This commit is contained in:
parent
c3c3a50bdd
commit
e7d9b76b98
3 changed files with 44 additions and 42 deletions
|
@ -1,6 +1,7 @@
|
||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
var intl = require('../intl');
|
var intl = require('../intl');
|
||||||
|
|
||||||
|
var Graph = require('../graph');
|
||||||
var Errors = require('../util/errors');
|
var Errors = require('../util/errors');
|
||||||
var CommandProcessError = Errors.CommandProcessError;
|
var CommandProcessError = Errors.CommandProcessError;
|
||||||
var GitError = Errors.GitError;
|
var GitError = Errors.GitError;
|
||||||
|
@ -177,7 +178,7 @@ var commandConfig = {
|
||||||
|
|
||||||
command.validateArgBounds(generalArgs, 1, Number.MAX_VALUE);
|
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)
|
// first resolve all the refs (as an error check)
|
||||||
var toCherrypick = _.map(generalArgs, function(arg) {
|
var toCherrypick = _.map(generalArgs, function(arg) {
|
||||||
var commit = engine.getCommitFromRef(arg);
|
var commit = engine.getCommitFromRef(arg);
|
||||||
|
|
|
@ -27,12 +27,6 @@ function catchShortCircuit(err) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function invariant(truthy, reason) {
|
|
||||||
if (!truthy) {
|
|
||||||
throw new Error(reason);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function GitEngine(options) {
|
function GitEngine(options) {
|
||||||
this.rootCommit = null;
|
this.rootCommit = null;
|
||||||
this.refs = {};
|
this.refs = {};
|
||||||
|
@ -174,7 +168,7 @@ GitEngine.prototype.exportTreeForBranch = function(branchName) {
|
||||||
// is not connected to branchname
|
// is not connected to branchname
|
||||||
var tree = this.exportTree();
|
var tree = this.exportTree();
|
||||||
// get the upstream set
|
// get the upstream set
|
||||||
var set = this.getUpstreamSet(branchName);
|
var set = Graph.getUpstreamSet(this, branchName);
|
||||||
// now loop through and delete commits
|
// now loop through and delete commits
|
||||||
var commitsToLoop = tree.commits;
|
var commitsToLoop = tree.commits;
|
||||||
tree.commits = {};
|
tree.commits = {};
|
||||||
|
@ -934,7 +928,7 @@ GitEngine.prototype.checkUpstreamOfSource = function(
|
||||||
// target. Hence target should be strictly upstream of source
|
// target. Hence target should be strictly upstream of source
|
||||||
|
|
||||||
// lets first get the upstream set from source's dest branch
|
// 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');
|
var targetLocationID = target.getCommitFromRef(targetBranch).get('id');
|
||||||
if (!upstream[targetLocationID]) {
|
if (!upstream[targetLocationID]) {
|
||||||
|
@ -954,7 +948,7 @@ GitEngine.prototype.getTargetGraphDifference = function(
|
||||||
options = options || {};
|
options = options || {};
|
||||||
sourceBranch = source.resolveID(sourceBranch);
|
sourceBranch = source.resolveID(sourceBranch);
|
||||||
|
|
||||||
var targetSet = target.getUpstreamSet(targetBranch);
|
var targetSet = Graph.getUpstreamSet(target, targetBranch);
|
||||||
var sourceStartCommit = source.getCommitFromRef(sourceBranch);
|
var sourceStartCommit = source.getCommitFromRef(sourceBranch);
|
||||||
|
|
||||||
var sourceTree = source.exportTree();
|
var sourceTree = source.exportTree();
|
||||||
|
@ -1707,7 +1701,7 @@ GitEngine.prototype.pruneTreeAndPlay = function() {
|
||||||
GitEngine.prototype.pruneTree = function() {
|
GitEngine.prototype.pruneTree = function() {
|
||||||
var set = this.getUpstreamBranchSet();
|
var set = this.getUpstreamBranchSet();
|
||||||
// dont prune commits that HEAD depends on
|
// dont prune commits that HEAD depends on
|
||||||
var headSet = this.getUpstreamSet('HEAD');
|
var headSet = Graph.getUpstreamSet(this, 'HEAD');
|
||||||
_.each(headSet, function(val, commitID) {
|
_.each(headSet, function(val, commitID) {
|
||||||
set[commitID] = true;
|
set[commitID] = true;
|
||||||
});
|
});
|
||||||
|
@ -1802,7 +1796,7 @@ GitEngine.prototype.getUpstreamCollectionSet = function(collection) {
|
||||||
};
|
};
|
||||||
|
|
||||||
GitEngine.prototype.getUpstreamHeadSet = function() {
|
GitEngine.prototype.getUpstreamHeadSet = function() {
|
||||||
var set = this.getUpstreamSet('HEAD');
|
var set = Graph.getUpstreamSet(this, 'HEAD');
|
||||||
var including = this.getCommitFromRef('HEAD').get('id');
|
var including = this.getCommitFromRef('HEAD').get('id');
|
||||||
|
|
||||||
set[including] = true;
|
set[including] = true;
|
||||||
|
@ -1950,7 +1944,7 @@ GitEngine.prototype.hgRebase = function(destination, base) {
|
||||||
// we need everything BELOW ourselves...
|
// we need everything BELOW ourselves...
|
||||||
var downstream = this.getDownstreamSet(base);
|
var downstream = this.getDownstreamSet(base);
|
||||||
// and we need to go upwards to the stop set
|
// 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);
|
var upstream = this.getUpstreamDiffSetFromSet(stopSet, base);
|
||||||
|
|
||||||
// and NOWWWwwww get all the descendants of this set
|
// 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.
|
// 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
|
// 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
|
// 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);
|
var toRebaseRough = this.getUpstreamDiffFromSet(stopSet, currentLocation);
|
||||||
return this.rebaseFinish(toRebaseRough, stopSet, targetSource, currentLocation, options);
|
return this.rebaseFinish(toRebaseRough, stopSet, targetSource, currentLocation, options);
|
||||||
};
|
};
|
||||||
|
@ -2064,7 +2058,7 @@ GitEngine.prototype.rebaseInteractive = function(targetSource, currentLocation,
|
||||||
}
|
}
|
||||||
|
|
||||||
// now get the stop set
|
// now get the stop set
|
||||||
var stopSet = this.getUpstreamSet(targetSource);
|
var stopSet = Graph.getUpstreamSet(this, targetSource);
|
||||||
|
|
||||||
var toRebaseRough = [];
|
var toRebaseRough = [];
|
||||||
// standard BFS
|
// standard BFS
|
||||||
|
@ -2567,7 +2561,7 @@ GitEngine.prototype.status = function() {
|
||||||
GitEngine.prototype.logWithout = function(ref, omitBranch) {
|
GitEngine.prototype.logWithout = function(ref, omitBranch) {
|
||||||
// slice off the ^branch
|
// slice off the ^branch
|
||||||
omitBranch = omitBranch.slice(1);
|
omitBranch = omitBranch.slice(1);
|
||||||
this.log(ref, this.getUpstreamSet(omitBranch));
|
this.log(ref, Graph.getUpstreamSet(this, omitBranch));
|
||||||
};
|
};
|
||||||
|
|
||||||
GitEngine.prototype.log = function(ref, omitSet) {
|
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!');
|
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
|
// now BFS off of cousin until you find something
|
||||||
|
|
||||||
var queue = [this.getCommitFromRef(cousin)];
|
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
|
// basically just do a completely BFS search on ancestor to the root, then
|
||||||
// check for membership of child in that set of explored nodes
|
// 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;
|
return upstream[child.get('id')] !== undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2658,29 +2652,6 @@ GitEngine.prototype.getDownstreamSet = function(ancestor) {
|
||||||
return exploredSet;
|
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({
|
var Ref = Backbone.Model.extend({
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
if (!this.get('target')) {
|
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
|
* With that in mind, we change our branch model to support the following
|
||||||
*/
|
*/
|
||||||
|
|
||||||
setRemoteTrackingBranchID: function(id) {
|
setRemoteTrackingBranchID: function(id) {
|
||||||
this.set('remoteTrackingBranchID', id);
|
this.set('remoteTrackingBranchID', id);
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,7 +6,14 @@ var Branch = Git.Branch;
|
||||||
var Tag = Git.Tag;
|
var Tag = Git.Tag;
|
||||||
var Ref = Git.Ref;
|
var Ref = Git.Ref;
|
||||||
|
|
||||||
|
function invariant(truthy, reason) {
|
||||||
|
if (!truthy) {
|
||||||
|
throw new Error(reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var Graph = {
|
var Graph = {
|
||||||
|
|
||||||
getOrMakeRecursive: function(
|
getOrMakeRecursive: function(
|
||||||
tree,
|
tree,
|
||||||
createdSoFar,
|
createdSoFar,
|
||||||
|
@ -118,6 +125,30 @@ var Graph = {
|
||||||
return result;
|
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) {
|
getUniqueObjects: function(objects) {
|
||||||
var unique = {};
|
var unique = {};
|
||||||
var result = [];
|
var result = [];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue