diff --git a/main.go b/main.go index 03b7fb8..fe06058 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,7 @@ var gogetTemplate = template.Must(template.New("").Parse(` +{{$root := .GitHubRoot}}{{$tree := .GitHubTree}} go get {{.GopkgPath}} @@ -73,7 +74,26 @@ type Repo struct { SubPath string OldFormat bool // The old /v2/pkg format. MajorVersion Version - AllVersions VersionList + + // FullVersion is the best version in AllVersions that matches MajorVersion. + // It defaults to InvalidVersion if there are no matches. + FullVersion Version + + // AllVersions holds all versions currently available in the repository, + // either coming from branch names or from tag names. Version zero (v0) + // is only present in the list if it really exists in the repository. + AllVersions VersionList +} + +// SetVersions records in the relevant fields the details about which +// package versions are available in the repository. +func (repo *Repo) SetVersions(all []Version) { + repo.AllVersions = all + for _, v := range repo.AllVersions { + if v.Major == repo.MajorVersion.Major && v.Unstable == repo.MajorVersion.Unstable && repo.FullVersion.Less(v) { + repo.FullVersion = v + } + } } // GitHubRoot returns the repository root at GitHub, without a schema. @@ -85,6 +105,14 @@ func (repo *Repo) GitHubRoot() string { } } +// GitHubTree returns the repository tree name at GitHub for the selected version. +func (repo *Repo) GitHubTree() string { + if repo.FullVersion == InvalidVersion { + return "master" + } + return repo.FullVersion.String() +} + // GopkgRoot returns the package root at gopkg.in, without a schema. func (repo *Repo) GopkgRoot() string { return repo.GopkgVersionRoot(repo.MajorVersion) @@ -153,10 +181,11 @@ func handler(resp http.ResponseWriter, req *http.Request) { } repo := &Repo{ - User: m[1], - Name: m[2], - SubPath: m[5], - OldFormat: oldFormat, + User: m[1], + Name: m[2], + SubPath: m[5], + OldFormat: oldFormat, + FullVersion: InvalidVersion, } var ok bool @@ -167,9 +196,11 @@ func handler(resp http.ResponseWriter, req *http.Request) { } var changed []byte + var versions VersionList original, err := fetchRefs(repo) if err == nil { - changed, repo.AllVersions, err = changeRefs(original, repo.MajorVersion) + changed, versions, err = changeRefs(original, repo.MajorVersion) + repo.SetVersions(versions) } switch err { @@ -338,7 +369,7 @@ func changeRefs(data []byte, major Version) (changed []byte, versions VersionLis } var buf bytes.Buffer - buf.Grow(len(data)+256) + buf.Grow(len(data) + 256) // Copy the header as-is. buf.Write(data[:hlinei]) @@ -346,7 +377,7 @@ func changeRefs(data []byte, major Version) (changed []byte, versions VersionLis // Extract the original capabilities. caps := "" if i := strings.Index(sdata[hlinei:hlinej], "\x00"); i > 0 { - caps = strings.Replace(sdata[hlinei+i+1:hlinej-1], "symref=", "oldref=", -1) + caps = strings.Replace(sdata[hlinei+i+1:hlinej-1], "symref=", "oldref=", -1) } // Insert the HEAD reference line with the right hash and a proper symref capability. diff --git a/page.go b/page.go index 0178520..c31ac8e 100644 --- a/page.go +++ b/page.go @@ -131,7 +131,7 @@ const packageTemplateString = ` {{ end }}
- Source Code + Source Code API Documentation
@@ -212,9 +212,9 @@ func init() { type packageData struct { Repo *Repo LatestVersions VersionList // Contains only the latest version for each major - FullVersion Version // Version that the major requested resolves to PackageName string // Actual package identifier as specified in http://golang.org/ref/spec#PackageClause Synopsis string + GitTreeName string } // SearchResults is used with the godoc.org search API @@ -234,15 +234,13 @@ func renderPackagePage(resp http.ResponseWriter, req *http.Request, repo *Repo) // Calculate the latest version for each major version, both stable and unstable. latestVersions := make(map[int]Version) - latestUnstable := make(map[int]Version) for _, v := range repo.AllVersions { - m := latestVersions if v.Unstable { - m = latestUnstable + continue } - v2, exists := m[v.Major] + v2, exists := latestVersions[v.Major] if !exists || v2.Less(v) { - m[v.Major] = v + latestVersions[v.Major] = v } } data.LatestVersions = make(VersionList, 0, len(latestVersions)) @@ -251,12 +249,9 @@ func renderPackagePage(resp http.ResponseWriter, req *http.Request, repo *Repo) } sort.Sort(sort.Reverse(data.LatestVersions)) - if repo.MajorVersion.Unstable { - data.FullVersion = latestUnstable[repo.MajorVersion.Major] - // Prepend post-sorting so it's show first. - data.LatestVersions = append([]Version{data.FullVersion}, data.LatestVersions...) - } else { - data.FullVersion = latestVersions[repo.MajorVersion.Major] + if repo.FullVersion.Unstable { + // Prepend post-sorting so it shows first. + data.LatestVersions = append([]Version{repo.FullVersion}, data.LatestVersions...) } var dataMutex sync.Mutex