diff --git a/README.md b/README.md index 1025260..3b29415 100644 --- a/README.md +++ b/README.md @@ -197,3 +197,4 @@ Fork our code, make a new branch, and send a pull request. * undo last commit * add custom files/directories to .gitignore * **introduction scene to Git** (for those new to Git) +* [switch to previously selected branch](https://github.com/qw3rtman/gg/issues/40) diff --git a/bin/gg b/bin/gg index 772a42e..95fd94b 100755 --- a/bin/gg +++ b/bin/gg @@ -9,130 +9,133 @@ var cli = require('cli'); cli.parse(null, ['v', 'version', '-v', '--version', 'i', 'init', 'initialize', 'ig', 'ignore', 'cl', 'clone', 'a', 'aa', 'add', 'c', 'ci', 'commit', 'cn', 'commitnoadd', 'p', 'push', 'pl', 'pull', 'f', 'fetch', 'fa', 's', 'status', 'l', 'log', 'b', 'branch', 'ch', 'checkout']); -switch (cli.command) { - // Version. - case 'v': - case 'version': - case '-v': - case '--version': - gg.version(); - break; - - // Initialize. - case 'i': - case 'init': - case 'initialize': - gg.init(); - break; - - // .gitignores. - case 'ig': - case 'ignore': - var template = cli.args.join('-'); - - gg.ignore(template); - break; - - // Clone. - case 'cl': - case 'clone': - var repository = cli.args[0]; - - if (typeof repository === 'undefined') { - repository = ''; - } - - if (repository.substring(0, 7) === 'http://' || repository.substring(0, 8) === 'https://') { // URL. - gg.clone(repository); - } else if (repository.indexOf('@') > -1 && repository.indexOf(':') > -1) { // SSH. - gg.clone(repository); - } else { - gg.cloneGitHub(repository); - } - break; - - // Add. - case 'a': - case 'add': - if (cli.args[1] === '' || typeof cli.args[1] !== 'undefined') { - var options = cli.args[0]; - var target = cli.args[1]; - gg.addOptions(options, target); - } else { - var target = cli.args[0]; - gg.addSingle(target); - } - break; - - // Add all. - case 'aa': // Short for 'add all'. - gg.addAll(); - break; - - // Commit. - case 'c': - case 'ci': - case 'commit': - var message = cli.args.join(' '); - gg.commit(message, true); - break; - - // Commit without adding. - case 'cn': - var message = cli.args.join(' '); - gg.commit(message, false); - break; - - // Push. - case 'p': - case 'push': - gg.push(); - break; - - // Pull. - case 'pl': - case 'pull': - gg.pull(); - break; - - // Fetch. - case 'f': - case 'fetch': - gg.fetch(); - break; - - // Fetch all. - case 'fa': // Short for 'fetch --all'. - gg.fetchAll(); - break; - - // Status. - case 's': - case 'st': - case 'status': - gg.status(); - break; - - // Log. - case 'l': - case 'log': - var file = cli.args.join(' '); - gg.log(file); - break; - - // Branch. - case 'b': - case 'branch': - var branch = cli.args.join('-'); - gg.branch(branch); - break; - - // Checkout. - case 'ch': - case 'checkout': - var branch = cli.args.join('-'); - gg.checkout(branch); - break; +gg.repoCheck(parseCommand); +function parseCommand() { + switch (cli.command) { + // Version. + case 'v': + case 'version': + case '-v': + case '--version': + gg.version(); + break; + + // Initialize. + case 'i': + case 'init': + case 'initialize': + gg.init(); + break; + + // .gitignores. + case 'ig': + case 'ignore': + var template = cli.args.join('-'); + + gg.ignore(template); + break; + + // Clone. + case 'cl': + case 'clone': + var repository = cli.args[0]; + + if (typeof repository === 'undefined') { + repository = ''; + } + + if (repository.substring(0, 7) === 'http://' || repository.substring(0, 8) === 'https://') { // URL. + gg.clone(repository); + } else if (repository.indexOf('@') > -1 && repository.indexOf(':') > -1) { // SSH. + gg.clone(repository); + } else { + gg.cloneGitHub(repository); + } + break; + + // Add. + case 'a': + case 'add': + if (cli.args[1] === '' || typeof cli.args[1] !== 'undefined') { + var options = cli.args[0]; + var target = cli.args[1]; + gg.addOptions(options, target); + } else { + var target = cli.args[0]; + gg.addSingle(target); + } + break; + + // Add all. + case 'aa': // Short for 'add all'. + gg.addAll(); + break; + + // Commit. + case 'c': + case 'ci': + case 'commit': + var message = cli.args.join(' '); + gg.commit(message, true); + break; + + // Commit without adding. + case 'cn': + var message = cli.args.join(' '); + gg.commit(message, false); + break; + + // Push. + case 'p': + case 'push': + gg.push(); + break; + + // Pull. + case 'pl': + case 'pull': + gg.pull(); + break; + + // Fetch. + case 'f': + case 'fetch': + gg.fetch(); + break; + + // Fetch all. + case 'fa': // Short for 'fetch --all'. + gg.fetchAll(); + break; + + // Status. + case 's': + case 'st': + case 'status': + gg.status(); + break; + + // Log. + case 'l': + case 'log': + var file = cli.args.join(' '); + gg.log(file); + break; + + // Branch. + case 'b': + case 'branch': + var branch = cli.args.join('-'); + gg.branch(branch); + break; + + // Checkout. + case 'ch': + case 'checkout': + var branch = cli.args.join('-'); + gg.checkout(branch); + break; + } } gg.upToDate(false, true); diff --git a/lib/gg.js b/lib/gg.js index 12835bf..6660e41 100644 --- a/lib/gg.js +++ b/lib/gg.js @@ -600,3 +600,12 @@ exports.checkout = function(branch) { }); } }; + +exports.repoCheck = function(callback) { + exec('git rev-parse', function(error, stdout, stderr) { + if (stderr.length === 0) { + return callback(); + } + console.log(whoops('[✖] Not in a valid git repository! Run "gg i" to initialise one.')); + }); +};