summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-08-08 17:10:34 +0800
committerUnknwon <u@gogs.io>2015-08-08 17:10:34 +0800
commit9db4acc62e2d253c21ce9aed2e21003ff235d5b5 (patch)
tree1718b390dce8fb2e0771f4033cb1f6b24d66f0b3 /models/repo.go
parente6862e9cec49acf1cf8bbb5d1baee299a52b4967 (diff)
downloadgitea-9db4acc62e2d253c21ce9aed2e21003ff235d5b5.tar.gz
gitea-9db4acc62e2d253c21ce9aed2e21003ff235d5b5.zip
improve fork process
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go129
1 files changed, 50 insertions, 79 deletions
diff --git a/models/repo.go b/models/repo.go
index 73807a2f65..910ccee38c 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -21,6 +21,7 @@ import (
"github.com/Unknwon/cae/zip"
"github.com/Unknwon/com"
+ "github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/bindata"
@@ -35,7 +36,6 @@ const (
)
var (
- ErrRepoAlreadyExist = errors.New("Repository already exist")
ErrRepoFileNotExist = errors.New("Repository file does not exist")
ErrRepoFileNotLoaded = errors.New("Repository file not loaded")
ErrMirrorNotExist = errors.New("Mirror does not exist")
@@ -222,15 +222,19 @@ func (repo *Repository) DescriptionHtml() template.HTML {
return template.HTML(DescPattern.ReplaceAllStringFunc(base.Sanitizer.Sanitize(repo.Description), sanitize))
}
-// IsRepositoryExist returns true if the repository with given name under user has already existed.
-func IsRepositoryExist(u *User, repoName string) (bool, error) {
- has, err := x.Get(&Repository{
+func isRepositoryExist(e Engine, u *User, repoName string) (bool, error) {
+ has, err := e.Get(&Repository{
OwnerId: u.Id,
LowerName: strings.ToLower(repoName),
})
return has && com.IsDir(RepoPath(u.Name, repoName)), err
}
+// IsRepositoryExist returns true if the repository with given name under user has already existed.
+func IsRepositoryExist(u *User, repoName string) (bool, error) {
+ return isRepositoryExist(x, u, repoName)
+}
+
// CloneLink represents different types of clone URLs of repository.
type CloneLink struct {
SSH string
@@ -525,19 +529,50 @@ func initRepository(e Engine, repoPath string, u *User, repo *Repository, initRe
return initRepoCommit(tmpDir, u.NewGitSig())
}
-// CreateRepository creates a repository for given user or organization.
-func CreateRepository(u *User, name, desc, lang, license string, isPrivate, isMirror, initReadme bool) (_ *Repository, err error) {
- if err = IsUsableName(name); err != nil {
- return nil, err
+func createRepository(e *xorm.Session, u *User, repo *Repository) (err error) {
+ if err = IsUsableName(repo.Name); err != nil {
+ return err
}
- has, err := IsRepositoryExist(u, name)
+ has, err := isRepositoryExist(e, u, repo.Name)
if err != nil {
- return nil, fmt.Errorf("IsRepositoryExist: %v", err)
+ return fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
- return nil, ErrRepoAlreadyExist
+ return ErrRepoAlreadyExist{u.Name, repo.Name}
}
+ if _, err = e.Insert(repo); err != nil {
+ return err
+ } else if _, err = e.Exec("UPDATE `user` SET num_repos=num_repos+1 WHERE id=?", u.Id); err != nil {
+ return err
+ }
+
+ // Give access to all members in owner team.
+ if u.IsOrganization() {
+ t, err := u.getOwnerTeam(e)
+ if err != nil {
+ return fmt.Errorf("getOwnerTeam: %v", err)
+ } else if err = t.addRepository(e, repo); err != nil {
+ return fmt.Errorf("addRepository: %v", err)
+ }
+ } else {
+ // Organization automatically called this in addRepository method.
+ if err = repo.recalculateAccesses(e); err != nil {
+ return fmt.Errorf("recalculateAccesses: %v", err)
+ }
+ }
+
+ if err = watchRepo(e, u.Id, repo.Id, true); err != nil {
+ return fmt.Errorf("watchRepo: %v", err)
+ } else if err = newRepoAction(e, u, repo); err != nil {
+ return fmt.Errorf("newRepoAction: %v", err)
+ }
+
+ return nil
+}
+
+// CreateRepository creates a repository for given user or organization.
+func CreateRepository(u *User, name, desc, lang, license string, isPrivate, isMirror, initReadme bool) (_ *Repository, err error) {
repo := &Repository{
OwnerId: u.Id,
Owner: u,
@@ -553,35 +588,10 @@ func CreateRepository(u *User, name, desc, lang, license string, isPrivate, isMi
return nil, err
}
- if _, err = sess.Insert(repo); err != nil {
- return nil, err
- } else if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil {
+ if err = createRepository(sess, u, repo); err != nil {
return nil, err
}
- // TODO fix code for mirrors?
-
- // Give access to all members in owner team.
- if u.IsOrganization() {
- t, err := u.getOwnerTeam(sess)
- if err != nil {
- return nil, fmt.Errorf("getOwnerTeam: %v", err)
- } else if err = t.addRepository(sess, repo); err != nil {
- return nil, fmt.Errorf("addRepository: %v", err)
- }
- } else {
- // Organization called this in addRepository method.
- if err = repo.recalculateAccesses(sess); err != nil {
- return nil, fmt.Errorf("recalculateAccesses: %v", err)
- }
- }
-
- if err = watchRepo(sess, u.Id, repo.Id, true); err != nil {
- return nil, fmt.Errorf("watchRepo: %v", err)
- } else if err = newRepoAction(sess, u, repo); err != nil {
- return nil, fmt.Errorf("newRepoAction: %v", err)
- }
-
// No need for init mirror.
if !isMirror {
repoPath := RepoPath(u.Name, repo.Name)
@@ -649,7 +659,7 @@ func TransferOwnership(u *User, newOwnerName string, repo *Repository) error {
if err != nil {
return fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
- return ErrRepoAlreadyExist
+ return ErrRepoAlreadyExist{newOwnerName, repo.Name}
}
sess := x.NewSession()
@@ -767,7 +777,7 @@ func ChangeRepositoryName(u *User, oldRepoName, newRepoName string) (err error)
if err != nil {
return fmt.Errorf("IsRepositoryExist: %v", err)
} else if has {
- return ErrRepoAlreadyExist
+ return ErrRepoAlreadyExist{u.Name, newRepoName}
}
// Change repository directory name.
@@ -1412,21 +1422,6 @@ func IsStaring(uid, repoId int64) bool {
// \/ \/
func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
- has, err := IsRepositoryExist(u, name)
- if err != nil {
- return nil, fmt.Errorf("IsRepositoryExist: %v", err)
- } else if has {
- return nil, ErrRepoAlreadyExist
- }
-
- // In case the old repository is a fork.
- if oldRepo.IsFork {
- oldRepo, err = GetRepositoryById(oldRepo.ForkId)
- if err != nil {
- return nil, err
- }
- }
-
repo := &Repository{
OwnerId: u.Id,
Owner: u,
@@ -1444,34 +1439,10 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (_ *Reposit
return nil, err
}
- if _, err = sess.Insert(repo); err != nil {
+ if err = createRepository(sess, u, repo); err != nil {
return nil, err
}
- if err = repo.recalculateAccesses(sess); err != nil {
- return nil, err
- } else if _, err = sess.Exec("UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil {
- return nil, err
- }
-
- if u.IsOrganization() {
- // Update owner team info and count.
- t, err := u.getOwnerTeam(sess)
- if err != nil {
- return nil, fmt.Errorf("getOwnerTeam: %v", err)
- } else if err = t.addRepository(sess, repo); err != nil {
- return nil, fmt.Errorf("addRepository: %v", err)
- }
- } else {
- if err = watchRepo(sess, u.Id, repo.Id, true); err != nil {
- return nil, fmt.Errorf("watchRepo: %v", err)
- }
- }
-
- if err = newRepoAction(sess, u, repo); err != nil {
- return nil, fmt.Errorf("newRepoAction: %v", err)
- }
-
if _, err = sess.Exec("UPDATE `repository` SET num_forks=num_forks+1 WHERE id=?", oldRepo.Id); err != nil {
return nil, err
}