Update CreateRepository
This commit is contained in:
parent
76ce6f9848
commit
0d9b2f3860
2
gogs.go
2
gogs.go
|
@ -19,7 +19,7 @@ import (
|
|||
// Test that go1.1 tag above is included in builds. main.go refers to this definition.
|
||||
const go11tag = true
|
||||
|
||||
const APP_VER = "0.0.2.0308"
|
||||
const APP_VER = "0.0.2.0309"
|
||||
|
||||
func init() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
|
|
@ -90,7 +90,7 @@ func setEngine() {
|
|||
|
||||
func init() {
|
||||
setEngine()
|
||||
err := orm.Sync(new(User), new(PublicKey), new(Repo), new(Access))
|
||||
err := orm.Sync(new(User), new(PublicKey), new(Repository), new(Access))
|
||||
if err != nil {
|
||||
fmt.Printf("sync database struct error: %s\n", err)
|
||||
os.Exit(2)
|
||||
|
|
107
models/repo.go
107
models/repo.go
|
@ -5,6 +5,7 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -15,17 +16,19 @@ import (
|
|||
"github.com/gogits/gogs/modules/log"
|
||||
)
|
||||
|
||||
type Repo struct {
|
||||
Id int64
|
||||
OwnerId int64 `xorm:"unique(s)"`
|
||||
ForkId int64
|
||||
LowerName string `xorm:"unique(s) index not null"`
|
||||
Name string `xorm:"index not null"`
|
||||
NumWatchs int
|
||||
NumStars int
|
||||
NumForks int
|
||||
Created time.Time `xorm:"created"`
|
||||
Updated time.Time `xorm:"updated"`
|
||||
type Repository struct {
|
||||
Id int64
|
||||
OwnerId int64 `xorm:"unique(s)"`
|
||||
ForkId int64
|
||||
LowerName string `xorm:"unique(s) index not null"`
|
||||
Name string `xorm:"index not null"`
|
||||
Description string
|
||||
Private bool
|
||||
NumWatchs int
|
||||
NumStars int
|
||||
NumForks int
|
||||
Created time.Time `xorm:"created"`
|
||||
Updated time.Time `xorm:"updated"`
|
||||
}
|
||||
|
||||
type Star struct {
|
||||
|
@ -35,14 +38,18 @@ type Star struct {
|
|||
Created time.Time `xorm:"created"`
|
||||
}
|
||||
|
||||
var (
|
||||
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
||||
)
|
||||
|
||||
// check if repository is exist
|
||||
func IsRepositoryExist(user *User, reposName string) (bool, error) {
|
||||
repo := Repo{OwnerId: user.Id}
|
||||
has, err := orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
|
||||
func IsRepositoryExist(user *User, repoName string) (bool, error) {
|
||||
repo := Repository{OwnerId: user.Id}
|
||||
has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo)
|
||||
if err != nil {
|
||||
return has, err
|
||||
}
|
||||
s, err := os.Stat(RepoPath(user.Name, reposName))
|
||||
s, err := os.Stat(RepoPath(user.Name, repoName))
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
@ -50,64 +57,74 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
|
|||
}
|
||||
|
||||
// CreateRepository creates a repository for given user or orgnaziation.
|
||||
func CreateRepository(user *User, reposName string) (*Repo, error) {
|
||||
f := RepoPath(user.Name, reposName)
|
||||
_, err := git.InitRepository(f, true)
|
||||
func CreateRepository(user *User, repoName, desc string, private bool) (*Repository, error) {
|
||||
isExist, err := IsRepositoryExist(user, repoName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if isExist {
|
||||
return nil, ErrRepoAlreadyExist
|
||||
}
|
||||
|
||||
f := RepoPath(user.Name, repoName)
|
||||
if _, err = git.InitRepository(f, true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
repo := Repo{OwnerId: user.Id, Name: reposName, LowerName: strings.ToLower(reposName)}
|
||||
repo := &Repository{
|
||||
OwnerId: user.Id,
|
||||
Name: repoName,
|
||||
LowerName: strings.ToLower(repoName),
|
||||
Description: desc,
|
||||
Private: private,
|
||||
}
|
||||
session := orm.NewSession()
|
||||
defer session.Close()
|
||||
session.Begin()
|
||||
_, err = session.Insert(&repo)
|
||||
if err != nil {
|
||||
err2 := os.RemoveAll(f)
|
||||
if err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||
|
||||
if _, err = session.Insert(repo); err != nil {
|
||||
if err2 := os.RemoveAll(f); err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||
}
|
||||
session.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
access := Access{UserName: user.Name,
|
||||
|
||||
// TODO: RemoveAll may fail due to not root access.
|
||||
access := Access{
|
||||
UserName: user.Name,
|
||||
RepoName: repo.Name,
|
||||
Mode: AU_WRITABLE,
|
||||
}
|
||||
_, err = session.Insert(&access)
|
||||
if err != nil {
|
||||
err2 := os.RemoveAll(f)
|
||||
if err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||
if _, err = session.Insert(&access); err != nil {
|
||||
if err2 := os.RemoveAll(f); err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||
}
|
||||
session.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
|
||||
if err != nil {
|
||||
err2 := os.RemoveAll(f)
|
||||
if err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||
|
||||
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
|
||||
if err2 := os.RemoveAll(f); err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||
}
|
||||
session.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
err = session.Commit()
|
||||
if err != nil {
|
||||
err2 := os.RemoveAll(f)
|
||||
if err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
||||
|
||||
if err = session.Commit(); err != nil {
|
||||
if err2 := os.RemoveAll(f); err2 != nil {
|
||||
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||
}
|
||||
session.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
return &repo, nil
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
// GetRepositories returns the list of repositories of given user.
|
||||
func GetRepositories(user *User) ([]Repo, error) {
|
||||
repos := make([]Repo, 0)
|
||||
err := orm.Find(&repos, &Repo{OwnerId: user.Id})
|
||||
func GetRepositories(user *User) ([]Repository, error) {
|
||||
repos := make([]Repository, 0, 10)
|
||||
err := orm.Find(&repos, &Repository{OwnerId: user.Id})
|
||||
return repos, err
|
||||
}
|
||||
|
||||
|
@ -138,7 +155,7 @@ func RepoPath(userName, repoName string) string {
|
|||
// DeleteRepository deletes a repository for a user or orgnaztion.
|
||||
func DeleteRepository(user *User, reposName string) (err error) {
|
||||
session := orm.NewSession()
|
||||
if _, err = session.Delete(&Repo{OwnerId: user.Id, Name: reposName}); err != nil {
|
||||
if _, err = session.Delete(&Repository{OwnerId: user.Id, Name: reposName}); err != nil {
|
||||
session.Rollback()
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -42,17 +42,29 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
|
|||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(models.RepoPath(user.Name, form.RepoName))
|
||||
if err == nil {
|
||||
// TODO: init description and readme
|
||||
if _, err = models.CreateRepository(user, form.RepoName); err == nil {
|
||||
if _, err = models.CreateRepository(user,
|
||||
form.RepoName, form.Description, form.Visibility == "private"); err == nil {
|
||||
// Initialize README.
|
||||
if form.InitReadme == "true" {
|
||||
// TODO
|
||||
}
|
||||
// TODO: init .gitignore file
|
||||
data["RepoName"] = user.Name + "/" + form.RepoName
|
||||
r.HTML(200, "repo/created", data)
|
||||
fmt.Println("good!!!!")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err.Error() == models.ErrRepoAlreadyExist.Error() {
|
||||
data["HasError"] = true
|
||||
data["ErrorMsg"] = "Repository name has already been used"
|
||||
auth.AssignForm(form, data)
|
||||
r.HTML(200, "repo/create", data)
|
||||
return
|
||||
}
|
||||
|
||||
data["ErrorMsg"] = err
|
||||
log.Error("repo.Create: %v", err)
|
||||
r.HTML(200, "base/error", data)
|
||||
|
|
2
serve.go
2
serve.go
|
@ -123,7 +123,7 @@ func runServ(*cli.Context) {
|
|||
println("Repository", user.Name+"/"+repoName, "is not exist")
|
||||
return
|
||||
} else if isWrite {
|
||||
_, err := models.CreateRepository(user, repoName)
|
||||
_, err := models.CreateRepository(user, repoName, "", false)
|
||||
if err != nil {
|
||||
println("Create repository failed")
|
||||
return
|
||||
|
|
Loading…
Reference in a new issue