From cb393ccd3aa3d09d8c563fac7e3c23a69053f5df Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 14 Jan 2023 17:18:26 -0300 Subject: [PATCH] Add CHANGELOG entry + small adjustments to #977 --- CHANGELOG.md | 3 ++ internal/status/checksum.go | 5 ++-- internal/status/checksum_test.go | 2 +- internal/status/timestamp.go | 48 +++++++++++++++++++------------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1d0cf5f..98bef110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- Improve behavior and performance of status checking when using the + `timestamp` mode + ([#976](https://github.com/go-task/task/issues/976), [#977](https://github.com/go-task/task/pull/977) by @aminya). - Performance optimizations were made for large Taskfiles ([#982](https://github.com/go-task/task/pull/982) by @pd93). - Add ability to configure options for the [`set`](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html) diff --git a/internal/status/checksum.go b/internal/status/checksum.go index 9468ff78..d1ed712e 100644 --- a/internal/status/checksum.go +++ b/internal/status/checksum.go @@ -85,6 +85,7 @@ func (c *Checksum) checksum(files ...string) (string, error) { if _, err = io.Copy(h, f); err != nil { return "", err } + f.Close() } return fmt.Sprintf("%x", h.Sum(nil)), nil @@ -109,12 +110,12 @@ func (*Checksum) Kind() string { } func (c *Checksum) checksumFilePath() string { - return filepath.Join(c.TempDir, "checksum", NormalizeFilename(c.Task)) + return filepath.Join(c.TempDir, "checksum", normalizeFilename(c.Task)) } var checksumFilenameRegexp = regexp.MustCompile("[^A-z0-9]") // replaces invalid caracters on filenames with "-" -func NormalizeFilename(f string) string { +func normalizeFilename(f string) string { return checksumFilenameRegexp.ReplaceAllString(f, "-") } diff --git a/internal/status/checksum_test.go b/internal/status/checksum_test.go index b13c0eab..9d616685 100644 --- a/internal/status/checksum_test.go +++ b/internal/status/checksum_test.go @@ -16,6 +16,6 @@ func TestNormalizeFilename(t *testing.T) { {"foo1bar2baz3", "foo1bar2baz3"}, } for _, test := range tests { - assert.Equal(t, test.Out, NormalizeFilename(test.In)) + assert.Equal(t, test.Out, normalizeFilename(test.In)) } } diff --git a/internal/status/timestamp.go b/internal/status/timestamp.go index 59c351fd..708b8668 100644 --- a/internal/status/timestamp.go +++ b/internal/status/timestamp.go @@ -34,38 +34,46 @@ func (t *Timestamp) IsUpToDate() (bool, error) { timestampFile := t.timestampFilePath() - // if the file exists, add the file path to the generates - // if the generate file is old, the task will be executed + // If the file exists, add the file path to the generates. + // If the generate file is old, the task will be executed. _, err = os.Stat(timestampFile) if err == nil { generates = append(generates, timestampFile) } else { - // create the timestamp file for the next execution when the file does not exist + // Create the timestamp file for the next execution when the file does not exist. if !t.Dry { - _ = os.MkdirAll(filepath.Dir(timestampFile), 0o755) - _, _ = os.Create(timestampFile) + if err := os.MkdirAll(filepath.Dir(timestampFile), 0o755); err != nil { + return false, err + } + f, err := os.Create(timestampFile) + if err != nil { + return false, err + } + f.Close() } } taskTime := time.Now() - // compare the time of the generates and sources. If the generates are old, the task will be executed + // Compare the time of the generates and sources. If the generates are old, the task will be executed. - // get the max time of the generates + // Get the max time of the generates. generateMaxTime, err := getMaxTime(generates...) if err != nil || generateMaxTime.IsZero() { return false, nil } - // check if any of the source files is newer than the max time of the generates + // Check if any of the source files is newer than the max time of the generates. shouldUpdate, err := anyFileNewerThan(sources, generateMaxTime) if err != nil { return false, nil } - // modify the metadata of the file to the the current time + // Modify the metadata of the file to the the current time. if !t.Dry { - _ = os.Chtimes(timestampFile, taskTime, taskTime) + if err := os.Chtimes(timestampFile, taskTime, taskTime); err != nil { + return false, err + } } return !shouldUpdate, nil @@ -106,8 +114,15 @@ func getMaxTime(files ...string) (time.Time, error) { return t, nil } -// if the modification time of any of the files is newer than the the given time, returns true -// This function is lazy, as it stops when it finds a file newer than the given time +func maxTime(a, b time.Time) time.Time { + if a.After(b) { + return a + } + return b +} + +// If the modification time of any of the files is newer than the the given time, returns true. +// This function is lazy, as it stops when it finds a file newer than the given time. func anyFileNewerThan(files []string, givenTime time.Time) (bool, error) { for _, f := range files { info, err := os.Stat(f) @@ -121,18 +136,11 @@ func anyFileNewerThan(files []string, givenTime time.Time) (bool, error) { return false, nil } -func maxTime(a, b time.Time) time.Time { - if a.After(b) { - return a - } - return b -} - // OnError implements the Checker interface func (*Timestamp) OnError() error { return nil } func (t *Timestamp) timestampFilePath() string { - return filepath.Join(t.TempDir, "timestamp", NormalizeFilename(t.Task)) + return filepath.Join(t.TempDir, "timestamp", normalizeFilename(t.Task)) }