gpt4 book ai didi

node.js - NodeGit 如何获取本地分支列表?

转载 作者:行者123 更新时间:2023-12-02 12:06:11 24 4
gpt4 key购买 nike

我正在编写一个 Node API 服务器,它需要向用户发送驻留在服务器上的 git 存储库中的本地分支列表。很多地方建议使用Repository#getReferenceNames来自 NodeGit,这就是我所做的:

exports.getBranches = function (req, res) {
NodeGit.Repository.open(config.database).then(function(repo) {
return repo.getReferenceNames(NodeGit.Reference.TYPE.LISTALL);
}).then(function (arrayString) {
res.status(200).json({message: "OK", data: arrayString});
}).catch(function (err) {
res.status(500).json({message: err, data: []});
}).done(function () {
console.log("finish");
});
};

但是,该函数返回所有分支。有没有办法像命令行 gitbranch 那样只获取本地分支?

最佳答案

当然有!本地分支的前缀为 refs/heads,而远程分支的前缀为 refs/remotes,如下所示:

enter image description here

祝你好运!

编辑:

还有更好的方法。您可以在返回之前遍历返回的引用,并使用reference.isRemote()清除 Remote 。

关于node.js - NodeGit 如何获取本地分支列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com