You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用NodeGit获取文件对应的Git提交信息?

你已经在使用Git的JS绑定库(推测是nodegit)来处理仓库操作了,我把你现有的代码整理成更清晰的结构,方便你参考和扩展:

获取Git树中的文件列表及OID

这段代码的作用是通过分支引用名,获取对应提交的树结构,进而提取树中的文件条目和OID:

repository.getBranchCommit(treeRefName)
  .then((commit) => commit.getTree())
  .then((tree) => {
    obj.repositoryTree = tree;
    // 提取树中的所有条目,包含文件/目录的OID等信息
    obj.repositoryTreeEntries = tree.entries();
    fullfill(obj);
  })
  .catch(reject);

说明:tree.entries()返回的每个条目里,你可以通过entry.oid()方法获取对应的OID值。

通过OID获取提交信息

下面这段代码是通过指定的OID查找对应的提交对象,并输出提交消息:

Git.Commit
  .lookup(repository, oid)
  .then((commit) => {
    // 打印提交的消息内容
    console.log(commit.message());
  })
  .then(() => fullfill(obj))
  .catch(reject);

说明:如果需要更多提交信息,还可以调用commit.author()、commit.date()等方法获取提交作者、提交时间等内容。

内容的提问来源于stack exchange,提问作者georoot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:38:01