repo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. git "github.com/libgit2/git2go"
  11. )
  12. type Repo struct {
  13. Id int64
  14. OwnerId int64 `xorm:"unique(s)"`
  15. ForkId int64
  16. LowerName string `xorm:"unique(s) index not null"`
  17. Name string `xorm:"index not null"`
  18. NumWatchs int
  19. NumStars int
  20. NumForks int
  21. Created time.Time `xorm:"created"`
  22. Updated time.Time `xorm:"updated"`
  23. }
  24. // check if repository is exist
  25. func IsRepositoryExist(user *User, reposName string) (bool, error) {
  26. repo := Repo{OwnerId: user.Id}
  27. has, err := orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
  28. if err != nil {
  29. return has, err
  30. }
  31. s, err := os.Stat(filepath.Join(RepoRootPath, user.Name, reposName))
  32. if err != nil {
  33. return false, err
  34. }
  35. return s.IsDir(), nil
  36. }
  37. //
  38. // create a repository for a user or orgnaziation
  39. //
  40. func CreateRepository(user *User, reposName string) (*Repo, error) {
  41. p := filepath.Join(RepoRootPath, user.Name)
  42. os.MkdirAll(p, os.ModePerm)
  43. f := filepath.Join(p, reposName+".git")
  44. _, err := git.InitRepository(f, false)
  45. if err != nil {
  46. return nil, err
  47. }
  48. repo := Repo{OwnerId: user.Id, Name: reposName}
  49. session := orm.NewSession()
  50. defer session.Close()
  51. session.Begin()
  52. _, err = session.Insert(&repo)
  53. if err != nil {
  54. os.RemoveAll(f)
  55. session.Rollback()
  56. return nil, err
  57. }
  58. _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
  59. if err != nil {
  60. os.RemoveAll(f)
  61. session.Rollback()
  62. return nil, err
  63. }
  64. err = session.Commit()
  65. if err != nil {
  66. os.RemoveAll(f)
  67. session.Rollback()
  68. return nil, err
  69. }
  70. return &repo, nil
  71. }
  72. // GetRepositories returns the list of repositories of given user.
  73. func GetRepositories(user *User) ([]Repo, error) {
  74. repos := make([]Repo, 0)
  75. err := orm.Find(&repos, &Repo{OwnerId: user.Id})
  76. return repos, err
  77. }
  78. func StarReposiory(user *User, repoName string) error {
  79. return nil
  80. }
  81. func UnStarRepository() {
  82. }
  83. func WatchRepository() {
  84. }
  85. func UnWatchRepository() {
  86. }
  87. // DeleteRepository deletes a repository for a user or orgnaztion.
  88. func DeleteRepository(user *User, reposName string) (err error) {
  89. session := orm.NewSession()
  90. if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); err != nil {
  91. session.Rollback()
  92. return err
  93. }
  94. if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil {
  95. session.Rollback()
  96. return err
  97. }
  98. if err = session.Commit(); err != nil {
  99. session.Rollback()
  100. return err
  101. }
  102. if err = os.RemoveAll(filepath.Join(RepoRootPath, user.Name, reposName+".git")); err != nil {
  103. // TODO: log and delete manully
  104. return err
  105. }
  106. return nil
  107. }