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.

76 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/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "xorm.io/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.CountObjects(repoPath)
  55. if err != nil {
  56. log.Warn("CountObjects: %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. }