Skip to content

Commit

Permalink
fix(vcs): .gitignore files were not respected
Browse files Browse the repository at this point in the history
This also applied to other scenarios where users had multiple
dotIgnoreFiles set in their project configs.

Fixes #1121
  • Loading branch information
edvald committed Aug 26, 2019
1 parent ac88300 commit 5c08d61
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
14 changes: 9 additions & 5 deletions garden-service/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import * as execa from "execa"
import { join, resolve } from "path"
import { flatten } from "lodash"
import { flatten, uniq } from "lodash"
import { ensureDir, pathExists, stat, createReadStream } from "fs-extra"
import { PassThrough } from "stream"
import * as hasha from "hasha"
Expand Down Expand Up @@ -93,11 +93,15 @@ export class GitHandler extends VcsHandler {
// property is configurable.
lines = await git("ls-files", "-s", "--others", "--exclude=.garden", path)

// List ignored files from .gardenignore. We need to run ls-files twice to get both tracked and untracked files.
const lsIgnoredFiles = ["ls-files", "--ignored", ...this.ignoreFiles.map(f => `--exclude-per-directory=${f}`)]
const lsUntrackedIgnoredFiles = [...lsIgnoredFiles, "--others"]
// List ignored files for each ignore file. We need to run ls-files twice to get both tracked and untracked files.
const commands = flatten(this.ignoreFiles.map(f => {
const lsIgnoredFiles = ["ls-files", "--ignored", `--exclude-per-directory=${f}`]
const lsUntrackedIgnoredFiles = [...lsIgnoredFiles, "--others"]

ignored = flatten(await Bluebird.map([lsIgnoredFiles, lsUntrackedIgnoredFiles], async (cmd) => git(...cmd, path)))
return [lsIgnoredFiles, lsUntrackedIgnoredFiles]
}))

ignored = uniq(flatten(await Bluebird.map(commands, 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
39 changes: 31 additions & 8 deletions garden-service/test/unit/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { LogEntry } from "../../../../src/logger/log-entry"
import { hashRepoUrl } from "../../../../src/util/ext-source-util"

// Overriding this to make sure any ignorefile name is respected
const ignoreFileName = ".testignore"
const defaultIgnoreFilename = ".testignore"

async function getCommitMsg(repoPath: string) {
const res = (await execa("git", ["log", "-1", "--pretty=%B"], { cwd: repoPath })).stdout
Expand All @@ -46,8 +46,8 @@ async function makeTempGitRepo(initCommitMsg: string = "test commit") {
return tmpDir
}

async function addToIgnore(tmpPath: string, pathToExclude: string) {
const gardenignorePath = resolve(tmpPath, ignoreFileName)
async function addToIgnore(tmpPath: string, pathToExclude: string, ignoreFilename = defaultIgnoreFilename) {
const gardenignorePath = resolve(tmpPath, ignoreFilename)

await createFile(gardenignorePath)
await writeFile(gardenignorePath, pathToExclude)
Expand All @@ -65,7 +65,7 @@ describe("GitHandler", () => {
log = garden.log
tmpDir = await tmp.dir({ unsafeCleanup: true })
tmpPath = await realpath(tmpDir.path)
handler = new GitHandler(tmpPath, [ignoreFileName])
handler = new GitHandler(tmpPath, [defaultIgnoreFilename])
git = (<any>handler).gitCli(log, tmpPath)
await git("init")
})
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("GitHandler", () => {
await addToIgnore(tmpPath, name)

const files = (await handler.getFiles({ path: tmpPath, exclude: [], log }))
.filter(f => !f.path.includes(ignoreFileName))
.filter(f => !f.path.includes(defaultIgnoreFilename))

expect(files).to.eql([])
})
Expand All @@ -267,7 +267,30 @@ describe("GitHandler", () => {
await git("commit", "-m", "foo")

const files = (await handler.getFiles({ path: tmpPath, exclude: [], log }))
.filter(f => !f.path.includes(ignoreFileName))
.filter(f => !f.path.includes(defaultIgnoreFilename))

expect(files).to.eql([])
})

it("should correctly handle multiple ignore files", async () => {
const nameA = "foo.txt"
const nameB = "boo.txt"
const pathA = resolve(tmpPath, nameA)
const pathB = resolve(tmpPath, nameB)
await createFile(pathA)
await createFile(pathB)

await addToIgnore(tmpPath, nameA)
await addToIgnore(tmpPath, nameB, ".testignore2")

// We only add path A, to check if untracked files work okay
await git("add", pathA)
await git("commit", "-m", "foo")

const _handler = new GitHandler(tmpPath, [defaultIgnoreFilename, ".testignore2"])

const files = (await _handler.getFiles({ path: tmpPath, exclude: [], log }))
.filter(f => !f.path.includes(defaultIgnoreFilename))

expect(files).to.eql([])
})
Expand All @@ -281,7 +304,7 @@ describe("GitHandler", () => {
}

const files = (await handler.getFiles({ path: tmpPath, exclude: [...fixedExcludes], log }))
.filter(f => !f.path.includes(ignoreFileName))
.filter(f => !f.path.includes(defaultIgnoreFilename))

expect(files).to.eql([])
})
Expand All @@ -296,7 +319,7 @@ describe("GitHandler", () => {
await symlink(tmpPathB, path)

const files = (await handler.getFiles({ path: tmpPath, exclude: [], log }))
.filter(f => !f.path.includes(ignoreFileName))
.filter(f => !f.path.includes(defaultIgnoreFilename))

expect(files).to.eql([])
})
Expand Down

0 comments on commit 5c08d61

Please sign in to comment.