1

I am trying to get a script rolling that:

  • clones a repo (e.g. git clone someClonePath.git), then
  • checks out a tag (git checkout tags/someTag)
gulp.task('clone', function(){
  git.clone('somepath/cloneDir.git', function (err) {
    if (err) {
      throw err;
    }
  });
});


gulp.task('checkout',['clone'], function(){
  process.chdir('./cloneDir');
  git.checkout('tags/' + argv.tag,function (err) {
    if (err) {
      throw err;
    }
  });
});

The checkout completes (fails) before the clone does. The problem here lies with my understanding of async tasks in gulp. How can I verify that the clone has succeeded to 'cloneDir' prior to checking out a tag using gulp-git?

RhinoWalrus
  • 3,009
  • 2
  • 32
  • 41

1 Answers1

1

A few things after reviewing your updated post.

  1. For dependent tasks to work you can use a callback function or simply return something, which is preferable so that you don't tightly couple tasks which is what using callbacks does in this case.
  2. it looks like you want to clone to a directory of your choosing. to do that, you have to specify some arguments to the git.clone command as well as an argument when trying to checkout.
  3. Also verify that you are using a valid URL to do the clone.

Try this:

gulp.task('clone', function(){
  return git.clone('URL-TO-REMOTE-REPO', {args: './cloneDir'}, function (err) {
    if (err) {
      throw err;
    }
  });
});


gulp.task('checkout',['clone'], function(){
  git.checkout('tags/' + argv.tag,{ cwd: './cloneDir' }, function (err) {
    if (err) {
      throw err;
    }
  });
});
Anil
  • 2,539
  • 6
  • 33
  • 42
  • Maybe you should better use callbacks? [Check this](http://stackoverflow.com/a/26390567/5279817) – stek29 Apr 06 '16 at 15:41
  • @stek29, callbacks are another option, which were noted in the link that I posted as a comment to the post. the callback option didn't work for the person that made the post at the link I shared though. – Anil Apr 06 '16 at 15:41
  • isn't they better? They could shorten code, because it also replaces `if`/`throw` part. Also AFAIK callbacks are more JS-style. – stek29 Apr 06 '16 at 15:43
  • Regarding returning git.clone, I get the same behavior. Console looks like this (to demonstrate the timing issue): [10:42:56] Starting 'clone'... [10:42:56] Finished 'clone' after 4.97 ms [10:42:56] Starting 'checkout'... [10:42:56] 'checkout' errored after 157 μs [10:42:56] Error: ENOENT, no such file or directory ... [10:42:59] Cloning into 'cloneDir'... – RhinoWalrus Apr 06 '16 at 15:45
  • callbacks may be more 'JS Style' however, gulp excels with the use of returning streams rather than using callbacks. Also, using callbacks tightly couples tasks; which is not how gulp tasks should be setup. using return is a better way of solving particular this issue IMO. – Anil Apr 06 '16 at 15:45
  • git.clone is supposed to checkout from a URL, not a local folder as you have stated in your post. Are you sure you're cloning from the proper URL? Also, looking at [these examples](https://www.npmjs.com/package/gulp-git), it doesn't look like you have to change directories when checking out. – Anil Apr 06 '16 at 15:50
  • Now if you do want to clone to a different directory, you have to pass the git.clone command args like this: `{args: './sub/folder'}`. – Anil Apr 06 '16 at 15:51
  • Sorry, could have been more clear there - is pointing at the appropriate url, so 'someUrl/cloneDir.git'. Using a callback has me rolling. Thanks for the info! – RhinoWalrus Apr 06 '16 at 15:53
  • Updated my post to use proper arguments for checkout for alternate working directory – Anil Apr 06 '16 at 16:02