mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-28 00:40:07 +02:00
commandline changes
This commit is contained in:
parent
34f547b56a
commit
8d42c362eb
2 changed files with 57 additions and 70 deletions
|
@ -3,10 +3,9 @@
|
||||||
* @desc A parser for commands given
|
* @desc A parser for commands given
|
||||||
*/
|
*/
|
||||||
function Command(str) {
|
function Command(str) {
|
||||||
this.results = {
|
this.fullCommand = null;
|
||||||
msgs: []
|
this.options = null;
|
||||||
};
|
this.method = null;
|
||||||
this.command = null;
|
|
||||||
|
|
||||||
this.parse(str);
|
this.parse(str);
|
||||||
}
|
}
|
||||||
|
@ -22,17 +21,20 @@ Command.prototype.getShortcutMap = function() {
|
||||||
|
|
||||||
Command.prototype.getRegexMap = function() {
|
Command.prototype.getRegexMap = function() {
|
||||||
return {
|
return {
|
||||||
commit: /^commit\s*/,
|
// ($|\s) means that we either have to end the string
|
||||||
add: /^add\s*/,
|
// after the command or there needs to be a space for options
|
||||||
checkout: /^checkout\s*/,
|
commit: /^commit($|\s)/,
|
||||||
rebase: /^rebase\s*/,
|
add: /^add($|\s)/,
|
||||||
reset: /^reset\s*/
|
checkout: /^checkout($|\s)/,
|
||||||
|
rebase: /^rebase($|\s)/,
|
||||||
|
reset: /^reset($|\s)/,
|
||||||
|
revert: /^revert($|\s)/
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Command.prototype.parse = function(str) {
|
Command.prototype.parse = function(str) {
|
||||||
// first check if shortcut exists, and replace, but
|
// first check if shortcut exists, and replace, but
|
||||||
// preserve options
|
// preserve options if so
|
||||||
_.each(this.getShortcutMap(), function(regex, method) {
|
_.each(this.getShortcutMap(), function(regex, method) {
|
||||||
var results = regex.exec(str);
|
var results = regex.exec(str);
|
||||||
if (results) {
|
if (results) {
|
||||||
|
@ -42,17 +44,18 @@ Command.prototype.parse = function(str) {
|
||||||
|
|
||||||
// see if begins with git
|
// see if begins with git
|
||||||
if (str.slice(0,3) !== 'git') {
|
if (str.slice(0,3) !== 'git') {
|
||||||
return this.nonGitCommand();
|
throw new Error('Git commands only, sorry!');
|
||||||
}
|
}
|
||||||
|
|
||||||
// now slice off command part
|
// 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;
|
var matched = false;
|
||||||
_.each(this.getRegexMap(), function(regex, method) {
|
_.each(this.getRegexMap(), function(regex, method) {
|
||||||
if (regex.exec(this.command)) {
|
if (regex.exec(this.fullCommand)) {
|
||||||
this.options = this.command.slice(method.length + 1);
|
this.options = this.fullCommand.slice(method.length + 1);
|
||||||
this[method]();
|
this.method = method;
|
||||||
// we should stop iterating, but the regex will only match
|
// we should stop iterating, but the regex will only match
|
||||||
// one command in practice
|
// one command in practice
|
||||||
matched = true;
|
matched = true;
|
||||||
|
@ -60,71 +63,48 @@ Command.prototype.parse = function(str) {
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
if (!matched) {
|
if (!matched) {
|
||||||
this.results.msgs.push('The git command "' + this.command +
|
throw new Error(
|
||||||
'" is not supported, sorry!');
|
"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
|
* OptionParser
|
||||||
*/
|
*/
|
||||||
function OptionParser(str, supportedMap) {
|
function OptionParser(method, options) {
|
||||||
this.str = str;
|
this.method = metho;d
|
||||||
this.supportedMap = supportedMap;
|
this.supportedMap = this.getMasterOptionMap()[method];
|
||||||
this.results = {
|
this.unsupportedOptions = [];
|
||||||
unsupportedOptions: []
|
|
||||||
};
|
if (this.supportedMap === undefined) {
|
||||||
|
throw new Error('No option map for ' + method);
|
||||||
|
}
|
||||||
|
|
||||||
this.explodeAndSet();
|
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() {
|
OptionParser.prototype.explodeAndSet = function() {
|
||||||
var exploded = this.str.split(' ');
|
var exploded = this.str.split(' ');
|
||||||
var options =[];
|
var options =[];
|
||||||
|
@ -138,7 +118,7 @@ OptionParser.prototype.explodeAndSet = function() {
|
||||||
if (this.supportedMap[option] !== undefined) {
|
if (this.supportedMap[option] !== undefined) {
|
||||||
this.supportedMap[option] = true;
|
this.supportedMap[option] = true;
|
||||||
} else {
|
} else {
|
||||||
this.results.unsupportedOptions.push(option);
|
this.unsupportedOptions.push(option);
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
// done!
|
// done!
|
||||||
|
|
|
@ -249,6 +249,13 @@ GitEngine.prototype._deleteBranch = function(name) {
|
||||||
delete this.refs[id];
|
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) {
|
GitEngine.prototype.execute = function(command, callback) {
|
||||||
// execute command, and when it's finished, call the callback
|
// execute command, and when it's finished, call the callback
|
||||||
// we still need to figure this out
|
// we still need to figure this out
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue