Skip to content

Commit

Permalink
exclude git-ignored files from patch creation
Browse files Browse the repository at this point in the history
fixes ds300#254
  • Loading branch information
gomain committed Sep 7, 2020
1 parent 50f73bd commit 5f78e24
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/filterFiles.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
import { join } from "./path"
import { removeSync } from "fs-extra"
import klawSync from "klaw-sync"
import { spawnSafeSync } from "./spawnSafe"

const gitExcludePaths = (dir: string): RegExp => {
const anyRegExp = (regExps: RegExp[], flags?: string): RegExp =>
regExps.length <= 0
? /(?!)/ // never matches
: new RegExp(regExps.map(regExp => regExp.source).join("|"), flags)
const escapeRegExp = (str: string): string =>
str.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")
const gitExcludeFilesResult = spawnSafeSync(
"git",
["ls-files", "-o", "-i", "--exclude-standard"],
{
cwd: dir,
throwOnError: false,
logStdErrOnError: false,
},
)
return anyRegExp(
gitExcludeFilesResult.status === 0
? gitExcludeFilesResult.stdout
.toString()
.split(/\n/g)
.filter(path => path.length > 0)
.map(escapeRegExp)
.map(escaped => new RegExp(escaped))
: [],
"i",
)
}

export function removeIgnoredFiles(
dir: string,
includePaths: RegExp,
excludePaths: RegExp,
) {
const gitIgnoredPaths = gitExcludePaths(dir)
klawSync(dir, { nodir: true })
.map(item => item.path.slice(`${dir}/`.length))
.filter(
relativePath =>
!relativePath.match(includePaths) || relativePath.match(excludePaths),
!relativePath.match(includePaths) ||
relativePath.match(excludePaths) ||
relativePath.match(gitIgnoredPaths),
)
.forEach(relativePath => removeSync(join(dir, relativePath)))
}

0 comments on commit 5f78e24

Please sign in to comment.