Skip to content

Commit

Permalink
Ignore files within a .git directory (#2087)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
8W9aG authored Dec 10, 2024
1 parent 3ca6205 commit f1ff027
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/weights/weights.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/weights/weights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f1ff027

Please sign in to comment.