Skip to content

Commit c1141c0

Browse files
committed
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
1 parent 17791dc commit c1141c0

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

internal/code/module.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ import (
3535
// versions from a VCS repository:
3636
//
3737
// • It serves only pseudo-versions derived from commits
38-
// on master branch. No other versions or module queries
39-
// are supported at this time.
38+
// on master branch. Temporarily, it also supports module
39+
// queries that are 12-character lower-case commit hashes.
40+
// No other versions or module queries are supported at
41+
// this time.
4042
//
4143
// • It serves a single module corresponding to the root
4244
// of each repository. Multi-module repositories are not
@@ -84,6 +86,35 @@ func (h ModuleHandler) ServeModule(w http.ResponseWriter, req *http.Request) err
8486
return h.serveList(req.Context(), w, gitDir)
8587
}
8688

89+
// Handle "/@v/<12-character git hash>.info" module queries.
90+
// This is a temporary workaround for golang.org/issue/32542.
91+
// TODO: Remove this once upstream issue is resolved.
92+
if typ == "info" && len(version) == 12 && mod.AllHex(version) {
93+
// Open the git repository and get the commit that corresponds to the hash.
94+
repo, err := git.Open(gitDir)
95+
if err != nil {
96+
return err
97+
}
98+
defer func() {
99+
if err := repo.Close(); err != nil {
100+
log.Println("ModuleHandler.ServeModule: repo.Close:", err)
101+
}
102+
}()
103+
commitID, err := repo.ResolveRevision(version)
104+
if err != nil {
105+
return os.ErrNotExist
106+
}
107+
commit, err := repo.GetCommit(commitID)
108+
if err != nil || commit.Committer == nil {
109+
return os.ErrNotExist
110+
}
111+
112+
// Construct the canonical pseudo-version for this commit, and serve its info.
113+
t := time.Unix(commit.Committer.Date.Seconds, 0).UTC()
114+
pseudoVersion := mod.PseudoVersion("", "", t, string(commit.ID)[:12])
115+
return h.serveInfo(w, pseudoVersion, t)
116+
}
117+
87118
// Parse the time and revision from the pseudo-version.
88119
versionTime, versionRevision, err := mod.ParsePseudoVersion(version)
89120
if err != nil || len(versionRevision) != 12 || !mod.AllHex(versionRevision) {

internal/code/module_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ v0.0.0-20170914162131-bf160e40a791
6161
"Version": "v0.0.0-20170912031248-a1d95f8919b5",
6262
"Time": "2017-09-12T03:12:48Z"
6363
}
64+
`,
65+
},
66+
{
67+
url: "/api/module/dmitri.shuralyov.com/kebabcase/@v/a1d95f8919b5.info",
68+
wantType: "application/json",
69+
wantBody: `{
70+
"Version": "v0.0.0-20170912031248-a1d95f8919b5",
71+
"Time": "2017-09-12T03:12:48Z"
72+
}
6473
`,
6574
},
6675
{
@@ -70,6 +79,15 @@ v0.0.0-20170914162131-bf160e40a791
7079
"Version": "v0.0.0-20170914162131-bf160e40a791",
7180
"Time": "2017-09-14T16:21:31Z"
7281
}
82+
`,
83+
},
84+
{
85+
url: "/api/module/dmitri.shuralyov.com/kebabcase/@v/bf160e40a791.info",
86+
wantType: "application/json",
87+
wantBody: `{
88+
"Version": "v0.0.0-20170914162131-bf160e40a791",
89+
"Time": "2017-09-14T16:21:31Z"
90+
}
7391
`,
7492
},
7593
{

0 commit comments

Comments
 (0)