Skip to content
Permalink
Browse files

internal/code: support 12-character git hash module queries

Add support for 12-character lower-case git hash module queries.
This is a temporary workaround for golang.org/issue/32542; my intention
is to take it out when the upstream issue is resolved. /cc @heschik

Updates golang/go#32542
  • Loading branch information...
dmitshur committed Jun 12, 2019
1 parent 17791dc commit c1141c0e0197df42c3dbad3e8c5200dbd83ca9f9
Showing with 51 additions and 2 deletions.
  1. +33 −2 internal/code/module.go
  2. +18 −0 internal/code/module_test.go
@@ -35,8 +35,10 @@ import (
// versions from a VCS repository:
//
// • It serves only pseudo-versions derived from commits
// on master branch. No other versions or module queries
// are supported at this time.
// on master branch. Temporarily, it also supports module
// queries that are 12-character lower-case commit hashes.
// No other versions or module queries are supported at
// this time.
//
// • It serves a single module corresponding to the root
// of each repository. Multi-module repositories are not
@@ -84,6 +86,35 @@ func (h ModuleHandler) ServeModule(w http.ResponseWriter, req *http.Request) err
return h.serveList(req.Context(), w, gitDir)
}

// Handle "/@v/<12-character git hash>.info" module queries.
// This is a temporary workaround for golang.org/issue/32542.
// TODO: Remove this once upstream issue is resolved.
if typ == "info" && len(version) == 12 && mod.AllHex(version) {
// Open the git repository and get the commit that corresponds to the hash.
repo, err := git.Open(gitDir)
if err != nil {
return err
}
defer func() {
if err := repo.Close(); err != nil {
log.Println("ModuleHandler.ServeModule: repo.Close:", err)
}
}()
commitID, err := repo.ResolveRevision(version)
if err != nil {
return os.ErrNotExist
}
commit, err := repo.GetCommit(commitID)
if err != nil || commit.Committer == nil {
return os.ErrNotExist
}

// Construct the canonical pseudo-version for this commit, and serve its info.
t := time.Unix(commit.Committer.Date.Seconds, 0).UTC()
pseudoVersion := mod.PseudoVersion("", "", t, string(commit.ID)[:12])
return h.serveInfo(w, pseudoVersion, t)
}

// Parse the time and revision from the pseudo-version.
versionTime, versionRevision, err := mod.ParsePseudoVersion(version)
if err != nil || len(versionRevision) != 12 || !mod.AllHex(versionRevision) {
@@ -61,6 +61,15 @@ v0.0.0-20170914162131-bf160e40a791
"Version": "v0.0.0-20170912031248-a1d95f8919b5",
"Time": "2017-09-12T03:12:48Z"
}
`,
},
{
url: "/api/module/dmitri.shuralyov.com/kebabcase/@v/a1d95f8919b5.info",
wantType: "application/json",
wantBody: `{
"Version": "v0.0.0-20170912031248-a1d95f8919b5",
"Time": "2017-09-12T03:12:48Z"
}
`,
},
{
@@ -70,6 +79,15 @@ v0.0.0-20170914162131-bf160e40a791
"Version": "v0.0.0-20170914162131-bf160e40a791",
"Time": "2017-09-14T16:21:31Z"
}
`,
},
{
url: "/api/module/dmitri.shuralyov.com/kebabcase/@v/bf160e40a791.info",
wantType: "application/json",
wantBody: `{
"Version": "v0.0.0-20170914162131-bf160e40a791",
"Time": "2017-09-14T16:21:31Z"
}
`,
},
{

0 comments on commit c1141c0

Please sign in to comment.
You can’t perform that action at this time.