Skip to content

Commit

Permalink
Skip .git folders in file listings
Browse files Browse the repository at this point in the history
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 <dmikusa@vmware.com>
  • Loading branch information
Daniel Mikusa committed Sep 3, 2021
1 parent 26a8039 commit 9b64107
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sherpa/file_listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
16 changes: 16 additions & 0 deletions sherpa/file_listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 9b64107

Please sign in to comment.