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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "regexp"
  11. "strings"
  12. )
  13. var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+@)?([a-zA-Z0-9._-]+):(.*)$`)
  14. // SubModule submodule is a reference on git repository
  15. type SubModule struct {
  16. Name string
  17. URL string
  18. }
  19. // SubModuleFile represents a file with submodule type.
  20. type SubModuleFile struct {
  21. *Commit
  22. refURL string
  23. refID string
  24. }
  25. // NewSubModuleFile create a new submodule file
  26. func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
  27. return &SubModuleFile{
  28. Commit: c,
  29. refURL: refURL,
  30. refID: refID,
  31. }
  32. }
  33. func getRefURL(refURL, urlPrefix, parentPath string) string {
  34. if refURL == "" {
  35. return ""
  36. }
  37. refURI := strings.TrimSuffix(refURL, ".git")
  38. prefixURL, _ := url.Parse(urlPrefix)
  39. urlPrefixHostname, _, err := net.SplitHostPort(prefixURL.Host)
  40. if err != nil {
  41. urlPrefixHostname = prefixURL.Host
  42. }
  43. // Relative url prefix check (according to git submodule documentation)
  44. if strings.HasPrefix(refURI, "./") || strings.HasPrefix(refURI, "../") {
  45. // ...construct and return correct submodule url here...
  46. idx := strings.Index(parentPath, "/src/")
  47. if idx == -1 {
  48. return refURI
  49. }
  50. return strings.TrimSuffix(urlPrefix, "/") + parentPath[:idx] + "/" + 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. path := m[3]
  60. if !strings.HasPrefix(path, "/") {
  61. path = "/" + path
  62. }
  63. if urlPrefixHostname == refHostname {
  64. return prefixURL.Scheme + "://" + urlPrefixHostname + path
  65. }
  66. return "http://" + refHostname + path
  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 prefixURL.Scheme + "://" + prefixURL.Host + 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, parentPath string) string {
  96. return getRefURL(sf.refURL, urlPrefix, parentPath)
  97. }
  98. // RefID returns reference ID.
  99. func (sf *SubModuleFile) RefID() string {
  100. return sf.refID
  101. }