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

143 Views Asked by At

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);
3

There are 3 best solutions below

2
Vadorequest On BEST ANSWER

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');
  }
0
LeGEC On

In general : from the command line, another way to see if there are differences at all is to run git diff HEAD. The diff is empty iff there are no differences.

You seem to care only about schema.json in your script : from the command line that would be git diff HEAD -- schema.json.

If you manage to generate a similar command in nodegit (get the diff for schema.json between HEAD and the worktree ), you can use it as a condition at the very beginning of your script.

1
Mike Jerred On

I am detecting changes by doing a diff between the head commit and the index like this:

import { Diff, Repository, Tree } from 'nodegit';

const EmptyTree = '4b825dc642cb6eb9a060e54bf8d69288fbee4904';

async function getStagedChanges(repository: Repository) {
  const head = await repository.getHeadCommit();
  const tree = await (head ? head.getTree() : Tree.lookup(repository, EmptyTree));
  const diff = await Diff.treeToIndex(repository, tree);

  return diff;
}

If there is no head commit (i.e. the repository has no commits yet) then I use the empty tree instead to show everything in the index.