summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUnknown <joe2010xtmf@163.com>2014-03-09 20:06:29 -0400
committerUnknown <joe2010xtmf@163.com>2014-03-09 20:06:29 -0400
commit0d9b2f38600325e3a8d87526061efbe9e3b78b91 (patch)
tree3a4be95f75d5ef206be5987f430b8f52ec768df2
parent76ce6f98487b3754992da18969df37560e49ee82 (diff)
downloadgitea-0d9b2f38600325e3a8d87526061efbe9e3b78b91.tar.gz
gitea-0d9b2f38600325e3a8d87526061efbe9e3b78b91.zip
Update CreateRepository
-rw-r--r--gogs.go2
-rw-r--r--models/models.go2
-rw-r--r--models/repo.go107
-rw-r--r--routers/repo/repo.go20
-rw-r--r--serve.go2
5 files changed, 81 insertions, 52 deletions
diff --git a/gogs.go b/gogs.go
index 1e689e83b5..a63cc571e3 100644
--- a/gogs.go
+++ b/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())
diff --git a/models/models.go b/models/models.go
index e0ab3ca15a..7dfc0aaf0a 100644
--- a/models/models.go
+++ b/models/models.go
@@ -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)
diff --git a/models/repo.go b/models/repo.go
index 22b98e7975..05612a4229 100644
--- a/models/repo.go
+++ b/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
}
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index 834195d83f..7353436a5f 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -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)
diff --git a/serve.go b/serve.go
index 5bf44f4540..0c0584cd91 100644
--- a/serve.go
+++ b/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