mirror of
https://github.com/go-gitea/gitea
synced 2024-12-22 12:37:53 +01:00
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.
|
// Test that go1.1 tag above is included in builds. main.go refers to this definition.
|
||||||
const go11tag = true
|
const go11tag = true
|
||||||
|
|
||||||
const APP_VER = "0.0.2.0308"
|
const APP_VER = "0.0.2.0309"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
|
@ -90,7 +90,7 @@ func setEngine() {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
setEngine()
|
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 {
|
if err != nil {
|
||||||
fmt.Printf("sync database struct error: %s\n", err)
|
fmt.Printf("sync database struct error: %s\n", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
|
107
models/repo.go
107
models/repo.go
@ -5,6 +5,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -15,17 +16,19 @@ import (
|
|||||||
"github.com/gogits/gogs/modules/log"
|
"github.com/gogits/gogs/modules/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repo struct {
|
type Repository struct {
|
||||||
Id int64
|
Id int64
|
||||||
OwnerId int64 `xorm:"unique(s)"`
|
OwnerId int64 `xorm:"unique(s)"`
|
||||||
ForkId int64
|
ForkId int64
|
||||||
LowerName string `xorm:"unique(s) index not null"`
|
LowerName string `xorm:"unique(s) index not null"`
|
||||||
Name string `xorm:"index not null"`
|
Name string `xorm:"index not null"`
|
||||||
NumWatchs int
|
Description string
|
||||||
NumStars int
|
Private bool
|
||||||
NumForks int
|
NumWatchs int
|
||||||
Created time.Time `xorm:"created"`
|
NumStars int
|
||||||
Updated time.Time `xorm:"updated"`
|
NumForks int
|
||||||
|
Created time.Time `xorm:"created"`
|
||||||
|
Updated time.Time `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Star struct {
|
type Star struct {
|
||||||
@ -35,14 +38,18 @@ type Star struct {
|
|||||||
Created time.Time `xorm:"created"`
|
Created time.Time `xorm:"created"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrRepoAlreadyExist = errors.New("Repository already exist")
|
||||||
|
)
|
||||||
|
|
||||||
// check if repository is exist
|
// check if repository is exist
|
||||||
func IsRepositoryExist(user *User, reposName string) (bool, error) {
|
func IsRepositoryExist(user *User, repoName string) (bool, error) {
|
||||||
repo := Repo{OwnerId: user.Id}
|
repo := Repository{OwnerId: user.Id}
|
||||||
has, err := orm.Where("lower_name = ?", strings.ToLower(reposName)).Get(&repo)
|
has, err := orm.Where("lower_name = ?", strings.ToLower(repoName)).Get(&repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return has, err
|
return has, err
|
||||||
}
|
}
|
||||||
s, err := os.Stat(RepoPath(user.Name, reposName))
|
s, err := os.Stat(RepoPath(user.Name, repoName))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, 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.
|
// CreateRepository creates a repository for given user or orgnaziation.
|
||||||
func CreateRepository(user *User, reposName string) (*Repo, error) {
|
func CreateRepository(user *User, repoName, desc string, private bool) (*Repository, error) {
|
||||||
f := RepoPath(user.Name, reposName)
|
isExist, err := IsRepositoryExist(user, repoName)
|
||||||
_, err := git.InitRepository(f, true)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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()
|
session := orm.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
session.Begin()
|
session.Begin()
|
||||||
_, err = session.Insert(&repo)
|
|
||||||
if err != nil {
|
if _, err = session.Insert(repo); err != nil {
|
||||||
err2 := os.RemoveAll(f)
|
if err2 := os.RemoveAll(f); err2 != nil {
|
||||||
if err2 != nil {
|
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
||||||
}
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
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,
|
RepoName: repo.Name,
|
||||||
Mode: AU_WRITABLE,
|
Mode: AU_WRITABLE,
|
||||||
}
|
}
|
||||||
_, err = session.Insert(&access)
|
if _, err = session.Insert(&access); err != nil {
|
||||||
if err != nil {
|
if err2 := os.RemoveAll(f); err2 != nil {
|
||||||
err2 := os.RemoveAll(f)
|
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||||
if err2 != nil {
|
|
||||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
||||||
}
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
|
|
||||||
if err != nil {
|
if _, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id); err != nil {
|
||||||
err2 := os.RemoveAll(f)
|
if err2 := os.RemoveAll(f); err2 != nil {
|
||||||
if err2 != nil {
|
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
||||||
}
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = session.Commit()
|
|
||||||
if err != nil {
|
if err = session.Commit(); err != nil {
|
||||||
err2 := os.RemoveAll(f)
|
if err2 := os.RemoveAll(f); err2 != nil {
|
||||||
if err2 != nil {
|
log.Error("delete repo directory %s/%s failed", user.Name, repoName)
|
||||||
log.Error("delete repo directory %s/%s failed", user.Name, reposName)
|
|
||||||
}
|
}
|
||||||
session.Rollback()
|
session.Rollback()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &repo, nil
|
return repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRepositories returns the list of repositories of given user.
|
// GetRepositories returns the list of repositories of given user.
|
||||||
func GetRepositories(user *User) ([]Repo, error) {
|
func GetRepositories(user *User) ([]Repository, error) {
|
||||||
repos := make([]Repo, 0)
|
repos := make([]Repository, 0, 10)
|
||||||
err := orm.Find(&repos, &Repo{OwnerId: user.Id})
|
err := orm.Find(&repos, &Repository{OwnerId: user.Id})
|
||||||
return repos, err
|
return repos, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +155,7 @@ func RepoPath(userName, repoName string) string {
|
|||||||
// DeleteRepository deletes a repository for a user or orgnaztion.
|
// DeleteRepository deletes a repository for a user or orgnaztion.
|
||||||
func DeleteRepository(user *User, reposName string) (err error) {
|
func DeleteRepository(user *User, reposName string) (err error) {
|
||||||
session := orm.NewSession()
|
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()
|
session.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -42,17 +42,29 @@ func Create(form auth.CreateRepoForm, req *http.Request, r render.Render, data b
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fmt.Println(models.RepoPath(user.Name, form.RepoName))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// TODO: init description and readme
|
if _, err = models.CreateRepository(user,
|
||||||
if _, err = models.CreateRepository(user, form.RepoName); err == nil {
|
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
|
data["RepoName"] = user.Name + "/" + form.RepoName
|
||||||
r.HTML(200, "repo/created", data)
|
r.HTML(200, "repo/created", data)
|
||||||
fmt.Println("good!!!!")
|
|
||||||
return
|
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
|
data["ErrorMsg"] = err
|
||||||
log.Error("repo.Create: %v", err)
|
log.Error("repo.Create: %v", err)
|
||||||
r.HTML(200, "base/error", data)
|
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")
|
println("Repository", user.Name+"/"+repoName, "is not exist")
|
||||||
return
|
return
|
||||||
} else if isWrite {
|
} else if isWrite {
|
||||||
_, err := models.CreateRepository(user, repoName)
|
_, err := models.CreateRepository(user, repoName, "", false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("Create repository failed")
|
println("Create repository failed")
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user