mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
3.1 KiB
143 lines
3.1 KiB
11 years ago
|
// 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 (
|
||
8 years ago
|
"time"
|
||
|
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
|
|
||
8 years ago
|
"github.com/gogits/git-module"
|
||
|
|
||
8 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
BRANCHES_OVERVIEW = "repo/branches/overview"
|
||
|
BRANCHES_ALL = "repo/branches/all"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
type Branch struct {
|
||
|
Name string
|
||
|
Commit *git.Commit
|
||
|
IsProtected bool
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
func loadBranches(c *context.Context) []*Branch {
|
||
|
rawBranches, err := c.Repo.Repository.GetBranches()
|
||
11 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetBranches", err)
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
protectBranches, err := models.GetProtectBranchesByRepoID(c.Repo.Repository.ID)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetProtectBranchesByRepoID", err)
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
branches := make([]*Branch, len(rawBranches))
|
||
|
for i := range rawBranches {
|
||
|
commit, err := rawBranches[i].GetCommit()
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetCommit", err)
|
||
8 years ago
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["AllowPullRequest"] = c.Repo.Repository.AllowsPulls()
|
||
8 years ago
|
return branches
|
||
|
}
|
||
|
|
||
8 years ago
|
func Branches(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.git_branches")
|
||
|
c.Data["PageIsBranchesOverview"] = true
|
||
8 years ago
|
|
||
8 years ago
|
branches := loadBranches(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
8 years ago
|
}
|
||
|
|
||
|
now := time.Now()
|
||
|
activeBranches := make([]*Branch, 0, 3)
|
||
|
staleBranches := make([]*Branch, 0, 3)
|
||
|
for i := range branches {
|
||
|
switch {
|
||
8 years ago
|
case branches[i].Name == c.Repo.BranchName:
|
||
|
c.Data["DefaultBranch"] = branches[i]
|
||
8 years ago
|
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])
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["ActiveBranches"] = activeBranches
|
||
|
c.Data["StaleBranches"] = staleBranches
|
||
|
c.HTML(200, BRANCHES_OVERVIEW)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func AllBranches(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.git_branches")
|
||
|
c.Data["PageIsBranchesAll"] = true
|
||
8 years ago
|
|
||
8 years ago
|
branches := loadBranches(c)
|
||
|
if c.Written() {
|
||
11 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Branches"] = branches
|
||
11 years ago
|
|
||
8 years ago
|
c.HTML(200, BRANCHES_ALL)
|
||
11 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
func DeleteBranchPost(c *context.Context) {
|
||
|
branchName := c.Params("*")
|
||
|
commitID := c.Query("commit")
|
||
8 years ago
|
|
||
|
defer func() {
|
||
8 years ago
|
redirectTo := c.Query("redirect_to")
|
||
8 years ago
|
if len(redirectTo) == 0 {
|
||
8 years ago
|
redirectTo = c.Repo.RepoLink
|
||
8 years ago
|
}
|
||
8 years ago
|
c.Redirect(redirectTo)
|
||
8 years ago
|
}()
|
||
|
|
||
8 years ago
|
if !c.Repo.GitRepo.IsBranchExist(branchName) {
|
||
8 years ago
|
return
|
||
|
}
|
||
|
if len(commitID) > 0 {
|
||
8 years ago
|
branchCommitID, err := c.Repo.GitRepo.GetBranchCommitID(branchName)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
log.Error(2, "GetBranchCommitID: %v", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if branchCommitID != commitID {
|
||
8 years ago
|
c.Flash.Error(c.Tr("repo.pulls.delete_branch_has_new_commits"))
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if err := c.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
|
||
8 years ago
|
Force: true,
|
||
8 years ago
|
}); err != nil {
|
||
8 years ago
|
log.Error(2, "DeleteBranch '%s': %v", branchName, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
}
|