go_get.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package context
  2. import (
  3. "net/http"
  4. "path"
  5. "strings"
  6. "github.com/unknwon/com"
  7. "gopkg.in/macaron.v1"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/db"
  10. "gogs.io/gogs/internal/repoutil"
  11. )
  12. // ServeGoGet does quick responses for appropriate go-get meta with status OK
  13. // regardless of whether the user has access to the repository, or the repository
  14. // does exist at all. This is particular a workaround for "go get" command which
  15. // does not respect .netrc file.
  16. func ServeGoGet() macaron.Handler {
  17. return func(c *macaron.Context) {
  18. if c.Query("go-get") != "1" {
  19. return
  20. }
  21. ownerName := c.Params(":username")
  22. repoName := c.Params(":reponame")
  23. branchName := "master"
  24. owner, err := db.Users.GetByUsername(c.Req.Context(), ownerName)
  25. if err == nil {
  26. repo, err := db.Repositories.GetByName(c.Req.Context(), owner.ID, repoName)
  27. if err == nil && repo.DefaultBranch != "" {
  28. branchName = repo.DefaultBranch
  29. }
  30. }
  31. prefix := conf.Server.ExternalURL + path.Join(ownerName, repoName, "src", branchName)
  32. insecureFlag := ""
  33. if !strings.HasPrefix(conf.Server.ExternalURL, "https://") {
  34. insecureFlag = "--insecure "
  35. }
  36. c.PlainText(http.StatusOK, []byte(com.Expand(`<!doctype html>
  37. <html>
  38. <head>
  39. <meta name="go-import" content="{GoGetImport} git {CloneLink}">
  40. <meta name="go-source" content="{GoGetImport} _ {GoDocDirectory} {GoDocFile}">
  41. </head>
  42. <body>
  43. go get {InsecureFlag}{GoGetImport}
  44. </body>
  45. </html>
  46. `,
  47. map[string]string{
  48. "GoGetImport": path.Join(conf.Server.URL.Host, conf.Server.Subpath, ownerName, repoName),
  49. "CloneLink": repoutil.HTTPSCloneURL(ownerName, repoName),
  50. "GoDocDirectory": prefix + "{/dir}",
  51. "GoDocFile": prefix + "{/dir}/{file}#L{line}",
  52. "InsecureFlag": insecureFlag,
  53. },
  54. )))
  55. }
  56. }