← 输入框|github.com|刷新
Skip to content

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

Open

Description

@paxcom-sgarg

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?

Activity

  1. weedz commented on Mar 21, 2023

    @weedz
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions