submodule.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. type SubModule struct {
  7. Name string
  8. URL string
  9. }
  10. // SubModuleFile represents a file with submodule type.
  11. type SubModuleFile struct {
  12. *Commit
  13. refURL string
  14. refID string
  15. }
  16. func NewSubModuleFile(c *Commit, refURL, refID string) *SubModuleFile {
  17. return &SubModuleFile{
  18. Commit: c,
  19. refURL: refURL,
  20. refID: refID,
  21. }
  22. }
  23. // RefURL guesses and returns reference URL.
  24. func (sf *SubModuleFile) RefURL(urlPrefix string, parentPath string) string {
  25. if sf.refURL == "" {
  26. return ""
  27. }
  28. url := strings.TrimSuffix(sf.refURL, ".git")
  29. // git://xxx/user/repo
  30. if strings.HasPrefix(url, "git://") {
  31. return "http://" + strings.TrimPrefix(url, "git://")
  32. }
  33. // http[s]://xxx/user/repo
  34. if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
  35. return url
  36. }
  37. // Relative url prefix check (according to git submodule documentation)
  38. if strings.HasPrefix(url, "./") || strings.HasPrefix(url, "../") {
  39. // ...construct and return correct submodule url here...
  40. idx := strings.Index(parentPath, "/src/")
  41. if idx == -1 {
  42. return url
  43. }
  44. return strings.TrimSuffix(urlPrefix, "/") + parentPath[:idx] + "/" + url
  45. }
  46. // sysuser@xxx:user/repo
  47. i := strings.Index(url, "@")
  48. j := strings.LastIndex(url, ":")
  49. // Only process when i < j because git+ssh://git@git.forwardbias.in/npploader.git
  50. if i > -1 && j > -1 && i < j {
  51. // fix problem with reverse proxy works only with local server
  52. if strings.Contains(urlPrefix, url[i+1:j]) {
  53. return urlPrefix + url[j+1:]
  54. } else {
  55. return "http://" + url[i+1:j] + "/" + url[j+1:]
  56. }
  57. }
  58. return url
  59. }
  60. // RefID returns reference ID.
  61. func (sf *SubModuleFile) RefID() string {
  62. return sf.refID
  63. }