2017-04-11 19:00:15 +05:30
|
|
|
// Copyright 2017 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2017 Gitea. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-03-27 15:03:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-04-11 19:00:15 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
2019-10-17 14:56:49 +05:30
|
|
|
"xorm.io/xorm"
|
2017-04-11 19:00:15 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func addRepoSize(x *xorm.Engine) (err error) {
|
|
|
|
log.Info("This migration could take up to minutes, please be patient.")
|
|
|
|
type Repository struct {
|
|
|
|
ID int64
|
|
|
|
OwnerID int64
|
|
|
|
Name string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
type User struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
if err = x.Sync2(new(Repository)); err != nil {
|
|
|
|
return fmt.Errorf("Sync2: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the sake of SQLite3, we can't use x.Iterate here.
|
|
|
|
offset := 0
|
|
|
|
for {
|
|
|
|
repos := make([]*Repository, 0, 10)
|
2017-08-04 10:12:51 +05:30
|
|
|
if err = x.Table("repository").Asc("id").Limit(10, offset).Find(&repos); err != nil {
|
2017-04-11 19:00:15 +05:30
|
|
|
return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
|
|
|
|
}
|
|
|
|
log.Trace("Select [offset: %d, repos: %d]", offset, len(repos))
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
offset += 10
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
if repo.Name == "." || repo.Name == ".." {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
has, err := x.Where("id = ?", repo.OwnerID).Get(user)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
|
|
|
|
} else if !has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
|
|
|
|
countObject, err := git.GetRepoSize(repoPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("GetRepoSize: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Size = countObject.Size + countObject.SizePack
|
2017-10-05 10:13:04 +05:30
|
|
|
if _, err = x.ID(repo.ID).Cols("size").Update(repo); err != nil {
|
2017-04-11 19:00:15 +05:30
|
|
|
return fmt.Errorf("update size: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|