diff options
| author | 2025-09-08 20:53:25 +0200 | |
|---|---|---|
| committer | 2025-09-08 20:53:25 +0200 | |
| commit | a6429b5410062cb8067e6dbbc9999d7d579a751d (patch) | |
| tree | c10458d8a5e41ec561b0d139d83547353fdb69bc /vendor/golang.org/x/tools/internal/modindex/modindex.go | |
| parent | [bugfix] check for nil notification (#4417) (diff) | |
| download | gotosocial-a6429b5410062cb8067e6dbbc9999d7d579a751d.tar.xz | |
[chore] update dependencies (#4422)
- github.com/jackc/pgx/v5 v5.7.5 -> v5.7.6
- github.com/ncruces/go-sqlite3 v0.28.0 -> v0.29.0
- github.com/tdewolff/minify/v2 v2.24.2 -> v2.24.3
- golang.org/x/oauth2 v0.30.0 -> v0.31.0
- golang.org/x/sys v0.35.0 -> v0.36.0
- golang.org/x/text v0.28.0 -> v0.29.0
Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4422
Co-authored-by: kim <grufwub@gmail.com>
Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/golang.org/x/tools/internal/modindex/modindex.go')
| -rw-r--r-- | vendor/golang.org/x/tools/internal/modindex/modindex.go | 205 |
1 files changed, 80 insertions, 125 deletions
diff --git a/vendor/golang.org/x/tools/internal/modindex/modindex.go b/vendor/golang.org/x/tools/internal/modindex/modindex.go index 355a53e71..5fa285d98 100644 --- a/vendor/golang.org/x/tools/internal/modindex/modindex.go +++ b/vendor/golang.org/x/tools/internal/modindex/modindex.go @@ -2,17 +2,21 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Package modindex contains code for building and searching an index to -// the Go module cache. The directory containing the index, returned by -// IndexDir(), contains a file index-name-<ver> that contains the name +// Package modindex contains code for building and searching an +// [Index] of the Go module cache. +package modindex + +// The directory containing the index, returned by +// [IndexDir], contains a file index-name-<ver> that contains the name // of the current index. We believe writing that short file is atomic. -// ReadIndex reads that file to get the file name of the index. +// [Read] reads that file to get the file name of the index. // WriteIndex writes an index with a unique name and then // writes that name into a new version of index-name-<ver>. // (<ver> stands for the CurrentVersion of the index format.) -package modindex import ( + "maps" + "os" "path/filepath" "slices" "strings" @@ -21,144 +25,95 @@ import ( "golang.org/x/mod/semver" ) -// Create always creates a new index for the go module cache that is in cachedir. -func Create(cachedir string) error { - _, err := indexModCache(cachedir, true) - return err -} - -// Update the index for the go module cache that is in cachedir, -// If there is no existing index it will build one. -// If there are changed directories since the last index, it will -// write a new one and return true. Otherwise it returns false. -func Update(cachedir string) (bool, error) { - return indexModCache(cachedir, false) +// Update updates the index for the specified Go +// module cache directory, creating it as needed. +// On success it returns the current index. +func Update(gomodcache string) (*Index, error) { + prev, err := Read(gomodcache) + if err != nil { + if !os.IsNotExist(err) { + return nil, err + } + prev = nil + } + return update(gomodcache, prev) } -// indexModCache writes an index current as of when it is called. -// If clear is true the index is constructed from all of GOMODCACHE -// otherwise the index is constructed from the last previous index -// and the updates to the cache. It returns true if it wrote an index, -// false otherwise. -func indexModCache(cachedir string, clear bool) (bool, error) { - cachedir, err := filepath.Abs(cachedir) +// update builds, writes, and returns the current index. +// +// If old is nil, the new index is built from all of GOMODCACHE; +// otherwise it is built from the old index plus cache updates +// since the previous index's time. +func update(gomodcache string, old *Index) (*Index, error) { + gomodcache, err := filepath.Abs(gomodcache) if err != nil { - return false, err + return nil, err } - cd := Abspath(cachedir) - future := time.Now().Add(24 * time.Hour) // safely in the future - ok, err := modindexTimed(future, cd, clear) + new, changed, err := build(gomodcache, old) if err != nil { - return false, err + return nil, err } - return ok, nil -} - -// modindexTimed writes an index current as of onlyBefore. -// If clear is true the index is constructed from all of GOMODCACHE -// otherwise the index is constructed from the last previous index -// and all the updates to the cache before onlyBefore. -// It returns true if it wrote a new index, false if it wrote nothing. -func modindexTimed(onlyBefore time.Time, cachedir Abspath, clear bool) (bool, error) { - var curIndex *Index - if !clear { - var err error - curIndex, err = ReadIndex(string(cachedir)) - if clear && err != nil { - return false, err + if old == nil || changed { + if err := write(gomodcache, new); err != nil { + return nil, err } - // TODO(pjw): check that most of those directories still exist - } - cfg := &work{ - onlyBefore: onlyBefore, - oldIndex: curIndex, - cacheDir: cachedir, - } - if curIndex != nil { - cfg.onlyAfter = curIndex.Changed - } - if err := cfg.buildIndex(); err != nil { - return false, err } - if len(cfg.newIndex.Entries) == 0 && curIndex != nil { - // no changes from existing curIndex, don't write a new index - return false, nil - } - if err := cfg.writeIndex(); err != nil { - return false, err - } - return true, nil -} - -type work struct { - onlyBefore time.Time // do not use directories later than this - onlyAfter time.Time // only interested in directories after this - // directories from before onlyAfter come from oldIndex - oldIndex *Index - newIndex *Index - cacheDir Abspath + return new, nil } -func (w *work) buildIndex() error { - // The effective date of the new index should be at least - // slightly earlier than when the directories are scanned - // so set it now. - w.newIndex = &Index{Changed: time.Now(), Cachedir: w.cacheDir} - dirs := findDirs(string(w.cacheDir), w.onlyAfter, w.onlyBefore) - if len(dirs) == 0 { - return nil +// build returns a new index for the specified Go module cache (an +// absolute path). +// +// If an old index is provided, only directories more recent than it +// that it are scanned; older directories are provided by the old +// Index. +// +// The boolean result indicates whether new entries were found. +func build(gomodcache string, old *Index) (*Index, bool, error) { + // Set the time window. + var start time.Time // = dawn of time + if old != nil { + start = old.ValidAt } - newdirs, err := byImportPath(dirs) + now := time.Now() + end := now.Add(24 * time.Hour) // safely in the future + + // Enumerate GOMODCACHE package directories. + // Choose the best (latest) package for each import path. + pkgDirs := findDirs(gomodcache, start, end) + dirByPath, err := bestDirByImportPath(pkgDirs) if err != nil { - return err + return nil, false, err } - // for each import path it might occur only in newdirs, - // only in w.oldIndex, or in both. - // If it occurs in both, use the semantically later one - if w.oldIndex != nil { - for _, e := range w.oldIndex.Entries { - found, ok := newdirs[e.ImportPath] - if !ok { - w.newIndex.Entries = append(w.newIndex.Entries, e) - continue // use this one, there is no new one - } - if semver.Compare(found[0].version, e.Version) > 0 { - // use the new one - } else { - // use the old one, forget the new one - w.newIndex.Entries = append(w.newIndex.Entries, e) - delete(newdirs, e.ImportPath) + + // For each import path it might occur only in + // dirByPath, only in old, or in both. + // If both, use the semantically later one. + var entries []Entry + if old != nil { + for _, entry := range old.Entries { + dir, ok := dirByPath[entry.ImportPath] + if !ok || semver.Compare(dir.version, entry.Version) <= 0 { + // New dir is missing or not more recent; use old entry. + entries = append(entries, entry) + delete(dirByPath, entry.ImportPath) } } } - // get symbol information for all the new diredtories - getSymbols(w.cacheDir, newdirs) - // assemble the new index entries - for k, v := range newdirs { - d := v[0] - pkg, names := processSyms(d.syms) - if pkg == "" { - continue // PJW: does this ever happen? - } - entry := Entry{ - PkgName: pkg, - Dir: d.path, - ImportPath: k, - Version: d.version, - Names: names, - } - w.newIndex.Entries = append(w.newIndex.Entries, entry) - } - // sort the entries in the new index - slices.SortFunc(w.newIndex.Entries, func(l, r Entry) int { - if n := strings.Compare(l.PkgName, r.PkgName); n != 0 { + + // Extract symbol information for all the new directories. + newEntries := extractSymbols(gomodcache, maps.Values(dirByPath)) + entries = append(entries, newEntries...) + slices.SortFunc(entries, func(x, y Entry) int { + if n := strings.Compare(x.PkgName, y.PkgName); n != 0 { return n } - return strings.Compare(l.ImportPath, r.ImportPath) + return strings.Compare(x.ImportPath, y.ImportPath) }) - return nil -} -func (w *work) writeIndex() error { - return writeIndex(w.cacheDir, w.newIndex) + return &Index{ + GOMODCACHE: gomodcache, + ValidAt: now, // time before the directories were scanned + Entries: entries, + }, len(newEntries) > 0, nil } |
