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.

158 lines
3.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 models
  5. import (
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "github.com/gogits/gogs/utils/log"
  11. git "github.com/libgit2/git2go"
  12. )
  13. type Repo struct {
  14. Id int64
  15. OwnerId int64 `xorm:"unique(s)"`
  16. ForkId int64
  17. LowerName string `xorm:"unique(s) index not null"`
  18. Name string `xorm:"index not null"`
  19. NumWatchs int
  20. NumStars int
  21. NumForks int
  22. Created time.Time `xorm:"created"`
  23. Updated time.Time `xorm:"updated"`
  24. }
  25. type Star struct {
  26. Id int64
  27. RepoId int64
  28. UserId int64
  29. Created time.Time `xorm:"created"`
  30. }
  31. // check if repository is exist
  32. func IsRepositoryExist(user *User, reposName string) (bool, error) {
  33. repo := Repo{OwnerId: user.Id}
  34. has, err := orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
  35. if err != nil {
  36. return has, err
  37. }
  38. s, err := os.Stat(RepoPath(user.Name, reposName))
  39. if err != nil {
  40. return false, nil
  41. }
  42. return s.IsDir(), nil
  43. }
  44. // CreateRepository creates a repository for given user or orgnaziation.
  45. func CreateRepository(user *User, reposName string) (*Repo, error) {
  46. f := RepoPath(user.Name, reposName)
  47. _, err := git.InitRepository(f, true)
  48. if err != nil {
  49. return nil, err
  50. }
  51. repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)}
  52. session := orm.NewSession()
  53. defer session.Close()
  54. session.Begin()
  55. _, err = session.Insert(&repo)
  56. if err != nil {
  57. err2 := os.RemoveAll(f)
  58. if err2 != nil {
  59. log.Error("delete repo directory %s/%s failed", user.Name, reposName)
  60. }
  61. session.Rollback()
  62. return nil, err
  63. }
  64. access := Access{UserName: user.Name,
  65. RepoName: repo.Name,
  66. Mode: AU_WRITABLE,
  67. }
  68. _, err = session.Insert(&access)
  69. if err != nil {
  70. err2 := os.RemoveAll(f)
  71. if err2 != nil {
  72. log.Error("delete repo directory %s/%s failed", user.Name, reposName)
  73. }
  74. session.Rollback()
  75. return nil, err
  76. }
  77. _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
  78. if err != nil {
  79. err2 := os.RemoveAll(f)
  80. if err2 != nil {
  81. log.Error("delete repo directory %s/%s failed", user.Name, reposName)
  82. }
  83. session.Rollback()
  84. return nil, err
  85. }
  86. err = session.Commit()
  87. if err != nil {
  88. err2 := os.RemoveAll(f)
  89. if err2 != nil {
  90. log.Error("delete repo directory %s/%s failed", user.Name, reposName)
  91. }
  92. session.Rollback()
  93. return nil, err
  94. }
  95. return &repo, nil
  96. }
  97. // GetRepositories returns the list of repositories of given user.
  98. func GetRepositories(user *User) ([]Repo, error) {
  99. repos := make([]Repo, 0)
  100. err := orm.Find(&repos, &Repo{OwnerId: user.Id})
  101. return repos, err
  102. }
  103. func StarReposiory(user *User, repoName string) error {
  104. return nil
  105. }
  106. func UnStarRepository() {
  107. }
  108. func WatchRepository() {
  109. }
  110. func UnWatchRepository() {
  111. }
  112. func ForkRepository(reposName string, userId int64) {
  113. }
  114. func RepoPath(userName, repoName string) string {
  115. return filepath.Join(UserPath(userName), repoName+".git")
  116. }
  117. // DeleteRepository deletes a repository for a user or orgnaztion.
  118. func DeleteRepository(user *User, reposName string) (err error) {
  119. session := orm.NewSession()
  120. if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); err != nil {
  121. session.Rollback()
  122. return err
  123. }
  124. if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil {
  125. session.Rollback()
  126. return err
  127. }
  128. if err = session.Commit(); err != nil {
  129. session.Rollback()
  130. return err
  131. }
  132. if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
  133. // TODO: log and delete manully
  134. log.Error("delete repo %s/%s failed", user.Name, reposName)
  135. return err
  136. }
  137. return nil
  138. }