From a10bb289b28a6dbe3b2223cd66dfb33b2b42918c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ey=C3=BE=C3=B3r=20Magn=C3=BAsson?= Date: Tue, 23 Jul 2019 17:33:54 +0200 Subject: [PATCH] fix(core): ensure untracked files from .gardenignore are excluded --- garden-service/src/vcs/git.ts | 8 ++++++- garden-service/test/unit/src/vcs/git.ts | 28 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/garden-service/src/vcs/git.ts b/garden-service/src/vcs/git.ts index 2288c3e081..55fee48980 100644 --- a/garden-service/src/vcs/git.ts +++ b/garden-service/src/vcs/git.ts @@ -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" @@ -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) { diff --git a/garden-service/test/unit/src/vcs/git.ts b/garden-service/test/unit/src/vcs/git.ts index 8299122668..130b90cbf0 100644 --- a/garden-service/test/unit/src/vcs/git.ts +++ b/garden-service/test/unit/src/vcs/git.ts @@ -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 @@ -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", () => {