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

NodeGit(Node.js)避免空提交:entryCount异常问题排查

问题

使用Node.js的NodeGit库编写Bitbucket仓库提交代码时,通过index.entryCount()判断是否有可提交内容,但执行git reset --hard后,entryCount仍大于0,导致生成空提交。核心代码如下:

提交创建函数:

function createCommit(commitMsg) {
  console.log("Creating commit");
  let repository, index, oid, author, committer;
  return NodeGit.Repository.open(bb.repoPath)
    .then(function (repo) {
      repository = repo;
      return repository.refreshIndex();
    })
    .then(function (idx) {
      index = idx;
      const entryCount = index.entryCount();
      console.log("file: nodegit.js:74 ~ entryCount:", entryCount);
      if (entryCount === 0) {
        console.log("No changes to commit.");
        return;
      }
      return index.addAll();
    })
    .then(function () {
      return index.write();
    })
    .then(function () {
      return index.writeTree();
    })
    .then(async function (oidResult) {
      const head = await NodeGit.Reference.nameToId(repository, "HEAD"); // get reference to the current state
      const parent = await repository.getCommit(head); // get the commit for current state
      oid = oidResult;
      author = NodeGit.Signature.now(bb.name, bb.email);
      committer = NodeGit.Signature.now(bb.name, bb.email);
      return repository.createCommit("HEAD", author, committer, commitMsg, oid, [parent]);
    })
    .then(function (info) {
      console.log("Commit created successfully!");
      return info;
    })
    .catch((err) => {
      console.log("Error creating commit:", err);
      throw err;
    });
}

判断逻辑:

const entryCount = index.entryCount();
console.log("file: nodegit.js:74 ~ entryCount:", entryCount);
if (entryCount === 0) {
  console.log("No changes to commit.");
  return;
}

原因分析

index.entryCount()返回的是暂存区中所有已跟踪文件的总数,而非相对于HEAD的变更文件数量。即使执行git reset --hard让暂存区与HEAD完全一致,暂存区依然保存着所有已跟踪文件的条目,因此entryCount永远大于0(只要仓库有已跟踪文件),无法正确判断是否有可提交的变更。

解决办法

需要通过比较暂存区与HEAD的差异,或者工作区、暂存区与HEAD的整体差异来判断是否有可提交内容。以下是修改后的代码,通过diffIndexToHead检查暂存区和HEAD的差异:

function createCommit(commitMsg) {
  console.log("Creating commit");
  let repository, index, oid, author, committer;
  return NodeGit.Repository.open(bb.repoPath)
    .then(function (repo) {
      repository = repo;
      return repository.refreshIndex();
    })
    .then(function (idx) {
      index = idx;
      // 检查暂存区与HEAD的差异
      return repository.diffIndexToHead(index, {})
        .then(diff => {
          // 无差异则直接终止流程
          if (diff.numDeltas() === 0) {
            console.log("No changes to commit.");
            return Promise.resolve(null);
          }
          // 有差异则继续暂存文件
          return index.addAll();
        });
    })
    .then(function (addResult) {
      // 无变更时直接返回
      if (addResult === null) return;
      return index.write();
    })
    .then(function () {
      // 无变更时直接返回
      if (!index) return;
      return index.writeTree();
    })
    .then(async function (oidResult) {
      if (!oidResult) return;
      const head = await NodeGit.Reference.nameToId(repository, "HEAD");
      const parent = await repository.getCommit(head);
      oid = oidResult;
      author = NodeGit.Signature.now(bb.name, bb.email);
      committer = NodeGit.Signature.now(bb.name, bb.email);
      return repository.createCommit("HEAD", author, committer, commitMsg, oid, [parent]);
    })
    .then(function (info) {
      if (info) {
        console.log("Commit created successfully!");
        return info;
      }
    })
    .catch((err) => {
      console.log("Error creating commit:", err);
      throw err;
    });
}

补充说明

如果需要同时检查工作区未暂存的变更,可以先调用index.addAll()暂存所有变更,再执行diffIndexToHead检查;或者先通过diffWorkdirToIndex检查工作区与暂存区的差异,确保所有变更都被暂存后,再判断是否有可提交内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 12:53:21