repoutil.go 2.4 KB

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