From f374b30e99bd2bac462023c6f13e68081e78254b Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Fri, 19 Jul 2024 14:59:06 +0800 Subject: [PATCH 1/3] fix divergence cache bug --- services/repository/branch.go | 15 +++++++++++++++ services/repository/push.go | 10 ++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 869921bfbc56b..64dcd85d341ec 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -147,6 +147,21 @@ func DelDivergenceFromCache(repoID int64, branchName string) error { return cache.GetCache().Delete(getDivergenceCacheKey(repoID, branchName)) } +// DelRepoDivergenceFromCache deletes all divergence caches of a repository +func DelRepoDivergenceFromCache(ctx context.Context, repoID int64) error { + dbBranches, _, err := db.FindAndCount[git_model.Branch](ctx, git_model.FindBranchOptions{ + RepoID: repoID, + ListOptions: db.ListOptionsAll, + }) + if err != nil { + return err + } + for i := range dbBranches { + DelDivergenceFromCache(repoID, dbBranches[i].Name) + } + return nil +} + func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *git_model.Branch, protectedBranches *git_model.ProtectedBranchRules, repoIDToRepo map[int64]*repo_model.Repository, repoIDToGitRepo map[int64]*git.Repository, diff --git a/services/repository/push.go b/services/repository/push.go index 39843249a53a7..f27e6a58dd6ce 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -221,8 +221,14 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } // delete cache for divergence - if err := DelDivergenceFromCache(repo.ID, branch); err != nil { - log.Error("DelDivergenceFromCache: %v", err) + if branch == repo.DefaultBranch { + if err := DelRepoDivergenceFromCache(ctx, repo.ID); err != nil { + log.Error("DelRepoDivergenceFromCache: %v", err) + } + } else { + if err := DelDivergenceFromCache(repo.ID, branch); err != nil { + log.Error("DelDivergenceFromCache: %v", err) + } } commits := repo_module.GitToPushCommits(l) From 69204ccb1e52cfd047d1abfea253ee2f246722a8 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Fri, 19 Jul 2024 15:06:48 +0800 Subject: [PATCH 2/3] update log --- services/repository/branch.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 64dcd85d341ec..cc5c4bba29990 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -157,7 +157,9 @@ func DelRepoDivergenceFromCache(ctx context.Context, repoID int64) error { return err } for i := range dbBranches { - DelDivergenceFromCache(repoID, dbBranches[i].Name) + if err := DelDivergenceFromCache(repoID, dbBranches[i].Name); err != nil { + log.Error("DelDivergenceFromCache: %v", err) + } } return nil } From 2a85ace1ccb6802c42c74833899b0b0892576c51 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Fri, 19 Jul 2024 15:19:06 +0800 Subject: [PATCH 3/3] fix find --- services/repository/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index cc5c4bba29990..7fc99930776a1 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -149,7 +149,7 @@ func DelDivergenceFromCache(repoID int64, branchName string) error { // DelRepoDivergenceFromCache deletes all divergence caches of a repository func DelRepoDivergenceFromCache(ctx context.Context, repoID int64) error { - dbBranches, _, err := db.FindAndCount[git_model.Branch](ctx, git_model.FindBranchOptions{ + dbBranches, err := db.Find[git_model.Branch](ctx, git_model.FindBranchOptions{ RepoID: repoID, ListOptions: db.ListOptionsAll, })