Skip to content

Commit 754d5d6

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

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/filterFiles.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
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): string[] => {
7+
const removeQuotes = (str: string): string => str.replace(/^"(.*)"$/s, "$1")
8+
const gitExcludeFilesResult = spawnSafeSync(
9+
"git",
10+
["ls-files", "-o", "-i", "--exclude-standard", "--fullname"],
11+
{
12+
cwd: dir,
13+
throwOnError: false,
14+
logStdErrOnError: false,
15+
},
16+
)
17+
return gitExcludeFilesResult.status === 0
18+
? gitExcludeFilesResult.stdout
19+
.toString()
20+
.split(/\n/g)
21+
.map(removeQuotes)
22+
: []
23+
}
424

525
export function removeIgnoredFiles(
626
dir: string,
727
includePaths: RegExp,
828
excludePaths: RegExp,
929
) {
30+
const gitIgnoredPaths = gitExcludePaths(dir)
1031
klawSync(dir, { nodir: true })
1132
.map(item => item.path.slice(`${dir}/`.length))
1233
.filter(
1334
relativePath =>
14-
!relativePath.match(includePaths) || relativePath.match(excludePaths),
35+
!relativePath.match(includePaths) ||
36+
relativePath.match(excludePaths) ||
37+
gitIgnoredPaths.includes(relativePath),
1538
)
1639
.forEach(relativePath => removeSync(join(dir, relativePath)))
1740
}

0 commit comments

Comments
 (0)