admin管理员组

文章数量:1320661

I am trying to determine if my node process is running in a git directory. The following works, but is still outputting a fatal error in the console.

function testForGit() {
    try {
        var test = execSync('git rev-parse --is-inside-work-tree', {encoding: 'utf8'});
    } catch (e) {
    }
    return !!test;
}

console.log(testForGit());

When in a directory under the control of git, I get true as the result. But when outside of a directory under the control of git, I get:

fatal: Not a git repository (or any of the parent directories): .git
false

My question(s):

Is there a way to suppress the error being logged? Or is there a better way to determine if I am in a directory under git control?

Essentially, I am trying to do the bash equivalent of

if git rev-parse --git-dir > /dev/null 2>&1; then
    ... do something
fi

I am trying to determine if my node process is running in a git directory. The following works, but is still outputting a fatal error in the console.

function testForGit() {
    try {
        var test = execSync('git rev-parse --is-inside-work-tree', {encoding: 'utf8'});
    } catch (e) {
    }
    return !!test;
}

console.log(testForGit());

When in a directory under the control of git, I get true as the result. But when outside of a directory under the control of git, I get:

fatal: Not a git repository (or any of the parent directories): .git
false

My question(s):

Is there a way to suppress the error being logged? Or is there a better way to determine if I am in a directory under git control?

Essentially, I am trying to do the bash equivalent of

if git rev-parse --git-dir > /dev/null 2>&1; then
    ... do something
fi
Share Improve this question edited May 12, 2016 at 22:49 KevBot asked May 12, 2016 at 22:47 KevBotKevBot 18.9k5 gold badges54 silver badges70 bronze badges 3
  • Why the downvote? This is a legitimate attempt at a legitimate problem... – KevBot Commented May 12, 2016 at 22:54
  • seems like looking for the hidden .git folder would be simpler/faster.... – dandavis Commented May 12, 2016 at 23:02
  • @dandavis, I thought about that, but wasn't sure what kind of repercussions there would be for recursively running up parent directories until I hit the root. – KevBot Commented May 12, 2016 at 23:04
Add a ment  | 

2 Answers 2

Reset to default 7

If you use docker and don't want to install git as a system-level dependency when build image for your application. Maybe because we want to build the image faster and keep the image size as small as possible.

The way @janos provides will not work.

Another way is to check is there a .git directory or not in root path of your project. But it depends on your requirements. For my case, it's enough.

exports.isGitSync = function isGitSync (dir) {
  return fs.existsSync(path.join(dir, '.git'))
}

You can try to redirect stdout inside the execSync call, like this:

var test = execSync('git rev-parse --is-inside-work-tree 2>/dev/null', {encoding: 'utf8'});

本文标签: javascriptUse nodejs to determine if in git directoryStack Overflow