repo_branch.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "github.com/gogits/git-module"
  8. )
  9. type Branch struct {
  10. Path string
  11. Name string
  12. }
  13. func GetBranchesByPath(path string) ([]*Branch, error) {
  14. gitRepo, err := git.OpenRepository(path)
  15. if err != nil {
  16. return nil, err
  17. }
  18. brs, err := gitRepo.GetBranches()
  19. if err != nil {
  20. return nil, err
  21. }
  22. branches := make([]*Branch, len(brs))
  23. for i := range brs {
  24. branches[i] = &Branch{
  25. Path: path,
  26. Name: brs[i],
  27. }
  28. }
  29. return branches, nil
  30. }
  31. func (repo *Repository) GetBranch(br string) (*Branch, error) {
  32. if !git.IsBranchExist(repo.RepoPath(), br) {
  33. return nil, ErrBranchNotExist{br}
  34. }
  35. return &Branch{
  36. Path: repo.RepoPath(),
  37. Name: br,
  38. }, nil
  39. }
  40. func (repo *Repository) GetBranches() ([]*Branch, error) {
  41. return GetBranchesByPath(repo.RepoPath())
  42. }
  43. func (br *Branch) GetCommit() (*git.Commit, error) {
  44. gitRepo, err := git.OpenRepository(br.Path)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return gitRepo.GetBranchCommit(br.Name)
  49. }
  50. // ProtectBranch contains options of a protected branch.
  51. type ProtectBranch struct {
  52. ID int64
  53. RepoID int64 `xorm:"UNIQUE(protect_branch)"`
  54. Name string `xorm:"UNIQUE(protect_branch)"`
  55. Protected bool
  56. RequirePullRequest bool
  57. }
  58. // GetProtectBranchOfRepoByName returns *ProtectBranch by branch name in given repostiory.
  59. func GetProtectBranchOfRepoByName(repoID int64, name string) (*ProtectBranch, error) {
  60. protectBranch := &ProtectBranch{
  61. RepoID: repoID,
  62. Name: name,
  63. }
  64. has, err := x.Get(protectBranch)
  65. if err != nil {
  66. return nil, err
  67. } else if !has {
  68. return nil, ErrBranchNotExist{name}
  69. }
  70. return protectBranch, nil
  71. }
  72. // IsBranchOfRepoRequirePullRequest returns true if branch requires pull request in given repository.
  73. func IsBranchOfRepoRequirePullRequest(repoID int64, name string) bool {
  74. protectBranch, err := GetProtectBranchOfRepoByName(repoID, name)
  75. if err != nil {
  76. return false
  77. }
  78. return protectBranch.Protected && protectBranch.RequirePullRequest
  79. }
  80. // UpdateProtectBranch saves branch protection options.
  81. // If ID is 0, it creates a new record. Otherwise, updates existing record.
  82. func UpdateProtectBranch(protectBranch *ProtectBranch) (err error) {
  83. if protectBranch.ID == 0 {
  84. if _, err = x.Insert(protectBranch); err != nil {
  85. return fmt.Errorf("Insert: %v", err)
  86. }
  87. return
  88. }
  89. _, err = x.Id(protectBranch.ID).AllCols().Update(protectBranch)
  90. return err
  91. }
  92. // GetProtectBranchesByRepoID returns a list of *ProtectBranch in given repostiory.
  93. func GetProtectBranchesByRepoID(repoID int64) ([]*ProtectBranch, error) {
  94. protectBranches := make([]*ProtectBranch, 0, 2)
  95. return protectBranches, x.Where("repo_id = ?", repoID).Asc("name").Find(&protectBranches)
  96. }