Skip to content

Commit 075c5a1

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

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/filterFiles.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
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+
.map(escapeRegExp)
28+
.map(escaped => new RegExp(escaped))
29+
: [],
30+
"i",
31+
)
32+
}
433

534
export function removeIgnoredFiles(
635
dir: string,
736
includePaths: RegExp,
837
excludePaths: RegExp,
938
) {
39+
const gitIgnoredPaths = gitExcludePaths(dir)
1040
klawSync(dir, { nodir: true })
1141
.map(item => item.path.slice(`${dir}/`.length))
1242
.filter(
1343
relativePath =>
14-
!relativePath.match(includePaths) || relativePath.match(excludePaths),
44+
!relativePath.match(includePaths) ||
45+
relativePath.match(excludePaths) ||
46+
relativePath.match(gitIgnoredPaths),
1547
)
1648
.forEach(relativePath => removeSync(join(dir, relativePath)))
1749
}

0 commit comments

Comments
 (0)