8000 fix: use new `--diff` and `--diff-filter` options when checking task … · lint-staged/lint-staged@1a5a66a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a5a66a

Browse files
fix: use new --diff and --diff-filter options when checking task modifications
1 parent 32806da commit 1a5a66a

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

lib/getDiffCommand.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function getDiffCommand(diff, diffFilter) {
2+
/**
3+
* Docs for --diff-filter option:
4+
* @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
5+
*/
6+
const diffFilterArg = diffFilter !== undefined ? diffFilter.trim() : 'ACMR'
7+
8+
/** Use `--diff branch1...branch2` or `--diff="branch1 branch2", or fall back to default staged files */
9+
const diffArgs = diff !== undefined ? diff.trim().split(' ') : ['--staged']
10+
11+
/**
12+
* Docs for -z option:
13+
* @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--z
14+
*/
15+
const diffCommand = ['diff', '--name-only', '-z', `--diff-filter=${diffFilterArg}`, ...diffArgs]
16+
17+
return diffCommand
18+
}

lib/getStagedFiles.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,12 @@ import path from 'node:path'
33
import normalize from 'normalize-path'
44

55
import { execGit } from './execGit.js'
6+
import { getDiffCommand } from './getDiffCommand.js'
67
import { parseGitZOutput } from './parseGitZOutput.js'
78

89
export const getStagedFiles = async ({ cwd = process.cwd(), diff, diffFilter } = {}) => {
910
try {
10-
/**
11-
* Docs for --diff-filter option:
12-
* @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
13-
*/
14-
const diffFilterArg = diffFilter !== undefined ? diffFilter.trim() : 'ACMR'
15-
16-
/** Use `--diff branch1...branch2` or `--diff="branch1 branch2", or fall back to default staged files */
17-
const diffArgs = diff !== undefined ? diff.trim().split(' ') : ['--staged']
18-
19-
/**
20-
* Docs for -z option:
21-
* @see https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--z
22-
*/
23-
const lines = await execGit(
24-
['diff', '--name-only', '-z', `--diff-filter=${diffFilterArg}`, ...diffArgs],
25-
{ cwd }
26-
)
11+
const lines = await execGit(getDiffCommand(diff, diffFilter), { cwd })
2712
if (!lines) return []
2813

2914
return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file)))

lib/gitWorkflow.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import debug from 'debug'
44

55
import { execGit } from './execGit.js'
66
import { readFile, unlink, writeFile } from './file.js'
7+
import { getDiffCommand } from './getDiffCommand.js'
78
import {
89
GitError,
910
RestoreOriginalStateError,
@@ -65,12 +66,13 @@ const handleError = (error, ctx, symbol) => {
6566
}
6667

6768
export class GitWorkflow {
68-
constructor({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks }) {
69+
constructor({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks, diff, diffFilter }) {
6970
this.execGit = (args, options = {}) => execGit(args, { ...options, cwd: gitDir })
7071
this.deletedFiles = []
7172
this.gitConfigDir = gitConfigDir
7273
this.gitDir = gitDir
73-
this.unstagedDiff = null
74+
this.diff = diff
75+
this.diffFilter = diffFilter
7476
this.allowEmpty = allowEmpty
7577
this.matchedFileChunks = matchedFileChunks
7678

@@ -262,7 +264,7 @@ export class GitWorkflow {
262264

263265
debugLog('Done adding task modifications to index!')
264266

265-
const stagedFilesAfterAdd = await this.execGit(['diff', '--name-only', '--cached'])
267+
const stagedFilesAfterAdd = await this.execGit(getDiffCommand(this.diff, this.diffFilter))
266268
if (!stagedFilesAfterAdd && !this.allowEmpty) {
267269
// Tasks reverted all staged changes and the commit would be empty
268270
// Throw error to stop commit unless `--allow-empty` was used

lib/runAll.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,14 @@ export const runAll = async (
262262
relative: false,
263263
})
264264

265-
const git = new GitWorkflow({ allowEmpty, gitConfigDir, gitDir, matchedFileChunks })
265+
const git = new GitWorkflow({
266+
allowEmpty,
267+
gitConfigDir,
268+
gitDir,
269+
matchedFileChunks,
270+
diff,
271+
diffFilter,
272+
})
266273

267274
const runner = new Listr(
268275
[

test/unit/getDiffCommand.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { getDiffCommand } from '../../lib/getDiffCommand.js'
2+
3+
describe('chunkFiles', () => {
4+
const customDiffString = 'origin/main..custom-branch'
5+
const customDiffSpaceSeparatedString = 'origin/main custom-branch'
6+
const customDiffFilter = 'a'
7+
8+
it('should default to sane value', () => {
9+
const diff = getDiffCommand()
10+
expect(diff).toEqual(['diff', '--name-only', '-z', `--diff-filter=ACMR`, '--staged'])
11+
})
12+
13+
it('should work only with diff set as string', () => {
14+
const diff = getDiffCommand(customDiffString)
15+
expect(diff).toEqual([
16+
'diff',
17+
'--name-only',
18+
'-z',
19+
`--diff-filter=ACMR`,
20+
'origin/main..custom-branch',
21+
])
22+
})
23+
24+
it('should work only with diff set as space separated string', () => {
25+
const diff = getDiffCommand(customDiffSpaceSeparatedString)
26+
expect(diff).toEqual([
27+
'diff',
28+
'--name-only',
29+
'-z',
30+
`--diff-filter=ACMR`,
31+
'origin/main',
32+
'custom-branch',
33+
])
34+
})
35+
36+
it('should work only with diffFilter set', () => {
37+
const diff = getDiffCommand(undefined, customDiffFilter)
38+
expect(diff).toEqual(['diff', '--name-only', '-z', `--diff-filter=a`, '--staged'])
39+
})
40+
41+
it('should work with both diff and diffFilter set', () => {
42+
const diff = getDiffCommand(customDiffString, customDiffFilter)
43+
expect(diff).toEqual([
44+
'diff',
45+
'--name-only',
46+
'-z',
47+
`--diff-filter=a`,
48+
'origin/main..custom-branch',
49+
])
50+
})
51+
})

0 commit comments

Comments
 (0)
0