repo.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // TODO: get repository by nocase name
  28. return orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
  29. }
  30. //
  31. // create a repository for a user or orgnaziation
  32. //
  33. func CreateRepository(user *User, reposName string) (*Repo, error) {
  34. p := filepath.Join(repoRootPath, user.Name)
  35. os.MkdirAll(p, os.ModePerm)
  36. f := filepath.Join(p, reposName+".git")
  37. _, err := git.InitRepository(f, false)
  38. if err != nil {
  39. return nil, err
  40. }
  41. repo := Repo{OwnerId: user.Id, Name: reposName}
  42. session := orm.NewSession()
  43. defer session.Close()
  44. session.Begin()
  45. _, err = session.Insert(&repo)
  46. if err != nil {
  47. os.RemoveAll(f)
  48. session.Rollback()
  49. return nil, err
  50. }
  51. _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
  52. if err != nil {
  53. os.RemoveAll(f)
  54. session.Rollback()
  55. return nil, err
  56. }
  57. err = session.Commit()
  58. if err != nil {
  59. os.RemoveAll(f)
  60. session.Rollback()
  61. return nil, err
  62. }
  63. return &repo, nil
  64. }
  65. // list one user's repository
  66. func GetRepositories(user *User) ([]Repo, error) {
  67. repos := make([]Repo, 0)
  68. err := orm.Find(&repos, &Repo{OwnerId: user.Id})
  69. return repos, err
  70. }
  71. func StarReposiory(user *User, repoName string) error {
  72. return nil
  73. }
  74. func UnStarRepository() {
  75. }
  76. func WatchRepository() {
  77. }
  78. func UnWatchRepository() {
  79. }
  80. // DeleteRepository deletes a repository for a user or orgnaztion.
  81. func DeleteRepository(user *User, reposName string) (err error) {
  82. session := orm.NewSession()
  83. if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); err != nil {
  84. session.Rollback()
  85. return err
  86. }
  87. if _, err = session.Exec("update user set num_repos = num_repos - 1 where id = ?", user.Id); err != nil {
  88. session.Rollback()
  89. return err
  90. }
  91. if err = session.Commit(); err != nil {
  92. session.Rollback()
  93. return err
  94. }
  95. if err = os.RemoveAll(filepath.Join(repoRootPath, user.Name, reposName+".git")); err != nil {
  96. // TODO: log and delete manully
  97. return err
  98. }
  99. return nil
  100. }