From f1ff027bd846a8ec4fe4f0a93d5f7a8ebabdf108 Mon Sep 17 00:00:00 2001 From: Will Sackfield Date: Tue, 10 Dec 2024 09:31:05 -0500 Subject: [PATCH] Ignore files within a .git directory (#2087) * Ignore files within a .git directory * Do not consider files within a .git directory when we are using separate-weights * Check for the .git directory explicitly --- pkg/weights/weights.go | 14 ++++++++++++++ pkg/weights/weights_test.go | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pkg/weights/weights.go b/pkg/weights/weights.go index a3b97f14a7..8ca54c252c 100644 --- a/pkg/weights/weights.go +++ b/pkg/weights/weights.go @@ -30,6 +30,9 @@ func FindWeights(fw FileWalker) ([]string, []string, error) { if info.IsDir() { return nil } + if isGitFile(path) { + return nil + } if isCodeFile(path) { codeFiles = append(codeFiles, path) return nil @@ -97,6 +100,17 @@ func isCodeFile(path string) bool { return ext == ".py" || ext == ".ipynb" } +func isGitFile(path string) bool { + dir, _ := filepath.Split(path) + folders := strings.Split(filepath.Clean(dir), string(filepath.Separator)) + for _, folder := range folders { + if folder == ".git" { + return true + } + } + return false +} + // filterDirsContainingCode filters out directories that contain code files. // If a dir is a prefix for any given codeFiles, it will be filtered out. func filterDirsContainingCode(dirs []string, codeFiles []string) []string { diff --git a/pkg/weights/weights_test.go b/pkg/weights/weights_test.go index 515af6ffe3..e00e1b3ceb 100644 --- a/pkg/weights/weights_test.go +++ b/pkg/weights/weights_test.go @@ -160,3 +160,19 @@ func TestSubDirMerge(t *testing.T) { require.Empty(t, rootFiles) require.Equal(t, []string{"models"}, dirs) } + +// Test case for ignoring files within a .git directory +func TestIgnoreGitFiles(t *testing.T) { + mockFileWalker := func(root string, walkFn filepath.WalkFunc) error { + sizes := []int64{sizeThreshold, sizeThreshold, 1024} + for i, path := range []string{".git/root-large", "root-large", "predict.py"} { + walkFn(path, mockFileInfo{size: sizes[i]}, nil) + } + return nil + } + + dirs, rootFiles, err := FindWeights(mockFileWalker) + require.NoError(t, err) + require.Equal(t, []string{"root-large"}, rootFiles) + require.Empty(t, dirs) +}