func (m *MissingPackageHandler) OnGopath(pkg string) (bool, error) { // If useGopath is false, we fall back to the strategy of fetching from // remote. if !m.useGopath { return m.NotFound(pkg) } root := util.GetRootFromPackage(pkg) // Skip any references to the root package. if root == m.RootPackage { return false, nil } msg.Info("Copying package %s from the GOPATH.", pkg) dest := filepath.Join(m.destination, pkg) // Find package on Gopath for _, gp := range gpath.Gopaths() { src := filepath.Join(gp, pkg) // FIXME: Should probably check if src is a dir or symlink. if _, err := os.Stat(src); err == nil { if err := os.MkdirAll(dest, os.ModeDir|0755); err != nil { return false, err } if err := gpath.CopyDir(src, dest); err != nil { return false, err } return true, nil } } msg.Error("Could not locate %s on the GOPATH, though it was found before.", pkg) return false, nil }
// EnsureGopath fails if GOPATH is not set, or if $GOPATH/src is missing. // // Otherwise it returns the value of GOPATH. func EnsureGopath() string { gps := gpath.Gopaths() if len(gps) == 0 { msg.Die("$GOPATH is not set.") } for _, gp := range gps { _, err := os.Stat(path.Join(gp, "src")) if err != nil { msg.Warn("%s", err) continue } return gp } msg.Error("Could not find any of %s/src.\n", strings.Join(gps, "/src, ")) msg.Info("As of Glide 0.5/Go 1.5, this is required.\n") msg.Die("Wihtout src, cannot continue.") return "" }
// VcsGet figures out how to fetch a dependency, and then gets it. // // VcsGet installs into the dest. func VcsGet(dep *cfg.Dependency, dest, home string, cache, cacheGopath, useGopath bool) error { // When not skipping the $GOPATH look in it for a copy of the package if useGopath { // Check if the $GOPATH has a viable version to use and if so copy to vendor gps := gpath.Gopaths() for _, p := range gps { d := filepath.Join(p, "src", dep.Name) if _, err := os.Stat(d); err == nil { empty, err := gpath.IsDirectoryEmpty(d) if empty || err != nil { continue } repo, err := dep.GetRepo(d) if err != nil { continue } // Dirty repos have uncomitted changes. if repo.IsDirty() { continue } // Having found a repo we copy it to vendor and update it. msg.Info("Copying package %s from the GOPATH.", dep.Name) msg.Debug("Found %s in GOPATH at %s. Copying to %s", dep.Name, d, dest) err = gpath.CopyDir(d, dest) if err != nil { return err } // Update the repo in the vendor directory msg.Debug("Updating %s, now in the vendor path at %s", dep.Name, dest) repo, err = dep.GetRepo(dest) if err != nil { return err } err = repo.Update() if err != nil { return err } // If there is no reference set on the dep we try to checkout // the default branch. if dep.Reference == "" { db := defaultBranch(repo, home) if db != "" { err = repo.UpdateVersion(db) if err != nil && msg.Default.IsDebugging { msg.Debug("Attempting to set the version on %s to %s failed. Error %s", dep.Name, db, err) } } } return nil } } } // When opting in to cache in the GOPATH attempt to do put a copy there. if cacheGopath { // Since we didn't find an existing copy in the GOPATHs try to clone there. gp := gpath.Gopath() if gp != "" { d := filepath.Join(gp, "src", dep.Name) if _, err := os.Stat(d); os.IsNotExist(err) { // Empty directory so we checkout out the code here. msg.Debug("Retrieving %s to %s before copying to vendor", dep.Name, d) repo, err := dep.GetRepo(d) if err != nil { return err } repo.Get() branch := findCurrentBranch(repo) if branch != "" { // we know the default branch so we can store it in the cache var loc string if dep.Repository != "" { loc = dep.Repository } else { loc = "https://" + dep.Name } key, err := cacheCreateKey(loc) if err == nil { msg.Debug("Saving default branch for %s", repo.Remote()) c := cacheRepoInfo{DefaultBranch: branch} err = saveCacheRepoData(key, c, home) if msg.Default.IsDebugging && err == errCacheDisabled { msg.Debug("Unable to cache default branch because caching is disabled") } } } msg.Debug("Copying %s from GOPATH at %s to %s", dep.Name, d, dest) err = gpath.CopyDir(d, dest) if err != nil { return err } return nil } } } // If opting in to caching attempt to put it in the cache folder if cache { // Check if the cache has a viable version and try to use that. var loc string if dep.Repository != "" { loc = dep.Repository } else { loc = "https://" + dep.Name } key, err := cacheCreateKey(loc) if err == nil { d := filepath.Join(home, "cache", "src", key) repo, err := dep.GetRepo(d) if err != nil { return err } // If the directory does not exist this is a first cache. if _, err = os.Stat(d); os.IsNotExist(err) { msg.Debug("Adding %s to the cache for the first time", dep.Name) err = repo.Get() if err != nil { return err } branch := findCurrentBranch(repo) if branch != "" { // we know the default branch so we can store it in the cache var loc string if dep.Repository != "" { loc = dep.Repository } else { loc = "https://" + dep.Name } key, err := cacheCreateKey(loc) if err == nil { msg.Debug("Saving default branch for %s", repo.Remote()) c := cacheRepoInfo{DefaultBranch: branch} err = saveCacheRepoData(key, c, home) if err == errCacheDisabled { msg.Debug("Unable to cache default branch because caching is disabled") } else if err != nil { msg.Debug("Error saving %s to cache. Error: %s", repo.Remote(), err) } } } } else { msg.Debug("Updating %s in the cache", dep.Name) err = repo.Update() if err != nil { return err } } msg.Debug("Copying %s from the cache to %s", dep.Name, dest) err = gpath.CopyDir(d, dest) if err != nil { return err } return nil } else { msg.Warn("Cache key generation error: %s", err) } } // If unable to cache pull directly into the vendor/ directory. repo, err := dep.GetRepo(dest) if err != nil { return err } gerr := repo.Get() // Attempt to cache the default branch branch := findCurrentBranch(repo) if branch != "" { // we know the default branch so we can store it in the cache var loc string if dep.Repository != "" { loc = dep.Repository } else { loc = "https://" + dep.Name } key, err := cacheCreateKey(loc) if err == nil { msg.Debug("Saving default branch for %s", repo.Remote()) c := cacheRepoInfo{DefaultBranch: branch} err = saveCacheRepoData(key, c, home) if err == errCacheDisabled { msg.Debug("Unable to cache default branch because caching is disabled") } else if err != nil { msg.Debug("Error saving %s to cache - Error: %s", repo.Remote(), err) } } } return gerr }