repoutil.go 1.8 KB

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