forked from Gitlink/gitea_hat
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package git
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
)
|
|
|
|
type Branch struct {
|
|
*api.Branch
|
|
CommitID string `json:"commit_id"` // add configure
|
|
CommitTime string `json:"commit_time"` // add configure
|
|
DefaultBranch string `json:"default_branch"`
|
|
BranchKind int `json:"branch_kind"`
|
|
}
|
|
|
|
type BranchKind int
|
|
|
|
const (
|
|
None BranchKind = iota
|
|
DefaultBranch
|
|
ProtectedBranch
|
|
OtherBranch
|
|
)
|
|
|
|
func (bk BranchKind) Name() string {
|
|
return strings.ToLower(bk.Title())
|
|
}
|
|
func (bk BranchKind) Title() string {
|
|
switch bk {
|
|
case DefaultBranch:
|
|
return "default"
|
|
case ProtectedBranch:
|
|
return "protected"
|
|
case OtherBranch:
|
|
return "other"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type BranchesSlice struct {
|
|
BranchName string `json:"branch_name"`
|
|
// BranchKind int `json:"branch_kind"`
|
|
Branches []*Branch `json:"branches"`
|
|
}
|
|
|
|
// sort by branchkind
|
|
type SortBranch []*Branch
|
|
|
|
func (s SortBranch) Len() int { return len(s) }
|
|
func (s SortBranch) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
func (s SortBranch) Less(i, j int) bool { return s[i].BranchKind < s[j].BranchKind }
|
|
|
|
// sort by CommiTime of the branch
|
|
type SortBranchTime []*Branch
|
|
|
|
func (s SortBranchTime) Len() int { return len(s) }
|
|
func (s SortBranchTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
func (s SortBranchTime) Less(i, j int) bool { return s[i].CommitTime > s[j].CommitTime }
|
|
|
|
func GetBranchesByPathNoLimit(ctx context.Context, path string) ([]*git.Branch, int, error) {
|
|
gitRepo, err := git.OpenRepository(ctx, path)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer gitRepo.Close()
|
|
brs, countAll, err := gitRepo.GetBranchNames(0, 0)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
branches := make([]*git.Branch, len(brs))
|
|
for i := range brs {
|
|
branches[i] = &git.Branch{
|
|
Path: gitRepo.Path,
|
|
Name: brs[i],
|
|
}
|
|
}
|
|
|
|
return branches, countAll, nil
|
|
}
|