Skip to content

Commit cb393cc

Browse files
committed
Add CHANGELOG entry + small adjustments to #977
1 parent 347fcf9 commit cb393cc

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- Improve behavior and performance of status checking when using the
6+
`timestamp` mode
7+
([#976](https://github.com/go-task/task/issues/976), [#977](https://github.com/go-task/task/pull/977) by @aminya).
58
- Performance optimizations were made for large Taskfiles
69
([#982](https://github.com/go-task/task/pull/982) by @pd93).
710
- Add ability to configure options for the [`set`](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html)

internal/status/checksum.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (c *Checksum) checksum(files ...string) (string, error) {
8585
if _, err = io.Copy(h, f); err != nil {
8686
return "", err
8787
}
88+
f.Close()
8889
}
8990

9091
return fmt.Sprintf("%x", h.Sum(nil)), nil
@@ -109,12 +110,12 @@ func (*Checksum) Kind() string {
109110
}
110111

111112
func (c *Checksum) checksumFilePath() string {
112-
return filepath.Join(c.TempDir, "checksum", NormalizeFilename(c.Task))
113+
return filepath.Join(c.TempDir, "checksum", normalizeFilename(c.Task))
113114
}
114115

115116
var checksumFilenameRegexp = regexp.MustCompile("[^A-z0-9]")
116117

117118
// replaces invalid caracters on filenames with "-"
118-
func NormalizeFilename(f string) string {
119+
func normalizeFilename(f string) string {
119120
return checksumFilenameRegexp.ReplaceAllString(f, "-")
120121
}

internal/status/checksum_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ func TestNormalizeFilename(t *testing.T) {
1616
{"foo1bar2baz3", "foo1bar2baz3"},
1717
}
1818
for _, test := range tests {
19-
assert.Equal(t, test.Out, NormalizeFilename(test.In))
19+
assert.Equal(t, test.Out, normalizeFilename(test.In))
2020
}
2121
}

internal/status/timestamp.go

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,46 @@ func (t *Timestamp) IsUpToDate() (bool, error) {
3434

3535
timestampFile := t.timestampFilePath()
3636

37-
// if the file exists, add the file path to the generates
38-
// if the generate file is old, the task will be executed
37+
// If the file exists, add the file path to the generates.
38+
// If the generate file is old, the task will be executed.
3939
_, err = os.Stat(timestampFile)
4040
if err == nil {
4141
generates = append(generates, timestampFile)
4242
} else {
43-
// create the timestamp file for the next execution when the file does not exist
43+
// Create the timestamp file for the next execution when the file does not exist.
4444
if !t.Dry {
45-
_ = os.MkdirAll(filepath.Dir(timestampFile), 0o755)
46-
_, _ = os.Create(timestampFile)
45+
if err := os.MkdirAll(filepath.Dir(timestampFile), 0o755); err != nil {
46+
return false, err
47+
}
48+
f, err := os.Create(timestampFile)
49+
if err != nil {
50+
return false, err
51+
}
52+
f.Close()
4753
}
4854
}
4955

5056
taskTime := time.Now()
5157

52-
// compare the time of the generates and sources. If the generates are old, the task will be executed
58+
// Compare the time of the generates and sources. If the generates are old, the task will be executed.
5359

54-
// get the max time of the generates
60+
// Get the max time of the generates.
5561
generateMaxTime, err := getMaxTime(generates...)
5662
if err != nil || generateMaxTime.IsZero() {
5763
return false, nil
5864
}
5965

60-
// check if any of the source files is newer than the max time of the generates
66+
// Check if any of the source files is newer than the max time of the generates.
6167
shouldUpdate, err := anyFileNewerThan(sources, generateMaxTime)
6268
if err != nil {
6369
return false, nil
6470
}
6571

66-
// modify the metadata of the file to the the current time
72+
// Modify the metadata of the file to the the current time.
6773
if !t.Dry {
68-
_ = os.Chtimes(timestampFile, taskTime, taskTime)
74+
if err := os.Chtimes(timestampFile, taskTime, taskTime); err != nil {
75+
return false, err
76+
}
6977
}
7078

7179
return !shouldUpdate, nil
@@ -106,8 +114,15 @@ func getMaxTime(files ...string) (time.Time, error) {
106114
return t, nil
107115
}
108116

109-
// if the modification time of any of the files is newer than the the given time, returns true
110-
// This function is lazy, as it stops when it finds a file newer than the given time
117+
func maxTime(a, b time.Time) time.Time {
118+
if a.After(b) {
119+
return a
120+
}
121+
return b
122+
}
123+
124+
// If the modification time of any of the files is newer than the the given time, returns true.
125+
// This function is lazy, as it stops when it finds a file newer than the given time.
111126
func anyFileNewerThan(files []string, givenTime time.Time) (bool, error) {
112127
for _, f := range files {
113128
info, err := os.Stat(f)
@@ -121,18 +136,11 @@ func anyFileNewerThan(files []string, givenTime time.Time) (bool, error) {
121136
return false, nil
122137
}
123138

124-
func maxTime(a, b time.Time) time.Time {
125-
if a.After(b) {
126-
return a
127-
}
128-
return b
129-
}
130-
131139
// OnError implements the Checker interface
132140
func (*Timestamp) OnError() error {
133141
return nil
134142
}
135143

136144
func (t *Timestamp) timestampFilePath() string {
137-
return filepath.Join(t.TempDir, "timestamp", NormalizeFilename(t.Task))
145+
return filepath.Join(t.TempDir, "timestamp", normalizeFilename(t.Task))
138146
}

0 commit comments

Comments
 (0)