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.

62 lines
1.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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. "code.gitea.io/git"
  7. )
  8. // Branch holds the branch information
  9. type Branch struct {
  10. Path string
  11. Name string
  12. }
  13. // GetBranchesByPath returns a branch by it's path
  14. func GetBranchesByPath(path string) ([]*Branch, error) {
  15. gitRepo, err := git.OpenRepository(path)
  16. if err != nil {
  17. return nil, err
  18. }
  19. brs, err := gitRepo.GetBranches()
  20. if err != nil {
  21. return nil, err
  22. }
  23. branches := make([]*Branch, len(brs))
  24. for i := range brs {
  25. branches[i] = &Branch{
  26. Path: path,
  27. Name: brs[i],
  28. }
  29. }
  30. return branches, nil
  31. }
  32. // GetBranch returns a branch by it's name
  33. func (repo *Repository) GetBranch(branch string) (*Branch, error) {
  34. if !git.IsBranchExist(repo.RepoPath(), branch) {
  35. return nil, &ErrBranchNotExist{branch}
  36. }
  37. return &Branch{
  38. Path: repo.RepoPath(),
  39. Name: branch,
  40. }, nil
  41. }
  42. // GetBranches returns all the branches of a repository
  43. func (repo *Repository) GetBranches() ([]*Branch, error) {
  44. return GetBranchesByPath(repo.RepoPath())
  45. }
  46. // GetCommit returns all the commits of a branch
  47. func (branch *Branch) GetCommit() (*git.Commit, error) {
  48. gitRepo, err := git.OpenRepository(branch.Path)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return gitRepo.GetBranchCommit(branch.Name)
  53. }