Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safe invoke of git command #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* global error */
'use strict';

require('shelljs/global');
var fs = require("fs");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, the call of exec() in this file was the only need for this requirement. As I kicked those calls shelljs/global is not needed anymore IMHO. Hence, it was code cleaning. If it's needed somewhere else, let's bring it back in.

Sidenote: My personal opinion on this library isn't quite positive at all as it pollutes the global scope and it becomes harder to trace where such global functions like exec actually come from. But that's just a matter of taste. ;-)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, thanks for clarifying!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm open to other solutions that don't pollute the global namespace. Feel free to create another PR if you'd like to see that changed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll wait for your response before merging this, since it seems this PR may be obsoleted by removing shelljs.

var gup = require('../');
var yargs = require('yargs')
Expand Down Expand Up @@ -40,8 +39,12 @@ var dest;
if (argv.dest) {
dest = argv.dest;
} else {
var topLevel = exec('git rev-parse --show-toplevel', { silent: true })
.output.slice(0, -1);
var topLevel = gup.findGitRoot();
if (topLevel instanceof Error) {
console.error('fatal: ' + topLevel.message);
exit(1);
}

if (test('-f', topLevel + '/.git')) {
// this is a sub module
var buf = fs.readFileSync(topLevel + '/.git', "utf8").trim();
Expand All @@ -52,11 +55,6 @@ if (argv.dest) {
} else {
dest = topLevel + '/.git/hooks/';
}

if (error()) {
console.error('fatal: Not a git repository (or any of the parent directories): .git');
exit(1);
}
}

var hook = argv._[0];
Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,26 @@ var hooks = [
'update'
];

function findGitRoot() {
var gitRevParseTopLevel = exec('git rev-parse --show-toplevel', { silent: true });
if (gitRevParseTopLevel.code !== 0) {
return new Error('Not a git repository (or any of the parent directories): .git');
}

return gitRevParseTopLevel.output.slice(0, -1);
}

function install(hook, dest, cb) {
cb = cb || dest;

if (!cb) cb(new Error('Callback must be supplied.'));
if (typeof cb !== 'function') cb(new Error('Callback must be a function.'));
if (hooks.indexOf(hook) === -1) cb(new Error('Invalid hook name.'));

dest = ((typeof dest === 'function' ? null : dest) ||
exec('git rev-parse --show-toplevel')
.output.slice(0, -1) + '/.git/hooks/');
var gitRoot = findGitRoot();
if (gitRoot instanceof Error) cb(gitRoot);

dest = (typeof dest === 'function' ? null : dest) || gitRoot + '/.git/hooks/';

var destHook = path.join(dest, hook);

Expand Down Expand Up @@ -74,5 +84,6 @@ function installAll(dest, cb) {
);
}

module.exports.findGitRoot = findGitRoot;
module.exports.install = install;
module.exports.installAll = installAll;