Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pausing file watchers until git checkout complete

What does git do when starting the checkout operation? Does it write out a lock file or something?

I use gulp, and I'd like it to "pause" the watchers if git is actively performing a checkout operation.

I'm okay with possibly having to "resave" something to kick the file watcher back into gear.

like image 517
enorl76 Avatar asked Jan 30 '15 14:01

enorl76


1 Answers

The function gulp.watch() returns an object that has, amongst other things, an end() method you can call - see the definition in the glob-watcher module.

You could do something along these lines:

var gulp = require('gulp');

function doGitCheckout(callback) {
  console.log('doing checkout...');
  setTimeout(function() {
    console.log('done checkout...');
    callback();
  }, 1000);
}

var watcher;

gulp.task('watch:start', function() {
  if (!watcher) {
    watcher = gulp.watch('dist/**/*', ['build']);
    console.log('started watcher');
  }
});

gulp.task('watch:stop', function() {
  if (watcher) {
    watcher.end();
    watcher = null;
    console.log('stopped watcher');
  }
});


gulp.task('git:checkout', ['watch:stop'], function(cb) {
  console.log('isWatching = ' + !!watcher);

  // Start the Git checkout...
  doGitCheckout(function() {
    // The Git checkout is finished!
    gulp.start('watch:start', function() {
      cb();
    });
  });
});

gulp.task('default', ['watch:start', 'git:checkout']);

Gist with this code here

So we keep a reference to the watcher object when we start watching, and have a task to stop the watcher if running. Our Git task depends on watch:stop to begin, and then starts the Git checkout, with a callback for when it's completed. The callback starts the task watch:start, and when that's done, we finally trigger the function cb to indicate that git:checkout has finished!

An alternative might be using the add(pattern) and remove(pattern) on the watch object to turn it off and then on again instead of destroying the object and recreating it.

like image 100
spiralx Avatar answered Nov 07 '22 08:11

spiralx