summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2014-02-20 14:53:56 +0800
committerLunny Xiao <xiaolunwen@gmail.com>2014-02-20 14:53:56 +0800
commitb1b6def5bcb016d555e8078af404e8e2fa948748 (patch)
treea45ee3b4130597ba02cc5f45a6251ad16e7b3495 /models
parentd4728fafafdd1d9a54d9337c7bddac086d58579c (diff)
downloadgitea-b1b6def5bcb016d555e8078af404e8e2fa948748.tar.gz
gitea-b1b6def5bcb016d555e8078af404e8e2fa948748.zip
improved ssh supports
Diffstat (limited to 'models')
-rw-r--r--models/models.go2
-rw-r--r--models/repo.go29
-rw-r--r--models/user.go23
3 files changed, 44 insertions, 10 deletions
diff --git a/models/models.go b/models/models.go
index 8341f99616..0f8fcac1a8 100644
--- a/models/models.go
+++ b/models/models.go
@@ -77,6 +77,8 @@ func setEngine() {
//x.ShowSQL = true
log.Trace("Initialized database -> %s", dbName)
+
+ RepoRootPath = utils.Cfg.MustValue("repository", "ROOT")
}
func init() {
diff --git a/models/repo.go b/models/repo.go
index d48c9a9787..27807303f9 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -11,6 +11,7 @@ import (
"time"
git "github.com/libgit2/git2go"
+ "github.com/qiniu/log"
)
type Repo struct {
@@ -35,7 +36,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
}
s, err := os.Stat(filepath.Join(RepoRootPath, user.Name, reposName))
if err != nil {
- return false, err
+ return false, nil
}
return s.IsDir(), nil
}
@@ -44,9 +45,7 @@ func IsRepositoryExist(user *User, reposName string) (bool, error) {
// create a repository for a user or orgnaziation
//
func CreateRepository(user *User, reposName string) (*Repo, error) {
- p := filepath.Join(RepoRootPath, user.Name)
- os.MkdirAll(p, os.ModePerm)
- f := filepath.Join(p, reposName+".git")
+ f := RepoPath(user.Name, reposName)
_, err := git.InitRepository(f, false)
if err != nil {
return nil, err
@@ -58,19 +57,28 @@ func CreateRepository(user *User, reposName string) (*Repo, error) {
session.Begin()
_, err = session.Insert(&repo)
if err != nil {
- os.RemoveAll(f)
+ err2 := os.RemoveAll(f)
+ if err2 != nil {
+ log.Error("delete repo directory %s/%s failed", user.Name, reposName)
+ }
session.Rollback()
return nil, err
}
_, err = session.Exec("update user set num_repos = num_repos + 1 where id = ?", user.Id)
if err != nil {
- os.RemoveAll(f)
+ err2 := os.RemoveAll(f)
+ if err2 != nil {
+ log.Error("delete repo directory %s/%s failed", user.Name, reposName)
+ }
session.Rollback()
return nil, err
}
err = session.Commit()
if err != nil {
- os.RemoveAll(f)
+ err2 := os.RemoveAll(f)
+ if err2 != nil {
+ log.Error("delete repo directory %s/%s failed", user.Name, reposName)
+ }
session.Rollback()
return nil, err
}
@@ -100,6 +108,10 @@ func UnWatchRepository() {
}
+func RepoPath(userName, repoName string) string {
+ return filepath.Join(UserPath(userName), repoName+".git")
+}
+
// DeleteRepository deletes a repository for a user or orgnaztion.
func DeleteRepository(user *User, reposName string) (err error) {
session := orm.NewSession()
@@ -115,8 +127,9 @@ func DeleteRepository(user *User, reposName string) (err error) {
session.Rollback()
return err
}
- if err = os.RemoveAll(filepath.Join(RepoRootPath, user.Name, reposName+".git")); err != nil {
+ if err = os.RemoveAll(RepoPath(user.Name, reposName)); err != nil {
// TODO: log and delete manully
+ log.Error("delete repo %s/%s failed", user.Name, reposName)
return err
}
return nil
diff --git a/models/user.go b/models/user.go
index 2527a19774..4618273b3d 100644
--- a/models/user.go
+++ b/models/user.go
@@ -7,12 +7,15 @@ package models
import (
"errors"
"fmt"
+ "os"
+ "path/filepath"
"strings"
"time"
"github.com/dchest/scrypt"
"github.com/gogits/gogs/utils"
+ "github.com/gogits/gogs/utils/log"
)
// User types.
@@ -96,10 +99,22 @@ func RegisterUser(user *User) (err error) {
user.LowerName = strings.ToLower(user.Name)
user.Avatar = utils.EncodeMd5(user.Email)
- user.Updated = time.Now()
user.EncodePasswd()
_, err = orm.Insert(user)
- return err
+ if err != nil {
+ return err
+ }
+
+ err = os.MkdirAll(UserPath(user.Name), os.ModePerm)
+ if err != nil {
+ _, err2 := orm.Id(user.Id).Delete(&User{})
+ if err2 != nil {
+ log.Error("create userpath %s failed and delete table record faild",
+ user.Name)
+ }
+ return err
+ }
+ return nil
}
// UpdateUser updates user's information.
@@ -129,6 +144,10 @@ func (user *User) EncodePasswd() error {
return err
}
+func UserPath(userName string) string {
+ return filepath.Join(RepoRootPath, userName)
+}
+
func GetUserByKeyId(keyId int64) (*User, error) {
user := new(User)
has, err := orm.Sql("select a.* from user as a, public_key as b where a.id = b.owner_id and b.id=?", keyId).Get(user)