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.

62 lines
1.2 KiB

  1. // Copyright 2014 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 (
  6. "strings"
  7. )
  8. type SubModule struct {
  9. Name string
  10. Url string
  11. }
  12. // SubModuleFile represents a file with submodule type.
  13. type SubModuleFile struct {
  14. *Commit
  15. refUrl string
  16. refId string
  17. }
  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() 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. // sysuser@xxx:user/repo
  40. i := strings.Index(url, "@")
  41. j := strings.LastIndex(url, ":")
  42. if i > -1 && j > -1 {
  43. return "http://" + url[i+1:j] + "/" + url[j+1:]
  44. }
  45. return url
  46. }
  47. // RefId returns reference ID.
  48. func (sf *SubModuleFile) RefId() string {
  49. return sf.refId
  50. }