• This repository has been archived on 24/Nov/2023
  • Stars
    star
    191
  • Rank 202,877 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GitHub Actions to get git diff

Get Diff Action

CI Status codecov CodeFactor License: MIT

Read this in other languages: English, ζ—₯本θͺž.

GitHub actions to get git diff.
You can get the differences via env or action output.

Table of Contents

Details

generated with TOC Generator

Screenshots

  1. Example workflow

    Example workflow

  2. Skip

    Skip

Usage

Basic 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@v6
        with:
          PATTERNS: |
            +(src|__tests__)/**/*.ts
            !src/exclude.ts
          FILES: |
            yarn.lock
            .eslintrc
      - name: Install Package dependencies
        run: yarn install
        if: env.GIT_DIFF
      - name: Check code style
        # Check only if there are differences in the source code
        run: yarn lint
        if: env.GIT_DIFF

Details of the patterns that can be specified

Example of matching files

  • src/main.ts
  • src/utils/abc.ts
  • __tests__/test.ts
  • yarn.lock
  • .eslintrc
  • anywhere/yarn.lock

Examples of non-matching files

  • main.ts
  • src/xyz.txt
  • src/exclude.ts

Examples of env

name value
GIT_DIFF 'src/main.ts' 'src/utils/abc.ts' '__tests__/test.ts' 'yarn.lock' '.eslintrc' 'anywhere/yarn.lock'
GIT_DIFF_FILTERED 'src/main.ts' 'src/utils/abc.ts' '__tests__/test.ts'
MATCHED_FILES 'yarn.lock' '.eslintrc' 'anywhere/yarn.lock'

Specify a little more detail

on: pull_request
name: CI
jobs:
  eslint:
    name: ESLint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v6
        with:
          PATTERNS: |
            +(src|__tests__)/**/*.ts
          FILES: |
            yarn.lock
            .eslintrc
      - name: Install Package dependencies
        run: yarn install
        if: env.GIT_DIFF
      - name: Check code style
        # Check only source files with differences
        run: yarn eslint ${{ env.GIT_DIFF_FILTERED }}  # e.g. yarn eslint 'src/main.ts' '__tests__/test.ts'
        if: env.GIT_DIFF && !env.MATCHED_FILES
      - name: Check code style
        # Check only if there are differences in the source code (Run a lint on all files if there are changes to yarn.lock or .eslintrc)
        run: yarn lint
        if: env.GIT_DIFF && env.MATCHED_FILES

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: 'AMRC'

    =>

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

    =>

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

    ${FROM}, ${TO}

  2. Filtered by PATTERNS option

    e.g.

    PATTERNS: |
      src/**/*.+(ts|md)
      !src/utils/*

    =>

    src/main.ts
    src/docs.md
    
  3. Filtered by FILES option

    e.g.

    FILES: package.json

    =>

    package.json
    anywhere/package.json
    
  4. 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/docs.md
    
  5. 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/docs.md
    

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/docs.md
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. (Available only if GET_FILE_DIFF is true)
If inputs SET_ENV_NAME_INSERTIONS(default: '') is set, an environment variable is set with that name.
100
deletions The number of deletions lines. (Available only if GET_FILE_DIFF is true)
If inputs SET_ENV_NAME_DELETIONS(default: '') is set, an environment variable is set with that name.
100
lines The number of diff lines. (Available only if GET_FILE_DIFF is true)
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, ready_for_review
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. main) context.ref (e.g. refs/pull/123/merge)
push (which has related pull request) pull.base.ref (e.g. main) refs/pull/${pull.number}/merge (e.g. refs/pull/123/merge)
context.payload.before = '000...000' default branch (e.g. main) context.payload.after
else context.payload.before context.payload.after

Check only the latest commit differences in a draft Pull Request

on:
  pull_request:
    types: [opened, reopened, synchronize, closed, ready_for_review]

jobs:
  eslint:
    name: ESLint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v6
        with:
          CHECK_ONLY_COMMIT_WHEN_DRAFT: true
      # ...

To get the result in Json format

on: pull_request
name: CI
jobs:
  dump:
    name: Dump
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v6
        with:
          PATTERNS: |
            +(src|__tests__)/**/*.ts
            !src/exclude.ts
          FORMAT: json
      - run: echo '${{ env.GIT_DIFF }}' | jq .

Result:

> Run echo '["yarn.lock"]' | jq .
[
  "yarn.lock"
]

Specify a relative path

GitHub Actions doesn't support working-directory for uses, so you can't run this action separately for monorepo configuration, etc. However, if you specify the RELATIVE option, it will be used as --relative=<RELATIVE> for git diff.

https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---relativeltpathgt

on: pull_request
name: CI
jobs:
  dump:
    name: Dump
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: technote-space/get-diff-action@v6
        with:
          PATTERNS: '*.ts'
          RELATIVE: 'src/abc'
      - run: echo ${{ env.GIT_DIFF }}

If the files src/abc/test1.ts, src/abc/test2.ts, src/abc/test3.txt, and src/test4.ts exist, the result will be as follows:

> Run echo 'test1.ts' 'test2.ts'
test1.ts test2.ts

Author

GitHub (Technote)

Blog

More Repositories

1

toc-generator

GitHub Actions to generate TOC (Table of Contents)
TypeScript
219
star
2

workflow-conclusion-action

GitHub action to get workflow conclusion.
TypeScript
143
star
3

release-github-actions

GitHub Actions to automate the release of GitHub Actions
TypeScript
80
star
4

assign-author

GitHub Actions to assign author to issue or PR
TypeScript
70
star
5

auto-cancel-redundant-workflow

GitHub Actions to automatically cancel redundant workflow.
TypeScript
59
star
6

package-version-check-action

GitHub Actions to check package version before publish
TypeScript
36
star
7

auto-card-labeler

GitHub actions to auto label a pull request or an issue based on project card move
TypeScript
35
star
8

gh-actions-template

Template for GitHub Actions
TypeScript
31
star
9

create-pr-action

GitHub Actions to manage PullRequest
TypeScript
31
star
10

broken-link-checker-action

GitHub Actions to check broken links and create issues.
TypeScript
24
star
11

add-richtext-toolbar-button

WordPress plugin to add richtext toolbar button
PHP
17
star
12

create-project-card-action

GitHub actions to create project card
TypeScript
15
star
13

pr-commit-body-action

GitHub action to add commit history to PR body
TypeScript
13
star
14

frourio-demo

TypeScript
12
star
15

jquery.marker-animation

Marker animation jQuery plugin
JavaScript
12
star
16

marker-animation

WordPress plugin to add underline animation
PHP
11
star
17

github-action-helper

Helper for GitHub Actions
TypeScript
10
star
18

y-proofreading

WordPress plugin to add feature of Japanese proofreading
PHP
10
star
19

load-config-action

TypeScript
6
star
20

github-action-pr-helper

PullRequest Helper for GitHub Actions
TypeScript
5
star
21

hide-blocks-temporarily

WordPress plugin to hide Gutenberg blocks temporarily
JavaScript
5
star
22

prisma-seeder-tools

TypeScript
4
star
23

ga-framework

Genetic Algorithm Framework
TypeScript
4
star
24

release-github-actions-cli

CLI tool of Release GitHub Actions
TypeScript
4
star
25

register-grouped-format-type

Gutenberg's library to provide method to register grouped RichText format type
JavaScript
4
star
26

laravel-reservation-management-system

Reservation system
PHP
4
star
27

genetic-algorithms-py

Genetic Algorithm
Python
3
star
28

can-npm-publish-action

GitHub Actions to check if it can be published to npm.
TypeScript
3
star
29

github-action-test-helper

Test helper for GitHub Actions.
TypeScript
3
star
30

ga-framework-template

TypeScript
3
star
31

use-local-storage

TypeScript
3
star
32

change-block-keywords

WordPress plugin to change keywords for block search
JavaScript
3
star
33

get-next-version-action

GitHub Actions to get next version from commit histories.
TypeScript
3
star
34

csrf-detector

CSRF Detector for WordPress
PHP
3
star
35

ts-package-template

Template for npm package.
Shell
2
star
36

technote-space

2
star
37

download-annotations-action

GitHub actions to download annotations
TypeScript
2
star
38

github-actions-workflows

Shell
2
star
39

gutenberg-package-versions

Versions of Gutenberg's packages (auto updated by GitHub Actions)
Shell
2
star
40

laravel-search-helper

Search helper for Laravel
PHP
2
star
41

wp-related-post-jp

WordPress recommendation plugin for Japanese
PHP
2
star
42

genetic-algorithms-js

Genetic Algorithms Library
TypeScript
2
star
43

get-git-comment-action

GitHub actions to get git comment.
TypeScript
1
star
44

laravel-crud-helper

CRUD helper for Laravel
PHP
1
star
45

contact-form-7-huge-file-upload

CF7 extension plugin to upload huge file
PHP
1
star
46

worker-controller

Worker Controller
TypeScript
1
star
47

github-action-log-helper

Logging helpers for GitHub Actions
TypeScript
1
star
48

advanced-block-editor

WordPress plugin to add some useful features to block editor
JavaScript
1
star
49

python-setup

Shell
1
star
50

gutenberg-packages

Library to get versions of gutenberg packages
PHP
1
star
51

hello-genetic-algorithm

Generate `Hello,World!` string using Genetic Algorithm
TypeScript
1
star
52

gutenberg-utils

Utility for gutenberg
JavaScript
1
star
53

laravel-transaction-fire-event

PHP
1
star