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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Copyright 2015 The Gogs Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "fmt"
  7. "net"
  8. "net/url"
  9. "path"
  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, repoFullName, sshDomain 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. urlPrefix = strings.TrimSuffix(urlPrefix, "/")
  44. // FIXME: Need to consider branch - which will require changes in modules/git/commit.go:GetSubModules
  45. // Relative url prefix check (according to git submodule documentation)
  46. if strings.HasPrefix(refURI, "./") || strings.HasPrefix(refURI, "../") {
  47. return urlPrefix + path.Clean(path.Join("/", repoFullName, refURI))
  48. }
  49. if !strings.Contains(refURI, "://") {
  50. // scp style syntax which contains *no* port number after the : (and is not parsed by net/url)
  51. // ex: git@try.gitea.io:go-gitea/gitea
  52. match := scpSyntax.FindAllStringSubmatch(refURI, -1)
  53. if len(match) > 0 {
  54. m := match[0]
  55. refHostname := m[2]
  56. pth := m[3]
  57. if !strings.HasPrefix(pth, "/") {
  58. pth = "/" + pth
  59. }
  60. if urlPrefixHostname == refHostname || refHostname == sshDomain {
  61. return urlPrefix + path.Clean(path.Join("/", pth))
  62. }
  63. return "http://" + refHostname + pth
  64. }
  65. }
  66. ref, err := url.Parse(refURI)
  67. if err != nil {
  68. return ""
  69. }
  70. refHostname, _, err := net.SplitHostPort(ref.Host)
  71. if err != nil {
  72. refHostname = ref.Host
  73. }
  74. supportedSchemes := []string{"http", "https", "git", "ssh", "git+ssh"}
  75. for _, scheme := range supportedSchemes {
  76. if ref.Scheme == scheme {
  77. if ref.Scheme == "http" || ref.Scheme == "https" {
  78. if len(ref.User.Username()) > 0 {
  79. return ref.Scheme + "://" + fmt.Sprintf("%v", ref.User) + "@" + ref.Host + ref.Path
  80. }
  81. return ref.Scheme + "://" + ref.Host + ref.Path
  82. } else if urlPrefixHostname == refHostname || refHostname == sshDomain {
  83. return urlPrefix + path.Clean(path.Join("/", ref.Path))
  84. }
  85. return "http://" + refHostname + ref.Path
  86. }
  87. }
  88. return ""
  89. }
  90. // RefURL guesses and returns reference URL.
  91. func (sf *SubModuleFile) RefURL(urlPrefix, repoFullName, sshDomain string) string {
  92. return getRefURL(sf.refURL, urlPrefix, repoFullName, sshDomain)
  93. }
  94. // RefID returns reference ID.
  95. func (sf *SubModuleFile) RefID() string {
  96. return sf.refID
  97. }