rubenv / grunt-git

Git commands for grunt.
MIT License
227 stars 83 forks source link

Problems with gitcommit for a repository in a sub-directory #79

Open djmccormick opened 9 years ago

djmccormick commented 9 years ago

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 commented 9 years ago

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

Orphestrator commented 9 years ago

Hi,

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

regards

dylancwood commented 9 years ago

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.

chasingmaxwell commented 9 years ago

Has anyone found a workaround for this in the meantime?

dylancwood commented 9 years ago

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 dwood@mrn.org

On Tue, Dec 16, 2014 at 10:53 PM, Peter Sieg notifications@github.com 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.

chasingmaxwell commented 9 years ago

@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);
  });
andobolocco commented 9 years ago

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

andobolocco commented 9 years ago

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...");
});