Skip to content

Commit fa15d68

Browse files
committed
refactor: extract parseGitZOutput util
1 parent 4b605cd commit fa15d68

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

lib/getStagedFiles.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from 'path'
33
import normalize from 'normalize-path'
44

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

78
export const getStagedFiles = async ({ cwd = process.cwd() } = {}) => {
89
try {
@@ -14,15 +15,7 @@ export const getStagedFiles = async ({ cwd = process.cwd() } = {}) => {
1415

1516
if (!lines) return []
1617

17-
// With `-z`, git prints `fileA\u0000fileB\u0000fileC\u0000` so we need to
18-
// remove the last occurrence of `\u0000` before splitting
19-
return (
20-
lines
21-
// eslint-disable-next-line no-control-regex
22-
.replace(/\u0000$/, '')
23-
.split('\u0000')
24-
.map((file) => normalize(path.resolve(cwd, file)))
25-
)
18+
return parseGitZOutput(lines).map((file) => normalize(path.resolve(cwd, file)))
2619
} catch {
2720
return null
2821
}

lib/parseGitZOutput.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Return array of strings split from the output of `git <something> -z`.
3+
* With `-z`, git prints `fileA\u0000fileB\u0000fileC\u0000` so we need to
4+
* remove the last occurrence of `\u0000` before splitting
5+
*/
6+
export const parseGitZOutput = (input) =>
7+
input
8+
.replace(/\u0000$/, '') // eslint-disable-line no-control-regex
9+
.split('\u0000')

lib/searchConfigs.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import normalize from 'normalize-path'
66

77
import { execGit } from './execGit.js'
88
import { loadConfig, searchPlaces } from './loadConfig.js'
9+
import { parseGitZOutput } from './parseGitZOutput.js'
910
import { validateConfig } from './validateConfig.js'
1011

1112
const EXEC_GIT = ['ls-files', '-z', '--full-name']
@@ -25,20 +26,14 @@ const sortDeepestParth = (a, b) => (numberOfLevels(a) > numberOfLevels(b) ? -1 :
2526
*/
2627
export const searchConfigs = async (gitDir = process.cwd(), logger) => {
2728
/** Get all possible config files known to git */
28-
const cachedFiles = (await execGit(EXEC_GIT, { cwd: gitDir }))
29-
// eslint-disable-next-line no-control-regex
30-
.replace(/\u0000$/, '')
31-
.split('\u0000')
32-
.filter(filterPossibleConfigFiles)
29+
const cachedFiles = parseGitZOutput(await execGit(EXEC_GIT, { cwd: gitDir })).filter(
30+
filterPossibleConfigFiles
31+
)
3332

3433
/** Get all possible config files from uncommitted files */
35-
const otherFiles = (
34+
const otherFiles = parseGitZOutput(
3635
await execGit([...EXEC_GIT, '--others', '--exclude-standard'], { cwd: gitDir })
37-
)
38-
// eslint-disable-next-line no-control-regex
39-
.replace(/\u0000$/, '')
40-
.split('\u0000')
41-
.filter(filterPossibleConfigFiles)
36+
).filter(filterPossibleConfigFiles)
4237

4338
/** Sort possible config files so that deepest is first */
4439
const possibleConfigFiles = [...cachedFiles, ...otherFiles]

test/parseGitZOutput.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { parseGitZOutput } from '../lib/parseGitZOutput'
2+
3+
describe('parseGitZOutput', () => {
4+
it('should split string from `git -z` control character', () => {
5+
const input = 'a\u0000b\u0000c'
6+
expect(parseGitZOutput(input)).toEqual(['a', 'b', 'c'])
7+
})
8+
9+
it('should remove trailing `git -z` control character', () => {
10+
const input = 'a\u0000'
11+
expect(parseGitZOutput(input)).toEqual(['a'])
12+
})
13+
})

0 commit comments

Comments
 (0)