From a30e5bcaf83a82f5f7d1c89a6f9f7e52036d74af Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 13 Feb 2014 23:23:23 +0800 Subject: [PATCH] add createrepository & deleterepository --- models/models.go | 66 +++++++++++++++++++++++++++++++++++++++++++ models/models_test.go | 30 ++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 models/models_test.go diff --git a/models/models.go b/models/models.go index 20fc0eb09..d5a8c8cf5 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 +} diff --git a/models/models_test.go b/models/models_test.go new file mode 100644 index 000000000..80653d57f --- /dev/null +++ b/models/models_test.go @@ -0,0 +1,30 @@ +package models + +import ( + "fmt" + "testing" + + "github.com/lunny/xorm" + _ "github.com/mattn/go-sqlite3" +) + +func init() { + var err error + orm, err = xorm.NewEngine("sqlite3", "./test.db") + if err != nil { + fmt.Println(err) + } + + err = orm.Sync(&User{}, &Org{}, &Repo{}) + if err != nil { + fmt.Println(err) + } +} + +func TestCreateRepository(t *testing.T) { + user := User{Id: 1} + err := CreateUserRepository("test", &user, "test") + if err != nil { + t.Error(err) + } +}