node.js - 使用 node.js 获取当前 git 分支
问题描述
如何在没有外部库的情况下使用 node.js 获取当前的 git 分支?我需要能够获取当前分支名称以在我的节点文件中执行另一个功能。
使用部分工作代码更新
stdout
我可以用这个获取分支名称,但如果匹配给定的字符串,似乎无法注销消息。
const { exec } = require('child_process');
exec('git rev-parse --abbrev-ref HEAD', (err, stdout, stderr) => {
if (stdout === 'name-of-branch') {
console.log(this is the correct branch);
}
});
解决方案
请试试这个作品
const { exec } = require('child_process');
exec('git rev-parse --abbrev-ref HEAD', (err, stdout, stderr) => {
if (err) {
// handle your error
}
if (typeof stdout === 'string' && (stdout.trim() === 'master')) {
console.log(`The branch is master`);
// Call your function here conditionally as per branch
}
});
接收输出为
$: node test.js
The branch is master
推荐阅读
- python-3.x - 给定 csv 文件,我需要根据相似度得分打印那些最相似的句子
- python - 通过信号调用函数将默认键控参数更改为“False”。为什么?
- javascript - 基于 Promise 的序列转换为基于 Callback 的序列
- android - 使用 LiveData 的 JUnit5 测试不执行订阅者的回调
- javascript - Prettier - 使用通配符
- javascript - 为什么“背面可见性”会影响其他元素,当它们的父元素按比例变换时?
- python - 如何在不使用服务器的情况下从 HTML 页面运行 python 脚本?它应该服务于 GUI 框架的目的
- powershell - 每个域的特定 OU
- javascript - jQuery年份计算
- wso2 - WSO2 企业集成器和 WSO2 应用程序服务器