MCPcopy Index your code
hub / github.com/devspace-sh/devspace / Directory

Function Directory

pkg/util/hash/hash.go:45–81  ·  view source on GitHub ↗

Directory creates the hash value of a directory

(path string)

Source from the content-addressed store, hash-verified

43
44// Directory creates the hash value of a directory
45func Directory(path string) (string, error) {
46 hash := sha256.New()
47
48 // Stat dir / file
49 fileInfo, err := os.Stat(path)
50 if err != nil {
51 return "", err
52 }
53
54 // Hash file
55 if !fileInfo.IsDir() {
56 size := strconv.FormatInt(fileInfo.Size(), 10)
57 mTime := strconv.FormatInt(fileInfo.ModTime().UnixNano(), 10)
58 _, _ = io.WriteString(hash, path+";"+size+";"+mTime)
59
60 return fmt.Sprintf("%x", hash.Sum(nil)), nil
61 }
62
63 // Hash directory
64 err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
65 if err != nil {
66 // We ignore errors
67 return nil
68 }
69
70 size := strconv.FormatInt(info.Size(), 10)
71 mTime := strconv.FormatInt(info.ModTime().UnixNano(), 10)
72 _, _ = io.WriteString(hash, path+";"+size+";"+mTime)
73
74 return nil
75 })
76 if err != nil {
77 return "", err
78 }
79
80 return fmt.Sprintf("%x", hash.Sum(nil)), nil
81}
82
83// DirectoryExcludes calculates a hash for a directory and excludes the submitted patterns
84func DirectoryExcludes(srcPath string, excludePatterns []string, fast bool) (string, error) {

Callers 2

DeployMethod · 0.92
TestHashDirectoryFunction · 0.85

Calls 4

WriteStringMethod · 0.65
IsDirMethod · 0.45
SizeMethod · 0.45
ModTimeMethod · 0.45

Tested by 1

TestHashDirectoryFunction · 0.68