Skip to content

Commit 214b2d5

Browse files
committed
add branches pages
1 parent 4f9a28e commit 214b2d5

File tree

5 files changed

+237
-12
lines changed

5 files changed

+237
-12
lines changed

routers/repo/branch.go

Lines changed: 123 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,143 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2017 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package repo
67

78
import (
8-
"code.gitea.io/gitea/modules/base"
9+
"time"
10+
11+
"code.gitea.io/git"
912
"code.gitea.io/gitea/modules/context"
13+
"code.gitea.io/gitea/modules/log"
1014
)
1115

1216
const (
13-
tplBranch base.TplName = "repo/branch"
17+
tplBranchOverview = "repo/branches/overview"
18+
tplBranchAll = "repo/branches/all"
1419
)
1520

16-
// Branches render repository branch page
17-
func Branches(ctx *context.Context) {
18-
ctx.Data["Title"] = "Branches"
19-
ctx.Data["IsRepoToolbarBranches"] = true
21+
type Branch struct {
22+
Name string
23+
Commit *git.Commit
24+
IsProtected bool
25+
}
2026

21-
brs, err := ctx.Repo.GitRepo.GetBranches()
27+
func loadBranches(c *context.Context) []*Branch {
28+
rawBranches, err := c.Repo.Repository.GetBranches()
2229
if err != nil {
23-
ctx.Handle(500, "repo.Branches(GetBranches)", err)
30+
c.Handle(500, "GetBranches", err)
31+
return nil
32+
}
33+
34+
protectBranches, err := c.Repo.Repository.GetProtectedBranches()
35+
if err != nil {
36+
c.Handle(500, "GetProtectBranchesByRepoID", err)
37+
return nil
38+
}
39+
40+
branches := make([]*Branch, len(rawBranches))
41+
for i := range rawBranches {
42+
commit, err := rawBranches[i].GetCommit()
43+
if err != nil {
44+
c.Handle(500, "GetCommit", err)
45+
return nil
46+
}
47+
48+
branches[i] = &Branch{
49+
Name: rawBranches[i].Name,
50+
Commit: commit,
51+
}
52+
53+
for j := range protectBranches {
54+
if branches[i].Name == protectBranches[j].BranchName {
55+
branches[i].IsProtected = true
56+
break
57+
}
58+
}
59+
}
60+
61+
c.Data["AllowPullRequest"] = c.Repo.Repository.AllowsPulls()
62+
return branches
63+
}
64+
65+
// Branches render repository branch page
66+
func Branches(c *context.Context) {
67+
c.Data["Title"] = c.Tr("repo.git_branches")
68+
c.Data["PageIsBranchesOverview"] = true
69+
70+
branches := loadBranches(c)
71+
if c.Written() {
72+
return
73+
}
74+
75+
now := time.Now()
76+
activeBranches := make([]*Branch, 0, 3)
77+
staleBranches := make([]*Branch, 0, 3)
78+
for i := range branches {
79+
switch {
80+
case branches[i].Name == c.Repo.BranchName:
81+
c.Data["DefaultBranch"] = branches[i]
82+
case branches[i].Commit.Committer.When.Add(30 * 24 * time.Hour).After(now): // 30 days
83+
activeBranches = append(activeBranches, branches[i])
84+
case branches[i].Commit.Committer.When.Add(3 * 30 * 24 * time.Hour).Before(now): // 90 days
85+
staleBranches = append(staleBranches, branches[i])
86+
}
87+
}
88+
89+
c.Data["ActiveBranches"] = activeBranches
90+
c.Data["StaleBranches"] = staleBranches
91+
c.HTML(200, tplBranchOverview)
92+
}
93+
94+
// AllBranches all branches UI
95+
func AllBranches(c *context.Context) {
96+
c.Data["Title"] = c.Tr("repo.git_branches")
97+
c.Data["PageIsBranchesAll"] = true
98+
99+
branches := loadBranches(c)
100+
if c.Written() {
24101
return
25-
} else if len(brs) == 0 {
26-
ctx.Handle(404, "repo.Branches(GetBranches)", nil)
102+
}
103+
c.Data["Branches"] = branches
104+
105+
c.HTML(200, tplBranchAll)
106+
}
107+
108+
// DeleteBranchPost executes branch deletation operation
109+
func DeleteBranchPost(c *context.Context) {
110+
branchName := c.Params("*")
111+
commitID := c.Query("commit")
112+
113+
defer func() {
114+
redirectTo := c.Query("redirect_to")
115+
if len(redirectTo) == 0 {
116+
redirectTo = c.Repo.RepoLink
117+
}
118+
c.Redirect(redirectTo)
119+
}()
120+
121+
if !c.Repo.GitRepo.IsBranchExist(branchName) {
27122
return
28123
}
124+
if len(commitID) > 0 {
125+
branchCommitID, err := c.Repo.GitRepo.GetBranchCommitID(branchName)
126+
if err != nil {
127+
log.Error(2, "GetBranchCommitID: %v", err)
128+
return
129+
}
29130

30-
ctx.Data["Branches"] = brs
31-
ctx.HTML(200, tplBranch)
131+
if branchCommitID != commitID {
132+
c.Flash.Error(c.Tr("repo.pulls.delete_branch_has_new_commits"))
133+
return
134+
}
135+
}
136+
137+
if err := c.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
138+
Force: true,
139+
}); err != nil {
140+
log.Error(2, "DeleteBranch '%s': %v", branchName, err)
141+
return
142+
}
32143
}

routers/routes/routes.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,14 @@ func RegisterRoutes(m *macaron.Macaron) {
579579
m.Get("/milestones", repo.Milestones)
580580
}, context.RepoRef())
581581

582+
m.Group("/branches", func() {
583+
m.Get("", repo.Branches)
584+
m.Get("/all", repo.AllBranches)
585+
m.Post("/delete/*", reqSignIn, reqRepoWriter, repo.DeleteBranchPost)
586+
}, repo.MustBeNotBare, func(c *context.Context) {
587+
c.Data["PageIsViewFiles"] = true
588+
})
589+
582590
m.Group("/wiki", func() {
583591
m.Get("/?:page", repo.Uncyclo)
584592
m.Get("/_pages", repo.UncycloPages)

templates/repo/branches/all.tmpl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{template "base/head" .}}
2+
<div class="repository branches all">
3+
{{template "repo/header" .}}
4+
<div class="ui container">
5+
<div class="navbar">
6+
{{template "repo/branches/navbar" .}}
7+
</div>
8+
<div class="ui top attached header">
9+
{{.i18n.Tr "repo.branches.all"}}
10+
</div>
11+
<div class="ui attached segment list">
12+
{{range .Branches}}
13+
<div class="item ui grid">
14+
<div class="ui eleven wide column">
15+
{{if .IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.Name}}"><code>{{.Name}}</code></a>
16+
{{$timeSince := TimeSince .Commit.Committer.When $.Lang}}
17+
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .Commit.Committer.Name | Safe}}</span>
18+
</div>
19+
<div class="ui four wide column">
20+
{{if and (and (eq $.BranchName .Name) $.IsRepositoryAdmin) (not $.Repository.IsMirror)}}
21+
<a class="ui basic blue button" href="{{$.RepoLink}}/settings/branches">{{$.i18n.Tr "repo.branches.change_default_branch"}}</a>
22+
{{else if and $.IsRepositoryWriter $.AllowPullRequest}}
23+
<a class="ui basic button" href="{{$.RepoLink}}/compare/{{$.BranchName}}...{{.Name}}"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.new"}}</a>
24+
{{end}}
25+
</div>
26+
</div>
27+
{{end}}
28+
</div>
29+
</div>
30+
</div>
31+
{{template "base/footer" .}}

templates/repo/branches/navbar.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="ui compact small menu">
2+
<a class="{{if .PageIsBranchesOverview}}active{{end}} item" href="{{.RepoLink}}/branches">{{.i18n.Tr "repo.branches.overview"}}</a>
3+
<a class="{{if .PageIsBranchesAll}}active{{end}} item" href="{{.RepoLink}}/branches/all">{{.i18n.Tr "repo.branches.all"}}</a>
4+
</div>

templates/repo/branches/overview.tmpl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{{template "base/head" .}}
2+
<div class="repository branches overview">
3+
{{template "repo/header" .}}
4+
<div class="ui container">
5+
<div class="navbar">
6+
{{template "repo/branches/navbar" .}}
7+
</div>
8+
<div class="ui top attached header">
9+
{{.i18n.Tr "repo.settings.default_branch"}}
10+
</div>
11+
<div class="ui attached segment list">
12+
<div class="item ui grid">
13+
<div class="ui eleven wide column">
14+
{{if .DefaultBranch.IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.DefaultBranch.Name}}"><code>{{.DefaultBranch.Name}}</code></a>
15+
{{$timeSince := TimeSince .DefaultBranch.Commit.Committer.When $.Lang}}
16+
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .DefaultBranch.Commit.Committer.Name | Safe}}</span>
17+
</div>
18+
{{if and $.IsRepositoryAdmin (not $.Repository.IsMirror)}}
19+
<div class="ui four wide column">
20+
<a class="ui basic blue button" href="{{$.RepoLink}}/settings/branches">{{.i18n.Tr "repo.branches.change_default_branch"}}</a>
21+
</div>
22+
{{end}}
23+
</div>
24+
</div>
25+
26+
{{if .ActiveBranches}}
27+
<div class="ui top attached header">
28+
{{.i18n.Tr "repo.branches.active_branches"}}
29+
</div>
30+
<div class="ui attached segment list">
31+
{{range .ActiveBranches}}
32+
<div class="item ui grid">
33+
<div class="ui eleven wide column">
34+
{{if .IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.Name}}"><code>{{.Name}}</code></a>
35+
{{$timeSince := TimeSince .Commit.Committer.When $.Lang}}
36+
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .Commit.Committer.Name | Safe}}</span>
37+
</div>
38+
{{if and $.IsRepositoryWriter $.AllowPullRequest}}
39+
<div class="ui four wide column">
40+
<a class="ui basic button" href="{{$.RepoLink}}/compare/{{$.BranchName}}...{{.Name}}"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.new"}}</a>
41+
</div>
42+
{{end}}
43+
</div>
44+
{{end}}
45+
</div>
46+
{{end}}
47+
48+
{{if .StaleBranches}}
49+
<div class="ui top attached header">
50+
{{.i18n.Tr "repo.branches.stale_branches"}}
51+
</div>
52+
<div class="ui attached segment list">
53+
{{range .StaleBranches}}
54+
<div class="item ui grid">
55+
<div class="ui eleven wide column">
56+
{{if .IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.Name}}"><code>{{.Name}}</code></a>
57+
{{$timeSince := TimeSince .Commit.Committer.When $.Lang}}
58+
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .Commit.Committer.Name | Safe}}</span>
59+
</div>
60+
{{if and $.IsRepositoryWriter $.AllowPullRequest}}
61+
<div class="ui four wide column">
62+
<a class="ui basic button" href="{{$.RepoLink}}/compare/{{$.BranchName}}...{{.Name}}"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.new"}}</a>
63+
</div>
64+
{{end}}
65+
</div>
66+
{{end}}
67+
</div>
68+
{{end}}
69+
</div>
70+
</div>
71+
{{template "base/footer" .}}

0 commit comments

Comments
 (0)