gitpb/branches.go

85 lines
2.1 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"
2025-02-15 17:07:49 -06:00
"fmt"
2025-01-29 16:19:11 -06:00
"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
)
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
}
2025-01-29 16:19:11 -06:00
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}
2025-01-30 01:48:17 -06:00
r, err := repo.RunStrict(cmd)
2025-01-29 16:19:11 -06:00
if err != nil {
return "", err
}
if len(r.Stdout) == 0 {
return "", errors.New("git describe was empty")
}
return r.Stdout[0], nil
}
2025-01-29 20:01:07 -06:00
// lookup a hash from a tag with 'git rev-list'
func (repo *Repo) GetTagHash(t string) string {
// git rev-list -n 1 v0.0.66
cmd := []string{"git", "rev-list", "-n", "1", t}
2025-01-30 01:48:17 -06:00
result, _ := repo.RunStrict(cmd)
2025-01-29 20:01:07 -06:00
// log.Info("getLastTagVersion()", result.Stdout)
if len(result.Stdout) == 0 {
// log.Log(WARN, "no gitpb.LastTag() repo is broken. ignore this.", repo.GetGoPath())
return ""
}
return result.Stdout[0]
}
2025-02-15 17:07:49 -06:00
// deletes the devel local branch if it is a subset of the remote devel branch
func (repo *Repo) DeleteLocalDevelBranch() error {
branch := repo.GetDevelBranchName()
remote := filepath.Join("origin", branch)
if !repo.IsDevelRemote() {
return fmt.Errorf("no remote branch")
}
b1 := repo.CountDiffObjects(branch, remote) // should be zero
if b1 == 0 {
cmd := []string{"git", "branch", "-D", repo.GetDevelBranchName()}
log.Info("DEVEL IS IN REMOTE", repo.GetGoPath(), cmd)
err := repo.RunVerbose(cmd)
return err
} else {
return fmt.Errorf("local branch has patches not in remote")
}
}