really feeling good about where i am

This commit is contained in:
Peter Cottle 2012-09-11 23:34:26 -07:00
parent 9b62f06bc2
commit 9221088853
4 changed files with 110 additions and 38 deletions

View file

@ -119,14 +119,14 @@ Command.prototype.gitParse = function(str) {
*/ */
function OptionParser(method, options) { function OptionParser(method, options) {
this.method = method; this.method = method;
this.options = options; this.rawOptions = options;
this.supportedMap = this.getMasterOptionMap()[method]; this.supportedMap = this.getMasterOptionMap()[method];
if (this.supportedMap === undefined) { if (this.supportedMap === undefined) {
throw new Error('No option map for ' + method); throw new Error('No option map for ' + method);
} }
this.generalArgs = [];
this.explodeAndSet(); this.explodeAndSet();
} }
@ -137,14 +137,17 @@ OptionParser.prototype.getMasterOptionMap = function() {
return { return {
commit: { commit: {
'--amend': false, '--amend': false,
'-a': false '-a': false,
'-am': false
}, },
add: {}, add: {},
branch: { branch: {
'-d': false, '-d': false,
'-D': false '-D': false
}, },
checkout: {}, checkout: {
'-b': false
},
reset: { reset: {
'--hard': false, '--hard': false,
}, },
@ -154,22 +157,39 @@ OptionParser.prototype.getMasterOptionMap = function() {
}; };
OptionParser.prototype.explodeAndSet = function() { OptionParser.prototype.explodeAndSet = function() {
var exploded = this.options.split(' '); // split on spaces, except when inside quotes, and strip quotes after.
var options =[]; // for some reason the regex includes the quotes even if i move the parantheses
// inside
_.each(exploded, function(part) { var exploded = this.rawOptions.match(/('.*?'|".*?"|\S+)/g) || [];
if (part.slice(0,1) == '-') { _.each(exploded, function(part, i) {
options.push(part); exploded[i] = part.replace(/['"]/g, '');
}
}); });
_.each(options, function(option) {
if (this.supportedMap[option] !== undefined) { for (var i = 0; i < exploded.length; i++) {
this.supportedMap[option] = true; var part = exploded[i];
if (part.slice(0,1) == '-') {
// it's an option, check supportedMap
if (this.supportedMap[part] === undefined) {
throw new CommandProcessError('The option "' + part + '" is not supported');
}
// go through and include all the next args until we hit another option or the end
var optionArgs = [];
var next = i + 1;
while (next < exploded.length && exploded[next].slice(0,1) != '-') {
optionArgs.push(exploded[next]);
next += 1;
}
i = next - 1;
// **phew** we are done grabbing those. theseArgs is truthy even with an empty array
this.supportedMap[part] = optionArgs;
} else { } else {
// this option is not supported // must be a general arg
throw new CommandProcessError('The option "' + option + '" is not supported'); this.generalArgs.push(part);
} }
}, this); }
// done! // done!
}; };

View file

@ -21,3 +21,11 @@ CommandResult.prototype.toString = function() {
CommandResult.prototype.toResult = function() { CommandResult.prototype.toResult = function() {
return this.msg.replace('\n', '</br>'); return this.msg.replace('\n', '</br>');
}; };
function GitError(msg) {
this.msg = msg;
}
GitError.prototype.toString = function() {
return 'Git Error: ' + this.msg;
};

View file

@ -16,7 +16,8 @@ function GitEngine() {
// global variable to keep track of the options given // global variable to keep track of the options given
// along with the command call. // along with the command call.
this.currentOptions = {}; this.commandOptions = {};
this.generalArgs = [];
events.on('gitCommandReady', _.bind(this.dispatch, this)); events.on('gitCommandReady', _.bind(this.dispatch, this));
@ -98,18 +99,31 @@ GitEngine.prototype.makeCommit = function(parent) {
return commit; return commit;
}; };
GitEngine.prototype.acceptNoGeneralArgs = function() {
if (this.generalArgs.length) {
throw new GitError("That command accepts no general arguments");
}
};
GitEngine.prototype.commitStarter = function() {
this.acceptNoGeneralArgs();
if (this.commandOptions['-a']) {
events.trigger('commandProcessWarn', 'No need to add files in this demo');
}
if (this.commandOptions['-am']) {
events.trigger('commandProcessWarn', "Don't worry about adding files or commit messages in this demo");
}
this.commit();
};
GitEngine.prototype.commit = function() { GitEngine.prototype.commit = function() {
var targetCommit = this.getCommitFromRef(this.HEAD); var targetCommit = this.getCommitFromRef(this.HEAD);
// if we want to ammend, go one above // if we want to ammend, go one above
if (this.currentOptions['--amend']) { if (this.commandOptions['--amend']) {
targetCommit = this.resolveId('HEAD~1'); targetCommit = this.resolveId('HEAD~1');
} }
if (this.currentOptions['-a']) {
events.trigger('commandProcessWarn', 'No need to add files in this demo');
}
var newCommit = this.makeCommit(targetCommit); var newCommit = this.makeCommit(targetCommit);
if (this.getDetachedHead()) { if (this.getDetachedHead()) {
events.trigger('commandProcessWarn', 'Warning!! Detached HEAD state'); events.trigger('commandProcessWarn', 'Warning!! Detached HEAD state');
} else { } else {
@ -215,6 +229,14 @@ GitEngine.prototype.numBackFrom = function(commit, numBack) {
return pQueue.shift(0); return pQueue.shift(0);
}; };
GitEngine.prototype.checkoutStarter = function() {
if (this.generalArgs.length != 1) {
throw new GitError('I expect one argument along with git checkout (dont reference files)');
}
this.checkout(this.generalArgs[0]);
};
GitEngine.prototype.checkout = function(idOrTarget) { GitEngine.prototype.checkout = function(idOrTarget) {
console.log('the target', idOrTarget); console.log('the target', idOrTarget);
var target = this.resolveId(idOrTarget); var target = this.resolveId(idOrTarget);
@ -233,15 +255,36 @@ GitEngine.prototype.checkout = function(idOrTarget) {
this.HEAD.set('target', target); this.HEAD.set('target', target);
}; };
GitEngine.prototype.branch = function(name, ref, options) { GitEngine.prototype.branchStarter = function() {
ref = ref || 'HEAD'; // handle deletion first
options = options || {}; if (this.commandOptions['-d'] || this.commandOptions['-D']) {
if (!this.generalArgs.length) {
if (options['-d'] || options['-D']) { throw new GitError('I expect branch names when deleting');
this.deleteBranch(name); }
_.each(this.generalArgs, function(name) {
this.deleteBranch(name);
});
return; return;
} }
var len = this.generalArgs.length;
if (len > 2) {
throw new GitError('git branch with more than two general args does not make sense!');
}
if (len == 0) {
throw new Error('going to implement eventually');
//TODO: print branches, etc
return;
}
if (len == 1) {
// making a branch from where we are now
this.generalArgs.push('HEAD');
}
this.branch(this.generalArgs[0], this.generalArgs[1]);
};
GitEngine.prototype.branch = function(name, ref) {
var target = this.getCommitFromRef(ref); var target = this.getCommitFromRef(ref);
this.makeBranch(name, target); this.makeBranch(name, target);
}; };
@ -250,13 +293,13 @@ GitEngine.prototype.deleteBranch = function(name) {
// trying to delete, lets check our refs // trying to delete, lets check our refs
var target = this.resolveId(name); var target = this.resolveId(name);
if (target.get('type') !== 'branch') { if (target.get('type') !== 'branch') {
throw new Error("You can't delete things that arent branches with branch command"); throw new GitError("You can't delete things that arent branches with branch command");
} }
if (target.get('id') == 'master') { if (target.get('id') == 'master') {
throw new Error("You can't delete the master branch!"); throw new GitError("You can't delete the master branch!");
} }
if (this.HEAD.get('target') === target) { if (this.HEAD.get('target') === target) {
throw new Error("Cannot delete the branch you are currently on"); throw new GitError("Cannot delete the branch you are currently on");
} }
var id = target.get('id'); var id = target.get('id');
@ -265,8 +308,10 @@ GitEngine.prototype.deleteBranch = function(name) {
}; };
GitEngine.prototype.dispatch = function(commandObj) { GitEngine.prototype.dispatch = function(commandObj) {
this.currentOptions = commandObj.optionParser.supportedMap; this.commandOptions = commandObj.optionParser.supportedMap;
this[commandObj.method](); this.generalArgs = commandObj.optionParser.generalArgs;
this[commandObj.method + 'Starter']();
}; };
GitEngine.prototype.add = function() { GitEngine.prototype.add = function() {

View file

@ -13,9 +13,8 @@ var CommandLineView = Backbone.View.extend({
}, },
keyUp: function(e) { keyUp: function(e) {
console.log(e); // we need to capture some of these events.
// WARNING: this key map is not internationalized :(
// we need to capture some of these events
var keyMap = { var keyMap = {
// enter // enter
13: _.bind(function() { 13: _.bind(function() {