go_get.go 1.7 KB

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