From 37136d5363b5d937aced1c6cadb8e0149589d846 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 15 Jan 2022 21:26:30 +0000 Subject: [PATCH 1/2] Handle missing default branch better in owner/repo/branches page This PR more nicely handles a missing default branch in owner/repo/branches Fix #18265 Signed-off-by: Andrew Thornton --- routers/web/repo/branch.go | 54 +++++++++++++++++---------- templates/repo/branch/list.tmpl | 66 ++++++++++++++++----------------- 2 files changed, 66 insertions(+), 54 deletions(-) diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 9247202086462..26a3c74c8aa54 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -72,11 +72,12 @@ func Branches(ctx *context.Context) { skip := (page - 1) * limit log.Debug("Branches: skip: %d limit: %d", skip, limit) - branches, branchesCount := loadBranches(ctx, skip, limit) + defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, limit) if ctx.Written() { return } ctx.Data["Branches"] = branches + ctx.Data["DefaultBranchBranch"] = defaultBranchBranch pager := context.NewPagination(int(branchesCount), setting.Git.BranchesRangeSize, page, 5) pager.SetDefaultParams(ctx) ctx.Data["Page"] = pager @@ -165,25 +166,29 @@ func redirect(ctx *context.Context) { // loadBranches loads branches from the repository limited by page & pageSize. // NOTE: May write to context on error. -func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) { +func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, int) { defaultBranch, err := ctx.Repo.GitRepo.GetBranch(ctx.Repo.Repository.DefaultBranch) if err != nil { - log.Error("loadBranches: get default branch: %v", err) - ctx.ServerError("GetDefaultBranch", err) - return nil, 0 + if !git.IsErrBranchNotExist(err) { + log.Error("loadBranches: get default branch: %v", err) + ctx.ServerError("GetDefaultBranch", err) + return nil, nil, 0 + } + log.Warn("loadBranches: missing default branch %s for %-v", ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository) + err = nil } rawBranches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, limit) if err != nil { log.Error("GetBranches: %v", err) ctx.ServerError("GetBranches", err) - return nil, 0 + return nil, nil, 0 } protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID) if err != nil { ctx.ServerError("GetProtectedBranches", err) - return nil, 0 + return nil, nil, 0 } repoIDToRepo := map[int64]*repo_model.Repository{} @@ -194,36 +199,40 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) { var branches []*Branch for i := range rawBranches { - if rawBranches[i].Name == defaultBranch.Name { + if defaultBranch != nil && rawBranches[i].Name == defaultBranch.Name { // Skip default branch continue } - var branch = loadOneBranch(ctx, rawBranches[i], protectedBranches, repoIDToRepo, repoIDToGitRepo) + var branch = loadOneBranch(ctx, rawBranches[i], defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo) if branch == nil { - return nil, 0 + return nil, nil, 0 } branches = append(branches, branch) } - // Always add the default branch - log.Debug("loadOneBranch: load default: '%s'", defaultBranch.Name) - branches = append(branches, loadOneBranch(ctx, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)) + var defaultBranchBranch *Branch + if defaultBranch != nil { + // Always add the default branch + log.Debug("loadOneBranch: load default: '%s'", defaultBranch.Name) + defaultBranchBranch = loadOneBranch(ctx, defaultBranch, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo) + branches = append(branches, defaultBranchBranch) + } if ctx.Repo.CanWrite(unit.TypeCode) { deletedBranches, err := getDeletedBranches(ctx) if err != nil { ctx.ServerError("getDeletedBranches", err) - return nil, 0 + return nil, nil, 0 } branches = append(branches, deletedBranches...) } - return branches, totalNumOfBranches + return defaultBranchBranch, branches, totalNumOfBranches } -func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranches []*models.ProtectedBranch, +func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, protectedBranches []*models.ProtectedBranch, repoIDToRepo map[int64]*repo_model.Repository, repoIDToGitRepo map[int64]*git.Repository) *Branch { log.Trace("loadOneBranch: '%s'", rawBranch.Name) @@ -243,10 +252,15 @@ func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranche } } - divergence, divergenceError := files_service.CountDivergingCommits(ctx.Repo.Repository, git.BranchPrefix+branchName) - if divergenceError != nil { - ctx.ServerError("CountDivergingCommits", divergenceError) - return nil + divergence := &git.DivergeObject{ + Ahead: -1, + Behind: -1, + } + if defaultBranch != nil { + divergence, err = files_service.CountDivergingCommits(ctx.Repo.Repository, git.BranchPrefix+branchName) + if err != nil { + log.Error("CountDivergingCommits", err) + } } pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName) diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index 3580808aac188..fc819010692c4 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -4,43 +4,41 @@
{{template "base/alert" .}} {{template "repo/sub_menu" .}} -

- {{.i18n.Tr "repo.default_branch"}} -

+ {{if .DefaultBranchBranch}} +

+ {{.i18n.Tr "repo.default_branch"}} +

-
- - - - + + +
- {{range .Branches}} - {{if eq .Name $.DefaultBranch}} - {{if .IsProtected}} +
+ + + + - + - - -
+ {{if .DefaultBranchBranch.IsProtected}} {{svg "octicon-shield-lock"}} {{end}} - {{$.DefaultBranch}} -

{{svg "octicon-git-commit" 16 "mr-2"}}{{ShortSha .Commit.ID.String}} · {{RenderCommitMessage .Commit.CommitMessage $.RepoLink $.Repository.ComposeMetas}} · {{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}

- {{end}} - {{end}} -
- {{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}} -
- {{svg "octicon-git-branch"}} -
- {{end}} -
+ {{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}} +
+ {{svg "octicon-git-branch"}} +
+ {{end}} + - -
-
+
+
+ {{end}} {{if gt (len .Branches) 1}}

@@ -65,7 +63,7 @@ {{end}} - {{if not .IsDeleted}} + {{if and (not .IsDeleted) $.DefaultBranchBranch}}
{{.CommitsBehind}}
From ae84cfa6ad3930b384fa0659028d63c82fa0d041 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 15 Jan 2022 22:32:51 +0000 Subject: [PATCH 2/2] placate lint Signed-off-by: Andrew Thornton --- routers/web/repo/branch.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 26a3c74c8aa54..29b8acd61c7b7 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -175,7 +175,6 @@ func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, in return nil, nil, 0 } log.Warn("loadBranches: missing default branch %s for %-v", ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository) - err = nil } rawBranches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, limit)