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.

submodule.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Copyright 2015 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package git
  6. import (
  7. "fmt"
  8. "net"
  9. "net/url"
  10. "path"
  11. "regexp"
  12. "strings"
  13. )
  14. var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
  15. // SubModule submodule is a reference on git repository
  16. type SubModule struct {
  17. Name string
  18. URL string
  19. }
  20. // SubModuleFile represents a file with submodule type.
  21. type SubModuleFile struct {
  22. *Commit
  23. refURL string
  24. refID string
  25. }
  26. // NewSubModuleFile create a new submodule file
  27. func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
  28. return &SubModuleFile{
  29. Commit: c,
  30. refURL: refURL,
  31. refID: refID,
  32. }
  33. }
  34. func getRefURL(refURL, urlPrefix, repoFullName string) string {
  35. if refURL == "" {
  36. return ""
  37. }
  38. refURI := strings.TrimSuffix(refURL, ".git")
  39. prefixURL, _ := url.Parse(urlPrefix)
  40. urlPrefixHostname, _, err := net.SplitHostPort(prefixURL.Host)
  41. if err != nil {
  42. urlPrefixHostname = prefixURL.Host
  43. }
  44. if strings.HasSuffix(urlPrefix, "/") {
  45. urlPrefix = urlPrefix[:len(urlPrefix)-1]
  46. }
  47. // FIXME: Need to consider branch - which will require changes in modules/git/commit.go:GetSubModules
  48. // Relative url prefix check (according to git submodule documentation)
  49. if strings.HasPrefix(refURI, "./") || strings.HasPrefix(refURI, "../") {
  50. return urlPrefix + path.Clean(path.Join("/", repoFullName, refURI))
  51. }
  52. if !strings.Contains(refURI, "://") {
  53. // scp style syntax which contains *no* port number after the : (and is not parsed by net/url)
  54. // ex: git@try.gitea.io:go-gitea/gitea
  55. match := scpSyntax.FindAllStringSubmatch(refURI, -1)
  56. if len(match) > 0 {
  57. m := match[0]
  58. refHostname := m[2]
  59. pth := m[3]
  60. if !strings.HasPrefix(pth, "/") {
  61. pth = "/" + pth
  62. }
  63. if urlPrefixHostname == refHostname {
  64. return urlPrefix + path.Clean(path.Join("/", pth))
  65. }
  66. return "http://" + refHostname + pth
  67. }
  68. }
  69. ref, err := url.Parse(refURI)
  70. if err != nil {
  71. return ""
  72. }
  73. refHostname, _, err := net.SplitHostPort(ref.Host)
  74. if err != nil {
  75. refHostname = ref.Host
  76. }
  77. supportedSchemes := []string{"http", "https", "git", "ssh", "git+ssh"}
  78. for _, scheme := range supportedSchemes {
  79. if ref.Scheme == scheme {
  80. if urlPrefixHostname == refHostname {
  81. return urlPrefix + path.Clean(path.Join("/", ref.Path))
  82. } else if ref.Scheme == "http" || ref.Scheme == "https" {
  83. if len(ref.User.Username()) > 0 {
  84. return ref.Scheme + "://" + fmt.Sprintf("%v", ref.User) + "@" + ref.Host + ref.Path
  85. }
  86. return ref.Scheme + "://" + ref.Host + ref.Path
  87. } else {
  88. return "http://" + refHostname + ref.Path
  89. }
  90. }
  91. }
  92. return ""
  93. }
  94. // RefURL guesses and returns reference URL.
  95. func (sf *SubModuleFile) RefURL(urlPrefix string, repoFullName string) string {
  96. return getRefURL(sf.refURL, urlPrefix, repoFullName)
  97. }
  98. // RefID returns reference ID.
  99. func (sf *SubModuleFile) RefID() string {
  100. return sf.refID
  101. }