Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
file

GitHub Action

Get Diff Action

v1.2.8

Get Diff Action

file

Get Diff Action

GitHub actions to get git diff

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Get Diff Action

uses: technote-space/[email protected]

Learn more about this action in technote-space/get-diff-action

Choose a version

Get Diff Action

CI Status codecov CodeFactor License: MIT

Read this in other languages: English, 日本語.

GitHub actions to get git diff.

Table of Contents

Details

Screenshots

  1. Example workflow
    Example workflow
  2. Skip
    Skip

Usage

on: pull_request
name: CI
jobs:
  eslint:
    name: ESLint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v1
        with:
          PREFIX_FILTER: |
            src
            __tests__
          SUFFIX_FILTER: .ts
      - name: Install Package dependencies
        run: yarn install
        if: env.GIT_DIFF
      - name: Check code style
        # Check only the source codes that have differences
        run: yarn eslint ${{ env.GIT_DIFF }}
        if: env.GIT_DIFF

  phpmd:
    name: PHPMD
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v1
        id: git_diff
        with:
          SUFFIX_FILTER: .php
          SEPARATOR: ','
      - name: Install composer
        run: composer install
        if: steps.git_diff.outputs.diff
      - name: Check code style
        # Check only the source codes that have differences
        run: vendor/bin/phpmd ${{ steps.git_diff.outputs.diff }} ansi phpmd.xml
        if: steps.git_diff.outputs.diff

If there is no difference in the source code below, this workflow will skip the code style check

  • src/**/*.ts
  • __tests__/**/*.ts

Behavior

  1. Get git diff

    git diff ${FROM}${DOT}${TO} '--diff-filter=${DIFF_FILTER}' --name-only

    e.g. (default)

    DOT: '...'
    DIFF_FILTER: 'AM'

    =>

    git diff ${FROM}...${TO} '--diff-filter=AM' --name-only

    =>

    .github/workflows/ci.yml
    __tests__/utils/command.test.ts
    package.json
    src/main.ts
    src/utils/command.ts
    yarn.lock
    

    ${FROM}, ${TO}

  2. Filtered by PREFIX_FILTER or SUFFIX_FILTER option

    e.g.

    SUFFIX_FILTER: .ts
    PREFIX_FILTER: src/

    =>

    src/main.ts
    src/utils/command.ts
    
  3. Mapped to absolute if ABSOLUTE option is true (default: false)

    e.g.

    /home/runner/work/my-repo-name/my-repo-name/src/main.ts
    /home/runner/work/my-repo-name/my-repo-name/src/utils/command.ts
    
  4. Combined by SEPARATOR option

    e.g. (default)

    SEPARATOR: ' '

    =>

    /home/runner/work/my-repo-name/my-repo-name/src/main.ts /home/runner/work/my-repo-name/my-repo-name/src/utils/command.ts
    

Outputs

name description e.g.
diff The results of diff file names.
If inputs SET_ENV_NAME(default: GIT_DIFF) is set, an environment variable is set with that name.
src/main.ts src/utils/command.ts
count The number of diff files.
If inputs SET_ENV_NAME_COUNT(default: '') is set, an environment variable is set with that name.
100
insertions The number of insertions lines.
If inputs SET_ENV_NAME_INSERTIONS(default: '') is set, an environment variable is set with that name.
100
deletions The number of deletions lines.
If inputs SET_ENV_NAME_DELETIONS(default: '') is set, an environment variable is set with that name.
100
lines The number of diff lines.
If inputs SET_ENV_NAME_LINES(default: '') is set, an environment variable is set with that name.
200

Action event details

Target events

eventName action
pull_request opened, reopened, synchronize, closed
push *

If called on any other event, the result will be empty.

Addition

FROM, TO

condition FROM TO
tag push --- ---
pull request pull.base.ref (e.g. master) context.ref (e.g. refs/pull/123/merge)
push (has related pull request) pull.base.ref (e.g. master) refs/pull/${pull.number}/merge (e.g. refs/pull/123/merge)
context.payload.before = '000...000' default branch (e.g. master) context.payload.after
else context.payload.before context.payload.after

Author

GitHub (Technote)
Blog