grunt-git icon indicating copy to clipboard operation
grunt-git copied to clipboard

Add submodule commands

Open dylancwood opened this issue 11 years ago • 4 comments

How would you feel about adding submodule commands to this project? The complete list of submodule commands can be found here: http://git-scm.com/docs/git-submodule

I would like to do the work, but do not want to go to the effort if it is not desired. Please tell me whether you would like submodule commands to be integrated, or if you would prefer me to create a separate repo.

I see two possible options for integrating submodule commands:

  1. create a single submodule task to perform all submodule commands. I've started down this road, but am not sure that it is a maintainable or testable approach. Here is an example command_submodule.js scriptl:
'use strict';

var async = require('grunt').util.async;
var grunt = require('grunt');

module.exports = function (task, exec, done) {
    var errorMsg = '';
    var options = task.options({
        subcommand: null,
        force: null,
        repository: null, //only for add or update subcommand
        branch: null, //only for add subcommand
        path: null, //not for foreach subcommand
        init: null, //for update subcommand
        recursive: null, //only for update subcommand
        //...
    });
    var knownSubcommands = [
        'add',
        'status',
        'init',
        'deinit',
        'upgrade',
        'summary',
        //'foreach', //Doesn't really make sense in this context IMO
        'sync'
    ]

    var args = ['submodule'];

    if (!options.subcommand) {
        errorMsg = 'Cannot invoke submodule command without a sub-command';
        throw new Error(errorMsg);
    } else if (knownSubcommands.indexOf(options.subcommand) === -1) {
        errorMsg = 'Unknown subcommand: "' + options.subcommand + '". '
        errorMsg += 'Possible subcommands are: ' + knownSubcommands.join(', ');
        throw new Error(errorMsg);
    } else {
        args.push(options.subcommand);
    }
    //TODO: loop through other options, or simply assign them in a loop?
    if (options.branch) {
        args.push(options.branch);
    }


    // Add callback
    args.push(done);

    exec.apply(this, args);
};

module.exports.description = 'Execute submodule command';
  1. An alternative approach would be to create tasks for each submodule command. Here's an example command_submodule_update.js file. I'm also curious what you think of the use of a loop to assign cli flags to the args array.
'use strict';

var async = require('grunt').util.async;
var grunt = require('grunt');

module.exports = function (task, exec, done) {
    var optionKey;
    var options = task.options({
        init: false,
        remote: false,
        noFetch: false,
        force: false,
        rebase: false,
        merge: false,
        reference: null,
        depth: null,
        recursive: false,
        path: null
    });

    var spawnOptions = ['cwd', 'verbose'];

    var args = ['submodule', 'update'];


    // options.path is not a cli flag, instead, the value is added
    var path = options.path
    // unset options.path so that it does not get interpreted below
    options.path = null;

    // loop through cli flags in options and add to args
    for (optionKey in options) {
        if (options.hasOwnProperty(optionKey) && options[optionKey] && spawnOptions.indexOf(optionKey) === -1) {
            // add flag
            args.push('--' + optionKey);
            // if not a boolean, add the value after the flag
            if (typeof options[optionKey] !== 'boolean') {
                args.push(options[optionKey]);
            }
        }
    }

    // Add callback
    args.push(done);

    exec.apply(this, args);
};

module.exports.description = 'Update git submodules.';

dylancwood avatar Jun 20 '14 20:06 dylancwood

Since you asked, I would love to see this feature :) it would help our build process greatly.

AoDev avatar Aug 16 '14 09:08 AoDev

Great i will try to add these in soon

dylancwood avatar Aug 16 '14 15:08 dylancwood

I saw your PR for git submodule update. I guess it's one of the most important. Thank you for your work :)

AoDev avatar Aug 16 '14 17:08 AoDev

a year later any word on this? :)

krwillxyz avatar Nov 09 '15 19:11 krwillxyz