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.

79 lines
1.9 KiB

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