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

Problems with gitcommit for a repository in a sub-directory

Open djmccormick opened this issue 11 years ago • 8 comments

I'm attempting to use gitcommit against a repository in a sub-directory within my project. The cwd option doesn't seem to be working properly.

My gitcommit task looks like this:

gitcommit: {
    dist: {
        options: {
            expand: true,
            cwd: 'dist',
            message: 'v<%= pkg.version %>'
        },
        files: [{
            expand: true,
            cwd: 'dist',
            src: ['**/*']
        }]
    }
},

And when I run it, I get something like this:

Running "gitcommit:dist" (gitcommit) task
Warning: fatal: pathspec 'dist/about.html' did not match any files Use --force to continue.

Aborted due to warnings.

Or, with verbose mode:

Running "gitcommit:dist" (gitcommit) task
Verifying property gitcommit.dist exists in config...OK
Files: dist/whatsnew.html -> whatsnew.html
Options: message="v0.0.84", ignoreEmpty=false, noVerify=false, noStatus=false, expand, cwd="dist"
Options: verbose=false, expand, cwd="dist", message="v0.0.84"
Warning: fatal: pathspec 'dist/about.html' did not match any files Use --force to continue.

Aborted due to warnings.

It appears that the dist/ portion is still being included despite my cwd, given that the file is in the appropriate location:

djmccormick at computer in ~/Projects/some-category/some-project on some-branch
± ls dist/

about.html

Am I doing something incorrectly or is this a bug?

node v0.10.28 grunt-cli v0.1.13 grunt v0.4.4 grunt-git v0.2.14

djmccormick avatar Oct 08 '14 17:10 djmccormick

I've now tried the same with v0.3.0-alpha and got similar results with the gitadd task.

djmccormick avatar Oct 08 '14 18:10 djmccormick

Hi,

got the same problem. Any idea when it would be resolved?

regards

Orphestrator avatar Oct 13 '14 12:10 Orphestrator

Thank you for bringing this up. I've traced the issue to lib/command_commit.js, which references each file's file.src property. This is an array, of what appear to be path names relative to the Grunt process' CWD (not relative to the cwd specified in the file options!). For instance, assume the following directory structure:

project/
    Gruntfile.js
    dist/
        foo.html

and the grunt files directive that you've supplied:

files: [{
    expand: true,
    cwd: 'dist',
    src: ['**/*']
}]

The resulting files array for the task will look like this:

[{ src: [ 'dist/foo.html' ],
  orig: { expand: true, src: [ '**/*' ] },
  dest: 'foo.html' }]

I would expect the src to be given relative to the file's cwd option, not relative to the Grunt process' CWD.

The solution may be to use the file's dest property instead of the src property.

I will continue investigating, and submit a fix when I'm confident in the solution.

dylancwood avatar Oct 23 '14 18:10 dylancwood

Has anyone found a workaround for this in the meantime?

chasingmaxwell avatar Dec 17 '14 06:12 chasingmaxwell

This is because the grunt file API returns paths relative to the Gruntfile, even if a cwd is specified. I am working on a solution.

Respectfully, Dylan Wood The Mind Research Network Neuroinformatics +1.505.480.5346 [email protected]

On Tue, Dec 16, 2014 at 10:53 PM, Peter Sieg [email protected] wrote:

Has anyone found a workaround for this in the meantime?

— Reply to this email directly or view it on GitHub https://github.com/rubenv/grunt-git/issues/79#issuecomment-67283927.

dylancwood avatar Dec 17 '14 19:12 dylancwood

@dylancwood thanks for your work on this!

I found a workaround yesterday that I will use until this is solved. For anyone needing an immediate workaround solution, you can use grunt.util.spawn to run git commands:

  grunt.registerTask('gitcommitworkaround', '', function() {
    var done, child;

    done = this.async();
    child = grunt.util.spawn({
      cmd: 'git',
      args: ['-C', 'the/path/to/your/subdirectory', 'commit', '-m', 'This is a commit message.'],
    }, function(err, result, code) {
      done();
    });

    child.stdout.pipe(process.stdout);
    child.stderr.pipe(process.stderr);
  });

chasingmaxwell avatar Dec 17 '14 20:12 chasingmaxwell

any news on this issue? grunt is still giving us the cwd relative to the gruntfile?

andobolocco avatar Jul 24 '15 18:07 andobolocco

I've found another possible workaroud, which is ugly too, this issue affects all grunt-git tasks, not just gitcommit:

var rootPath = process.cwd();

grunt.registerTask("cd", function(subpath) {

    var path = require('path');

    if(env === 'restore') {
        return grunt.file.setBase(rootPath);
    }

    return grunt.file.setBase(path.resolve('my/subdir/' + subpath));

});

grunt.registerTask("taskName", function(env) {

        grunt.task.run("cd:" + env);

        grunt.task.run("gitreset:" + env);
        grunt.task.run("gitpull:" + env);

        grunt.task.run("cd:restore");

        grunt.task.run("othertasks...");
});

andobolocco avatar Jul 25 '15 19:07 andobolocco