diff --git a/branches.go b/branches.go index a2635d7..ec86c20 100644 --- a/branches.go +++ b/branches.go @@ -1,7 +1,12 @@ package gitpb import ( + "errors" + "os" "path/filepath" + "strings" + + "go.wit.com/log" ) // returns true if 'git pull' will work @@ -50,3 +55,71 @@ func (repo *Repo) ExistsDevelBranch() bool { } return false } + +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 +} diff --git a/reloadRepoState.go b/reloadRepoState.go index b4928c7..9536234 100644 --- a/reloadRepoState.go +++ b/reloadRepoState.go @@ -26,8 +26,13 @@ func (repo *Repo) setRepoState() { repo.State = "merge to main" return } + // if IsGoTagVersionGreater(oldtag string, newtag string) bool { + if !IsGoTagVersionGreater(repo.GetLastTag(), repo.GetMasterVersion()) { + repo.State = "last tag greater error" + return + } if repo.GetLastTag() != repo.GetMasterVersion() { - repo.State = "unchanged" + repo.State = "ready to release" return } @@ -40,3 +45,29 @@ func (repo *Repo) setRepoState() { log.Info("Branches are not Perfect", repo.GetFullPath()) repo.State = "unknown branches" } + +// returns true if old="v0.2.4" and new="v0.3.3" +// returns true if equal +// todo: make all of this smarter someday +func IsGoTagVersionGreater(oldtag string, newtag string) bool { + olda, oldb, oldc := splitInts(oldtag) + newa, newb, newc := splitInts(newtag) + + if newa < olda { + return false + } + if newb < oldb { + return false + } + if newc < oldc { + return false + } + return true +} + +// returns true for "v0.2.4" and false for "v0.2.43-asdfj" +// actually returns false for anything not perfectly versioned +func IsGoTagPublished(oldtag string, newtag string) bool { + // todo + return true +}