From 511b810702232ccb9b33bc84e06199fe99f0ce46 Mon Sep 17 00:00:00 2001 From: bimlas Date: Tue, 14 Mar 2017 21:51:53 +0100 Subject: [PATCH] WIP: Use `git rev-parse` to check path correctness 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 #54 --- lib/command-requirements.js | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/lib/command-requirements.js b/lib/command-requirements.js index e22f78c..bd1ed37 100644 --- a/lib/command-requirements.js +++ b/lib/command-requirements.js @@ -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.'); }