Skip to content

Commit 52bfb7f

Browse files
authored
Handle missing default branch better in owner/repo/branches page (#18290)
This PR more nicely handles a missing default branch in owner/repo/branches Fix #18265 Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 4d0a72a commit 52bfb7f

File tree

2 files changed

+65
-54
lines changed

2 files changed

+65
-54
lines changed

routers/web/repo/branch.go

+33-20
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ func Branches(ctx *context.Context) {
7272

7373
skip := (page - 1) * limit
7474
log.Debug("Branches: skip: %d limit: %d", skip, limit)
75-
branches, branchesCount := loadBranches(ctx, skip, limit)
75+
defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, limit)
7676
if ctx.Written() {
7777
return
7878
}
7979
ctx.Data["Branches"] = branches
80+
ctx.Data["DefaultBranchBranch"] = defaultBranchBranch
8081
pager := context.NewPagination(int(branchesCount), setting.Git.BranchesRangeSize, page, 5)
8182
pager.SetDefaultParams(ctx)
8283
ctx.Data["Page"] = pager
@@ -165,25 +166,28 @@ func redirect(ctx *context.Context) {
165166

166167
// loadBranches loads branches from the repository limited by page & pageSize.
167168
// NOTE: May write to context on error.
168-
func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
169+
func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, int) {
169170
defaultBranch, err := ctx.Repo.GitRepo.GetBranch(ctx.Repo.Repository.DefaultBranch)
170171
if err != nil {
171-
log.Error("loadBranches: get default branch: %v", err)
172-
ctx.ServerError("GetDefaultBranch", err)
173-
return nil, 0
172+
if !git.IsErrBranchNotExist(err) {
173+
log.Error("loadBranches: get default branch: %v", err)
174+
ctx.ServerError("GetDefaultBranch", err)
175+
return nil, nil, 0
176+
}
177+
log.Warn("loadBranches: missing default branch %s for %-v", ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository)
174178
}
175179

176180
rawBranches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, limit)
177181
if err != nil {
178182
log.Error("GetBranches: %v", err)
179183
ctx.ServerError("GetBranches", err)
180-
return nil, 0
184+
return nil, nil, 0
181185
}
182186

183187
protectedBranches, err := models.GetProtectedBranches(ctx.Repo.Repository.ID)
184188
if err != nil {
185189
ctx.ServerError("GetProtectedBranches", err)
186-
return nil, 0
190+
return nil, nil, 0
187191
}
188192

189193
repoIDToRepo := map[int64]*repo_model.Repository{}
@@ -194,36 +198,40 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
194198

195199
var branches []*Branch
196200
for i := range rawBranches {
197-
if rawBranches[i].Name == defaultBranch.Name {
201+
if defaultBranch != nil && rawBranches[i].Name == defaultBranch.Name {
198202
// Skip default branch
199203
continue
200204
}
201205

202-
var branch = loadOneBranch(ctx, rawBranches[i], protectedBranches, repoIDToRepo, repoIDToGitRepo)
206+
var branch = loadOneBranch(ctx, rawBranches[i], defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)
203207
if branch == nil {
204-
return nil, 0
208+
return nil, nil, 0
205209
}
206210

207211
branches = append(branches, branch)
208212
}
209213

210-
// Always add the default branch
211-
log.Debug("loadOneBranch: load default: '%s'", defaultBranch.Name)
212-
branches = append(branches, loadOneBranch(ctx, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo))
214+
var defaultBranchBranch *Branch
215+
if defaultBranch != nil {
216+
// Always add the default branch
217+
log.Debug("loadOneBranch: load default: '%s'", defaultBranch.Name)
218+
defaultBranchBranch = loadOneBranch(ctx, defaultBranch, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)
219+
branches = append(branches, defaultBranchBranch)
220+
}
213221

214222
if ctx.Repo.CanWrite(unit.TypeCode) {
215223
deletedBranches, err := getDeletedBranches(ctx)
216224
if err != nil {
217225
ctx.ServerError("getDeletedBranches", err)
218-
return nil, 0
226+
return nil, nil, 0
219227
}
220228
branches = append(branches, deletedBranches...)
221229
}
222230

223-
return branches, totalNumOfBranches
231+
return defaultBranchBranch, branches, totalNumOfBranches
224232
}
225233

226-
func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranches []*models.ProtectedBranch,
234+
func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, protectedBranches []*models.ProtectedBranch,
227235
repoIDToRepo map[int64]*repo_model.Repository,
228236
repoIDToGitRepo map[int64]*git.Repository) *Branch {
229237
log.Trace("loadOneBranch: '%s'", rawBranch.Name)
@@ -243,10 +251,15 @@ func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranche
243251
}
244252
}
245253

246-
divergence, divergenceError := files_service.CountDivergingCommits(ctx.Repo.Repository, git.BranchPrefix+branchName)
247-
if divergenceError != nil {
248-
ctx.ServerError("CountDivergingCommits", divergenceError)
249-
return nil
254+
divergence := &git.DivergeObject{
255+
Ahead: -1,
256+
Behind: -1,
257+
}
258+
if defaultBranch != nil {
259+
divergence, err = files_service.CountDivergingCommits(ctx.Repo.Repository, git.BranchPrefix+branchName)
260+
if err != nil {
261+
log.Error("CountDivergingCommits", err)
262+
}
250263
}
251264

252265
pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)

templates/repo/branch/list.tmpl

+32-34
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,41 @@
44
<div class="ui container">
55
{{template "base/alert" .}}
66
{{template "repo/sub_menu" .}}
7-
<h4 class="ui top attached header">
8-
{{.i18n.Tr "repo.default_branch"}}
9-
</h4>
7+
{{if .DefaultBranchBranch}}
8+
<h4 class="ui top attached header">
9+
{{.i18n.Tr "repo.default_branch"}}
10+
</h4>
1011

11-
<div class="ui attached table segment">
12-
<table class="ui very basic striped fixed table single line">
13-
<tbody>
14-
<tr>
15-
<td>
16-
{{range .Branches}}
17-
{{if eq .Name $.DefaultBranch}}
18-
{{if .IsProtected}}
12+
<div class="ui attached table segment">
13+
<table class="ui very basic striped fixed table single line">
14+
<tbody>
15+
<tr>
16+
<td>
17+
{{if .DefaultBranchBranch.IsProtected}}
1918
{{svg "octicon-shield-lock"}}
2019
{{end}}
21-
<a href="{{$.RepoLink}}/src/branch/{{PathEscapeSegments $.DefaultBranch}}">{{$.DefaultBranch}}</a>
22-
<p class="info df ac my-2">{{svg "octicon-git-commit" 16 "mr-2"}}<a href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}">{{ShortSha .Commit.ID.String}}</a> · <span class="commit-message">{{RenderCommitMessage .Commit.CommitMessage $.RepoLink $.Repository.ComposeMetas}}</span> · {{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}</p>
23-
{{end}}
24-
{{end}}
25-
</td>
26-
<td class="right aligned overflow-visible">
27-
{{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}}
28-
<div class="ui basic jump button icon tooltip show-create-branch-modal" data-content="{{$.i18n.Tr "repo.branch.new_branch_from" ($.DefaultBranch)}}" data-branch-from="{{$.DefaultBranch}}" data-branch-from-urlcomponent="{{PathEscapeSegments $.DefaultBranch}}" data-modal="#create-branch-modal" data-position="top right">
29-
{{svg "octicon-git-branch"}}
30-
</div>
31-
{{end}}
32-
<div class="ui basic jump dropdown icon button tooltip" data-content="{{$.i18n.Tr "repo.branch.download" ($.DefaultBranch)}}" data-position="top right">
33-
{{svg "octicon-download"}}
34-
<div class="menu">
35-
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
36-
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
20+
<a href="{{.RepoLink}}/src/branch/{{PathEscapeSegments .DefaultBranch}}">{{.DefaultBranch}}</a>
21+
<p class="info df ac my-2">{{svg "octicon-git-commit" 16 "mr-2"}}<a href="{{.RepoLink}}/commit/{{PathEscape .DefaultBranchBranch.Commit.ID.String}}">{{ShortSha .DefaultBranchBranch.Commit.ID.String}}</a> · <span class="commit-message">{{RenderCommitMessage .DefaultBranchBranch.Commit.CommitMessage .RepoLink .Repository.ComposeMetas}}</span> · {{.i18n.Tr "org.repo_updated"}} {{TimeSince .DefaultBranchBranch.Commit.Committer.When .i18n.Lang}}</p>
22+
</td>
23+
<td class="right aligned overflow-visible">
24+
{{if and $.IsWriter (not $.Repository.IsArchived) (not .IsDeleted)}}
25+
<div class="ui basic jump button icon tooltip show-create-branch-modal" data-content="{{$.i18n.Tr "repo.branch.new_branch_from" ($.DefaultBranch)}}" data-branch-from="{{$.DefaultBranch}}" data-branch-from-urlcomponent="{{PathEscapeSegments $.DefaultBranch}}" data-modal="#create-branch-modal" data-position="top right">
26+
{{svg "octicon-git-branch"}}
27+
</div>
28+
{{end}}
29+
<div class="ui basic jump dropdown icon button tooltip" data-content="{{$.i18n.Tr "repo.branch.download" ($.DefaultBranch)}}" data-position="top right">
30+
{{svg "octicon-download"}}
31+
<div class="menu">
32+
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.zip" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;ZIP</a>
33+
<a class="item archive-link" href="{{$.RepoLink}}/archive/{{PathEscapeSegments $.DefaultBranch}}.tar.gz" rel="nofollow">{{svg "octicon-file-zip"}}&nbsp;TAR.GZ</a>
34+
</div>
3735
</div>
38-
</div>
39-
</td>
40-
</tr>
41-
</tbody>
42-
</table>
43-
</div>
36+
</td>
37+
</tr>
38+
</tbody>
39+
</table>
40+
</div>
41+
{{end}}
4442

4543
{{if gt (len .Branches) 1}}
4644
<h4 class="ui top attached header">
@@ -65,7 +63,7 @@
6563
{{end}}
6664
</td>
6765
<td class="three wide ui">
68-
{{if not .IsDeleted}}
66+
{{if and (not .IsDeleted) $.DefaultBranchBranch}}
6967
<div class="commit-divergence">
7068
<div class="bar-group">
7169
<div class="count count-behind">{{.CommitsBehind}}</div>

0 commit comments

Comments
 (0)