Skip to content

Commit

Permalink
fix(ultraignore): allow ultraignore to work with tracked git files (#170
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alexkrautmann committed Feb 27, 2021
1 parent 880073f commit 65cc044
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ build
stats.html
tsconfig.build.tsbuildinfo
.ultra.cache.json
.nyc_output
.nyc_output
.idea
2 changes: 2 additions & 0 deletions .ultraignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Used only for cache unit tests
__tests__/**/file-to-ignore.js
6 changes: 5 additions & 1 deletion __tests__/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ test("getGitFiles", async () => {
expect(files["__tests__/workspace/apps/app2/package.json"]).toMatch(
/^[a-z0-9]*$/u
)
expect(files["__tests__/workspace/apps/app1/file-to-ignore.js"]).toMatch(
/^[a-z0-9]*$/u
)
expect(files[""]).toMatch(/^\d+\.\d+$/u)

expect(Object.keys(files)).toHaveLength(3)
expect(Object.keys(files)).toHaveLength(4)
})

test("cache", async () => {
const files = f(await cache.getFiles(path.resolve(workspaceRoot, "apps")))
expect(files["app1/package.json"]).toMatch(/^[a-z0-9]*$/u)
expect(files["app2/package.json"]).toMatch(/^[a-z0-9]*$/u)
expect(files["app1/file-to-ignore.js"]).toBeUndefined()
expect(Object.keys(files)).toHaveLength(2)
expect(cache.cache.size).not.toBe(0)
await cache.getFiles(path.resolve(workspaceRoot, "apps"))
Expand Down
1 change: 1 addition & 0 deletions __tests__/workspace/apps/app1/file-to-ignore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FOO = 'foo'
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"cross-spawn": "^7.0.3",
"fast-glob": "^3.2.5",
"globrex": "^0.1.2",
"ignore": "^5.1.8",
"json5": "^2.2.0",
"micro-memoize": "^4.0.9",
"npm-run-path": "4.0.1",
Expand Down
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { exec } from "child_process"
import fs from "fs"
import path from "path"
import ignore, { Ignore } from "ignore"
import { findUp } from "./package"
import { HASH_FILE } from "./options"

Expand All @@ -10,6 +11,18 @@ type GitFiles = Record<string, string>

export class NoGitError extends Error {}

function getUltraIgnore(root: string): Ignore {
const ultraIgnorePath = path.resolve(root, ".ultraignore")
const ultraIgnoreExists = fs.existsSync(ultraIgnorePath)

const ultraIgnore = ignore()
if (ultraIgnoreExists) {
ultraIgnore.add(fs.readFileSync(ultraIgnorePath).toString())
}

return ultraIgnore
}

export function parseFiles(data: string, root: string): GitFiles {
const ret: GitFiles = {}
data.split("\n").forEach((line) => {
Expand All @@ -36,12 +49,8 @@ export function parseFiles(data: string, root: string): GitFiles {

export async function getGitFiles(root: string): Promise<GitFiles> {
return new Promise((resolve, reject) => {
const ultraIgnoreExists = fs.existsSync(path.resolve(root, ".ultraignore"))
const lsFiles = `git ls-files --full-name -s -d -c -m -o ${
ultraIgnoreExists ? "-X .ultraignore" : ""
} --directory -t`
exec(
lsFiles,
"git ls-files --full-name -s -d -c -m -o --directory -t",
{ cwd: root, maxBuffer: 1024 * 1024 * 1024 },
(error, stdout) => {
if (error) return reject(error)
Expand All @@ -68,13 +77,16 @@ class FilesCache {
const files = this.cache.get(root) || {}
const ret: GitFiles = {}

const ultraIgnore = getUltraIgnore(root)

Object.entries(files)
.filter(([file]) => {
const filePath = path.resolve(root, file)
return (
filePath == directory || filePath.startsWith(directory + path.sep)
)
})
.filter(([file]) => !ultraIgnore.ignores(file))
.map(([file, hash]) => [
path.relative(directory, path.resolve(root, file)),
hash,
Expand Down

0 comments on commit 65cc044

Please sign in to comment.