Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diffSummary is incorrect for the first commit #1035

Open
web-dev-sam opened this issue Dec 28, 2024 · 2 comments
Open

diffSummary is incorrect for the first commit #1035

web-dev-sam opened this issue Dec 28, 2024 · 2 comments

Comments

@web-dev-sam
Copy link

When comparing the first commit with the empty tree to see what changes the first commit introduced it displays the "changes" correctly but additions/deletions are wrong.

const EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
const fromRepo = simpleGit("...")
console.log(await fromRepo.diffSummary([EMPTY_TREE, await fromRepo.firstCommit()]))

Git Output: git diff 4b825dc642cb6eb9a060e54bf8d69288fbee4904 8a424bd942b041bc924989e8ab3c77a63e26826d

diff --git a/.env.example b/.env.example
new file mode 100644
index 0000000..5d329db
--- /dev/null
+++ b/.env.example
@@ -0,0 +1,12 @@
+NUXT_AUTH_ORIGIN= # No trailing slash
+NUXT_SESSION_PASSWORD=
+
+NUXT_OAUTH_GOOGLE_CLIENT_ID=
+NUXT_OAUTH_GOOGLE_CLIENT_SECRET=
+NUXT_OAUTH_GITHUB_CLIENT_ID=
+NUXT_OAUTH_GITHUB_CLIENT_SECRET=
+NUXT_OAUTH_GITHUB_CONNECT_CLIENT_ID=
+NUXT_OAUTH_GITHUB_CONNECT_CLIENT_SECRET=
+
+NUXT_POSTGRES_URL=
+RESEND_API_KEY=

git-js Output:

{
      file: ".env.example",   // correct
      changes: 12,            // correct
      insertions: 4,          // incorrect
      deletions: 0,           // incorrect
      binary: false           // correct
}
@web-dev-sam
Copy link
Author

This is my fix for now:

const EMPTY_TREE = { hash: "4b825dc642cb6eb9a060e54bf8d69288fbee4904" }
const diffs = []
for (let i = all.length - 1; i > 0; i--) {
  const isFirst = i === all.length - 1

  const olderCommit = isFirst ? EMPTY_TREE : all[i]
  const newerCommit = isFirst ? all[i] : all[i-1]
  const diff = await fromRepo.diffSummary([olderCommit.hash, newerCommit.hash])

  // Fix bug where diffSummary doesn't return correct values for the first commit
  if (isFirst) {
    diff.insertions = diff.changed
    diff.deletions = 0
    diff.files = diff.files.map(f => {
      if (f.binary) {
        return f
      }
      f.insertions = f.changes
      f.deletions = 0
      return f
    })
  }
  diffs.push(diff)
}

steveukx added a commit that referenced this issue Dec 31, 2024
@steveukx
Copy link
Owner

steveukx commented Jan 2, 2025

Hello, thank you for the detailed issue description!

The cause of the problem is that there are files larger than 4096b in size in the initial commit, so the --stat=4096 used to count the size of the insertion/deletion for each file is being rounded to a percentage rather than showing the full detail.

For a diff between the empty tree commit and any other commit, you can avoid this by using the --numstat mechanism for detecting changes, which will always be the same as having used the (default) --stat mechanism when diffing against the empty tree commit:

await fromRepo.diffSummary(['--numstat', EMPTY_TREE, await fromRepo.firstCommit()]));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants