You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

url.go 796 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package repository
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/setting"
  5. "net/url"
  6. "strings"
  7. )
  8. func FindRepoByUrl(url string) (*models.Repository, error) {
  9. ownerName, repoName := parseOpenIUrl(url)
  10. if ownerName == "" || repoName == "" {
  11. return nil, nil
  12. }
  13. r, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
  14. if err != nil {
  15. return nil, err
  16. }
  17. return r, nil
  18. }
  19. //parseOpenIUrl parse openI repo url,return ownerName and repoName
  20. func parseOpenIUrl(u string) (string, string) {
  21. url, err := url.Parse(u)
  22. if err != nil {
  23. return "", ""
  24. }
  25. if url.Host != setting.AppURL {
  26. return "", ""
  27. }
  28. array := strings.Split(url.Path, "/")
  29. if len(array) < 3 {
  30. return "", ""
  31. }
  32. ownerName := array[1]
  33. repoName := array[2]
  34. return ownerName, repoName
  35. }