]> source.dussan.org Git - gitea.git/commitdiff
Update repository size on cron gc task (#15177)
authorzeripath <art27@cantab.net>
Sun, 28 Mar 2021 03:56:28 +0000 (04:56 +0100)
committerGitHub <noreply@github.com>
Sun, 28 Mar 2021 03:56:28 +0000 (05:56 +0200)
git gc cron could change the size of the repository therefore we should update the
size of the repo stored in our database.

Also significantly improve the efficiency of counting lfs associated with the
repository

models/repo.go
modules/repository/check.go

index c807916ddc3a34a7b59a6f1ef34eb856383a0fb1..7f2ec1f742bba07a0f8cd3fe1debf6a69aa12ee4 100644 (file)
@@ -740,15 +740,12 @@ func (repo *Repository) updateSize(e Engine) error {
                return fmt.Errorf("updateSize: %v", err)
        }
 
-       objs, err := repo.GetLFSMetaObjects(-1, 0)
+       lfsSize, err := e.Where("repository_id = ?", repo.ID).SumInt(new(LFSMetaObject), "size")
        if err != nil {
                return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
        }
-       for _, obj := range objs {
-               size += obj.Size
-       }
 
-       repo.Size = size
+       repo.Size = size + lfsSize
        _, err = e.ID(repo.ID).Cols("size").Update(repo)
        return err
 }
index d13ddbb0b017cd173b1de1fac1e18480c7e28090..0485e9e83a50d1515b84801751a181073a104437 100644 (file)
@@ -91,6 +91,17 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro
                                }
                                return fmt.Errorf("Repository garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
                        }
+
+                       // Now update the size of the repository
+                       if err := repo.UpdateSize(models.DefaultDBContext()); err != nil {
+                               log.Error("Updating size as part of garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
+                               desc := fmt.Sprintf("Updating size as part of garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
+                               if err = models.CreateRepositoryNotice(desc); err != nil {
+                                       log.Error("CreateRepositoryNotice: %v", err)
+                               }
+                               return fmt.Errorf("Updating size as part of garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
+                       }
+
                        return nil
                },
        ); err != nil {