models.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. "github.com/lunny/xorm"
  9. git "github.com/libgit2/git2go"
  10. )
  11. const (
  12. UserRepo = iota
  13. OrgRepo
  14. )
  15. type User struct {
  16. Id int64
  17. Name string `xorm:"unique"`
  18. }
  19. type Org struct {
  20. Id int64
  21. }
  22. type Repo struct {
  23. Id int64
  24. OwnerId int64 `xorm:"unique(s)"`
  25. Type int `xorm:"unique(s)"`
  26. Name string `xorm:"unique(s)"`
  27. }
  28. var (
  29. orm *xorm.Engine
  30. )
  31. //
  32. // create a repository for a user or orgnaziation
  33. //
  34. func CreateUserRepository(root string, user *User, reposName string) error {
  35. p := filepath.Join(root, user.Name)
  36. os.MkdirAll(p, os.ModePerm)
  37. f := filepath.Join(p, reposName)
  38. _, err := git.InitRepository(f, false)
  39. if err != nil {
  40. return err
  41. }
  42. repo := Repo{OwnerId: user.Id, Type: UserRepo, Name: reposName}
  43. _, err = orm.Insert(&repo)
  44. if err != nil {
  45. os.RemoveAll(f)
  46. }
  47. return err
  48. }
  49. //
  50. // delete a repository for a user or orgnaztion
  51. //
  52. func DeleteUserRepository(root string, user *User, reposName string) error {
  53. err := os.RemoveAll(filepath.Join(root, user.Name, reposName))
  54. if err != nil {
  55. return err
  56. }
  57. _, err = orm.Delete(&Repo{OwnerId: user.Id, Type: UserRepo, Name: reposName})
  58. return err
  59. }