in git-codereview/api.go [73:136]
func loadGerritOriginInternal(origin, remoteOrigin string) error {
originUrl, err := url.Parse(remoteOrigin)
if err != nil {
return fmt.Errorf("failed to parse git's remote.origin.url %q as a URL: %v", remoteOrigin, err)
} else {
originUrl.User = nil
remoteOrigin = originUrl.String()
}
hasGerritConfig := true
if origin == "" {
hasGerritConfig = false
origin = remoteOrigin
}
if strings.Contains(origin, "github.com") {
return fmt.Errorf("git origin must be a Gerrit host, not GitHub: %s", origin)
}
if googlesourceIndex := strings.Index(origin, ".googlesource.com"); googlesourceIndex >= 0 {
if !strings.HasPrefix(origin, "https://") {
return fmt.Errorf("git origin must be an https:// URL: %s", origin)
}
// https:// prefix and then one slash between host and top-level name
if strings.Count(origin, "/") != 3 {
return fmt.Errorf("git origin is malformed: %s", origin)
}
host := origin[len("https://"):strings.LastIndex(origin, "/")]
// In the case of Google's Gerrit, host is go.googlesource.com
// and apiURL uses go-review.googlesource.com, but the Gerrit
// setup instructions do not write down a cookie explicitly for
// go-review.googlesource.com, so we look for the non-review
// host name instead.
url := origin
i := googlesourceIndex
url = url[:i] + "-review" + url[i:]
i = strings.LastIndex(url, "/")
url, project := url[:i], url[i+1:]
auth.host = host
auth.url = url
auth.project = project
return nil
}
// Origin is not *.googlesource.com.
//
// If the Gerrit origin is set from the codereview.cfg file than we handle it
// differently to allow for sub-path hosted Gerrit.
auth.host = originUrl.Host
if hasGerritConfig {
if !strings.HasPrefix(remoteOrigin, origin) {
return fmt.Errorf("Gerrit origin %q from %q different than git origin url %q", origin, configPath, originUrl)
}
auth.project = strings.Trim(strings.TrimPrefix(remoteOrigin, origin), "/")
auth.url = origin
} else {
auth.project = strings.Trim(originUrl.Path, "/")
auth.url = strings.TrimSuffix(remoteOrigin, originUrl.Path)
}
return nil
}