gitpb/branches.go

126 lines
3.0 KiB
Go
Raw Normal View History

2025-01-28 21:04:25 -06:00
package gitpb
import (
2025-01-29 16:19:11 -06:00
"errors"
"os"
2025-01-28 21:04:25 -06:00
"path/filepath"
2025-01-29 16:19:11 -06:00
"strings"
"go.wit.com/log"
2025-01-28 21:04:25 -06:00
)
// returns true if 'git pull' will work
func (repo *Repo) IsBranchRemote(branchname string) bool {
2025-01-29 01:12:32 -06:00
if branchname == "" {
return false
}
2025-01-28 21:04:25 -06:00
if repo.Exists(filepath.Join(".git/refs/remotes/origin", branchname)) {
// todo: actually use .git/config
return true
}
return false
}
2025-01-29 01:12:32 -06:00
// returns true if 'git pull' will work
func (repo *Repo) ExistsUserBranchRemote() bool {
branchname := repo.GetUserBranchName()
if repo.IsBranchRemote(branchname) {
return true
}
return false
}
// returns true if the user branch exists
func (repo *Repo) ExistsUserBranch() bool {
if repo.GetUserBranchName() == "" {
return false
}
branchname := repo.GetUserBranchName()
if repo.Exists(filepath.Join(".git/refs/heads", branchname)) {
// todo: actually use .git/config
return true
}
return false
}
2025-01-29 08:16:53 -06:00
// returns true if the devel branch exists
func (repo *Repo) ExistsDevelBranch() bool {
if repo.GetDevelBranchName() == "" {
return false
}
branchname := repo.GetDevelBranchName()
if repo.Exists(filepath.Join(".git/refs/heads", branchname)) {
// todo: actually use .git/config
return true
}
return false
}
2025-01-29 16:19:11 -06:00
func (repo *Repo) GetBranchHash(branchname string) string {
if branchname == "" {
return ""
}
if repo.Exists(filepath.Join(".git/refs/remotes", branchname)) {
return readRefHash(filepath.Join(repo.FullPath, ".git/refs/remotes", branchname))
}
if repo.Exists(filepath.Join(".git/refs/heads", branchname)) {
return readRefHash(filepath.Join(repo.FullPath, ".git/refs/heads", branchname))
}
return ""
}
func (repo *Repo) GetBranchVersion(branchname string) string {
if branchname == repo.GetUserBranchName() {
return "user"
}
if branchname == repo.GetDevelBranchName() {
return "devel"
}
if branchname == repo.GetMasterBranchName() {
return "master"
}
base, branchname := filepath.Split(branchname)
if base != "" {
if branchname == repo.GetUserBranchName() {
return "remote user"
}
if branchname == repo.GetDevelBranchName() {
return "remote devel"
}
if branchname == repo.GetMasterBranchName() {
return "remote master"
}
}
return ""
}
func readRefHash(filename string) string {
data, _ := os.ReadFile(filename)
return string(data)
}
func (repo *Repo) GetLocalBranches() []string {
return ListFiles(filepath.Join(repo.GetFullPath(), "/.git/refs/heads"))
}
func (repo *Repo) GetRemoteBranches() []string {
remotes := ListFiles(filepath.Join(repo.GetFullPath(), "/.git/refs/remotes"))
return remotes
}
// git describe --tags e548b0fb6d0d14cdfb693850d592419f247dc2b1
// v0.22.61-15-gbab84d7
func (repo *Repo) GetHashName(h string) (string, error) {
h = strings.TrimSpace(h)
log.Info("GetHashName() is looking for", repo.GetGoPath(), h)
cmd := []string{"git", "describe", "--tags", h}
r, err := repo.RunStrictNew(cmd)
if err != nil {
return "", err
}
if len(r.Stdout) == 0 {
return "", errors.New("git describe was empty")
}
return r.Stdout[0], nil
}