branch.go 771 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package models
  2. import (
  3. "github.com/gogits/git-module"
  4. )
  5. type Branch struct {
  6. Path string
  7. Name string
  8. }
  9. func GetBranchesByPath(path string) ([]*Branch, error) {
  10. gitRepo, err := git.OpenRepository(path)
  11. if err != nil {
  12. return nil, err
  13. }
  14. brs, err := gitRepo.GetBranches()
  15. if err != nil {
  16. return nil, err
  17. }
  18. Branches := make([]*Branch, len(brs))
  19. for i := range brs {
  20. Branches[i] = &Branch{
  21. Path: path,
  22. Name: brs[i],
  23. }
  24. }
  25. return Branches, nil
  26. }
  27. func GetBranchesByRepo(user,repo string) ([]*Branch, error) {
  28. return GetBranchesByPath(RepoPath(user, repo))
  29. }
  30. func (br *Branch) GetCommit() (*git.Commit, error) {
  31. gitRepo, err := git.OpenRepository(br.Path)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return gitRepo.GetBranchCommit(br.Name)
  36. }