Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

v28.go 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 Gitea. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package migrations
  6. import (
  7. "fmt"
  8. "path/filepath"
  9. "strings"
  10. "code.gitea.io/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/go-xorm/xorm"
  14. )
  15. func addRepoSize(x *xorm.Engine) (err error) {
  16. log.Info("This migration could take up to minutes, please be patient.")
  17. type Repository struct {
  18. ID int64
  19. OwnerID int64
  20. Name string
  21. Size int64
  22. }
  23. type User struct {
  24. ID int64
  25. Name string
  26. }
  27. if err = x.Sync2(new(Repository)); err != nil {
  28. return fmt.Errorf("Sync2: %v", err)
  29. }
  30. // For the sake of SQLite3, we can't use x.Iterate here.
  31. offset := 0
  32. for {
  33. repos := make([]*Repository, 0, 10)
  34. if err = x.Table("repository").Asc("id").Limit(10, offset).Find(&repos); err != nil {
  35. return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
  36. }
  37. log.Trace("Select [offset: %d, repos: %d]", offset, len(repos))
  38. if len(repos) == 0 {
  39. break
  40. }
  41. offset += 10
  42. for _, repo := range repos {
  43. if repo.Name == "." || repo.Name == ".." {
  44. continue
  45. }
  46. user := new(User)
  47. has, err := x.Where("id = ?", repo.OwnerID).Get(user)
  48. if err != nil {
  49. return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
  50. } else if !has {
  51. continue
  52. }
  53. repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
  54. countObject, err := git.GetRepoSize(repoPath)
  55. if err != nil {
  56. log.Warn("GetRepoSize: %v", err)
  57. continue
  58. }
  59. repo.Size = countObject.Size + countObject.SizePack
  60. if _, err = x.ID(repo.ID).Cols("size").Update(repo); err != nil {
  61. return fmt.Errorf("update size: %v", err)
  62. }
  63. }
  64. }
  65. return nil
  66. }