github.com/benchkram/bob@v0.0.0-20240314204020-b7a57f2f9be9/pkg/filepathutil/verify.go (about)

     1  package filepathutil
     2  
     3  import "strings"
     4  
     5  // IsChild checks if path2 is a child of path1.
     6  // assumes both path starts from same directory and
     7  // always returns true if path1 is root(e.g. `.`)
     8  func IsChild(path1 string, path2 string) bool {
     9  	path1Depth := len(strings.Split(path1, "/"))
    10  	path2Depth := len(strings.Split(path2, "/"))
    11  
    12  	if path1 == "." || (path2Depth > path1Depth && strings.HasPrefix(path2, path1)) {
    13  		return true
    14  	}
    15  
    16  	return false
    17  }