From 8ca4293f512feb762f02bd1e3fed438cfc3664f8 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Tue, 3 Sep 2024 19:56:24 +0100 Subject: [PATCH] refactor isMaxDepth for readability --- find/find.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/find/find.go b/find/find.go index 91c5e9d..5d70650 100644 --- a/find/find.go +++ b/find/find.go @@ -79,7 +79,7 @@ func skipFileIfHidden( // isMaxDepth reports whether the configured max depth has been reached. func isMaxDepth(rootPath, currentPath string, maxDepth int) bool { - if rootPath == filepath.Dir(currentPath) { + if rootPath == filepath.Dir(currentPath) || maxDepth == 0 { return false } @@ -87,13 +87,13 @@ func isMaxDepth(rootPath, currentPath string, maxDepth int) bool { return true } - p := strings.Replace(currentPath, rootPath+string(os.PathSeparator), "", 1) - - if strings.Count(p, string(os.PathSeparator)) > maxDepth && maxDepth != 0 { - return true - } + relativePath := strings.TrimPrefix( + currentPath, + rootPath+string(os.PathSeparator), + ) + depthCount := strings.Count(relativePath, string(os.PathSeparator)) - return false + return depthCount > maxDepth } func createFileChange(dirPath string, fileInfo fs.FileInfo) *file.Change {