npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@themoin/diff-calculator-cli

v0.2.3

Published

CLI for aggregating diff between two revisions, which can ignore deletions, whitespaces and comments

Downloads

343

Readme

Diff Calculator

CLI and Github Action pack for calculating the diff between two git revisions.
It can be configured to ignore comments, deletions, and whitespaces, so you can focus on the actual changes.

CLI

CLI is available through npm, yarn and pnpm.

Installation

npm install -g @themoin/diff-calculator-cli
yarn global add @themoin/diff-calculator-cli
pnpm install -g @themoin/diff-calculator-cli

Example Usage

Run the following command to see the how to use the CLI

calcdiff --help

CLI Example

# To check the diff between origin/dev and HEAD with comment, delete and whitespace ignored
calcdiff origin/dev -w -d -c

# To check the diff between origin/dev and origin/feat/something, and only show the total number of lines changed
calcdiff origin/dev origin/feat/something -q

# To check the diff between origin/dev and HEAD with comment, delete and whitespace ignored, and show the verbose output
calcdiff origin/dev -w -d -c -v

Github Action

The Github Action is available through the marketplace. See action.yaml for the available inputs and outputs.

Please note that you must call @actions/checkout@v4 action with the fetch-depth: 0 to get the full git history. It's because this action use git diff command to calculate diff

Example Usages

1. Fail step if the PR has more than 300 lines changed

jobs:
  check-pr-size:
    steps:
      # This step is required to get the full git history. Or you can use another way you prefer.
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Calculate diff size
        id: get-pr-size
        uses: themoin/[email protected]
        with:
          source: origin/${{ github.head_ref }}
          target: origin/${{ github.base_ref }}
          ignore-deletion: true
          ignore-whitespace: true
          ignore-comment: true
      - name: Fail if the PR has more than 300 lines changed
        run: |
          if [ ${{ steps.get-pr-size.outputs.size }} -gt 300 ]; then
            echo "The PR has more than 300 lines changed"
            exit 1
          fi

2. Label the PR based on the diff size

jobs:
  label-pr-size:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Calculate diff size
        id: get-pr-size
        uses: themoin/[email protected]
        with:
          source: origin/${{ github.head_ref }}
          target: origin/${{ github.base_ref }}
          ignore-deletion: true
          ignore-whitespace: true
          ignore-comment: true
      - name: Label PR size
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          VALUE=${{ steps.get-pr-size.outputs.size }}
          if [ $VALUE -le 10 ]; then
            LABEL="size/10"
          elif [ $VALUE -le 100 ]; then
            LABEL="size/100"
          elif [ $VALUE -le 200 ]; then
            LABEL="size/200"
          elif [ $VALUE -le 300 ]; then
            LABEL="size/300"
          else
            LABEL="size/300+"
          fi
          LABEL_EXISTS=$(gh label list --search $LABEL)
          if [ -z "$LABEL_EXISTS" ]; then
            echo "Creating label $LABEL"
            gh label create $LABEL -c 000000
          fi
          EXISTING=$(gh pr view ${{ github.event.number }} --json labels --jq ".labels[].name" | grep "^size/" || true)
          if [ -z "$EXISTING" ]; then
            echo "Adding label $LABEL"
            gh pr edit ${{ github.event.number }} --add-label $LABEL
          elif [ $EXISTING = $LABEL ]; then
            echo "Label is already set to $LABEL"
          else
            echo "Removing label $EXISTING and adding label $LABEL"
            gh pr edit ${{ github.event.number }} --remove-label $EXISTING
            gh pr edit ${{ github.event.number }} --add-label $LABEL
          fi

Ignoring Files

Both CLI and Github Action support ignoring files by providing a .gitdiffignore file in the root of the repository. Its format is the same as .gitignore file. See the specification of .gitignore.