UNPKG

724 BJavaScriptView Raw
1import path from 'path'
2
3import normalize from 'normalize-path'
4
5import { execGit } from './execGit.js'
6import { parseGitZOutput } from './parseGitZOutput.js'
7
8export const getStagedFiles = async ({ cwd = process.cwd() } = {}) => {
9 try {
10 // Docs for --diff-filter option: https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---diff-filterACDMRTUXB82308203
11 // Docs for -z option: https://git-scm.com/docs/git-diff#Documentation/git-diff.txt--z
12 const lines = await execGit(['diff', '--staged', '--diff-filter=ACMR', '--name-only', '-z'], {
13 cwd,
14 })
15
16 if (!lines) return []
17
18 return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file)))
19 } catch {
20 return null
21 }
22}