NodeGit 如何获取本地分支列表?
NodeGit how do I get a list of local branches?
我正在编写一个节点 API 服务器,它需要向用户发送驻留在服务器上的 git 存储库中的本地分支列表。很多地方建议使用 NodeGit 中的 Repository#getReferenceNames,我就是这样做的:
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");
});
};
但是,这个函数returns所有分支。有没有办法像命令行 git branch
那样只获取本地分支?
当然有!本地分支前缀为refs/heads,而远程分支前缀为refs/remotes,像这样:
祝你好运!
编辑:
还有更好的方法。您可以在返回之前遍历返回的引用,并使用 reference.isRemote().
清除遥控器
我正在编写一个节点 API 服务器,它需要向用户发送驻留在服务器上的 git 存储库中的本地分支列表。很多地方建议使用 NodeGit 中的 Repository#getReferenceNames,我就是这样做的:
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");
});
};
但是,这个函数returns所有分支。有没有办法像命令行 git branch
那样只获取本地分支?
当然有!本地分支前缀为refs/heads,而远程分支前缀为refs/remotes,像这样:
祝你好运!
编辑:
还有更好的方法。您可以在返回之前遍历返回的引用,并使用 reference.isRemote().
清除遥控器