Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
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);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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');
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!