Skip to content

Commit

Permalink
Always include file location in hash
Browse files Browse the repository at this point in the history
  • Loading branch information
rfay committed May 23, 2024
1 parent e6d4a3a commit 089a9e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/ddevapp/mutagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ func GetMutagenConfigFilePath(app *DdevApp) string {
// GetMutagenConfigFileHash returns the SHA1 hash of the mutagen.yml
func GetMutagenConfigFileHash(app *DdevApp) (string, error) {
f := GetMutagenConfigFilePath(app)
hash, err := fileutil.FileHash(f)
// Create hash based on mutagen.yml file contents, location,
//and global config
hash, err := fileutil.FileHash(f, globalconfig.GetGlobalDdevDirLocation())
if err != nil {
return "", err
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/fileutil/file_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

// FileHash returns string of hash of filePath passed in
func FileHash(filePath string) (string, error) {
// And optional string can be added to content that will be hashed
func FileHash(filePath string, optionalExtraString string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
Expand All @@ -25,6 +26,19 @@ func FileHash(filePath string) (string, error) {
if _, err := io.Copy(hash, file); err != nil {
return "", err
}
// Include file location in the hash, if in a different
// place it should not hash the same
if _, err := hash.Write([]byte(file.Name())); err != nil {
return "", err
}

// Add optional string to hash if provided
if len(optionalExtraString) > 0 {
if _, err := hash.Write([]byte(optionalExtraString)); err != nil {
return "", err
}
}

sum := hash.Sum(nil)

return fmt.Sprintf("%x", sum), nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/fileutil/file_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestFileHash(t *testing.T) {
expectedHash, err := externalComputeSha1Sum(testFile)
require.NoError(t, err)

result, err := fileutil.FileHash(testFile)
result, err := fileutil.FileHash(testFile, "")
require.NoError(t, err)

require.Equal(t, expectedHash, result)
Expand Down

0 comments on commit 089a9e8

Please sign in to comment.