Files
Gogs/internal/route/repo/branch.go

156 lines
3.6 KiB
Go
Raw Normal View History

2014-03-24 18:25:15 +08:00
// Copyright 2014 The Gogs 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 (
"time"
log "unknwon.dev/clog/v2"
2017-02-09 19:29:59 -05:00
2018-05-27 08:53:48 +08:00
"github.com/gogs/git-module"
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/tool"
2014-03-24 18:25:15 +08:00
)
2014-06-22 23:11:12 -04:00
const (
2017-04-05 09:17:21 -04:00
BRANCHES_OVERVIEW = "repo/branches/overview"
BRANCHES_ALL = "repo/branches/all"
2014-06-22 23:11:12 -04:00
)
type Branch struct {
Name string
Commit *git.Commit
IsProtected bool
}
2014-05-25 20:11:25 -04:00
2017-06-03 07:26:09 -04:00
func loadBranches(c *context.Context) []*Branch {
rawBranches, err := c.Repo.Repository.GetBranches()
2014-03-24 18:25:15 +08:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "GetBranches", err)
return nil
}
protectBranches, err := db.GetProtectBranchesByRepoID(c.Repo.Repository.ID)
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "GetProtectBranchesByRepoID", err)
return nil
}
branches := make([]*Branch, len(rawBranches))
for i := range rawBranches {
commit, err := rawBranches[i].GetCommit()
if err != nil {
2017-06-03 07:26:09 -04:00
c.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].Name {
branches[i].IsProtected = true
break
}
}
}
2017-06-03 07:26:09 -04:00
c.Data["AllowPullRequest"] = c.Repo.Repository.AllowsPulls()
return branches
}
2017-06-03 07:26:09 -04:00
func Branches(c *context.Context) {
c.Data["Title"] = c.Tr("repo.git_branches")
c.Data["PageIsBranchesOverview"] = true
2017-06-03 07:26:09 -04:00
branches := loadBranches(c)
if c.Written() {
2014-03-24 18:25:15 +08:00
return
}
now := time.Now()
activeBranches := make([]*Branch, 0, 3)
staleBranches := make([]*Branch, 0, 3)
for i := range branches {
switch {
2017-06-03 07:26:09 -04:00
case branches[i].Name == c.Repo.BranchName:
c.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])
}
}
2017-06-03 07:26:09 -04:00
c.Data["ActiveBranches"] = activeBranches
c.Data["StaleBranches"] = staleBranches
c.HTML(200, BRANCHES_OVERVIEW)
}
2017-06-03 07:26:09 -04:00
func AllBranches(c *context.Context) {
c.Data["Title"] = c.Tr("repo.git_branches")
c.Data["PageIsBranchesAll"] = true
2017-06-03 07:26:09 -04:00
branches := loadBranches(c)
if c.Written() {
2014-03-24 18:25:15 +08:00
return
}
2017-06-03 07:26:09 -04:00
c.Data["Branches"] = branches
2014-03-24 18:25:15 +08:00
2017-06-03 07:26:09 -04:00
c.HTML(200, BRANCHES_ALL)
2014-03-24 18:25:15 +08:00
}
2017-06-03 07:26:09 -04:00
func DeleteBranchPost(c *context.Context) {
branchName := c.Params("*")
commitID := c.Query("commit")
defer func() {
2017-06-03 07:26:09 -04:00
redirectTo := c.Query("redirect_to")
if !tool.IsSameSiteURLPath(redirectTo) {
2017-06-03 07:26:09 -04:00
redirectTo = c.Repo.RepoLink
}
2017-06-03 07:26:09 -04:00
c.Redirect(redirectTo)
}()
git: migrate to github.com/gogs/git-module@v1.0.0 (#5958) * WIP * Finish `internal/db/git_diff.go` * FInish internal/db/mirror.go * Finish internal/db/pull.go * Finish internal/db/release.go * Finish internal/db/repo.go * Finish internal/db/repo_branch.go * Finish internal/db/repo_editor.go * Finish internal/db/update.go * Save my work * Add license header * Compile! * Merge master * Finish internal/cmd/hook.go * Finish internal/conf/static.go * Finish internal/context/repo.go * Finish internal/db/action.go * Finish internal/db/git_diff.go * Fix submodule URL inferring * Finish internal/db/mirror.go * Updat to beta.4 * css: update fonts * Finish internal/db/pull.go * Finish internal/db/release.go * Finish internal/db/repo_branch.go * Finish internal/db/wiki.go * gitutil: enhance infer submodule UR * Finish internal/route/api/v1/repo/commits.go * mirror: only collect branch commits after sync * mirror: fix tag support * Finish internal/db/repo.go * Finish internal/db/repo_editor.go * Finish internal/db/update.go * Finish internal/gitutil/pull_request.go * Make it compile * Finish internal/route/repo/setting.go * Finish internal/route/repo/branch.go * Finish internal/route/api/v1/repo/file.go * Finish internal/route/repo/download.go * Finish internal/route/repo/editor.go * Use helper * Finish internal/route/repo/issue.go * Finish internal/route/repo/pull.go * Finish internal/route/repo/release.go * Finish internal/route/repo/repo.go * Finish internal/route/repo/wiki.go * Finish internal/route/repo/commit.go * Finish internal/route/repo/view.go * Finish internal/gitutil/tag.go * go.sum
2020-03-08 19:09:31 +08:00
if !c.Repo.GitRepo.HasBranch(branchName) {
return
}
if len(commitID) > 0 {
git: migrate to github.com/gogs/git-module@v1.0.0 (#5958) * WIP * Finish `internal/db/git_diff.go` * FInish internal/db/mirror.go * Finish internal/db/pull.go * Finish internal/db/release.go * Finish internal/db/repo.go * Finish internal/db/repo_branch.go * Finish internal/db/repo_editor.go * Finish internal/db/update.go * Save my work * Add license header * Compile! * Merge master * Finish internal/cmd/hook.go * Finish internal/conf/static.go * Finish internal/context/repo.go * Finish internal/db/action.go * Finish internal/db/git_diff.go * Fix submodule URL inferring * Finish internal/db/mirror.go * Updat to beta.4 * css: update fonts * Finish internal/db/pull.go * Finish internal/db/release.go * Finish internal/db/repo_branch.go * Finish internal/db/wiki.go * gitutil: enhance infer submodule UR * Finish internal/route/api/v1/repo/commits.go * mirror: only collect branch commits after sync * mirror: fix tag support * Finish internal/db/repo.go * Finish internal/db/repo_editor.go * Finish internal/db/update.go * Finish internal/gitutil/pull_request.go * Make it compile * Finish internal/route/repo/setting.go * Finish internal/route/repo/branch.go * Finish internal/route/api/v1/repo/file.go * Finish internal/route/repo/download.go * Finish internal/route/repo/editor.go * Use helper * Finish internal/route/repo/issue.go * Finish internal/route/repo/pull.go * Finish internal/route/repo/release.go * Finish internal/route/repo/repo.go * Finish internal/route/repo/wiki.go * Finish internal/route/repo/commit.go * Finish internal/route/repo/view.go * Finish internal/gitutil/tag.go * go.sum
2020-03-08 19:09:31 +08:00
branchCommitID, err := c.Repo.GitRepo.BranchCommitID(branchName)
if err != nil {
log.Error("Failed to get commit ID of branch %q: %v", branchName, err)
return
}
if branchCommitID != commitID {
2017-06-03 07:26:09 -04:00
c.Flash.Error(c.Tr("repo.pulls.delete_branch_has_new_commits"))
return
}
}
2017-06-03 07:26:09 -04:00
if err := c.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
Force: true,
}); err != nil {
log.Error("Failed to delete branch %q: %v", branchName, err)
return
}
if err := db.PrepareWebhooks(c.Repo.Repository, db.HOOK_EVENT_DELETE, &api.DeletePayload{
Ref: branchName,
RefType: "branch",
PusherType: api.PUSHER_TYPE_USER,
Repo: c.Repo.Repository.APIFormat(nil),
Sender: c.User.APIFormat(),
}); err != nil {
log.Error("Failed to prepare webhooks for %q: %v", db.HOOK_EVENT_DELETE, err)
return
}
}