Skip to content

Commit 5f78e24

Browse files
committed
exclude git-ignored files from patch creation
fixes ds300#254
1 parent 50f73bd commit 5f78e24

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/filterFiles.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
11
import { join } from "./path"
22
import { removeSync } from "fs-extra"
33
import klawSync from "klaw-sync"
4+
import { spawnSafeSync } from "./spawnSafe"
5+
6+
const gitExcludePaths = (dir: string): RegExp => {
7+
const anyRegExp = (regExps: RegExp[], flags?: string): RegExp =>
8+
regExps.length <= 0
9+
? /(?!)/ // never matches
10+
: new RegExp(regExps.map(regExp => regExp.source).join("|"), flags)
11+
const escapeRegExp = (str: string): string =>
12+
str.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")
13+
const gitExcludeFilesResult = spawnSafeSync(
14+
"git",
15+
["ls-files", "-o", "-i", "--exclude-standard"],
16+
{
17+
cwd: dir,
18+
throwOnError: false,
19+
logStdErrOnError: false,
20+
},
21+
)
22+
return anyRegExp(
23+
gitExcludeFilesResult.status === 0
24+
? gitExcludeFilesResult.stdout
25+
.toString()
26+
.split(/\n/g)
27+
.filter(path => path.length > 0)
28+
.map(escapeRegExp)
29+
.map(escaped => new RegExp(escaped))
30+
: [],
31+
"i",
32+
)
33+
}
434

535
export function removeIgnoredFiles(
636
dir: string,
737
includePaths: RegExp,
838
excludePaths: RegExp,
939
) {
40+
const gitIgnoredPaths = gitExcludePaths(dir)
1041
klawSync(dir, { nodir: true })
1142
.map(item => item.path.slice(`${dir}/`.length))
1243
.filter(
1344
relativePath =>
14-
!relativePath.match(includePaths) || relativePath.match(excludePaths),
45+
!relativePath.match(includePaths) ||
46+
relativePath.match(excludePaths) ||
47+
relativePath.match(gitIgnoredPaths),
1548
)
1649
.forEach(relativePath => removeSync(join(dir, relativePath)))
1750
}

0 commit comments

Comments
 (0)