slowly moving git command logic out of command model

This commit is contained in:
Peter Cottle 2012-12-25 18:02:13 -08:00
parent bbdaa37abd
commit 5e2bace639
7 changed files with 246 additions and 115 deletions

View file

@ -4394,6 +4394,7 @@ var CommitCollection = Collections.CommitCollection;
var BranchCollection = Collections.BranchCollection; var BranchCollection = Collections.BranchCollection;
var GitVisuals = require('../visuals').GitVisuals; var GitVisuals = require('../visuals').GitVisuals;
var InputWaterfall = require('../level/inputWaterfall').InputWaterfall;
var Visualization = Backbone.View.extend({ var Visualization = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
@ -4415,6 +4416,10 @@ var Visualization = Backbone.View.extend({
var Main = require('../app'); var Main = require('../app');
this.events = Main.getEvents(); this.events = Main.getEvents();
// hook the git engine up to the command input
this.inputWaterfall = new InputWaterfall();
this.commitCollection = new CommitCollection(); this.commitCollection = new CommitCollection();
this.branchCollection = new BranchCollection(); this.branchCollection = new BranchCollection();
@ -4641,7 +4646,7 @@ function GitEngine(options) {
this.commandOptions = {}; this.commandOptions = {};
this.generalArgs = []; this.generalArgs = [];
this.events.on('processCommand', this.dispatch, this); this.events.on('processGitCommand', this.dispatch, this);
// backbone or something uses _.uniqueId, so we make our own here // backbone or something uses _.uniqueId, so we make our own here
this.uniqueId = (function() { this.uniqueId = (function() {
@ -10975,6 +10980,7 @@ require.define("/src/js/models/commandModel.js",function(require,module,exports,
var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone; var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone;
var Errors = require('../util/errors'); var Errors = require('../util/errors');
var GitCommands = require('../git/commands');
var CommandProcessError = Errors.CommandProcessError; var CommandProcessError = Errors.CommandProcessError;
var GitError = Errors.GitError; var GitError = Errors.GitError;
@ -11077,35 +11083,6 @@ var Command = Backbone.Model.extend({
this.set('result', this.get('error').toResult()); this.set('result', this.get('error').toResult());
}, },
getShortcutMap: function() {
return {
'git commit': /^gc($|\s)/,
'git add': /^ga($|\s)/,
'git checkout': /^go($|\s)/,
'git rebase': /^gr($|\s)/,
'git branch': /^gb($|\s)/
};
},
getRegexMap: function() {
return {
// ($|\s) means that we either have to end the string
// after the command or there needs to be a space for options
commit: /^commit($|\s)/,
add: /^add($|\s)/,
checkout: /^checkout($|\s)/,
rebase: /^rebase($|\s)/,
reset: /^reset($|\s)/,
branch: /^branch($|\s)/,
revert: /^revert($|\s)/,
log: /^log($|\s)/,
merge: /^merge($|\s)/,
show: /^show($|\s)/,
status: /^status($|\s)/,
'cherry-pick': /^cherry-pick($|\s)/
};
},
getSandboxCommands: function() { getSandboxCommands: function() {
return [ return [
[/^ls/, function() { [/^ls/, function() {
@ -11196,7 +11173,7 @@ var Command = Backbone.Model.extend({
// then check if shortcut exists, and replace, but // then check if shortcut exists, and replace, but
// preserve options if so // preserve options if so
_.each(this.getShortcutMap(), function(regex, method) { _.each(GitCommands.getShortcutMap(), function(regex, method) {
var results = regex.exec(str); var results = regex.exec(str);
if (results) { if (results) {
str = method + ' ' + str.slice(results[0].length); str = method + ' ' + str.slice(results[0].length);
@ -11220,7 +11197,7 @@ var Command = Backbone.Model.extend({
var fullCommand = str.slice('git '.length); var fullCommand = str.slice('git '.length);
// see if we support this particular command // see if we support this particular command
_.each(this.getRegexMap(), function(regex, method) { _.each(GitCommands.getRegexMap(), function(regex, method) {
if (regex.exec(fullCommand)) { if (regex.exec(fullCommand)) {
this.set('options', fullCommand.slice(method.length + 1)); this.set('options', fullCommand.slice(method.length + 1));
this.set('method', method); this.set('method', method);
@ -11350,6 +11327,40 @@ exports.Command = Command;
}); });
require.define("/src/js/git/commands.js",function(require,module,exports,__dirname,__filename,process,global){var getRegexMap = function() {
return {
// ($|\s) means that we either have to end the string
// after the command or there needs to be a space for options
commit: /^commit($|\s)/,
add: /^add($|\s)/,
checkout: /^checkout($|\s)/,
rebase: /^rebase($|\s)/,
reset: /^reset($|\s)/,
branch: /^branch($|\s)/,
revert: /^revert($|\s)/,
log: /^log($|\s)/,
merge: /^merge($|\s)/,
show: /^show($|\s)/,
status: /^status($|\s)/,
'cherry-pick': /^cherry-pick($|\s)/
};
};
var getShortcutMap = function() {
return {
'git commit': /^gc($|\s)/,
'git add': /^ga($|\s)/,
'git checkout': /^go($|\s)/,
'git rebase': /^gr($|\s)/,
'git branch': /^gb($|\s)/
};
};
exports.getRegexMap = getRegexMap;
exports.getShortcutMap = getShortcutMap;
});
require.define("/src/js/app/index.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore'); require.define("/src/js/app/index.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
var Backbone = require('backbone'); var Backbone = require('backbone');
@ -13728,6 +13739,47 @@ var VisEdgeCollection = Backbone.Collection.extend({
exports.VisEdgeCollection = VisEdgeCollection; exports.VisEdgeCollection = VisEdgeCollection;
exports.VisEdge = VisEdge; exports.VisEdge = VisEdge;
});
require.define("/src/js/level/inputWaterfall.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
var Backbone = require('backbone');
var Main = require('../app');
/**
* This class supports a few things we need for levels:
~ A disabled map (to prevent certain git commands from firing)
~ A post-git command hook (to compare the git tree against the solution)
~ Extra level-specific commands (like help, hint, etc) that are async
**/
function InputWaterfall(options) {
options = options || {};
this.listenEvent = options.listenEvent || 'processCommand';
this.disabledMap = options.disabledMap || {};
console.log('made');
this.listen();
}
InputWaterfall.prototype.listen = function() {
Main.getEvents().on(this.listenEvent, this.process, this);
};
InputWaterfall.prototype.mute = function() {
Main.getEvents().off(this.listenEvent, this.process, this);
};
InputWaterfall.prototype.process = function(command, callback) {
console.log('processing');
// for now, just immediately fire it
Main.getEvents().trigger('processGitCommand', command, callback);
};
exports.InputWaterfall = InputWaterfall;
}); });
require.define("/src/js/util/mock.js",function(require,module,exports,__dirname,__filename,process,global){exports.mock = function(Constructor) { require.define("/src/js/util/mock.js",function(require,module,exports,__dirname,__filename,process,global){exports.mock = function(Constructor) {
@ -14058,9 +14110,40 @@ exports.init = init;
}); });
require("/src/js/app/index.js"); require("/src/js/app/index.js");
require.define("/src/js/app/inputWaterfall.js",function(require,module,exports,__dirname,__filename,process,global){undefined require.define("/src/js/git/commands.js",function(require,module,exports,__dirname,__filename,process,global){var getRegexMap = function() {
return {
// ($|\s) means that we either have to end the string
// after the command or there needs to be a space for options
commit: /^commit($|\s)/,
add: /^add($|\s)/,
checkout: /^checkout($|\s)/,
rebase: /^rebase($|\s)/,
reset: /^reset($|\s)/,
branch: /^branch($|\s)/,
revert: /^revert($|\s)/,
log: /^log($|\s)/,
merge: /^merge($|\s)/,
show: /^show($|\s)/,
status: /^status($|\s)/,
'cherry-pick': /^cherry-pick($|\s)/
};
};
var getShortcutMap = function() {
return {
'git commit': /^gc($|\s)/,
'git add': /^ga($|\s)/,
'git checkout': /^go($|\s)/,
'git rebase': /^gr($|\s)/,
'git branch': /^gb($|\s)/
};
};
exports.getRegexMap = getRegexMap;
exports.getShortcutMap = getShortcutMap;
}); });
require("/src/js/app/inputWaterfall.js"); require("/src/js/git/commands.js");
require.define("/src/js/git/headless.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore'); require.define("/src/js/git/headless.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
var Backbone = require('backbone'); var Backbone = require('backbone');
@ -14149,7 +14232,7 @@ function GitEngine(options) {
this.commandOptions = {}; this.commandOptions = {};
this.generalArgs = []; this.generalArgs = [];
this.events.on('processCommand', this.dispatch, this); this.events.on('processGitCommand', this.dispatch, this);
// backbone or something uses _.uniqueId, so we make our own here // backbone or something uses _.uniqueId, so we make our own here
this.uniqueId = (function() { this.uniqueId = (function() {
@ -15919,6 +16002,48 @@ exports.TreeCompare = TreeCompare;
}); });
require("/src/js/git/treeCompare.js"); require("/src/js/git/treeCompare.js");
require.define("/src/js/level/inputWaterfall.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
var Backbone = require('backbone');
var Main = require('../app');
/**
* This class supports a few things we need for levels:
~ A disabled map (to prevent certain git commands from firing)
~ A post-git command hook (to compare the git tree against the solution)
~ Extra level-specific commands (like help, hint, etc) that are async
**/
function InputWaterfall(options) {
options = options || {};
this.listenEvent = options.listenEvent || 'processCommand';
this.disabledMap = options.disabledMap || {};
console.log('made');
this.listen();
}
InputWaterfall.prototype.listen = function() {
Main.getEvents().on(this.listenEvent, this.process, this);
};
InputWaterfall.prototype.mute = function() {
Main.getEvents().off(this.listenEvent, this.process, this);
};
InputWaterfall.prototype.process = function(command, callback) {
console.log('processing');
// for now, just immediately fire it
Main.getEvents().trigger('processGitCommand', command, callback);
};
exports.InputWaterfall = InputWaterfall;
});
require("/src/js/level/inputWaterfall.js");
require.define("/src/js/models/collections.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore'); require.define("/src/js/models/collections.js",function(require,module,exports,__dirname,__filename,process,global){var _ = require('underscore');
// horrible hack to get localStorage Backbone plugin // horrible hack to get localStorage Backbone plugin
var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone; var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone;
@ -16033,6 +16158,7 @@ require.define("/src/js/models/commandModel.js",function(require,module,exports,
var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone; var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone;
var Errors = require('../util/errors'); var Errors = require('../util/errors');
var GitCommands = require('../git/commands');
var CommandProcessError = Errors.CommandProcessError; var CommandProcessError = Errors.CommandProcessError;
var GitError = Errors.GitError; var GitError = Errors.GitError;
@ -16135,35 +16261,6 @@ var Command = Backbone.Model.extend({
this.set('result', this.get('error').toResult()); this.set('result', this.get('error').toResult());
}, },
getShortcutMap: function() {
return {
'git commit': /^gc($|\s)/,
'git add': /^ga($|\s)/,
'git checkout': /^go($|\s)/,
'git rebase': /^gr($|\s)/,
'git branch': /^gb($|\s)/
};
},
getRegexMap: function() {
return {
// ($|\s) means that we either have to end the string
// after the command or there needs to be a space for options
commit: /^commit($|\s)/,
add: /^add($|\s)/,
checkout: /^checkout($|\s)/,
rebase: /^rebase($|\s)/,
reset: /^reset($|\s)/,
branch: /^branch($|\s)/,
revert: /^revert($|\s)/,
log: /^log($|\s)/,
merge: /^merge($|\s)/,
show: /^show($|\s)/,
status: /^status($|\s)/,
'cherry-pick': /^cherry-pick($|\s)/
};
},
getSandboxCommands: function() { getSandboxCommands: function() {
return [ return [
[/^ls/, function() { [/^ls/, function() {
@ -16254,7 +16351,7 @@ var Command = Backbone.Model.extend({
// then check if shortcut exists, and replace, but // then check if shortcut exists, and replace, but
// preserve options if so // preserve options if so
_.each(this.getShortcutMap(), function(regex, method) { _.each(GitCommands.getShortcutMap(), function(regex, method) {
var results = regex.exec(str); var results = regex.exec(str);
if (results) { if (results) {
str = method + ' ' + str.slice(results[0].length); str = method + ' ' + str.slice(results[0].length);
@ -16278,7 +16375,7 @@ var Command = Backbone.Model.extend({
var fullCommand = str.slice('git '.length); var fullCommand = str.slice('git '.length);
// see if we support this particular command // see if we support this particular command
_.each(this.getRegexMap(), function(regex, method) { _.each(GitCommands.getRegexMap(), function(regex, method) {
if (regex.exec(fullCommand)) { if (regex.exec(fullCommand)) {
this.set('options', fullCommand.slice(method.length + 1)); this.set('options', fullCommand.slice(method.length + 1));
this.set('method', method); this.set('method', method);
@ -19803,6 +19900,7 @@ var CommitCollection = Collections.CommitCollection;
var BranchCollection = Collections.BranchCollection; var BranchCollection = Collections.BranchCollection;
var GitVisuals = require('../visuals').GitVisuals; var GitVisuals = require('../visuals').GitVisuals;
var InputWaterfall = require('../level/inputWaterfall').InputWaterfall;
var Visualization = Backbone.View.extend({ var Visualization = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
@ -19824,6 +19922,10 @@ var Visualization = Backbone.View.extend({
var Main = require('../app'); var Main = require('../app');
this.events = Main.getEvents(); this.events = Main.getEvents();
// hook the git engine up to the command input
this.inputWaterfall = new InputWaterfall();
this.commitCollection = new CommitCollection(); this.commitCollection = new CommitCollection();
this.branchCollection = new BranchCollection(); this.branchCollection = new BranchCollection();

View file

@ -1,17 +0,0 @@
var _ = require('underscore');
var Backbone = require('backbone');
var Main = require('../main');
function InputWaterfall() {
Main.getEvents().on('processCommand', this.process)
};
InputWaterfall.prototype.listen = function() {
Main.getEvents().
};
exports.InputWaterfall = InputWaterfall;

31
src/js/git/commands.js Normal file
View file

@ -0,0 +1,31 @@
var getRegexMap = function() {
return {
// ($|\s) means that we either have to end the string
// after the command or there needs to be a space for options
commit: /^commit($|\s)/,
add: /^add($|\s)/,
checkout: /^checkout($|\s)/,
rebase: /^rebase($|\s)/,
reset: /^reset($|\s)/,
branch: /^branch($|\s)/,
revert: /^revert($|\s)/,
log: /^log($|\s)/,
merge: /^merge($|\s)/,
show: /^show($|\s)/,
status: /^status($|\s)/,
'cherry-pick': /^cherry-pick($|\s)/
};
};
var getShortcutMap = function() {
return {
'git commit': /^gc($|\s)/,
'git add': /^ga($|\s)/,
'git checkout': /^go($|\s)/,
'git rebase': /^gr($|\s)/,
'git branch': /^gb($|\s)/
};
};
exports.getRegexMap = getRegexMap;
exports.getShortcutMap = getShortcutMap;

View file

@ -28,7 +28,7 @@ function GitEngine(options) {
this.commandOptions = {}; this.commandOptions = {};
this.generalArgs = []; this.generalArgs = [];
this.events.on('processCommand', this.dispatch, this); this.events.on('processGitCommand', this.dispatch, this);
// backbone or something uses _.uniqueId, so we make our own here // backbone or something uses _.uniqueId, so we make our own here
this.uniqueId = (function() { this.uniqueId = (function() {

View file

@ -0,0 +1,38 @@
var _ = require('underscore');
var Backbone = require('backbone');
var Main = require('../app');
/**
* This class supports a few things we need for levels:
~ A disabled map (to prevent certain git commands from firing)
~ A post-git command hook (to compare the git tree against the solution)
~ Extra level-specific commands (like help, hint, etc) that are async
**/
function InputWaterfall(options) {
options = options || {};
this.listenEvent = options.listenEvent || 'processCommand';
this.disabledMap = options.disabledMap || {};
console.log('made');
this.listen();
}
InputWaterfall.prototype.listen = function() {
Main.getEvents().on(this.listenEvent, this.process, this);
};
InputWaterfall.prototype.mute = function() {
Main.getEvents().off(this.listenEvent, this.process, this);
};
InputWaterfall.prototype.process = function(command, callback) {
console.log('processing');
// for now, just immediately fire it
Main.getEvents().trigger('processGitCommand', command, callback);
};
exports.InputWaterfall = InputWaterfall;

View file

@ -3,6 +3,7 @@ var _ = require('underscore');
var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone; var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone;
var Errors = require('../util/errors'); var Errors = require('../util/errors');
var GitCommands = require('../git/commands');
var CommandProcessError = Errors.CommandProcessError; var CommandProcessError = Errors.CommandProcessError;
var GitError = Errors.GitError; var GitError = Errors.GitError;
@ -105,35 +106,6 @@ var Command = Backbone.Model.extend({
this.set('result', this.get('error').toResult()); this.set('result', this.get('error').toResult());
}, },
getShortcutMap: function() {
return {
'git commit': /^gc($|\s)/,
'git add': /^ga($|\s)/,
'git checkout': /^go($|\s)/,
'git rebase': /^gr($|\s)/,
'git branch': /^gb($|\s)/
};
},
getRegexMap: function() {
return {
// ($|\s) means that we either have to end the string
// after the command or there needs to be a space for options
commit: /^commit($|\s)/,
add: /^add($|\s)/,
checkout: /^checkout($|\s)/,
rebase: /^rebase($|\s)/,
reset: /^reset($|\s)/,
branch: /^branch($|\s)/,
revert: /^revert($|\s)/,
log: /^log($|\s)/,
merge: /^merge($|\s)/,
show: /^show($|\s)/,
status: /^status($|\s)/,
'cherry-pick': /^cherry-pick($|\s)/
};
},
getSandboxCommands: function() { getSandboxCommands: function() {
return [ return [
[/^ls/, function() { [/^ls/, function() {
@ -224,7 +196,7 @@ var Command = Backbone.Model.extend({
// then check if shortcut exists, and replace, but // then check if shortcut exists, and replace, but
// preserve options if so // preserve options if so
_.each(this.getShortcutMap(), function(regex, method) { _.each(GitCommands.getShortcutMap(), function(regex, method) {
var results = regex.exec(str); var results = regex.exec(str);
if (results) { if (results) {
str = method + ' ' + str.slice(results[0].length); str = method + ' ' + str.slice(results[0].length);
@ -248,7 +220,7 @@ var Command = Backbone.Model.extend({
var fullCommand = str.slice('git '.length); var fullCommand = str.slice('git '.length);
// see if we support this particular command // see if we support this particular command
_.each(this.getRegexMap(), function(regex, method) { _.each(GitCommands.getRegexMap(), function(regex, method) {
if (regex.exec(fullCommand)) { if (regex.exec(fullCommand)) {
this.set('options', fullCommand.slice(method.length + 1)); this.set('options', fullCommand.slice(method.length + 1));
this.set('method', method); this.set('method', method);

View file

@ -7,6 +7,7 @@ var CommitCollection = Collections.CommitCollection;
var BranchCollection = Collections.BranchCollection; var BranchCollection = Collections.BranchCollection;
var GitVisuals = require('../visuals').GitVisuals; var GitVisuals = require('../visuals').GitVisuals;
var InputWaterfall = require('../level/inputWaterfall').InputWaterfall;
var Visualization = Backbone.View.extend({ var Visualization = Backbone.View.extend({
initialize: function(options) { initialize: function(options) {
@ -28,6 +29,10 @@ var Visualization = Backbone.View.extend({
var Main = require('../app'); var Main = require('../app');
this.events = Main.getEvents(); this.events = Main.getEvents();
// hook the git engine up to the command input
this.inputWaterfall = new InputWaterfall();
this.commitCollection = new CommitCollection(); this.commitCollection = new CommitCollection();
this.branchCollection = new BranchCollection(); this.branchCollection = new BranchCollection();