Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GitHub Actions to get git diff

License

NotificationsYou must be signed in to change notification settings

PlatziDev/get-diff-action

 
 

Repository files navigation

CI StatuscodecovCodeFactorLicense: 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 withTOC Generator

Screenshots

  1. Example workflow

    Example workflow

  2. Skip

    Skip

Usage

Basic Usage

on:pull_requestname:CIjobs:eslint:name:ESLintruns-on:ubuntu-lateststeps:      -uses:actions/checkout@v4      -uses:PlatziDev/get-diff-action@v0.1.0with:PATTERNS:|            +(src|__tests__)/**/*.ts            !src/exclude.tsFILES:|            yarn.lock            .eslintrc      -name:Install Package dependenciesrun:yarn installif:env.GIT_DIFF      -name:Check code style# Check only if there are differences in the source coderun:yarn lintif: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

namevalue
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_requestname:CIjobs:eslint:name:ESLintruns-on:ubuntu-lateststeps:      -uses:actions/checkout@v4      -uses:PlatziDev/get-diff-action@v0.1.0with:PATTERNS:|            +(src|__tests__)/**/*.tsFILES:|            yarn.lock            .eslintrc      -name:Install Package dependenciesrun:yarn installif:env.GIT_DIFF      -name:Check code style# Check only source files with differencesrun: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 lintif: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.tspackage.jsonsrc/main.tssrc/utils/command.tssrc/docs.mdyarn.lock

    ${FROM}, ${TO}

  2. Filtered byPATTERNS option

    e.g.

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

    =>

    src/main.tssrc/docs.md
  3. Filtered byFILES option

    e.g.

    FILES:package.json

    =>

    package.jsonanywhere/package.json
  4. Mapped to absolute ifABSOLUTE 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 bySEPARATOR 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

namedescriptione.g.
diffThe results of diff file names.
If inputsSET_ENV_NAME(default:GIT_DIFF) is set, an environment variable is set with that name.
src/main.ts src/docs.md
countThe number of diff files.
If inputsSET_ENV_NAME_COUNT(default:'') is set, an environment variable is set with that name.
100
insertionsThe number of insertions lines. (Available only ifGET_FILE_DIFF istrue)
If inputsSET_ENV_NAME_INSERTIONS(default:'') is set, an environment variable is set with that name.
100
deletionsThe number of deletions lines. (Available only ifGET_FILE_DIFF istrue)
If inputsSET_ENV_NAME_DELETIONS(default:'') is set, an environment variable is set with that name.
100
linesThe number of diff lines. (Available only ifGET_FILE_DIFF istrue)
If inputsSET_ENV_NAME_LINES(default:'') is set, an environment variable is set with that name.
200

Action event details

Target events

eventNameaction
pull_requestopened, reopened, synchronize, closed, ready_for_review
push*

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

Addition

FROM, TO

conditionFROMTO
tag push------
pull requestpull.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
elsecontext.payload.beforecontext.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:ESLintruns-on:ubuntu-lateststeps:      -uses:actions/checkout@v4      -uses:PlatziDev/get-diff-action@v0.1.0with:CHECK_ONLY_COMMIT_WHEN_DRAFT:true# ...

To get the result in Json format

on:pull_requestname:CIjobs:dump:name:Dumpruns-on:ubuntu-lateststeps:      -uses:actions/checkout@v4      -uses:PlatziDev/get-diff-action@v0.1.0with:PATTERNS:|            +(src|__tests__)/**/*.ts            !src/exclude.tsFORMAT:json      -run:echo '${{ env.GIT_DIFF }}' | jq .

Result:

> Runecho'["yarn.lock"]'| jq.["yarn.lock"]

Specify a relative path

GitHub Actions doesn't supportworking-directory foruses, so you can't run this action separately for monorepo configuration, etc. However, if you specify theRELATIVE option, it will be used as--relative=<RELATIVE> forgit diff.

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

on:pull_requestname:CIjobs:dump:name:Dumpruns-on:ubuntu-lateststeps:      -uses:actions/checkout@v4      -uses:PlatziDev/get-diff-action@v0.1.0with:PATTERNS:'*.ts'RELATIVE:'src/abc'      -run:echo ${{ env.GIT_DIFF }}

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

> Runecho'test1.ts''test2.ts'test1.ts test2.ts

About

GitHub Actions to get git diff

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript99.1%
  • Other0.9%

[8]ページ先頭

©2009-2025 Movatter.jp