summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go83
1 files changed, 51 insertions, 32 deletions
diff --git a/models/repo.go b/models/repo.go
index 8db527477b..23b1c2ef52 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -126,6 +126,15 @@ func NewRepoContext() {
RemoveAllWithNotice("Clean up repository temporary data", filepath.Join(setting.AppDataPath, "tmp"))
}
+// RepositoryStatus defines the status of repository
+type RepositoryStatus int
+
+// all kinds of RepositoryStatus
+const (
+ RepositoryReady RepositoryStatus = iota // a normal repository
+ RepositoryBeingMigrated // repository is migrating
+)
+
// Repository represents a git repository.
type Repository struct {
ID int64 `xorm:"pk autoincr"`
@@ -156,9 +165,9 @@ type Repository struct {
IsPrivate bool `xorm:"INDEX"`
IsEmpty bool `xorm:"INDEX"`
IsArchived bool `xorm:"INDEX"`
-
- IsMirror bool `xorm:"INDEX"`
- *Mirror `xorm:"-"`
+ IsMirror bool `xorm:"INDEX"`
+ *Mirror `xorm:"-"`
+ Status RepositoryStatus `xorm:"NOT NULL DEFAULT 0"`
ExternalMetas map[string]string `xorm:"-"`
Units []*RepoUnit `xorm:"-"`
@@ -197,6 +206,16 @@ func (repo *Repository) ColorFormat(s fmt.State) {
repo.Name)
}
+// IsBeingMigrated indicates that repository is being migtated
+func (repo *Repository) IsBeingMigrated() bool {
+ return repo.Status == RepositoryBeingMigrated
+}
+
+// IsBeingCreated indicates that repository is being migrated or forked
+func (repo *Repository) IsBeingCreated() bool {
+ return repo.IsBeingMigrated()
+}
+
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (repo *Repository) AfterLoad() {
// FIXME: use models migration to solve all at once.
@@ -884,18 +903,6 @@ func (repo *Repository) CloneLink() (cl *CloneLink) {
return repo.cloneLink(x, false)
}
-// MigrateRepoOptions contains the repository migrate options
-type MigrateRepoOptions struct {
- Name string
- Description string
- OriginalURL string
- IsPrivate bool
- IsMirror bool
- RemoteAddr string
- Wiki bool // include wiki repository
- SyncReleasesWithTags bool // sync releases from tags
-}
-
/*
GitHub, GitLab, Gogs: *.wiki.git
BitBucket: *.git/wiki
@@ -915,20 +922,28 @@ func wikiRemoteURL(remote string) string {
return ""
}
-// MigrateRepository migrates an existing repository from other project hosting.
-func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, error) {
- repo, err := CreateRepository(doer, u, CreateRepoOptions{
- Name: opts.Name,
- Description: opts.Description,
- OriginalURL: opts.OriginalURL,
- IsPrivate: opts.IsPrivate,
- IsMirror: opts.IsMirror,
- })
+// CheckCreateRepository check if could created a repository
+func CheckCreateRepository(doer, u *User, name string) error {
+ if !doer.CanCreateRepo() {
+ return ErrReachLimitOfRepo{u.MaxRepoCreation}
+ }
+
+ if err := IsUsableRepoName(name); err != nil {
+ return err
+ }
+
+ has, err := isRepositoryExist(x, u, name)
if err != nil {
- return nil, err
+ return fmt.Errorf("IsRepositoryExist: %v", err)
+ } else if has {
+ return ErrRepoAlreadyExist{u.Name, name}
}
+ return nil
+}
- repoPath := RepoPath(u.Name, opts.Name)
+// MigrateRepositoryGitData starts migrating git related data after created migrating repository
+func MigrateRepositoryGitData(doer, u *User, repo *Repository, opts api.MigrateRepoOption) (*Repository, error) {
+ repoPath := RepoPath(u.Name, opts.RepoName)
if u.IsOrganization() {
t, err := u.GetOwnerTeam()
@@ -942,11 +957,12 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
migrateTimeout := time.Duration(setting.Git.Timeout.Migrate) * time.Second
- if err := os.RemoveAll(repoPath); err != nil {
+ var err error
+ if err = os.RemoveAll(repoPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %v", repoPath, err)
}
- if err = git.Clone(opts.RemoteAddr, repoPath, git.CloneRepoOptions{
+ if err = git.Clone(opts.CloneAddr, repoPath, git.CloneRepoOptions{
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
@@ -955,8 +971,8 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
}
if opts.Wiki {
- wikiPath := WikiPath(u.Name, opts.Name)
- wikiRemotePath := wikiRemoteURL(opts.RemoteAddr)
+ wikiPath := WikiPath(u.Name, opts.RepoName)
+ wikiRemotePath := wikiRemoteURL(opts.CloneAddr)
if len(wikiRemotePath) > 0 {
if err := os.RemoveAll(wikiPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %v", wikiPath, err)
@@ -986,7 +1002,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
return repo, fmt.Errorf("git.IsEmpty: %v", err)
}
- if opts.SyncReleasesWithTags && !repo.IsEmpty {
+ if !opts.Releases && !repo.IsEmpty {
// Try to get HEAD branch and set it as default branch.
headBranch, err := gitRepo.GetHEADBranch()
if err != nil {
@@ -1005,7 +1021,7 @@ func MigrateRepository(doer, u *User, opts MigrateRepoOptions) (*Repository, err
log.Error("Failed to update size for repository: %v", err)
}
- if opts.IsMirror {
+ if opts.Mirror {
if _, err = x.InsertOne(&Mirror{
RepoID: repo.ID,
Interval: setting.Mirror.DefaultInterval,
@@ -1143,6 +1159,7 @@ type CreateRepoOptions struct {
IsPrivate bool
IsMirror bool
AutoInit bool
+ Status RepositoryStatus
}
func getRepoInitFile(tp, name string) ([]byte, error) {
@@ -1410,6 +1427,7 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
IsPrivate: opts.IsPrivate,
IsFsckEnabled: !opts.IsMirror,
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
+ Status: opts.Status,
}
sess := x.NewSession()
@@ -1856,6 +1874,7 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
&CommitStatus{RepoID: repoID},
&RepoIndexerStatus{RepoID: repoID},
&Comment{RefRepoID: repoID},
+ &Task{RepoID: repoID},
); err != nil {
return fmt.Errorf("deleteBeans: %v", err)
}