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

How to check count of changed files to avoid empty commits #1976

Open
paxcom-sgarg opened this issue Mar 21, 2023 · 1 comment
Open

How to check count of changed files to avoid empty commits #1976

paxcom-sgarg opened this issue Mar 21, 2023 · 1 comment

Comments

@paxcom-sgarg
Copy link

System information

  • node version: v14.15.3
  • npm or yarn version: npm 6.14.9
  • OS/version/architecture: WIndows 10
  • Applicable nodegit version: ^0.26.1
node -v
npm -v # (or yarn -v)
node -e "console.log(process.platform)"
node -e "console.log(require('os').release())"
node -e "console.log(console.log(process.arch))"

trying to get number of files to avoid empty commits using entryCount(), but it is returning a number > 0 even if there is no file to commit

SO https://stackoverflow.com/questions/75755796/nodegit-avoid-creating-empty-commit-nodejs-bitbucket

any help?

@weedz
Copy link
Contributor

weedz commented Mar 21, 2023

Index.entryCount() does not return the number of staged files, rather the total number of files checked in at the current index. To get the number of staged changes you can use:

const repo = await Repository.open(repoPath);
const index = await repo.refreshIndex();

const flags: Diff.OPTION = Diff.OPTION.IGNORE_WHITESPACE; // not needed, can be 0

const head = await repo.getHeadCommit();

// In an "empty" repository the `oldTree` parameter (here `await head.getTree()`) must be `undefined`.
const stagedDiff = await Diff.treeToIndex(repo, await head.getTree(), index, { flags });
const numberOfStagedChanges = stagedDiff.numDeltas();

And to get the files which are changed, but not yet staged, could be something like:

const unstagedDiff = await Diff.indexToWorkdir(repo, index, {
    flags: Diff.OPTION.INCLUDE_UNTRACKED | Diff.OPTION.SHOW_UNTRACKED_CONTENT | Diff.OPTION.RECURSE_UNTRACKED_DIRS | flags
});

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