import (
"bytes"
+ "errors"
"fmt"
"io"
"io/ioutil"
+ "net/url"
"os"
"os/exec"
"path/filepath"
"cmd/go/internal/lockedfile"
"cmd/go/internal/par"
"cmd/go/internal/semver"
+ "cmd/go/internal/web"
)
// GitRepo returns the code repository at the given Git remote reference.
return newGitRepoCached(remote, true)
}
+// A notExistError wraps another error to retain its original text
+// but makes it opaquely equivalent to os.ErrNotExist.
+type notExistError struct {
+ err error
+}
+
+func (e notExistError) Error() string { return e.err.Error() }
+func (notExistError) Is(err error) bool { return err == os.ErrNotExist }
+
const gitWorkDirType = "git3"
var gitRepoCache par.Cache
os.RemoveAll(r.dir)
return nil, err
}
- r.remote = "origin"
}
+ r.remoteURL = r.remote
+ r.remote = "origin"
} else {
// Local path.
// Disallow colon (not in ://) because sometimes
}
type gitRepo struct {
- remote string
- local bool
- dir string
+ remote, remoteURL string
+ local bool
+ dir string
mu lockedfile.Mutex // protects fetchLevel and git repo state
// The git protocol sends all known refs and ls-remote filters them on the client side,
// so we might as well record both heads and tags in one shot.
// Most of the time we only care about tags but sometimes we care about heads too.
- out, err := Run(r.dir, "git", "ls-remote", "-q", r.remote)
- if err != nil {
- if rerr, ok := err.(*RunError); ok {
+ out, gitErr := Run(r.dir, "git", "ls-remote", "-q", r.remote)
+ if gitErr != nil {
+ if rerr, ok := gitErr.(*RunError); ok {
if bytes.Contains(rerr.Stderr, []byte("fatal: could not read Username")) {
rerr.HelpText = "Confirm the import path was entered correctly.\nIf this is a private repository, see https://golang.org/doc/faq#git_https for additional information."
}
}
- r.refsErr = err
+
+ // If the remote URL doesn't exist at all, ideally we should treat the whole
+ // repository as nonexistent by wrapping the error in a notExistError.
+ // For HTTP and HTTPS, that's easy to detect: we'll try to fetch the URL
+ // ourselves and see what code it serves.
+ if u, err := url.Parse(r.remoteURL); err == nil && (u.Scheme == "http" || u.Scheme == "https") {
+ if _, err := web.GetBytes(u); errors.Is(err, os.ErrNotExist) {
+ gitErr = notExistError{gitErr}
+ }
+ }
+
+ r.refsErr = gitErr
return
}
return f("off")
}
+ var lastAttemptErr error
for _, proxy := range proxies {
err = f(proxy)
if !errors.Is(err, os.ErrNotExist) {
+ lastAttemptErr = err
break
}
+
+ // The error indicates that the module does not exist.
+ // In general we prefer to report the last such error,
+ // because it indicates the error that occurs after all other
+ // options have been exhausted.
+ //
+ // However, for modules in the NOPROXY list, the most useful error occurs
+ // first (with proxy set to "noproxy"), and the subsequent errors are all
+ // errNoProxy (which is not particularly helpful). Do not overwrite a more
+ // useful error with errNoproxy.
+ if lastAttemptErr == nil || !errors.Is(err, errNoproxy) {
+ lastAttemptErr = err
+ }
}
- return err
+ return lastAttemptErr
}
type proxyRepo struct {