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 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import "strings"
  6. // SubModule submodule is a reference on git repository
  7. type SubModule struct {
  8. Name string
  9. URL string
  10. }
  11. // SubModuleFile represents a file with submodule type.
  12. type SubModuleFile struct {
  13. *Commit
  14. refURL string
  15. refID string
  16. }
  17. // NewSubModuleFile create a new submodule file
  18. func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
  19. return &SubModuleFile{
  20. Commit: c,
  21. refURL: refURL,
  22. refID: refID,
  23. }
  24. }
  25. func getRefURL(refURL, urlPrefix, parentPath string) string {
  26. if refURL == "" {
  27. return ""
  28. }
  29. url := strings.TrimSuffix(refURL, ".git")
  30. // git://xxx/user/repo
  31. if strings.HasPrefix(url, "git://") {
  32. return "http://" + strings.TrimPrefix(url, "git://")
  33. }
  34. // http[s]://xxx/user/repo
  35. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  36. return url
  37. }
  38. // Relative url prefix check (according to git submodule documentation)
  39. if strings.HasPrefix(url, "./") || strings.HasPrefix(url, "../") {
  40. // ...construct and return correct submodule url here...
  41. idx := strings.Index(parentPath, "/src/")
  42. if idx == -1 {
  43. return url
  44. }
  45. return strings.TrimSuffix(urlPrefix, "/") + parentPath[:idx] + "/" + url
  46. }
  47. // sysuser@xxx:user/repo
  48. i := strings.Index(url, "@")
  49. j := strings.LastIndex(url, ":")
  50. // Only process when i < j because git+ssh://git@git.forwardbias.in/npploader.git
  51. if i > -1 && j > -1 && i < j {
  52. // fix problem with reverse proxy works only with local server
  53. if strings.Contains(urlPrefix, url[i+1:j]) {
  54. return urlPrefix + url[j+1:]
  55. }
  56. if strings.HasPrefix(url, "ssh://") || strings.HasPrefix(url, "git+ssh://") {
  57. k := strings.Index(url[j+1:], "/")
  58. return "http://" + url[i+1:j] + "/" + url[j+1:][k+1:]
  59. }
  60. return "http://" + url[i+1:j] + "/" + url[j+1:]
  61. }
  62. return url
  63. }
  64. // RefURL guesses and returns reference URL.
  65. func (sf *SubModuleFile) RefURL(urlPrefix string, parentPath string) string {
  66. return getRefURL(sf.refURL, urlPrefix, parentPath)
  67. }
  68. // RefID returns reference ID.
  69. func (sf *SubModuleFile) RefID() string {
  70. return sf.refID
  71. }