diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-06-29 18:03:20 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-29 10:03:20 +0000 |
commit | 6e19484f4d3bf372212f2da462110a1a8c10cbf2 (patch) | |
tree | e8f1b4920b286241e4ad59151b4f00d9941a27aa /modules/context | |
parent | 5a871932f0efc19a731ee5c1202679653d3cefff (diff) | |
download | gitea-6e19484f4d3bf372212f2da462110a1a8c10cbf2.tar.gz gitea-6e19484f4d3bf372212f2da462110a1a8c10cbf2.zip |
Sync branches into databases (#22743)
Related #14180
Related #25233
Related #22639
Close #19786
Related #12763
This PR will change all the branches retrieve method from reading git
data to read database to reduce git read operations.
- [x] Sync git branches information into database when push git data
- [x] Create a new table `Branch`, merge some columns of `DeletedBranch`
into `Branch` table and drop the table `DeletedBranch`.
- [x] Read `Branch` table when visit `code` -> `branch` page
- [x] Read `Branch` table when list branch names in `code` page dropdown
- [x] Read `Branch` table when list git ref compare page
- [x] Provide a button in admin page to manually sync all branches.
- [x] Sync branches if repository is not empty but database branches are
empty when visiting pages with branches list
- [x] Use `commit_time desc` as the default FindBranch order by to keep
consistent as before and deleted branches will be always at the end.
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Diffstat (limited to 'modules/context')
-rw-r--r-- | modules/context/repo.go | 35 |
1 files changed, 30 insertions, 5 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go index 003309f1b0..e999085251 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -667,13 +667,38 @@ func RepoAssignment(ctx *Context) (cancel context.CancelFunc) { } ctx.Data["Tags"] = tags - brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) + branchOpts := git_model.FindBranchOptions{ + RepoID: ctx.Repo.Repository.ID, + IsDeletedBranch: util.OptionalBoolFalse, + ListOptions: db.ListOptions{ + ListAll: true, + }, + } + branchesTotal, err := git_model.CountBranches(ctx, branchOpts) + if err != nil { + ctx.ServerError("CountBranches", err) + return + } + + // non empty repo should have at least 1 branch, so this repository's branches haven't been synced yet + if branchesTotal == 0 { // fallback to do a sync immediately + branchesTotal, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0) + if err != nil { + ctx.ServerError("SyncRepoBranches", err) + return + } + } + + // FIXME: use paganation and async loading + branchOpts.ExcludeBranchNames = []string{ctx.Repo.Repository.DefaultBranch} + brs, err := git_model.FindBranchNames(ctx, branchOpts) if err != nil { ctx.ServerError("GetBranches", err) return } - ctx.Data["Branches"] = brs - ctx.Data["BranchesCount"] = len(brs) + // always put default branch on the top + ctx.Data["Branches"] = append(branchOpts.ExcludeBranchNames, brs...) + ctx.Data["BranchesCount"] = branchesTotal // If not branch selected, try default one. // If default branch doesn't exist, fall back to some other branch. @@ -897,9 +922,9 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context if len(ctx.Params("*")) == 0 { refName = ctx.Repo.Repository.DefaultBranch if !ctx.Repo.GitRepo.IsBranchExist(refName) { - brs, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) + brs, _, err := ctx.Repo.GitRepo.GetBranches(0, 1) if err == nil && len(brs) != 0 { - refName = brs[0] + refName = brs[0].Name } else if len(brs) == 0 { log.Error("No branches in non-empty repository %s", ctx.Repo.GitRepo.Path) ctx.Repo.Repository.MarkAsBrokenEmpty() |