From 8ba2227a8c204b72408cdd282aaeebea7f598f99 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Sun, 25 Jun 2023 21:42:31 +0300 Subject: [PATCH 01/16] fix: use commander to imply --no-stash from --diff --- bin/lint-staged.js | 8 +++++--- lib/index.js | 3 +-- lib/runAll.js | 3 +-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/bin/lint-staged.js b/bin/lint-staged.js index cf74b5389..6a4a4e21a 100755 --- a/bin/lint-staged.js +++ b/bin/lint-staged.js @@ -42,9 +42,11 @@ cli.option('--cwd [path]', 'run all tasks in specific directory, instead of the cli.option('-d, --debug', 'print additional debug information', false) -cli.option( - '--diff [string]', - 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".' +cli.addOption( + new Option( + '--diff [string]', + 'override the default "--staged" flag of "git diff" to get list of files. Implies "--no-stash".' + ).implies({ stash: false }) ) cli.option( diff --git a/lib/index.js b/lib/index.js index 7fdbebccc..d9c47a54a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -75,8 +75,7 @@ const lintStaged = async ( quiet = false, relative = false, shell = false, - // Stashing should be disabled by default when the `diff` option is used - stash = diff === undefined, + stash, verbose = false, } = {}, logger = console diff --git a/lib/runAll.js b/lib/runAll.js index aee972bcf..b2a2c5483 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -77,8 +77,7 @@ export const runAll = async ( quiet = false, relative = false, shell = false, - // Stashing should be disabled by default when the `diff` option is used - stash = diff === undefined, + stash, verbose = false, }, logger = console From 1252fc36c2d3a36e076a7368a97d5755c61b86e4 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Sun, 25 Jun 2023 21:43:32 +0300 Subject: [PATCH 02/16] test(e2e): add "--diff implies --no-stash" test --- test/e2e/__utils__/getLintStagedExecutor.js | 14 ++++++ test/e2e/no-stash.test.js | 50 +++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 test/e2e/__utils__/getLintStagedExecutor.js create mode 100644 test/e2e/no-stash.test.js diff --git a/test/e2e/__utils__/getLintStagedExecutor.js b/test/e2e/__utils__/getLintStagedExecutor.js new file mode 100644 index 000000000..7fd18b33d --- /dev/null +++ b/test/e2e/__utils__/getLintStagedExecutor.js @@ -0,0 +1,14 @@ +import { promisify } from 'node:util' +import { resolve } from 'node:path' + +const exec = promisify(require('child_process').exec) +const lintStagedPath = '../../../bin/lint-staged.js' + +/** + * @param {string} cwd + * @return {Function} + */ +export const getLintStagedExecutor = + (cwd) => + async (params = '') => + await exec(resolve(__dirname, `${lintStagedPath} --cwd=${cwd} ${params}`)) diff --git a/test/e2e/no-stash.test.js b/test/e2e/no-stash.test.js new file mode 100644 index 000000000..0e11a2073 --- /dev/null +++ b/test/e2e/no-stash.test.js @@ -0,0 +1,50 @@ +import '../integration/__mocks__/resolveConfig.js' + +import { jest } from '@jest/globals' + +import { withGitIntegration } from '../integration/__utils__/withGitIntegration.js' +import * as fileFixtures from '../integration/__fixtures__/files.js' +import * as configFixtures from '../integration/__fixtures__/configs.js' + +import { getLintStagedExecutor } from './__utils__/getLintStagedExecutor.js' + +jest.setTimeout(20000) +jest.retryTimes(2) + +describe('lint-staged', () => { + test( + '--diff implies --no-stash', + withGitIntegration(async ({ execGit, writeFile, cwd }) => { + const lintStaged = getLintStagedExecutor(cwd) + + await execGit(['checkout', '-b', 'my-branch']) + await writeFile('.lintstagedrc.json', JSON.stringify(configFixtures.prettierListDifferent)) + await writeFile('test.js', fileFixtures.prettyJS) + await execGit(['add', '.lintstagedrc.json']) + await execGit(['add', 'test.js']) + await execGit(['commit', '-m', 'test']) + + let res = await lintStaged() + expect(res.stdout).toMatch('No staged files found.') + + res = await lintStaged('--stash') + expect(res.stdout).toMatch('No staged files found.') + + res = await lintStaged('--no-stash') + expect(res.stdout).toMatch('No staged files found.') + expect(res.stderr).toMatch('Skipping backup because `--no-stash` was used.') + + res = await lintStaged('--diff=main...my-branch') + expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') + + try { + await lintStaged('--diff=main...my-branch --stash') + } catch (err) { + expect(err.stderr).toMatch('lint-staged failed due to a git error.') + } + + res = await lintStaged('--diff=main...my-branch --no-stash') + expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') + }) + ) +}) From ff52a2895cd15c16c1e597d18ce62979e9cedc2c Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Sun, 25 Jun 2023 21:44:05 +0300 Subject: [PATCH 03/16] docs(README): minor fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7a0077286..aa07571c5 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Options: - **`--diff`**: By default linters are filtered against all files staged in git, generated from `git diff --staged`. This option allows you to override the `--staged` flag with arbitrary revisions. For example to get a list of changed files between two branches, use `--diff="branch1...branch2"`. You can also read more from about [git diff](https://git-scm.com/docs/git-diff) and [gitrevisions](https://git-scm.com/docs/gitrevisions). This option also implies `--no-stash`. - **`--diff-filter`**: By default only files that are _added_, _copied_, _modified_, or _renamed_ are included. Use this flag to override the default `ACMR` value with something else: _added_ (`A`), _copied_ (`C`), _deleted_ (`D`), _modified_ (`M`), _renamed_ (`R`), _type changed_ (`T`), _unmerged_ (`U`), _unknown_ (`X`), or _pairing broken_ (`B`). See also the `git diff` docs for [--diff-filter](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203). - **`--max-arg-length`**: long commands (a lot of files) are automatically split into multiple chunks when it detects the current shell cannot handle them. Use this flag to override the maximum length of the generated command string. -- **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. Can be re-enabled with `--stash` +- **`--no-stash`**: By default a backup stash will be created before running the tasks, and all task modifications will be reverted in case of an error. This option will disable creating the stash, and instead leave all modifications in the index when aborting the commit. Can be re-enabled with `--stash`. - **`--quiet`**: Supress all CLI output, except from tasks. - **`--relative`**: Pass filepaths relative to `process.cwd()` (where `lint-staged` runs) to tasks. Default is `false`. - **`--shell`**: By default linter commands will be parsed for speed and security. This has the side-effect that regular shell scripts might not work as expected. You can skip parsing of commands with this option. To use a specific shell, use a path like `--shell "/bin/bash"`. From 5e5b9eeaaacd8673b7bce5c42a67ae9ad668031d Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Sun, 25 Jun 2023 22:23:14 +0300 Subject: [PATCH 04/16] fix: restore default --stash assignment to fix tests --- lib/index.js | 3 ++- lib/runAll.js | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index d9c47a54a..7fdbebccc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -75,7 +75,8 @@ const lintStaged = async ( quiet = false, relative = false, shell = false, - stash, + // Stashing should be disabled by default when the `diff` option is used + stash = diff === undefined, verbose = false, } = {}, logger = console diff --git a/lib/runAll.js b/lib/runAll.js index b2a2c5483..3ff7039a8 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -76,8 +76,9 @@ export const runAll = async ( maxArgLength, quiet = false, relative = false, - shell = false, - stash, + shell, + // Stashing should be disabled by default when the `diff` option is used + stash = diff === undefined, verbose = false, }, logger = console From bdf4363ec8ca2cdd1c6891e0ba221790a9ad68ca Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Sun, 25 Jun 2023 22:26:12 +0300 Subject: [PATCH 05/16] fix: restore --shell default value --- lib/runAll.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runAll.js b/lib/runAll.js index 3ff7039a8..aee972bcf 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -76,7 +76,7 @@ export const runAll = async ( maxArgLength, quiet = false, relative = false, - shell, + shell = false, // Stashing should be disabled by default when the `diff` option is used stash = diff === undefined, verbose = false, From 8001ac6069c374bc018c53b821ff8e1b695b6fb9 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Mon, 26 Jun 2023 21:44:38 +0300 Subject: [PATCH 06/16] feat: switch from exec to execa --- test/e2e/__utils__/getLintStagedExecutor.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/e2e/__utils__/getLintStagedExecutor.js b/test/e2e/__utils__/getLintStagedExecutor.js index 7fd18b33d..7067acd79 100644 --- a/test/e2e/__utils__/getLintStagedExecutor.js +++ b/test/e2e/__utils__/getLintStagedExecutor.js @@ -1,8 +1,8 @@ -import { promisify } from 'node:util' import { resolve } from 'node:path' -const exec = promisify(require('child_process').exec) -const lintStagedPath = '../../../bin/lint-staged.js' +import { execaCommand } from 'execa' + +const lintStagedRelativePath = '../../../bin/lint-staged.js' /** * @param {string} cwd @@ -10,5 +10,7 @@ const lintStagedPath = '../../../bin/lint-staged.js' */ export const getLintStagedExecutor = (cwd) => - async (params = '') => - await exec(resolve(__dirname, `${lintStagedPath} --cwd=${cwd} ${params}`)) + async (params = '') => { + let command = resolve(__dirname, lintStagedRelativePath) + return await execaCommand(`${command} --cwd=${cwd} ${params}`) + } From 3a63d00586bc1f1cac3c28c538973d5a151b9bd4 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Mon, 26 Jun 2023 21:49:37 +0300 Subject: [PATCH 07/16] refactor(getLintStagedExecutor.js): extract lintStagedBin command variable --- test/e2e/__utils__/getLintStagedExecutor.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/e2e/__utils__/getLintStagedExecutor.js b/test/e2e/__utils__/getLintStagedExecutor.js index 7067acd79..d1e94afe3 100644 --- a/test/e2e/__utils__/getLintStagedExecutor.js +++ b/test/e2e/__utils__/getLintStagedExecutor.js @@ -2,7 +2,7 @@ import { resolve } from 'node:path' import { execaCommand } from 'execa' -const lintStagedRelativePath = '../../../bin/lint-staged.js' +let lintStagedBin = resolve(__dirname, '../../../bin/lint-staged.js') /** * @param {string} cwd @@ -10,7 +10,5 @@ const lintStagedRelativePath = '../../../bin/lint-staged.js' */ export const getLintStagedExecutor = (cwd) => - async (params = '') => { - let command = resolve(__dirname, lintStagedRelativePath) - return await execaCommand(`${command} --cwd=${cwd} ${params}`) - } + async (params = '') => + await execaCommand(`${lintStagedBin} --cwd=${cwd} ${params}`) From 12c42182e56968ddda9382b6302bbfb69099b86e Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Mon, 26 Jun 2023 23:21:45 +0300 Subject: [PATCH 08/16] ci: add debug statements --- test/e2e/__utils__/getLintStagedExecutor.js | 2 +- test/e2e/no-stash.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/test/e2e/__utils__/getLintStagedExecutor.js b/test/e2e/__utils__/getLintStagedExecutor.js index d1e94afe3..324bd58c9 100644 --- a/test/e2e/__utils__/getLintStagedExecutor.js +++ b/test/e2e/__utils__/getLintStagedExecutor.js @@ -11,4 +11,4 @@ let lintStagedBin = resolve(__dirname, '../../../bin/lint-staged.js') export const getLintStagedExecutor = (cwd) => async (params = '') => - await execaCommand(`${lintStagedBin} --cwd=${cwd} ${params}`) + await execaCommand(`${lintStagedBin} --debug --cwd=${cwd} ${params}`, { cwd }) diff --git a/test/e2e/no-stash.test.js b/test/e2e/no-stash.test.js index 0e11a2073..a44ad4aca 100644 --- a/test/e2e/no-stash.test.js +++ b/test/e2e/no-stash.test.js @@ -25,25 +25,32 @@ describe('lint-staged', () => { await execGit(['commit', '-m', 'test']) let res = await lintStaged() + console.info(res.stdout) expect(res.stdout).toMatch('No staged files found.') res = await lintStaged('--stash') + console.info(res.stdout) expect(res.stdout).toMatch('No staged files found.') res = await lintStaged('--no-stash') + console.info(res.stdout) + console.info(res.stderr) expect(res.stdout).toMatch('No staged files found.') expect(res.stderr).toMatch('Skipping backup because `--no-stash` was used.') res = await lintStaged('--diff=main...my-branch') + console.info(res.stderr) expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') try { await lintStaged('--diff=main...my-branch --stash') } catch (err) { + console.info(err.stderr) expect(err.stderr).toMatch('lint-staged failed due to a git error.') } res = await lintStaged('--diff=main...my-branch --no-stash') + console.info(res.stderr) expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') }) ) From 95cb990fe4f3c0894b1ae9169f75071ac92b3a90 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 15:17:33 +0300 Subject: [PATCH 09/16] ci: try checking out all branches --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3c831377d..9780ed560 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,6 +58,8 @@ jobs: - if: matrix.os == 'windows-latest' run: git config --global core.autocrlf true - uses: actions/checkout@v3 + with: + fetch-depth: 0 - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node }} From 004945ecc137f08ba949fcd84426f21200962ce2 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 15:24:19 +0300 Subject: [PATCH 10/16] ci: temporarily run on all branches to test CI on forked repo --- .github/workflows/main.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9780ed560..ca408416f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,11 +2,8 @@ name: Test & Release on: push: - # Run on pushes to specific branches branches: - - master - - beta - - alpha + - "*" # Do not run on tags tags-ignore: - "*" From c9c3b2399c8d7d23dc18638fd61899016522e771 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 16:06:02 +0300 Subject: [PATCH 11/16] ci: rename main to master --- test/e2e/no-stash.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/e2e/no-stash.test.js b/test/e2e/no-stash.test.js index a44ad4aca..cf744bfbf 100644 --- a/test/e2e/no-stash.test.js +++ b/test/e2e/no-stash.test.js @@ -38,18 +38,18 @@ describe('lint-staged', () => { expect(res.stdout).toMatch('No staged files found.') expect(res.stderr).toMatch('Skipping backup because `--no-stash` was used.') - res = await lintStaged('--diff=main...my-branch') + res = await lintStaged('--diff=master...my-branch') console.info(res.stderr) expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') try { - await lintStaged('--diff=main...my-branch --stash') + await lintStaged('--diff=master...my-branch --stash') } catch (err) { console.info(err.stderr) expect(err.stderr).toMatch('lint-staged failed due to a git error.') } - res = await lintStaged('--diff=main...my-branch --no-stash') + res = await lintStaged('--diff=master...my-branch --no-stash') console.info(res.stderr) expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') }) From 483be94b2db11a995bb04359a92e6ee7984e3cba Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 16:15:14 +0300 Subject: [PATCH 12/16] Revert "ci: try checking out all branches" This reverts commit 95cb990fe4f3c0894b1ae9169f75071ac92b3a90. --- .github/workflows/main.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ca408416f..b3930833e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -55,8 +55,6 @@ jobs: - if: matrix.os == 'windows-latest' run: git config --global core.autocrlf true - uses: actions/checkout@v3 - with: - fetch-depth: 0 - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node }} From 0e24b1f2fa6d57d29c84d1fa7bdce6e7a18efb15 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 16:19:27 +0300 Subject: [PATCH 13/16] Revert "ci: add debug statements" This reverts commit 12c42182e56968ddda9382b6302bbfb69099b86e. --- test/e2e/__utils__/getLintStagedExecutor.js | 2 +- test/e2e/no-stash.test.js | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/test/e2e/__utils__/getLintStagedExecutor.js b/test/e2e/__utils__/getLintStagedExecutor.js index 324bd58c9..d1e94afe3 100644 --- a/test/e2e/__utils__/getLintStagedExecutor.js +++ b/test/e2e/__utils__/getLintStagedExecutor.js @@ -11,4 +11,4 @@ let lintStagedBin = resolve(__dirname, '../../../bin/lint-staged.js') export const getLintStagedExecutor = (cwd) => async (params = '') => - await execaCommand(`${lintStagedBin} --debug --cwd=${cwd} ${params}`, { cwd }) + await execaCommand(`${lintStagedBin} --cwd=${cwd} ${params}`) diff --git a/test/e2e/no-stash.test.js b/test/e2e/no-stash.test.js index cf744bfbf..e454835f9 100644 --- a/test/e2e/no-stash.test.js +++ b/test/e2e/no-stash.test.js @@ -25,32 +25,25 @@ describe('lint-staged', () => { await execGit(['commit', '-m', 'test']) let res = await lintStaged() - console.info(res.stdout) expect(res.stdout).toMatch('No staged files found.') res = await lintStaged('--stash') - console.info(res.stdout) expect(res.stdout).toMatch('No staged files found.') res = await lintStaged('--no-stash') - console.info(res.stdout) - console.info(res.stderr) expect(res.stdout).toMatch('No staged files found.') expect(res.stderr).toMatch('Skipping backup because `--no-stash` was used.') res = await lintStaged('--diff=master...my-branch') - console.info(res.stderr) expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') try { await lintStaged('--diff=master...my-branch --stash') } catch (err) { - console.info(err.stderr) expect(err.stderr).toMatch('lint-staged failed due to a git error.') } res = await lintStaged('--diff=master...my-branch --no-stash') - console.info(res.stderr) expect(res.stderr).toMatch('Skipping backup because `--diff` was used.') }) ) From 6320e925c38f845553af4d022b0fc772857ea761 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 16:29:27 +0300 Subject: [PATCH 14/16] fix: rename main branch to master --- test/integration/__utils__/withGitIntegration.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/integration/__utils__/withGitIntegration.js b/test/integration/__utils__/withGitIntegration.js index 670940fc4..1ff497c40 100644 --- a/test/integration/__utils__/withGitIntegration.js +++ b/test/integration/__utils__/withGitIntegration.js @@ -81,6 +81,9 @@ export const withGitIntegration = // Init repository with initial commit await utils.execGit('init') + // Rename default main branch to master for tests to pass on local environment + await utils.execGit(['branch', '-m', 'master']) + if (isWindowsActions()) { await utils.execGit(['config', 'core.autocrlf', 'input']) } From bf1c011c8ef8f5a21889dd34127f964a32f2ba3a Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Tue, 27 Jun 2023 16:43:57 +0300 Subject: [PATCH 15/16] Revert "ci: temporarily run on all branches to test CI on forked repo" This reverts commit 004945ecc137f08ba949fcd84426f21200962ce2. --- .github/workflows/main.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b3930833e..3c831377d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,8 +2,11 @@ name: Test & Release on: push: + # Run on pushes to specific branches branches: - - "*" + - master + - beta + - alpha # Do not run on tags tags-ignore: - "*" From e9d86de138a1994acfaed7b386cdfe80648166e2 Mon Sep 17 00:00:00 2001 From: Elias Papavasileiou Date: Wed, 28 Jun 2023 08:59:17 +0300 Subject: [PATCH 16/16] feat: rename init branch to master --- test/integration/__utils__/withGitIntegration.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/integration/__utils__/withGitIntegration.js b/test/integration/__utils__/withGitIntegration.js index 1ff497c40..ff05e1a9f 100644 --- a/test/integration/__utils__/withGitIntegration.js +++ b/test/integration/__utils__/withGitIntegration.js @@ -79,10 +79,7 @@ export const withGitIntegration = const utils = getGitUtils(cwd) // Init repository with initial commit - await utils.execGit('init') - - // Rename default main branch to master for tests to pass on local environment - await utils.execGit(['branch', '-m', 'master']) + await utils.execGit(['init', '--initial-branch', 'master']) if (isWindowsActions()) { await utils.execGit(['config', 'core.autocrlf', 'input'])