javascriptgitnodegit

nodegit - How to know if a commit is empty (to avoid pushing empty commits)


I'm using nodegit and I can't figure out how to detect whether the there has been changes to commit.

Currently, I'm committing when an external event happens, but I don't know if this will cause a real difference in the files. I don't check whether the commit is empty before pushing, which led me to end up with lots of empty commits, I'd like to avoid that.

When doing it manually, I usually run a git status to check whether there were changes to commit, I was thinking about doing something similar here, but can't figure out how. (here is the documentation)

  console.log('committing...');
  const commitResult = await commitFile(`schema.json`, schemaAsString, `${appName} (${appId})`);
  const statuses = await repo.getStatus();
  console.log('getStatus status', statuses);
  console.log('getStatus status', statuses[0].status());
  console.log('getStatus isModified', statuses[0].isModified());
  console.log('getStatus path', statuses[0].path());
  console.log('getStatus statusBit', statuses[0].statusBit());

  console.log('pushing...');
  const pushResult = await push(repo);
  console.log('pushResult', pushResult);

Solution

  • I figured it out by trying things a bit at random.

    I use the Index checksum, and compare it before and after committing.

    If the commit is empty, the checksum doesn't change. If the commit isn't empty, the checksum changes.

    Based on that, it's easy to write a simple condition to perform the push or not.

      const repo = await clone(repositoryLocalPath);
      console.log('repo', repo);
      const indexBeforeCommit: Index = await repo.refreshIndex();
      const indexBeforeCommitChecksum = indexBeforeCommit.checksum().tostrS();
      console.log('indexBeforeCommit checksum', indexBeforeCommitChecksum);
    
      console.log('committing...');
      const commitId = await commitFile(`schema.json`, schemaAsString, `Airtable - ${appName} (${appId})`);
      const indexAfterCommit: Index = await repo.refreshIndex();
      const indexAfterCommitChecksum = indexAfterCommit.checksum().tostrS();
      console.log('indexAfterCommit checksum', indexAfterCommitChecksum);
    
      if (indexBeforeCommitChecksum !== indexAfterCommitChecksum) {
        console.log('pushing...');
        const pushResult = await push(repo);
        console.log('pushResult', pushResult);
      } else {
        console.log('no changes to push');
      }