本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

124 lines
3.0KB

  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. }
上海开阖软件有限公司 沪ICP备12045867号-1