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
2 Answers
Reset to default 7If 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
版权声明:本文标题:javascript - Use node.js to determine if in git directory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742063094a2418675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论