From aaf7a462c0643691b20c50698fe3ab8e54bd0c61 Mon Sep 17 00:00:00 2001 From: Peter Cottle Date: Tue, 23 Oct 2012 09:27:19 -0700 Subject: [PATCH] local storage and general arg stuff --- lib/backbone.localStorage-min.js | 4 +++ src/git.js | 58 +++++++++++++++----------------- src/index.html | 1 + todo.txt | 3 +- 4 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 lib/backbone.localStorage-min.js diff --git a/lib/backbone.localStorage-min.js b/lib/backbone.localStorage-min.js new file mode 100644 index 00000000..577f6cf7 --- /dev/null +++ b/lib/backbone.localStorage-min.js @@ -0,0 +1,4 @@ +/** + * Backbone localStorage Adapter + * https://github.com/jeromegn/Backbone.localStorage + */(function(){function c(){return((1+Math.random())*65536|0).toString(16).substring(1)}function d(){return c()+c()+"-"+c()+"-"+c()+"-"+c()+"-"+c()+c()+c()}var a=this._,b=this.Backbone;b.LocalStorage=window.Store=function(a){this.name=a;var b=this.localStorage().getItem(this.name);this.records=b&&b.split(",")||[]},a.extend(b.LocalStorage.prototype,{save:function(){this.localStorage().setItem(this.name,this.records.join(","))},create:function(a){return a.id||(a.id=d(),a.set(a.idAttribute,a.id)),this.localStorage().setItem(this.name+"-"+a.id,JSON.stringify(a)),this.records.push(a.id.toString()),this.save(),a.toJSON()},update:function(b){return this.localStorage().setItem(this.name+"-"+b.id,JSON.stringify(b)),a.include(this.records,b.id.toString())||this.records.push(b.id.toString()),this.save(),b.toJSON()},find:function(a){return JSON.parse(this.localStorage().getItem(this.name+"-"+a.id))},findAll:function(){return a(this.records).chain().map(function(a){return JSON.parse(this.localStorage().getItem(this.name+"-"+a))},this).compact().value()},destroy:function(b){return this.localStorage().removeItem(this.name+"-"+b.id),this.records=a.reject(this.records,function(a){return a==b.id.toString()}),this.save(),b},localStorage:function(){return localStorage}}),b.LocalStorage.sync=window.Store.sync=b.localSync=function(a,b,c,d){var e=b.localStorage||b.collection.localStorage;typeof c=="function"&&(c={success:c,error:d});var f,g=$.Deferred&&$.Deferred();switch(a){case"read":f=b.id!=undefined?e.find(b):e.findAll();break;case"create":f=e.create(b);break;case"update":f=e.update(b);break;case"delete":f=e.destroy(b)}return f?(c.success(f),g&&g.resolve()):(c.error("Record not found"),g&&g.reject()),g&&g.promise()},b.ajaxSync=b.sync,b.getSyncMethod=function(a){return a.localStorage||a.collection&&a.collection.localStorage?b.localSync:b.ajaxSync},b.sync=function(a,c,d,e){return b.getSyncMethod(c).apply(this,[a,c,d,e])}})(); \ No newline at end of file diff --git a/src/git.js b/src/git.js index af332dd3..3e235904 100644 --- a/src/git.js +++ b/src/git.js @@ -314,8 +314,31 @@ GitEngine.prototype.acceptNoGeneralArgs = function() { } }; -GitEngine.prototype.validateArgBounds = function(args, lower, upper) { - // TODO +GitEngine.prototype.validateArgBounds = function(args, lower, upper, option) { + // this is a little utility class to help arg validation that happens over and over again + var what = 'with ' + (option === undefined) ? + 'git ' + this.command.get('method') : + this.command.get('method') + ' ' + option + ' '; + + if (args.length < lower) { + throw new GitError({ + msg: 'I expect at least ' + String(lower) + ' argument(s) ' + what + }); + } + if (args.length > upper) { + throw new GitError({ + msg: 'I expect at most ' + String(upper) + ' argument(s) ' + what + }); + } +}; + +GitEngine.prototype.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'); + } }; GitEngine.prototype.revertStarter = function() { @@ -1012,45 +1035,20 @@ GitEngine.prototype.branchStarter = function() { if (this.commandOptions['-f']) { var args = this.commandOptions['-f']; - if (args.length < 1) { - throw new GitError({ - msg: 'Give me an argument with -f so I know which branch to force!' - }); - } - - if (args.length > 2) { - throw new GitError({ - msg: 'Too many args for -f' - }); - } - - if (args.length == 1) { - args.push('HEAD'); - } + this.twoArgsImpliedHead(args, '-f'); // we want to force a branch somewhere this.forceBranch(args[0], args[1]); return; } - var len = this.generalArgs.length; - if (len > 2) { - throw new GitError({ - msg: 'git branch with more than two general args does not make sense!' - }); - } - - if (len == 0) { + if (this.generalArgs.length == 0) { this.printBranches(); return; } - - if (len == 1) { - // making a branch from where we are now - this.generalArgs.push('HEAD'); - } + this.twoArgsImpliedHead(this.generalArgs); this.branch(this.generalArgs[0], this.generalArgs[1]); }; diff --git a/src/index.html b/src/index.html index 3f1d439e..301d99fe 100644 --- a/src/index.html +++ b/src/index.html @@ -63,6 +63,7 @@ + diff --git a/todo.txt b/todo.txt index d358ca85..344329cb 100644 --- a/todo.txt +++ b/todo.txt @@ -9,8 +9,7 @@ Big Graphic things: Medium things: ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- Rebuilding trees from a JSON snapshot / blob. this should be easy... i think. if we remove the need for parents -- gitEngine loads from tree immediately, not the weird thing we have now +- gitEngine loads from tree immediately, not the weird thing we have now! Small things to implement: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~