@@ -21,36 +21,51 @@ import (
21
21
"golang.org/x/mod/semver"
22
22
)
23
23
24
- // Modindex writes an index current as of when it is called.
24
+ // Create always creates a new index for the go module cache that is in cachedir.
25
+ func Create (cachedir string ) error {
26
+ _ , err := indexModCache (cachedir , true )
27
+ return err
28
+ }
29
+
30
+ // Update the index for the go module cache that is in cachedir,
31
+ // If there is no existing index it will build one.
32
+ // If there are changed directories since the last index, it will
33
+ // write a new one and return true. Otherwise it returns false.
34
+ func Update (cachedir string ) (bool , error ) {
35
+ return indexModCache (cachedir , false )
36
+ }
37
+
38
+ // indexModCache writes an index current as of when it is called.
25
39
// If clear is true the index is constructed from all of GOMODCACHE
26
40
// otherwise the index is constructed from the last previous index
27
- // and the updates to the cache.
28
- func IndexModCache (cachedir string , clear bool ) error {
41
+ // and the updates to the cache. It returns true if it wrote an index,
42
+ // false otherwise.
43
+ func indexModCache (cachedir string , clear bool ) (bool , error ) {
29
44
cachedir , err := filepath .Abs (cachedir )
30
45
if err != nil {
31
- return err
46
+ return false , err
32
47
}
33
48
cd := Abspath (cachedir )
34
49
future := time .Now ().Add (24 * time .Hour ) // safely in the future
35
- err = modindexTimed (future , cd , clear )
50
+ ok , err : = modindexTimed (future , cd , clear )
36
51
if err != nil {
37
- return err
52
+ return false , err
38
53
}
39
- return nil
54
+ return ok , nil
40
55
}
41
56
42
57
// modindexTimed writes an index current as of onlyBefore.
43
58
// If clear is true the index is constructed from all of GOMODCACHE
44
59
// otherwise the index is constructed from the last previous index
45
60
// and all the updates to the cache before onlyBefore.
46
- // (this is useful for testing; perhaps it should not be exported)
47
- func modindexTimed (onlyBefore time.Time , cachedir Abspath , clear bool ) error {
61
+ // It returns true if it wrote a new index, false if it wrote nothing.
62
+ func modindexTimed (onlyBefore time.Time , cachedir Abspath , clear bool ) ( bool , error ) {
48
63
var curIndex * Index
49
64
if ! clear {
50
65
var err error
51
66
curIndex , err = ReadIndex (string (cachedir ))
52
67
if clear && err != nil {
53
- return err
68
+ return false , err
54
69
}
55
70
// TODO(pjw): check that most of those directorie still exist
56
71
}
@@ -63,12 +78,16 @@ func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) error {
63
78
cfg .onlyAfter = curIndex .Changed
64
79
}
65
80
if err := cfg .buildIndex (); err != nil {
66
- return err
81
+ return false , err
82
+ }
83
+ if len (cfg .newIndex .Entries ) == 0 {
84
+ // no changes, don't write a new index
85
+ return false , nil
67
86
}
68
87
if err := cfg .writeIndex (); err != nil {
69
- return err
88
+ return false , err
70
89
}
71
- return nil
90
+ return true , nil
72
91
}
73
92
74
93
type work struct {
@@ -86,6 +105,9 @@ func (w *work) buildIndex() error {
86
105
// so set it now.
87
106
w .newIndex = & Index {Changed : time .Now (), Cachedir : w .cacheDir }
88
107
dirs := findDirs (string (w .cacheDir ), w .onlyAfter , w .onlyBefore )
108
+ if len (dirs ) == 0 {
109
+ return nil
110
+ }
89
111
newdirs , err := byImportPath (dirs )
90
112
if err != nil {
91
113
return err
0 commit comments