5

I am working on project and I want to commit and push on git using by gulp but I am facing some issue when I am running git task so push then task is not waiting for commit....

Anyone can do my help! I want to make the task like first run commit and then push automatically and don't run push task until complete commit task....

Gulp Task for Gulp Git Commit and Push!

var gulp              = require('gulp'),
    runSequence       = require('run-sequence'),
    gutil             = require('gulp-util'),
    git               = require('gulp-git'),
    prompt            = require('gulp-prompt');


/* task to commit and push all file on git
******************************************************************/
// git commit task with gulp prompt
gulp.task('gulp:commit', function(){
  // just source anything here - we just wan't to call the prompt for now
  gulp.src('./*')
  .pipe(prompt.prompt({
    type: 'input',
    name: 'commit',
    message: 'Please enter commit message...'
  }, function(res){
    // now add all files that should be committed
    // but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too
    return gulp.src([ '!node_modules/', './*' ], {buffer:false})
    .pipe(git.commit(res.commit));
   }));
});

// Run git push, remote is the remote repo, branch is the remote branch to push to
gulp.task('gulp:push', ['gulp:commit'], function(cb){
    git.push('origin', 'master', cb, function(err){
        if (err) throw err;
    });
});

// # task completed notification with green color!
gulp.task('gulp:done', ['gulp:push'], function(){
  console.log('');
  gutil.log(gutil.colors.green('************** Git push is done! **************'));
  console.log('');
});

// gulp task to commit and push data on git
gulp.task('git', function(){
  runSequence(['gulp:commit', 'gulp:push', 'gulp:done'])
});
Faizy
  • 69
  • 9

2 Answers2

1

The problem is you're telling runSequence plugin to run the tasks in parallel. The solution is really simple:

// gulp task to commit and push data on git
gulp.task('git', function(){
  return runSequence('gulp:commit', 'gulp:push', 'gulp:done');
});

By doing this you ensure that gulp:push will run only after gulp:commit finished, and after the push is done gulp:done will run.

Also, I recommend you return the stream in gulp:push and use the done callback parameter in gulp:done, if you don't do that gulp doesn't know when this tasks ends:

// git commit task with gulp prompt
gulp.task('gulp:commit', function(){
  // just source anything here - we just wan't to call the prompt for now
  return gulp.src('./*')
  .pipe(prompt.prompt({
    type: 'input',
    name: 'commit',
    message: 'Please enter commit message...'
  }, function(res){
    // now add all files that should be committed
    // but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too
    return gulp.src([ '!node_modules/', './*' ], {buffer:false})
    .pipe(git.commit(res.commit));
   }));
});

// Run git push, remote is the remote repo, branch is the remote branch to push to
gulp.task('gulp:push', ['gulp:commit'], function(cb){
  return git.push('origin', 'master', cb, function(err){
    if (err) throw err;
  });
});

// # task completed notification with green color!
gulp.task('gulp:done', ['gulp:push'], function(done){
  console.log('');
  gutil.log(gutil.colors.green('************** Git push is done! **************'));
  console.log('');
  done();
});

EDIT: You have to return the stream in gulp:commit task too, I changed my answer to show you how.

franmartosr
  • 378
  • 1
  • 5
  • 10
  • not working... and push task is not waiting for completed for commit task! – Faizy May 08 '16 at 11:28
  • You don't return the stream in **gulp:commit** too. I editted my answer to show you how, try that and tell me if it works. – franmartosr May 08 '16 at 12:11
  • thanks, this code is working but i noticed one thing that it is not pushing my current commit but pushing recent commit on gitihub! – Faizy May 08 '16 at 18:44
  • I think I cannot help you with that problem, I don't use **gulp-git** plugin myself. I am glad to see my answer help you, please give me an upvote if it was useful to you :D – franmartosr May 08 '16 at 19:27
0

gulp-git commit doesn't actually return a stream (by design). See Commit doesn't fire end #49.

To get around this, you can use a promise library like bluebird.

Here's what worked for me, as suggested by github user bfricka :

gulp.task('commit-changes', function (cb) {


    var q = require('bluebird'); //commit needs a a promise
    return new q(function (resolve, reject) {



        gulp.src('../')
                .pipe(git.add())
                .pipe(stream = git.commit("my commit message")).on('error', function (e) {
            console.log('No changes to commit');
        })

                .on('error', resolve) //resolve to allow the commit to resume even when there are not changes.
                .on('end', resolve);



        stream.resume(); //needed since commmit doesnt return stream by design.


        ;


    });

});
AndrewD
  • 4,924
  • 3
  • 30
  • 32