summaryrefslogtreecommitdiffstats
path: root/models/models.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-02-13 23:23:23 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-02-13 23:23:23 +0800
commita30e5bcaf83a82f5f7d1c89a6f9f7e52036d74af (patch)
tree287c8f68b2b13f8a780f5b2184685d852388c9d1 /models/models.go
parentb5ba3878913705cd75cef975e7b1a63c351ab86f (diff)
downloadgitea-a30e5bcaf83a82f5f7d1c89a6f9f7e52036d74af.tar.gz
gitea-a30e5bcaf83a82f5f7d1c89a6f9f7e52036d74af.zip
add createrepository & deleterepository
Diffstat (limited to 'models/models.go')
-rw-r--r--models/models.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/models/models.go b/models/models.go
index 20fc0eb096..d5a8c8cf5c 100644
--- a/models/models.go
+++ b/models/models.go
@@ -3,3 +3,69 @@
// license that can be found in the LICENSE file.
package models
+
+import (
+ "os"
+ "path/filepath"
+ "github.com/lunny/xorm"
+
+ git "github.com/libgit2/git2go"
+)
+
+const (
+ UserRepo = iota
+ OrgRepo
+)
+
+type User struct {
+ Id int64
+ Name string `xorm:"unique"`
+}
+
+type Org struct {
+ Id int64
+}
+
+type Repo struct {
+ Id int64
+ OwnerId int64 `xorm:"unique(s)"`
+ Type int `xorm:"unique(s)"`
+ Name string `xorm:"unique(s)"`
+}
+
+var (
+ orm *xorm.Engine
+)
+
+//
+// create a repository for a user or orgnaziation
+//
+func CreateUserRepository(root string, user *User, reposName string) error {
+ p := filepath.Join(root, user.Name)
+ os.MkdirAll(p, os.ModePerm)
+ f := filepath.Join(p, reposName)
+ _, err := git.InitRepository(f, false)
+ if err != nil {
+ return err
+ }
+
+ repo := Repo{OwnerId: user.Id, Type: UserRepo, Name: reposName}
+ _, err = orm.Insert(&repo)
+ if err != nil {
+ os.RemoveAll(f)
+ }
+ return err
+}
+
+//
+// delete a repository for a user or orgnaztion
+//
+func DeleteUserRepository(root string, user *User, reposName string) error {
+ err := os.RemoveAll(filepath.Join(root, user.Name, reposName))
+ if err != nil {
+ return err
+ }
+
+ _, err = orm.Delete(&Repo{OwnerId: user.Id, Type: UserRepo, Name: reposName})
+ return err
+}