From 9b641070eb2a880abd4cbaa6812caecf1c07d8a6 Mon Sep 17 00:00:00 2001 From: Daniel Mikusa Date: Fri, 3 Sep 2021 10:07:10 -0400 Subject: [PATCH] Skip `.git` folders in file listings The file listing mechanism will traverse all directories given to it & return a listing of all files. The main purpose of this mechanism is to detect changes under a directory structure. This change will skip `.git` folders as the file listing is traversing subdirectories. This is because `.git` folders are well-know, can change independent of the application (like if you change git config) but shouldn't trigger rebuilds of the application. In additiona, the `.git` directory can for larger projects contain a large number of files, so skipping it can help with performance on large projects. This change will skip all folders with the name `.git`, not just the top-level `.git` folder. This resolves #55. Signed-off-by: Daniel Mikusa --- sherpa/file_listing.go | 4 ++++ sherpa/file_listing_test.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/sherpa/file_listing.go b/sherpa/file_listing.go index 4684505..ba74cad 100644 --- a/sherpa/file_listing.go +++ b/sherpa/file_listing.go @@ -84,6 +84,10 @@ func NewFileListing(roots ...string) ([]FileEntry, error) { return nil } + if info.IsDir() && info.Name() == ".git" { + return filepath.SkipDir + } + e := FileEntry{ Path: path, Mode: info.Mode().String(), diff --git a/sherpa/file_listing_test.go b/sherpa/file_listing_test.go index 0cd6660..b62163d 100644 --- a/sherpa/file_listing_test.go +++ b/sherpa/file_listing_test.go @@ -59,6 +59,22 @@ func testFileListing(t *testing.T, context spec.G, it spec.S) { Expect(e).To(HaveLen(3)) }) + it("create listing skipping .git folder", func() { + Expect(os.MkdirAll(filepath.Join(path, ".git"), 0755)).To(Succeed()) + Expect(ioutil.WriteFile(filepath.Join(path, ".git", "HEAD"), []byte{1}, 0644)).To(Succeed()) + Expect(ioutil.WriteFile(filepath.Join(path, ".git", "config"), []byte{1}, 0644)).To(Succeed()) + Expect(ioutil.WriteFile(filepath.Join(path, "alpha.txt"), []byte{1}, 0644)).To(Succeed()) + Expect(os.MkdirAll(filepath.Join(path, "test-directory"), 0755)).To(Succeed()) + Expect(ioutil.WriteFile(filepath.Join(path, "test-directory", "bravo.txt"), []byte{2}, 0644)).To(Succeed()) + Expect(os.MkdirAll(filepath.Join(path, "test-directory", ".git"), 0755)).To(Succeed()) + Expect(ioutil.WriteFile(filepath.Join(path, "test-directory", ".git", "config"), []byte{1}, 0644)).To(Succeed()) + + e, err := sherpa.NewFileListing(path) + Expect(err).NotTo(HaveOccurred()) + + Expect(e).To(HaveLen(3)) + }) + it("create listing as hash with non-regular file", func() { Expect(ioutil.WriteFile(filepath.Join(path, "alpha.txt"), []byte{1}, 0644)).To(Succeed()) Expect(os.MkdirAll(filepath.Join(path, "test-directory"), 0755)).To(Succeed())