Skip to content

Commit 82d1a7f

Browse files
authored
Update repository size on cron gc task (#15177)
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
1 parent 5ad65c8 commit 82d1a7f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

models/repo.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,12 @@ func (repo *Repository) updateSize(e Engine) error {
740740
return fmt.Errorf("updateSize: %v", err)
741741
}
742742

743-
objs, err := repo.GetLFSMetaObjects(-1, 0)
743+
lfsSize, err := e.Where("repository_id = ?", repo.ID).SumInt(new(LFSMetaObject), "size")
744744
if err != nil {
745745
return fmt.Errorf("updateSize: GetLFSMetaObjects: %v", err)
746746
}
747-
for _, obj := range objs {
748-
size += obj.Size
749-
}
750747

751-
repo.Size = size
748+
repo.Size = size + lfsSize
752749
_, err = e.ID(repo.ID).Cols("size").Update(repo)
753750
return err
754751
}

modules/repository/check.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ func GitGcRepos(ctx context.Context, timeout time.Duration, args ...string) erro
9191
}
9292
return fmt.Errorf("Repository garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
9393
}
94+
95+
// Now update the size of the repository
96+
if err := repo.UpdateSize(models.DefaultDBContext()); err != nil {
97+
log.Error("Updating size as part of garbage collection failed for %v. Stdout: %s\nError: %v", repo, stdout, err)
98+
desc := fmt.Sprintf("Updating size as part of garbage collection failed for %s. Stdout: %s\nError: %v", repo.RepoPath(), stdout, err)
99+
if err = models.CreateRepositoryNotice(desc); err != nil {
100+
log.Error("CreateRepositoryNotice: %v", err)
101+
}
102+
return fmt.Errorf("Updating size as part of garbage collection failed in repo: %s: Error: %v", repo.FullName(), err)
103+
}
104+
94105
return nil
95106
},
96107
); err != nil {

0 commit comments

Comments
 (0)