Skip to content

Commit

Permalink
fix(core): ensure untracked files from .gardenignore are excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Jul 23, 2019
1 parent c43490c commit a10bb28
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion garden-service/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import * as execa from "execa"
import { join, resolve } from "path"
import { flatten } from "lodash"
import { ensureDir, pathExists, stat, createReadStream } from "fs-extra"
import { PassThrough } from "stream"
import * as hasha from "hasha"
Expand Down Expand Up @@ -87,7 +88,12 @@ export class GitHandler extends VcsHandler {
* whose config is colocated with the project config, and that don't specify include paths/patterns.
*/
lines = await git("ls-files", "-s", "--other", "--exclude=.garden", path)
ignored = await git("ls-files", "--ignored", "--exclude-per-directory=.gardenignore", path)

// List ignored files from .gardenignore. We need to run ls-files twice to get both tracked and untracked files.
const lsFilesCmd = ["ls-files", "--ignored", "--exclude-per-directory=.gardenignore"]
const lsFilesUntrackedCmd = [...lsFilesCmd, "--others"]

ignored = flatten(await Bluebird.map([lsFilesCmd, lsFilesUntrackedCmd], async (cmd) => git(...cmd, path)))
} catch (err) {
// if we get 128 we're not in a repo root, so we get no files
if (err.code !== 128) {
Expand Down
28 changes: 28 additions & 0 deletions garden-service/test/unit/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { join, resolve } from "path"
import { expectError } from "../../../helpers"
import { getCommitIdFromRefList, parseGitUrl, GitHandler } from "../../../../src/vcs/git"

async function addToGardenIgnore(tmpPath: string, pathToExclude: string) {
const gardenignorePath = resolve(tmpPath, ".gardenignore")

await createFile(gardenignorePath)
await writeFile(gardenignorePath, pathToExclude)
}

describe("GitHandler", () => {
let tmpDir: tmp.DirectoryResult
let tmpPath: string
Expand Down Expand Up @@ -171,6 +178,27 @@ describe("GitHandler", () => {
{ path, hash },
])
})

it("should exclude untracked files that are listed in .gardenignore", async () => {
const name = "foo.txt"
const path = resolve(tmpPath, name)
await createFile(path)
await addToGardenIgnore(tmpPath, name)

expect((await handler.getFiles(tmpPath)).filter(f => !f.path.includes(".gardenignore"))).to.eql([])
})

it("should exclude tracked files that are listed in .gardenignore", async () => {
const name = "foo.txt"
const path = resolve(tmpPath, name)
await createFile(path)
await addToGardenIgnore(tmpPath, name)

await git("add", path)
await git("commit", "-m", "foo")

expect((await handler.getFiles(tmpPath)).filter(f => !f.path.includes(".gardenignore"))).to.eql([])
})
})

describe("hashObject", () => {
Expand Down

0 comments on commit a10bb28

Please sign in to comment.