mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-29 01:10:04 +02:00
big command factoring
This commit is contained in:
parent
af3d440874
commit
5ec8a2916f
9 changed files with 351 additions and 127 deletions
108
src/js/commands/index.js
Normal file
108
src/js/commands/index.js
Normal file
|
@ -0,0 +1,108 @@
|
|||
var _ = require('underscore');
|
||||
var Q = require('q');
|
||||
|
||||
var intl = require('../intl');
|
||||
|
||||
var Errors = require('../util/errors');
|
||||
var GitError = Errors.GitError;
|
||||
var CommandResult = Errors.CommandResult;
|
||||
|
||||
var Commands = {
|
||||
commit: function(engine, command) {
|
||||
var commandOptions = command.getSupportedMap();
|
||||
command.acceptNoGeneralArgs();
|
||||
|
||||
if (commandOptions['-am'] && (
|
||||
commandOptions['-a'] || commandOptions['-m'])) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-error-options')
|
||||
});
|
||||
}
|
||||
|
||||
var msg = null;
|
||||
var args = null;
|
||||
if (commandOptions['-a']) {
|
||||
command.addWarning(intl.str('git-warning-add'));
|
||||
}
|
||||
|
||||
if (commandOptions['-am']) {
|
||||
args = commandOptions['-am'];
|
||||
command.validateArgBounds(args, 1, 1, '-am');
|
||||
msg = args[0];
|
||||
}
|
||||
|
||||
if (commandOptions['-m']) {
|
||||
args = commandOptions['-m'];
|
||||
command.validateArgBounds(args, 1, 1, '-m');
|
||||
msg = args[0];
|
||||
}
|
||||
|
||||
var newCommit = engine.commit();
|
||||
if (msg) {
|
||||
msg = msg
|
||||
.replace(/"/g, '"')
|
||||
.replace(/^"/g, '')
|
||||
.replace(/"$/g, '');
|
||||
|
||||
newCommit.set('commitMessage', msg);
|
||||
}
|
||||
|
||||
var promise = engine.animationFactory.playCommitBirthPromiseAnimation(
|
||||
newCommit,
|
||||
engine.gitVisuals
|
||||
);
|
||||
engine.animationQueue.thenFinish(promise);
|
||||
},
|
||||
|
||||
checkout: function(engine, command) {
|
||||
var commandOptions = command.getSupportedMap();
|
||||
var generalArgs = command.getGeneralArgs();
|
||||
|
||||
var args = null;
|
||||
if (commandOptions['-b']) {
|
||||
if (generalArgs.length) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-error-options')
|
||||
});
|
||||
}
|
||||
|
||||
// the user is really trying to just make a branch and then switch to it. so first:
|
||||
args = commandOptions['-b'];
|
||||
command.twoArgsImpliedHead(args, '-b');
|
||||
|
||||
var validId = engine.validateBranchName(args[0]);
|
||||
engine.branch(validId, args[1]);
|
||||
engine.checkout(validId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandOptions['-']) {
|
||||
// get the heads last location
|
||||
var lastPlace = engine.HEAD.get('lastLastTarget');
|
||||
if (!lastPlace) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-result-nothing')
|
||||
});
|
||||
}
|
||||
engine.HEAD.set('target', lastPlace);
|
||||
return;
|
||||
}
|
||||
|
||||
if (commandOptions['-B']) {
|
||||
args = commandOptions['-B'];
|
||||
command.twoArgsImpliedHead(args, '-B');
|
||||
|
||||
engine.forceBranch(args[0], args[1]);
|
||||
engine.checkout(args[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
command.validateArgBounds(generalArgs, 1, 1);
|
||||
|
||||
engine.checkout(engine.crappyUnescape(generalArgs[0]));
|
||||
},
|
||||
|
||||
noComma: 123
|
||||
};
|
||||
|
||||
module.exports = Commands;
|
|
@ -14,6 +14,8 @@ var GitError = Errors.GitError;
|
|||
var CommandResult = Errors.CommandResult;
|
||||
var EventBaton = require('../util/eventBaton').EventBaton;
|
||||
|
||||
var Commands = require('../commands');
|
||||
|
||||
function GitEngine(options) {
|
||||
this.rootCommit = null;
|
||||
this.refs = {};
|
||||
|
@ -1207,50 +1209,6 @@ GitEngine.prototype.cherrypick = function(commit) {
|
|||
return newCommit;
|
||||
};
|
||||
|
||||
GitEngine.prototype.commitStarter = function() {
|
||||
this.acceptNoGeneralArgs();
|
||||
if (this.commandOptions['-am'] && (
|
||||
this.commandOptions['-a'] || this.commandOptions['-m'])) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-error-options')
|
||||
});
|
||||
}
|
||||
|
||||
var msg = null;
|
||||
var args = null;
|
||||
if (this.commandOptions['-a']) {
|
||||
this.command.addWarning(intl.str('git-warning-add'));
|
||||
}
|
||||
|
||||
if (this.commandOptions['-am']) {
|
||||
args = this.commandOptions['-am'];
|
||||
this.validateArgBounds(args, 1, 1, '-am');
|
||||
msg = args[0];
|
||||
}
|
||||
|
||||
if (this.commandOptions['-m']) {
|
||||
args = this.commandOptions['-m'];
|
||||
this.validateArgBounds(args, 1, 1, '-m');
|
||||
msg = args[0];
|
||||
}
|
||||
|
||||
var newCommit = this.commit();
|
||||
if (msg) {
|
||||
msg = msg
|
||||
.replace(/"/g, '"')
|
||||
.replace(/^"/g, '')
|
||||
.replace(/"$/g, '');
|
||||
|
||||
newCommit.set('commitMessage', msg);
|
||||
}
|
||||
|
||||
var promise = this.animationFactory.playCommitBirthPromiseAnimation(
|
||||
newCommit,
|
||||
this.gitVisuals
|
||||
);
|
||||
this.animationQueue.thenFinish(promise);
|
||||
};
|
||||
|
||||
GitEngine.prototype.commit = function() {
|
||||
var targetCommit = this.getCommitFromRef(this.HEAD);
|
||||
var id = null;
|
||||
|
@ -1856,51 +1814,6 @@ GitEngine.prototype.merge = function(targetSource) {
|
|||
return mergeCommit;
|
||||
};
|
||||
|
||||
GitEngine.prototype.checkoutStarter = function() {
|
||||
var args = null;
|
||||
if (this.commandOptions['-b']) {
|
||||
if (this.generalArgs.length) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-error-options')
|
||||
});
|
||||
}
|
||||
|
||||
// the user is really trying to just make a branch and then switch to it. so first:
|
||||
args = this.commandOptions['-b'];
|
||||
this.twoArgsImpliedHead(args, '-b');
|
||||
|
||||
var validId = this.validateBranchName(args[0]);
|
||||
this.branch(validId, args[1]);
|
||||
this.checkout(validId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.commandOptions['-']) {
|
||||
// get the heads last location
|
||||
var lastPlace = this.HEAD.get('lastLastTarget');
|
||||
if (!lastPlace) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-result-nothing')
|
||||
});
|
||||
}
|
||||
this.HEAD.set('target', lastPlace);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.commandOptions['-B']) {
|
||||
args = this.commandOptions['-B'];
|
||||
this.twoArgsImpliedHead(args, '-B');
|
||||
|
||||
this.forceBranch(args[0], args[1]);
|
||||
this.checkout(args[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
this.validateArgBounds(this.generalArgs, 1, 1);
|
||||
|
||||
this.checkout(this.crappyUnescape(this.generalArgs[0]));
|
||||
};
|
||||
|
||||
GitEngine.prototype.checkout = function(idOrTarget) {
|
||||
var target = this.resolveID(idOrTarget);
|
||||
if (target.get('id') === 'HEAD') {
|
||||
|
@ -2068,8 +1981,14 @@ GitEngine.prototype.dispatch = function(command, deferred) {
|
|||
});
|
||||
|
||||
try {
|
||||
var methodName = command.get('method').replace(/-/g, '') + 'Starter';
|
||||
this[methodName]();
|
||||
var methodName = command.get('method').replace(/-/g, '');
|
||||
// first check out module
|
||||
if (Commands[methodName]) {
|
||||
Commands[methodName](this, this.command);
|
||||
} else {
|
||||
var startName = methodName + 'Starter';
|
||||
this[startName]();
|
||||
}
|
||||
} catch (err) {
|
||||
this.filterError(err);
|
||||
// short circuit animation by just setting error and returning
|
||||
|
|
|
@ -7,6 +7,7 @@ var GitCommands = require('../git/commands');
|
|||
var GitOptionParser = GitCommands.GitOptionParser;
|
||||
|
||||
var ParseWaterfall = require('../level/parseWaterfall').ParseWaterfall;
|
||||
var intl = require('../intl');
|
||||
|
||||
var CommandProcessError = Errors.CommandProcessError;
|
||||
var GitError = Errors.GitError;
|
||||
|
@ -52,6 +53,62 @@ var Command = Backbone.Model.extend({
|
|||
this.set('warnings', []);
|
||||
},
|
||||
|
||||
getGeneralArgs: function() {
|
||||
return this.get('generalArgs');
|
||||
},
|
||||
|
||||
getSupportedMap: function() {
|
||||
return this.get('supportedMap');
|
||||
},
|
||||
|
||||
acceptNoGeneralArgs: function() {
|
||||
if (this.getGeneralArgs().length) {
|
||||
throw new GitError({
|
||||
msg: intl.str('git-error-no-general-args')
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
twoArgsImpliedHead: function(args, option) {
|
||||
// our args we expect to be between 1 and 2
|
||||
this.validateArgBounds(args, 1, 2, option);
|
||||
// and if it's one, add a HEAD to the back
|
||||
if (args.length == 1) {
|
||||
args.push('HEAD');
|
||||
}
|
||||
},
|
||||
|
||||
// this is a little utility class to help arg validation that happens over and over again
|
||||
validateArgBounds: function(args, lower, upper, option) {
|
||||
var what = (option === undefined) ?
|
||||
'git ' + this.get('method') :
|
||||
this.get('method') + ' ' + option + ' ';
|
||||
what = 'with ' + what;
|
||||
|
||||
if (args.length < lower) {
|
||||
throw new GitError({
|
||||
msg: intl.str(
|
||||
'git-error-args-few',
|
||||
{
|
||||
lower: String(lower),
|
||||
what: what
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
if (args.length > upper) {
|
||||
throw new GitError({
|
||||
msg: intl.str(
|
||||
'git-error-args-many',
|
||||
{
|
||||
upper: String(upper),
|
||||
what: what
|
||||
}
|
||||
)
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
validateAtInit: function() {
|
||||
if (this.get('rawStr') === null) {
|
||||
throw new Error('Give me a string!');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue