Skip to content

Branches management UI #2464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,13 @@ video_not_supported_in_browser = Your browser doesn't support HTML5 video tag.
stored_lfs = Stored with Git LFS
commit_graph = Commit graph

branches.overview = Overview
branches.active_branches = Active Branches
branches.stale_branches = Stale Branches
branches.all = All Branches
branches.updated_by = Updated %[1]s by %[2]s
branches.change_default_branch = Change Default Branch

editor.new_file = New file
editor.upload_file = Upload file
editor.edit_file = Edit file
Expand Down
134 changes: 123 additions & 11 deletions routers/repo/branch.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,144 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2017 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repo

import (
"code.gitea.io/gitea/modules/base"
"time"

"code.gitea.io/git"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
)

const (
tplBranch base.TplName = "repo/branch"
tplBranchOverview = "repo/branches/overview"
tplBranchAll = "repo/branches/all"
)

// Branch branch information on UI
type Branch struct {
Name string
Commit *git.Commit
IsProtected bool
}

func loadBranches(ctx *context.Context) []*Branch {
rawBranches, err := ctx.Repo.Repository.GetBranches()
if err != nil {
ctx.Handle(500, "GetBranches", err)
return nil
}

protectBranches, err := ctx.Repo.Repository.GetProtectedBranches()
if err != nil {
ctx.Handle(500, "GetProtectedBranches", err)
return nil
}

branches := make([]*Branch, len(rawBranches))
for i := range rawBranches {
commit, err := rawBranches[i].GetCommit()
if err != nil {
ctx.Handle(500, "GetCommit", err)
return nil
}

branches[i] = &Branch{
Name: rawBranches[i].Name,
Commit: commit,
}

for j := range protectBranches {
if branches[i].Name == protectBranches[j].BranchName {
branches[i].IsProtected = true
break
}
}
}

ctx.Data["AllowPullRequest"] = ctx.Repo.Repository.AllowsPulls()
return branches
}

// Branches render repository branch page
func Branches(ctx *context.Context) {
ctx.Data["Title"] = "Branches"
ctx.Data["IsRepoToolbarBranches"] = true
ctx.Data["Title"] = ctx.Tr("repo.git_branches")
ctx.Data["PageIsBranchesOverview"] = true

brs, err := ctx.Repo.GitRepo.GetBranches()
if err != nil {
ctx.Handle(500, "repo.Branches(GetBranches)", err)
branches := loadBranches(ctx)
if ctx.Written() {
return
}

now := time.Now()
activeBranches := make([]*Branch, 0, 3)
staleBranches := make([]*Branch, 0, 3)
for i := range branches {
switch {
case branches[i].Name == ctx.Repo.BranchName:
ctx.Data["DefaultBranch"] = branches[i]
case branches[i].Commit.Committer.When.Add(30 * 24 * time.Hour).After(now): // 30 days
activeBranches = append(activeBranches, branches[i])
case branches[i].Commit.Committer.When.Add(3 * 30 * 24 * time.Hour).Before(now): // 90 days
staleBranches = append(staleBranches, branches[i])
}
}

ctx.Data["ActiveBranches"] = activeBranches
ctx.Data["StaleBranches"] = staleBranches
ctx.HTML(200, tplBranchOverview)
}

// AllBranches all branches UI
func AllBranches(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.git_branches")
ctx.Data["PageIsBranchesAll"] = true

branches := loadBranches(ctx)
if ctx.Written() {
return
} else if len(brs) == 0 {
ctx.Handle(404, "repo.Branches(GetBranches)", nil)
}
ctx.Data["Branches"] = branches

ctx.HTML(200, tplBranchAll)
}

// DeleteBranchPost executes branch deletation operation
func DeleteBranchPost(ctx *context.Context) {
branchName := ctx.Params("*")
commitID := ctx.Query("commit")

defer func() {
redirectTo := ctx.Query("redirect_to")
if len(redirectTo) == 0 {
redirectTo = ctx.Repo.RepoLink
}
ctx.Redirect(redirectTo)
}()

if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
return
}
if len(commitID) > 0 {
branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
if err != nil {
log.Error(2, "GetBranchCommitID: %v", err)
return
}

ctx.Data["Branches"] = brs
ctx.HTML(200, tplBranch)
if branchCommitID != commitID {
ctx.Flash.Error(ctx.Tr("repo.pulls.delete_branch_has_new_commits"))
return
}
}

if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
Force: true,
}); err != nil {
log.Error(2, "DeleteBranch '%s': %v", branchName, err)
return
}
}
6 changes: 6 additions & 0 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,12 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/milestones", repo.Milestones)
}, context.RepoRef())

m.Group("/branches", func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this group needs context.CheckUnit(models.UnitTypeCode)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

m.Get("", repo.Branches)
m.Get("/all", repo.AllBranches)
m.Post("/delete/*", reqSignIn, reqRepoWriter, repo.DeleteBranchPost)
}, repo.MustBeNotBare, context.CheckUnit(models.UnitTypeCode))

m.Group("/wiki", func() {
m.Get("/?:page", repo.Uncyclo)
m.Get("/_pages", repo.UncycloPages)
Expand Down
31 changes: 31 additions & 0 deletions templates/repo/branches/all.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{{template "base/head" .}}
<div class="repository branches all">
{{template "repo/header" .}}
<div class="ui container">
<div class="navbar">
{{template "repo/branches/navbar" .}}
</div>
<div class="ui top attached header">
{{.i18n.Tr "repo.branches.all"}}
</div>
<div class="ui attached segment list">
{{range .Branches}}
<div class="item ui grid">
<div class="ui eleven wide column">
{{if .IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.Name}}"><code>{{.Name}}</code></a>
{{$timeSince := TimeSince .Commit.Committer.When $.Lang}}
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .Commit.Committer.Name | Safe}}</span>
</div>
<div class="ui four wide column">
{{if and (and (eq $.BranchName .Name) $.IsRepositoryAdmin) (not $.Repository.IsMirror)}}
<a class="ui basic blue button" href="{{$.RepoLink}}/settings/branches">{{$.i18n.Tr "repo.branches.change_default_branch"}}</a>
{{else if and $.IsRepositoryWriter $.AllowPullRequest}}
<a class="ui basic button" href="{{$.RepoLink}}/compare/{{$.BranchName}}...{{.Name}}"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.new"}}</a>
{{end}}
</div>
</div>
{{end}}
</div>
</div>
</div>
{{template "base/footer" .}}
4 changes: 4 additions & 0 deletions templates/repo/branches/navbar.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div class="ui compact small menu">
<a class="{{if .PageIsBranchesOverview}}active{{end}} item" href="{{.RepoLink}}/branches">{{.i18n.Tr "repo.branches.overview"}}</a>
<a class="{{if .PageIsBranchesAll}}active{{end}} item" href="{{.RepoLink}}/branches/all">{{.i18n.Tr "repo.branches.all"}}</a>
</div>
71 changes: 71 additions & 0 deletions templates/repo/branches/overview.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{{template "base/head" .}}
<div class="repository branches overview">
{{template "repo/header" .}}
<div class="ui container">
<div class="navbar">
{{template "repo/branches/navbar" .}}
</div>
<div class="ui top attached header">
{{.i18n.Tr "repo.default_branch"}}
</div>
<div class="ui attached segment list">
<div class="item ui grid">
<div class="ui eleven wide column">
{{if .DefaultBranch.IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.DefaultBranch.Name}}"><code>{{.DefaultBranch.Name}}</code></a>
{{$timeSince := TimeSince .DefaultBranch.Commit.Committer.When $.Lang}}
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .DefaultBranch.Commit.Committer.Name | Safe}}</span>
</div>
{{if and $.IsRepositoryAdmin (not $.Repository.IsMirror)}}
<div class="ui four wide column">
<a class="ui basic blue button" href="{{$.RepoLink}}/settings/branches">{{.i18n.Tr "repo.branches.change_default_branch"}}</a>
</div>
{{end}}
</div>
</div>

{{if .ActiveBranches}}
<div class="ui top attached header">
{{.i18n.Tr "repo.branches.active_branches"}}
</div>
<div class="ui attached segment list">
{{range .ActiveBranches}}
<div class="item ui grid">
<div class="ui eleven wide column">
{{if .IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.Name}}"><code>{{.Name}}</code></a>
{{$timeSince := TimeSince .Commit.Committer.When $.Lang}}
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .Commit.Committer.Name | Safe}}</span>
</div>
{{if and $.IsRepositoryWriter $.AllowPullRequest}}
<div class="ui four wide column">
<a class="ui basic button" href="{{$.RepoLink}}/compare/{{$.BranchName}}...{{.Name}}"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.new"}}</a>
</div>
{{end}}
</div>
{{end}}
</div>
{{end}}

{{if .StaleBranches}}
<div class="ui top attached header">
{{.i18n.Tr "repo.branches.stale_branches"}}
</div>
<div class="ui attached segment list">
{{range .StaleBranches}}
<div class="item ui grid">
<div class="ui eleven wide column">
{{if .IsProtected}}<i class="octicon octicon-shield"></i> {{end}}<a class="markdown" href="{{$.RepoLink}}/src/{{.Name}}"><code>{{.Name}}</code></a>
{{$timeSince := TimeSince .Commit.Committer.When $.Lang}}
<span class="ui text light grey">{{$.i18n.Tr "repo.branches.updated_by" $timeSince .Commit.Committer.Name | Safe}}</span>
</div>
{{if and $.IsRepositoryWriter $.AllowPullRequest}}
<div class="ui four wide column">
<a class="ui basic button" href="{{$.RepoLink}}/compare/{{$.BranchName}}...{{.Name}}"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.new"}}</a>
</div>
{{end}}
</div>
{{end}}
</div>
{{end}}
</div>
</div>
{{template "base/footer" .}}