15

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.

enorl76
  • 2,562
  • 1
  • 25
  • 38

1 Answers1

6

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.

spiralx
  • 1,035
  • 7
  • 16
  • Thats great! Except I'm not kicking off git from within the gulp script. ...And, the gulp watchers are kicking off gulp tasks because git checkout is rewriting working copy files. I'm looking for a way to first check to see if a git checkout is active (like because maybe a .git/checkout.lock file exists or something similar?) and prevents the watcher from running tasks temporarily until git checkout is finished rewriting the working copy – enorl76 Feb 25 '15 at 21:43
  • Well there is a lock file mechanism - see [this document](https://github.com/git/git/blob/master/Documentation/technical/api-lockfile.txt) for technical details. But Git isn't using a lock file for basic operations, it looks as though it's only used for longer tasks like `git rebase`. It also doesn't stop read actions, as it's supposed to keep things consistent. – spiralx Feb 26 '15 at 13:07
  • If you have control of the Git process, then it would be possible to have the Gulp watcher listen on a socket and accept "suspend" and "resume" messages. You could send suspend before the Git process, and then resume after it's done. – spiralx Feb 26 '15 at 13:10
  • @spiralx did you perhaps implement what you are saying at any point? :) – trainoasis Nov 08 '17 at 07:38