Skip to content

Commit

Permalink
WIP: 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 Mar 18, 2017
1 parent 923b15f commit 511b810
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions lib/command-requirements.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
var fs = require('fs'),
path = require('path');
path = require('path'),
exec = require('child_process').exec;

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) { }
}
}
exec('git rev-parse --show-prefix', {
cwd: req.path,
}, function(err, stdout, stderr) {
stdout = stdout.replace(/(\r\n|\n|\r)/gm,'').trim()
if(err !== null) {
console.log('*** Not a Git repository, should return false\n>>> req.path: "' + req.path + '"\n>>> err: ' + err + '\n>>> stdout: "' + stdout + '"\n>>> stderr: "' + stderr + '"');
}
if(stdout) {
console.log('*** Not in repository/submodule root dir, should return false\n>>> req.path: "' + req.path + '"\n>>> err: ' + err + '\n>>> stdout: "' + stdout + '"\n>>> stderr: "' + stderr + '"');
}
console.log('*** OK, return true or do nothing\n>>> req.path: "' + req.path + '"\n>>> err: ' + err + '\n>>> stdout: "' + stdout + '"\n>>> stderr: "' + stderr + '"');
});
return true;
} catch (e) { }
if (req.format === 'human') {
console.log('Skipped ' + req.path + ' as it does not have a .git subdirectory and is not a submodule.');
}
Expand Down

0 comments on commit 511b810

Please sign in to comment.