1
0

repoutil.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package repoutil
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. "gogs.io/gogs/internal/conf"
  8. )
  9. // CloneLink represents different types of clone URLs of repository.
  10. type CloneLink struct {
  11. SSH string
  12. HTTPS string
  13. }
  14. // NewCloneLink returns clone URLs using given owner and repository name.
  15. func NewCloneLink(owner, repo string, isWiki bool) *CloneLink {
  16. if isWiki {
  17. repo += ".wiki"
  18. }
  19. cl := new(CloneLink)
  20. if conf.SSH.Port != 22 {
  21. cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", conf.App.RunUser, conf.SSH.Domain, conf.SSH.Port, owner, repo)
  22. } else {
  23. cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", conf.App.RunUser, conf.SSH.Domain, owner, repo)
  24. }
  25. cl.HTTPS = HTTPSCloneURL(owner, repo)
  26. return cl
  27. }
  28. // HTTPSCloneURL returns HTTPS clone URL using given owner and repository name.
  29. func HTTPSCloneURL(owner, repo string) string {
  30. return fmt.Sprintf("%s%s/%s.git", conf.Server.ExternalURL, owner, repo)
  31. }
  32. // HTMLURL returns HTML URL using given owner and repository name.
  33. func HTMLURL(owner, repo string) string {
  34. return conf.Server.ExternalURL + owner + "/" + repo
  35. }
  36. // CompareCommitsPath returns the comparison path using given owner, repository,
  37. // and commit IDs.
  38. func CompareCommitsPath(owner, repo, oldCommitID, newCommitID string) string {
  39. return fmt.Sprintf("%s/%s/compare/%s...%s", owner, repo, oldCommitID, newCommitID)
  40. }
  41. // UserPath returns the absolute path for storing user repositories.
  42. func UserPath(user string) string {
  43. return filepath.Join(conf.Repository.Root, strings.ToLower(user))
  44. }
  45. // RepositoryPath returns the absolute path using given user and repository
  46. // name.
  47. func RepositoryPath(owner, repo string) string {
  48. return filepath.Join(UserPath(owner), strings.ToLower(repo)+".git")
  49. }
  50. // RepositoryLocalPath returns the absolute path of the repository local copy
  51. // with the given ID.
  52. func RepositoryLocalPath(repoID int64) string {
  53. return filepath.Join(conf.Server.AppDataPath, "tmp", "local-repo", strconv.FormatInt(repoID, 10))
  54. }
  55. // RepositoryLocalWikiPath returns the absolute path of the repository local
  56. // wiki copy with the given ID.
  57. func RepositoryLocalWikiPath(repoID int64) string {
  58. return filepath.Join(conf.Server.AppDataPath, "tmp", "local-wiki", strconv.FormatInt(repoID, 10))
  59. }