1 | import path from 'node:path'
|
2 |
|
3 | import normalize from 'normalize-path'
|
4 |
|
5 | import { execGit } from './execGit.js'
|
6 | import { parseGitZOutput } from './parseGitZOutput.js'
|
7 |
|
8 | export const getStagedFiles = async ({ cwd = process.cwd(), diff, diffFilter } = {}) => {
|
9 | try {
|
10 | |
11 |
|
12 |
|
13 |
|
14 | const diffFilterArg = diffFilter !== undefined ? diffFilter.trim() : 'ACMR'
|
15 |
|
16 |
|
17 | const diffArgs = diff !== undefined ? diff.trim().split(' ') : ['--staged']
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 | const lines = await execGit(
|
24 | ['diff', '--name-only', '-z', `--diff-filter=${diffFilterArg}`, ...diffArgs],
|
25 | { cwd }
|
26 | )
|
27 | if (!lines) return []
|
28 |
|
29 | return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file)))
|
30 | } catch {
|
31 | return null
|
32 | }
|
33 | }
|