-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Return go-get info on subdirs #15642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ace36a
Return go-get info on subdirs
zeripath 2e379de
Merge remote-tracking branch 'origin/master' into fix-15625-go-get-ha…
zeripath c9570e5
Merge branch 'master' into fix-15625-go-get-handler
zeripath ce705a7
Merge branch 'fix-15625-go-get-handler' of github.com:zeripath/gitea …
zeripath ac9d4ab
Merge remote-tracking branch 'origin/main' into fix-15625-go-get-handler
zeripath dd61d75
add testcase as per 6543
zeripath f9f502a
Merge branch 'main' into fix-15625-go-get-handler
6543 cf2eea9
Merge branch 'main' into fix-15625-go-get-handler
zeripath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integrations | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGoGet(t *testing.T) { | ||
defer prepareTestEnv(t)() | ||
|
||
req := NewRequest(t, "GET", "/blah/glah/plah?go-get=1") | ||
resp := MakeRequest(t, req, http.StatusOK) | ||
|
||
expected := fmt.Sprintf(`<!doctype html> | ||
<html> | ||
<head> | ||
<meta name="go-import" content="%[1]s:%[2]s/blah/glah git %[3]sblah/glah.git"> | ||
<meta name="go-source" content="%[1]s:%[2]s/blah/glah _ %[3]sblah/glah/src/branch/master{/dir} %[3]sblah/glah/src/branch/master{/dir}/{file}#L{line}"> | ||
</head> | ||
<body> | ||
go get --insecure %[1]s:%[2]s/blah/glah | ||
</body> | ||
</html> | ||
`, setting.Domain, setting.HTTPPort, setting.AppURL) | ||
|
||
assert.Equal(t, expected, resp.Body.String()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package routes | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/context" | ||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/util" | ||
"github.com/unknwon/com" | ||
) | ||
|
||
func goGet(ctx *context.Context) { | ||
if ctx.Req.Method != "GET" || ctx.Query("go-get") != "1" || len(ctx.Req.URL.Query()) > 1 { | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
|
||
parts := strings.SplitN(ctx.Req.URL.EscapedPath(), "/", 4) | ||
|
||
if len(parts) < 3 { | ||
return | ||
} | ||
|
||
ownerName := parts[1] | ||
repoName := parts[2] | ||
|
||
// Quick responses appropriate go-get meta with status 200 | ||
// regardless of if user have access to the repository, | ||
// or the repository does not exist at all. | ||
// This is particular a workaround for "go get" command which does not respect | ||
// .netrc file. | ||
|
||
trimmedRepoName := strings.TrimSuffix(repoName, ".git") | ||
|
||
if ownerName == "" || trimmedRepoName == "" { | ||
_, _ = ctx.Write([]byte(`<!doctype html> | ||
<html> | ||
<body> | ||
invalid import path | ||
</body> | ||
</html> | ||
`)) | ||
ctx.Status(400) | ||
return | ||
} | ||
branchName := setting.Repository.DefaultBranch | ||
|
||
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) | ||
if err == nil && len(repo.DefaultBranch) > 0 { | ||
branchName = repo.DefaultBranch | ||
} | ||
prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName)) | ||
|
||
appURL, _ := url.Parse(setting.AppURL) | ||
|
||
insecure := "" | ||
if appURL.Scheme == string(setting.HTTP) { | ||
insecure = "--insecure " | ||
} | ||
ctx.Header().Set("Content-Type", "text/html") | ||
ctx.Status(http.StatusOK) | ||
_, _ = ctx.Write([]byte(com.Expand(`<!doctype html> | ||
<html> | ||
<head> | ||
<meta name="go-import" content="{GoGetImport} git {CloneLink}"> | ||
<meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}"> | ||
</head> | ||
<body> | ||
go get {Insecure}{GoGetImport} | ||
</body> | ||
</html> | ||
`, map[string]string{ | ||
"GoGetImport": context.ComposeGoGetImport(ownerName, trimmedRepoName), | ||
"CloneLink": models.ComposeHTTPSCloneURL(ownerName, repoName), | ||
"GoDocDirectory": prefix + "{/dir}", | ||
"GoDocFile": prefix + "{/dir}/{file}#L{line}", | ||
"Insecure": insecure, | ||
}))) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because you're already fiddling here, maybe we could replace
com.Expand
withos.Expand
? 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's doing something different from os.Expand.
It might be better to actually change this to use a go-template instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both are used to expand variables in strings. A template would also work.