diff --git a/src/commandline.js b/src/commandline.js index 51388948..498e6e18 100644 --- a/src/commandline.js +++ b/src/commandline.js @@ -3,10 +3,9 @@ * @desc A parser for commands given */ function Command(str) { - this.results = { - msgs: [] - }; - this.command = null; + this.fullCommand = null; + this.options = null; + this.method = null; this.parse(str); } @@ -22,17 +21,20 @@ Command.prototype.getShortcutMap = function() { Command.prototype.getRegexMap = function() { return { - commit: /^commit\s*/, - add: /^add\s*/, - checkout: /^checkout\s*/, - rebase: /^rebase\s*/, - reset: /^reset\s*/ + // ($|\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)/, + revert: /^revert($|\s)/ }; }; Command.prototype.parse = function(str) { // first check if shortcut exists, and replace, but - // preserve options + // preserve options if so _.each(this.getShortcutMap(), function(regex, method) { var results = regex.exec(str); if (results) { @@ -42,17 +44,18 @@ Command.prototype.parse = function(str) { // see if begins with git if (str.slice(0,3) !== 'git') { - return this.nonGitCommand(); + throw new Error('Git commands only, sorry!'); } // now slice off command part - this.command = str.slice(4); + this.fullCommand = str.slice('git '.length); + // see if we support this particular command var matched = false; _.each(this.getRegexMap(), function(regex, method) { - if (regex.exec(this.command)) { - this.options = this.command.slice(method.length + 1); - this[method](); + if (regex.exec(this.fullCommand)) { + this.options = this.fullCommand.slice(method.length + 1); + this.method = method; // we should stop iterating, but the regex will only match // one command in practice matched = true; @@ -60,71 +63,48 @@ Command.prototype.parse = function(str) { }, this); if (!matched) { - this.results.msgs.push('The git command "' + this.command + - '" is not supported, sorry!'); + throw new Error( + "Sorry, this demo does not support that git command: " + this.fullCommand + ); } -}; - -Command.prototype.nonGitCommand = function() { - this.results.error = { - msg: 'Git only commands, sorry!' - }; -}; - -Command.prototype.commit = function() { - this.results.msgs.push( - 'Commiting with options "' + this.options + '"' - ); - - // commit for us means simply either ammending the current commit - // or just popping a new commit on top - var optionMap = { - // supported options - '--amend': false, - // pass through options, dont care but shouldnt fatal - '-a': false, - '-c': false, - '-C': false - }; - - this.options = new OptionParser(this.command, optionMap); - this.results.exec = function(gitEngine) { - gitEngine.commit(optionMap); - }; -}; - -Command.prototype.add = function() { - this.results.msgs.push( - "This demo is meant to demonstrate git branching, so don't worry " + - "about adding / staging files. Just go ahead and commit away!" - ); -}; - -Command.prototype.checkout = function() { - -}; - -Command.prototype.rebase = function() { - -}; - -Command.reset = function() { + this.optionParser = new OptionParser(this.method, this.options); }; /** * OptionParser */ -function OptionParser(str, supportedMap) { - this.str = str; - this.supportedMap = supportedMap; - this.results = { - unsupportedOptions: [] - }; +function OptionParser(method, options) { + this.method = metho;d + this.supportedMap = this.getMasterOptionMap()[method]; + this.unsupportedOptions = []; + + if (this.supportedMap === undefined) { + throw new Error('No option map for ' + method); + } this.explodeAndSet(); } +OptionParser.prototype.getMasterOptionMap = function() { + // here a value of false means that we support it, even if its just a pass-through + // option. If the value is not here (aka will be undefined later), we do not + // support it + return { + commit: { + '--amend': false, + '-a': false + }, + add: {}, + checkout: {}, + reset: { + '--hard': false, + }, + rebase: {}, + revert: {} + }; +}; + OptionParser.prototype.explodeAndSet = function() { var exploded = this.str.split(' '); var options =[]; @@ -138,7 +118,7 @@ OptionParser.prototype.explodeAndSet = function() { if (this.supportedMap[option] !== undefined) { this.supportedMap[option] = true; } else { - this.results.unsupportedOptions.push(option); + this.unsupportedOptions.push(option); } }, this); // done! diff --git a/src/git.js b/src/git.js index 76181ec2..719ade2d 100644 --- a/src/git.js +++ b/src/git.js @@ -249,6 +249,13 @@ GitEngine.prototype._deleteBranch = function(name) { delete this.refs[id]; }; +GitEngine.prototype.add = function() { + throw new Error( + "This demo is meant to demonstrate git branching, so don't worry about " + + "adding / staging files. Just go ahead and commit away!" + ); +}; + GitEngine.prototype.execute = function(command, callback) { // execute command, and when it's finished, call the callback // we still need to figure this out