Search code examples
gitgruntjsgithooksgrunt-contrib-requirejs

Adding git add to Grunt githooks plugin


I am currently working on implementing a pre-commit hook with grunt git hooks. I am new to using this plugin and it's a bit unclear if I can use this plugin to do what I initially set out to do.

Currently, I have two grunt tasks triggering for every git commit as below.

    githooks: {
        all: {
            'pre-commit' : 'compass requirejs'
        }
    }

Above generates git pre-commit hook as below.

#!/usr/bin/env node

// GRUNT-GITHOOKS START
var exec = require('child_process').exec;

exec('grunt compass requirejs', {
       cwd: 'C:\\development\\Sourcecode\\qnb-home'
     }, function (err, stdout, stderr) {

  console.log(stdout);

  var exitCode = 0;
  if (err) {
    console.log(stderr);
    exitCode = -1;
  }

  process.exit(exitCode);
});

// GRUNT-GITHOOKS END

Whilst above ensure the tasks are been run before the git commit, it does not add the newly created minified files (compiled SASS and r.js files) to the existing commit.

So ,I would like to add a git add --all to the pre-commit hook using grunt githooks. Is it this possible to do? Any comment/answer would be highly appreciated.


Solution

  • You can use grunt-githook's template option to create a custom template that would exec the git add --all command. This getting started article provides a good example of how to create a custom template.

    That said, running git add -all before every commit will make picking which files to commit a lot more work than usual. You'll have to stash the changes you don't want to be part of your commit before you do it and unstash them afterwards. It might be worth being a bit more targeted in what you add, if you need to add these files to the repository at all. They can be generated from the sources after all.