Fix relative paths inside sources

This commit is contained in:
adisbladis 2020-07-22 13:57:13 +02:00
parent e51f884b85
commit ecf0c76348
No known key found for this signature in database
GPG key ID: 110BFAD44C6249B7
2 changed files with 23 additions and 16 deletions

View file

@ -11,7 +11,9 @@ import (
"golang.org/x/mod/module" "golang.org/x/mod/module"
"golang.org/x/tools/go/vcs" "golang.org/x/tools/go/vcs"
"io/ioutil" "io/ioutil"
"os"
"os/exec" "os/exec"
"path/filepath"
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
@ -161,7 +163,7 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
rev = commitShaRev.FindAllStringSubmatch(rev, -1)[0][1] rev = commitShaRev.FindAllStringSubmatch(rev, -1)[0][1]
} }
goPackagePathPrefix, _, _ := module.SplitPathVersion(goPackagePath) goPackagePathPrefix, pathMajor, _ := module.SplitPathVersion(goPackagePath)
// Relative path within the repo // Relative path within the repo
relPath := strings.TrimPrefix(goPackagePathPrefix, repoRoot.Root+"/") relPath := strings.TrimPrefix(goPackagePathPrefix, repoRoot.Root+"/")
@ -169,9 +171,7 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
relPath = "" relPath = ""
} }
newRev := fmt.Sprintf("%s/%s", relPath, rev)
if len(caches) > 0 { if len(caches) > 0 {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"goPackagePath": goPackagePath, "goPackagePath": goPackagePath,
}).Info("Checking previous invocation cache") }).Info("Checking previous invocation cache")
@ -179,13 +179,11 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
for _, cache := range caches { for _, cache := range caches {
cached, ok := cache[goPackagePath] cached, ok := cache[goPackagePath]
if ok { if ok {
for _, rev := range []string{rev, newRev} { if cached.SumVersion == sumVersion {
if cached.Rev == rev { log.WithFields(log.Fields{
log.WithFields(log.Fields{ "goPackagePath": goPackagePath,
"goPackagePath": goPackagePath, }).Info("Returning cached entry")
}).Info("Returning cached entry") return cached, nil
return cached, nil
}
} }
} }
} }
@ -199,7 +197,7 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
URL string `json:"url"` URL string `json:"url"`
Rev string `json:"rev"` Rev string `json:"rev"`
Sha256 string `json:"sha256"` Sha256 string `json:"sha256"`
// path string Path string `json:"path"`
// date string // date string
// fetchSubmodules bool // fetchSubmodules bool
// deepClone bool // deepClone bool
@ -217,6 +215,8 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
"--url", repoRoot.Repo, "--url", repoRoot.Repo,
"--rev", rev).Output() "--rev", rev).Output()
if err != nil { if err != nil {
newRev := fmt.Sprintf("%s/%s", relPath, rev)
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"goPackagePath": goPackagePath, "goPackagePath": goPackagePath,
"rev": newRev, "rev": newRev,
@ -239,7 +239,6 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
} }
var output *prefetchOutput var output *prefetchOutput
err = json.Unmarshal(stdout, &output) err = json.Unmarshal(stdout, &output)
if err != nil { if err != nil {
return nil, err return nil, err
@ -247,8 +246,16 @@ func fetchPackage(caches []map[string]*types.Package, importPath string, goPacka
vendorPath := "" vendorPath := ""
if importPath != goPackagePath { if importPath != goPackagePath {
importPathPrefix, _, _ := module.SplitPathVersion(importPath) vendorPath = importPath
vendorPath = importPathPrefix }
if relPath == "" && pathMajor != "" {
p := filepath.Join(output.Path, pathMajor)
_, err := os.Stat(p)
if err == nil {
fmt.Println(pathMajor)
relPath = strings.TrimPrefix(pathMajor, "/")
}
} }
return &types.Package{ return &types.Package{

View file

@ -17,8 +17,8 @@ type fetchInfo struct {
type packageT struct { type packageT struct {
SumVersion string `toml:"sumVersion"` SumVersion string `toml:"sumVersion"`
RelPath string `toml:"relPath"` RelPath string `toml:"relPath,omitempty"`
VendorPath string `toml:"vendorPath"` VendorPath string `toml:"vendorPath,omitempty"`
Fetch *fetchInfo `toml:"fetch"` Fetch *fetchInfo `toml:"fetch"`
} }