Search code examples
gitgruntjsgit-submodulesgithookspost-commit-hook

Commit Git "Supermodule" using post-commit hook of submodule with grunt-git and grunt-githooks


I have a git repo which contains many submodules. When I commit in a submodule, I have a git hook which is supposed to commit in the "supermodule". Unfortunately, the commit in the post-commit hook fails because the "supermodule" can't seem to detect the changes in its submodule.

Is there any other way I can achieve this behavior?

I have all this set up through Grunt using grunt-githooks and grunt-git.

Below is my gruntfile:

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    githooks: {
      all: {
        options: {
            dest: '../../../.git/modules/server/modules/mymodule/hooks'
        },
        'post-commit': 'updateSuperModule'
      }
    },

    gitcommit: {
        all: {
            options: {
                message: 'Updated Submodule',
                cwd: '../../..',
                verbose: true
            },
            files: {
                src: ['.']
            }
        }
    },

    gitpush: {
        all: {
            options: {
                cwd: '../../..',
                verbose: true
            }
        }
    }
  });

  grunt.loadNpmTasks('grunt-githooks');
  grunt.loadNpmTasks('grunt-git');
};

Solution

  • When a commit of a repo (here a submodule) wants to perform a git operation in another repo (here the parent repo, in '../../..'), changing folder isn't enough.

    You need to unset GIT_DIR first, then perform git commands (as in this hook).

    If you don't, and status or add operation will be run in the context of the submodule (which has just had a new commit, and has a clean status).