You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

  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.Sql(fmt.Sprintf("SELECT * FROM `repository` ORDER BY id ASC LIMIT 10 OFFSET %d", offset)).
  35. Find(&repos); err != nil {
  36. return fmt.Errorf("select repos [offset: %d]: %v", offset, err)
  37. }
  38. log.Trace("Select [offset: %d, repos: %d]", offset, len(repos))
  39. if len(repos) == 0 {
  40. break
  41. }
  42. offset += 10
  43. for _, repo := range repos {
  44. if repo.Name == "." || repo.Name == ".." {
  45. continue
  46. }
  47. user := new(User)
  48. has, err := x.Where("id = ?", repo.OwnerID).Get(user)
  49. if err != nil {
  50. return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
  51. } else if !has {
  52. continue
  53. }
  54. repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
  55. countObject, err := git.GetRepoSize(repoPath)
  56. if err != nil {
  57. log.Warn("GetRepoSize: %v", err)
  58. continue
  59. }
  60. repo.Size = countObject.Size + countObject.SizePack
  61. if _, err = x.Id(repo.ID).Cols("size").Update(repo); err != nil {
  62. return fmt.Errorf("update size: %v", err)
  63. }
  64. }
  65. }
  66. return nil
  67. }