Skip to content

Commit

Permalink
Use git rev-parse to check path correctness
Browse files Browse the repository at this point in the history
Instead of check the existness of `.git` directory, use `git rev-parse`
to check that `req.path` is in

* a Git repository
* a Git submodule
* the root of the repo/submodule

The old method didn't worked with submodules more than one level deep in
the repo. Besides this if the $GIT_DIR differs from `.git` (cannot find
a real life example), then the old method fails to work.

Related to mixu#54
  • Loading branch information
bimlas committed Apr 7, 2017
1 parent 923b15f commit ebbacfb
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions lib/command-requirements.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
var fs = require('fs'),
path = require('path');
path = require('path'),
spawnSync = require('child_process').spawnSync;

module.exports = {
git: function(req) {
var stat;
try {
stat = fs.statSync(req.path + '/.git/');
if (stat.isDirectory()) {
return true;
}
} catch (e) {
// check if the directory is a submodule
// addresses https://github.com/mixu/gr/issues/54
if (e.code === 'ENOTDIR' || e.code === 'ENOENT') {
var parentPath = path.dirname(req.path);
try {
stat = fs.statSync(parentPath + '/.gitmodules');
if (stat.isFile()) {
return true;
}
} catch (e) { }
git: function (req) {
var gitCommand = spawnSync('git', ['rev-parse', '--show-prefix'], {cwd: req.path});
if (gitCommand.stderr.toString().replace(/(\r\n|\n|\r)/gm, '').trim()) {
if (req.format === 'human') {
console.log('Skipped ' + req.path + ' as it is not a Git repository.');
}
return false;
}
if (req.format === 'human') {
console.log('Skipped ' + req.path + ' as it does not have a .git subdirectory and is not a submodule.');
if (gitCommand.stdout.toString().replace(/(\r\n|\n|\r)/gm, '').trim()) {
if (req.format === 'human') {
console.log('Skipped ' + req.path + ' as it is not the root of Git repository or submodule.');
}
return false;
}
return false;
return true;
},
make: function(req) {
var stat;
Expand Down

0 comments on commit ebbacfb

Please sign in to comment.