You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

goget.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package web
  4. import (
  5. "fmt"
  6. "html"
  7. "net/http"
  8. "net/url"
  9. "path"
  10. "strings"
  11. repo_model "code.gitea.io/gitea/models/repo"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/util"
  15. )
  16. func goGet(ctx *context.Context) {
  17. if ctx.Req.Method != "GET" || len(ctx.Req.URL.RawQuery) < 8 || ctx.FormString("go-get") != "1" {
  18. return
  19. }
  20. parts := strings.SplitN(ctx.Req.URL.EscapedPath(), "/", 4)
  21. if len(parts) < 3 {
  22. return
  23. }
  24. ownerName := parts[1]
  25. repoName := parts[2]
  26. // Quick responses appropriate go-get meta with status 200
  27. // regardless of if user have access to the repository,
  28. // or the repository does not exist at all.
  29. // This is particular a workaround for "go get" command which does not respect
  30. // .netrc file.
  31. trimmedRepoName := strings.TrimSuffix(repoName, ".git")
  32. if ownerName == "" || trimmedRepoName == "" {
  33. _, _ = ctx.Write([]byte(`<!doctype html>
  34. <html>
  35. <body>
  36. invalid import path
  37. </body>
  38. </html>
  39. `))
  40. ctx.Status(http.StatusBadRequest)
  41. return
  42. }
  43. branchName := setting.Repository.DefaultBranch
  44. repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, ownerName, repoName)
  45. if err == nil && len(repo.DefaultBranch) > 0 {
  46. branchName = repo.DefaultBranch
  47. }
  48. prefix := setting.AppURL + path.Join(url.PathEscape(ownerName), url.PathEscape(repoName), "src", "branch", util.PathEscapeSegments(branchName))
  49. appURL, _ := url.Parse(setting.AppURL)
  50. insecure := ""
  51. if appURL.Scheme == string(setting.HTTP) {
  52. insecure = "--insecure "
  53. }
  54. goGetImport := context.ComposeGoGetImport(ownerName, trimmedRepoName)
  55. goImportContent := fmt.Sprintf("%s git %s", goGetImport, repo_model.ComposeHTTPSCloneURL(ownerName, repoName) /*CloneLink*/)
  56. goSourceContent := fmt.Sprintf("%s _ %s %s", goGetImport, prefix+"{/dir}" /*GoDocDirectory*/, prefix+"{/dir}/{file}#L{line}" /*GoDocFile*/)
  57. goGetCli := fmt.Sprintf("go get %s%s", insecure, goGetImport)
  58. res := fmt.Sprintf(`<!doctype html>
  59. <html>
  60. <head>
  61. <meta name="go-import" content="%s">
  62. <meta name="go-source" content="%s">
  63. </head>
  64. <body>
  65. %s
  66. </body>
  67. </html>`, html.EscapeString(goImportContent), html.EscapeString(goSourceContent), html.EscapeString(goGetCli))
  68. ctx.RespHeader().Set("Content-Type", "text/html")
  69. _, _ = ctx.Write([]byte(res))
  70. }