Giter Site home page Giter Site logo

Comments (7)

stephenlacy avatar stephenlacy commented on August 20, 2024

Are there any flags which must go after the command?

from gulp-git.

ajthor avatar ajthor commented on August 20, 2024

No, the command in its entirety should be something like: git checkout --orphan gh-pages.

I tried to do something like this:

gulp.src('.')
    .pipe(git.checkout('--orphan gh-pages'));

No good.
It has something to do with the way the arguments are cleaned up and organized.

Here is the code as is:

module.exports = function (branch, opt) {
  if(!opt) opt = {};
  if(!branch) throw new Error('gulp-git: Branch name is require git.checkout("name")');
  if(!opt.args) opt.args = ' ';

  function checkout(file, cb) {
    var cmd = "git checkout " + escape([branch]) + " " + opt.args;
    exec(cmd, {cwd: file.cwd}, function(err, stdout, stderr){
      if(err) cb(err);
      gutil.log(stdout, stderr);
      cb(null, file);
    });
  }

  // Return a stream
  return map(checkout);
};

Maybe, instead of using the flags passed in an object with only one property, it would be possible to leave it up to the user to put the flags in the correct place? That would add a little more openness to the API and be a little less restrictive.

Perhaps:

module.exports = function(args) {
  if(!args) throw new Error("Must supply args.");
  if(!Array.isArray(args)) args = [args];

  function checkout(file, cb) {
    var cmd = ["git checkout"].concat(args);
    exec(cmd.join(" "), {cwd: file.cwd()}, function(err, stdout, stderr) {

...

Any thoughts on this?

from gulp-git.

stephenlacy avatar stephenlacy commented on August 20, 2024

A thought:
Would this be capable of solving the argument issue?

var cmd = "git checkout " + opt.args + " " + escape([branch]);

result:

$ git checkout --orphan test2

How would having the concat and join allow the user to place the args as the second word or end of sentence?

from gulp-git.

ajthor avatar ajthor commented on August 20, 2024

Hi Steve,

The reason is that unless you're specifically looking to handle only the most basic use cases for git, the order of the flags and arguments can't really be determined by hard-coding an order in when you develop your library. The concat and join is just an idea to allow them to use any order they want.

It would be used like this:

git.checkout('--orphan', 'branchname');

Or:

git.commit('-a', '-m \"initial commit\"');

(The '-a' flag goes before the message, throws an error if it goes after)

At the very least, I suppose you could wrap the entire cmd in the escape function? It might allow the workaround I mentioned before... I'll have to test it out. Otherwise, you'll have to accept all possible arguments and code in an order manually for every single use-case scenario. For branch alone, there are about twelve or so...

Just a thought, you could do both ways. I.e. Accept the args object and accept an array as the main argument. Seems a little redundant to me, but it would cover all use cases and make the API more flexible.

Here's what that might look like:

module.exports = function() {
  var args = Array.prototype.slice.apply(arguments);
  if(!args) throw new Error("Must supply args.");

  var options, last = args.[args.length-1];
  if(last === Object(last)) {
    if(args.length < 1) throw new Error("Must supply something more than just an options object.");
    options = args.pop();
  }
  else {
    options = [];
  }

  function checkout(file, cb) {
    var cmd = ["git checkout"].concat(args, options);
    exec(cmd.join(" "), {cwd: file.cwd()}, function(err, stdout, stderr) {

...

from gulp-git.

ajthor avatar ajthor commented on August 20, 2024

Hi Steve,

The reason is that unless you're specifically looking to handle only the most basic use cases for git, the order of the flags and arguments can't really be determined by hard-coding an order in when you develop your library. The concat and join is just an idea to allow them to use any order they want.

It would be used like this:

git.checkout('--orphan', 'branchname');

Or:

git.commit('-a', '-m \"initial commit\"');

(The '-a' flag goes before the message, throws an error if it goes after)

At the very least, I suppose you could wrap the entire cmd in the escape function? It might allow the workaround I mentioned before... I'll have to test it out. Otherwise, you'll have to accept all possible arguments and code in an order manually for every single use-case scenario. For branch alone, there are about twelve or so...

Just a thought, you could do both ways. I.e. Accept the args object and accept an array as the main argument. Seems a little redundant to me, but it would cover all use cases and make the API more flexible.

Here's what that might look like:

module.exports = function() {
  var args = Array.prototype.slice.apply(arguments);
  if(!args) throw new Error("Must supply args.");

  var options, last = args.[args.length-1];
  if(last === Object(last)) {
    if(args.length < 1) throw new Error("Must supply something more than just an options object.");
    options = args.pop();
  }
  else {
    options = [];
  }

  function checkout(file, cb) {
    var cmd = ["git checkout"].concat(args, options);
    exec(cmd.join(" "), {cwd: file.cwd()}, function(err, stdout, stderr) {

...

from gulp-git.

stephenlacy avatar stephenlacy commented on August 20, 2024

Wrapping the entire cmd in the escape function would end up with the arguments detected as strings, and quoted.
What if the arguments had a second flag to go before the command? such as 'preargs' and 'args' ?

from gulp-git.

ajthor avatar ajthor commented on August 20, 2024

Adding a 'preargs' option would unnecessarily complicate the API in my opinion. It's not entirely clear and it still doesn't allow for the full flexibility of git commands. If you were to go that route, it'd almost make more sense to hard-code certain flags in to allow for them, but then you'd have to modify the code often to allow for a lot of flags and Git updates.

On May 14, 2014, at 11:01 PM, Steve Lacy [email protected] wrote:

Wrapping the entire cmd in the escape function would end up with the arguments detected as strings, and quoted.
What if the arguments had a second flag to go before the command? such as 'preargs' and 'args' ?


Reply to this email directly or view it on GitHub.

from gulp-git.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.