v16.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package migrations
  5. import (
  6. "fmt"
  7. "path/filepath"
  8. "strings"
  9. "github.com/go-xorm/xorm"
  10. log "gopkg.in/clog.v1"
  11. "github.com/gogits/git-module"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. func updateRepositorySizes(x *xorm.Engine) (err error) {
  15. type Repository struct {
  16. ID int64
  17. OwnerID int64
  18. Name string
  19. Size int64
  20. }
  21. type User struct {
  22. ID int64
  23. Name string
  24. }
  25. return x.Where("id > 0").Iterate(new(Repository),
  26. func(idx int, bean interface{}) error {
  27. repo := bean.(*Repository)
  28. if repo.Name == "." || repo.Name == ".." {
  29. return nil
  30. }
  31. user := new(User)
  32. has, err := x.Where("id = ?", repo.OwnerID).Get(user)
  33. if err != nil {
  34. return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
  35. } else if !has {
  36. return nil
  37. }
  38. repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
  39. log.Trace("[%04d]: %s", idx, repoPath)
  40. countObject, err := git.GetRepoSize(repoPath)
  41. if err != nil {
  42. return fmt.Errorf("GetRepoSize: %v", err)
  43. }
  44. repo.Size = countObject.Size + countObject.SizePack
  45. if _, err = x.Id(repo.ID).Cols("size").Update(repo); err != nil {
  46. return fmt.Errorf("update size: %v", err)
  47. }
  48. return nil
  49. })
  50. }