|
|
@ -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 |
|
|
|
} |
|
|
|
} |
|
|
|