summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-05 20:04:38 -0500
committerJeff Carr <[email protected]>2025-10-05 20:04:38 -0500
commitd9ed0838c403e982f572d108542bcc16705049d9 (patch)
tree5d8229be3dbfcd068219417cf0039e7e37735eca
parent7360716b35b452595e93c554d5f342b9f0338ea2 (diff)
logic for branch testing
-rw-r--r--branches.go23
-rw-r--r--compare.go75
-rw-r--r--deleteTags.go68
-rw-r--r--gitTag.common.go28
-rw-r--r--shell.go27
5 files changed, 172 insertions, 49 deletions
diff --git a/branches.go b/branches.go
index b656522..c717db9 100644
--- a/branches.go
+++ b/branches.go
@@ -2,7 +2,6 @@ package gitpb
import (
"errors"
- "fmt"
"os"
"path/filepath"
"strings"
@@ -76,28 +75,6 @@ func (repo *Repo) GetBranchDifferences(to string, from string) []string {
return result.Stdout
}
-// 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, err := repo.CountDiffObjects(branch, remote) // should be zero
- if err != nil {
- return err
- }
- if b1 == 0 {
- cmd := []string{"git", "branch", "-D", repo.GetDevelBranchName()}
- _, err := repo.RunVerboseOnError(cmd)
- return err
- } else {
- return fmt.Errorf("local branch has patches not in remote")
- }
-}
-
// makes a local branch based off of the master branch
// (unless a remote devel branch exists. then it uses that)
func (repo *Repo) MakeLocalDevelBranch() (*cmd.Status, error) {
diff --git a/compare.go b/compare.go
index 340206d..ed362f7 100644
--- a/compare.go
+++ b/compare.go
@@ -1,5 +1,11 @@
package gitpb
+import (
+ "fmt"
+
+ "go.wit.com/log"
+)
+
type RepoTag struct {
r *Repo
t *GitTag
@@ -16,16 +22,46 @@ func (r *Repo) NewCompareTag(refname string) *RepoTag {
return rt
}
-// if t1 is user branch, and t2 is devel branch, true if 0
-func (t1 *RepoTag) LessThan(t2 *RepoTag) bool {
- count, err := t1.r.CountDiffObjects(t1.t.Refname, t2.t.Refname)
+func (r *Repo) NewCompareRef(t *GitTag) *RepoTag {
+ if t == nil {
+ return nil
+ }
+ rt := new(RepoTag)
+ rt.r = r
+ rt.t = t
+ return rt
+}
+
+func (t1 *RepoTag) DeleteBranch(t2 *RepoTag) ([]string, []string, error) {
+ lines1, err1 := t1.r.CountDiffObjectsNew(t1.t.Refname, t2.t.Refname)
+ // log.Info("lessthan", t1.t.Refname, t2.t.Refname, count, t1.r.FullPath)
+ if err1 != nil {
+ // log.Info("lessthan", t1.t.Refname, t2.t.Refname, count, t1.r.FullPath, err)
+ return nil, nil, err1
+ }
+ lines2, err2 := t1.r.CountDiffObjectsNew(t2.t.Refname, t1.t.Refname)
+ // log.Info("lessthan", t1.t.Refname, t2.t.Refname, count, t1.r.FullPath)
+ if err2 != nil {
+ // log.Info("lessthan", t1.t.Refname, t2.t.Refname, count, t1.r.FullPath, err)
+ return nil, nil, err2
+ }
+ if (len(lines1) != 0) || (len(lines2) != 0) {
+ return lines1, lines2, fmt.Errorf("nope")
+ }
+ return lines1, lines2, nil
+}
+
+func (t1 *RepoTag) LessThanVerbose(t2 *RepoTag) []string {
+ count, err := t1.r.CountDiffObjectsNew(t1.t.Refname, t2.t.Refname)
+ log.Info("lessthan", t1.t.Refname, t2.t.Refname, len(count), t1.r.FullPath)
if err != nil {
- return false
+ log.Info("lessthan", t1.t.Refname, t2.t.Refname, len(count), t1.r.FullPath, err)
+ return nil
}
- if count == 0 {
- return true
+ if len(count) == 0 {
+ return nil
}
- return false
+ return count
}
func (t1 *RepoTag) Equal(t2 *RepoTag) bool {
@@ -33,13 +69,26 @@ func (t1 *RepoTag) Equal(t2 *RepoTag) bool {
}
// if t1 is user branch, and t2 is devel branch, true if 0
-func (t1 *RepoTag) GreaterThan(t2 *RepoTag) bool {
- count, err := t1.r.CountDiffObjects(t2.t.Refname, t1.t.Refname)
+func (t1 *RepoTag) GreaterThan(t2 *RepoTag) []string {
+ lines, err := t1.r.CountDiffObjectsNew(t2.t.Refname, t1.t.Refname)
+ // log.Info("greaterthan", t1.t.Refname, t2.t.Refname, count, t1.r.FullPath, err)
if err != nil {
- return false
+ return nil
}
- if count == 0 {
- return true
+ if len(lines) == 0 {
+ return lines
}
- return false
+ return nil
+}
+
+func (r *Repo) GetLocalUserRef() *GitTag {
+ return r.IsBranchLocal(r.GetUserBranchName())
+}
+
+func (r *Repo) GetLocalDevelRef() *GitTag {
+ return r.IsBranchLocal(r.GetDevelBranchName())
+}
+
+func (r *Repo) GetLocalMasterRef() *GitTag {
+ return r.IsBranchLocal(r.GetMasterBranchName())
}
diff --git a/deleteTags.go b/deleteTags.go
new file mode 100644
index 0000000..0b05238
--- /dev/null
+++ b/deleteTags.go
@@ -0,0 +1,68 @@
+package gitpb
+
+import (
+ "fmt"
+ "path/filepath"
+)
+
+/*
+func (repo *Repo) DeleteLocalTag(t *GitTag) bool {
+ b1, err := repo.CountDiffObjects(branch, remote) // should be zero
+ if err != nil {
+ return err
+ }
+ if b1 == 0 {
+ cmd := []string{"git", "branch", "-D", repo.GetDevelBranchName()}
+ _, err := repo.RunVerboseOnError(cmd)
+ return err
+ } else {
+ return fmt.Errorf("local branch has patches not in remote")
+ }
+}
+*/
+
+// deletes the devel local branch if it is a subset of the remote devel branch
+func (repo *Repo) DeleteLocalBranch(branch string) ([]string, error) {
+ remote := filepath.Join("origin", branch)
+
+ if !repo.IsBranchRemote(branch) {
+ return nil, fmt.Errorf("no remote branch")
+ }
+
+ b1, err := repo.CountDiffObjectsNew(branch, remote) // should be zero
+ if err != nil {
+ return nil, err
+ }
+ if len(b1) == 0 {
+ // log.Info("SAFE TO DELETE?")
+ /*
+ cmd := []string{"git", "branch", "-D", repo.GetDevelBranchName()}
+ _, err := repo.RunVerboseOnError(cmd)
+ */
+ return nil, nil
+ }
+ // log.Info("LOCAL NOT SAFE TO DELETE", repo.FullPath)
+ return b1, fmt.Errorf("local branch has patches not in remote")
+}
+
+// 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, err := repo.CountDiffObjects(branch, remote) // should be zero
+ if err != nil {
+ return err
+ }
+ if b1 == 0 {
+ cmd := []string{"git", "branch", "-D", repo.GetDevelBranchName()}
+ _, err := repo.RunVerboseOnError(cmd)
+ return err
+ } else {
+ return fmt.Errorf("local branch has patches not in remote")
+ }
+}
diff --git a/gitTag.common.go b/gitTag.common.go
index 7535cd9..ac0f09c 100644
--- a/gitTag.common.go
+++ b/gitTag.common.go
@@ -85,19 +85,8 @@ func (repo *Repo) IsBranchRemote(brname string) bool {
// this is the correct way. uses 'git show-ref'
func (repo *Repo) IsDevelRemote() bool {
- if repo.Tags == nil {
- return false
- }
-
devname := repo.GetDevelBranchName()
- refname := "refs/remotes/origin/" + devname
- ref := repo.Tags.FindByRefname(refname)
- if ref == nil {
- // log.Info("did not found refname!!!!!!!!", refname)
- return false
- }
- // log.Info("found refname!!!!!!!!")
- return true
+ return repo.IsBranchRemote(devname)
}
// find a branch namm
@@ -124,6 +113,21 @@ func (repo *Repo) IsBranch(findname string) bool {
return false
}
+func (repo *Repo) IsBranchLocal(findname string) *GitTag {
+ for t := range repo.Tags.IterAll() {
+ if !strings.HasPrefix(t.Refname, "refs/heads") {
+ continue
+ }
+ path, filename := filepath.Split(t.Refname)
+ log.Log(INFO, "gitpb.IsBranch() tag:", path, filename, "from", repo.GetGoPath())
+ if filename == findname {
+ log.Log(INFO, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GetGoPath())
+ return t
+ }
+ }
+ return nil
+}
+
func (repo *Repo) IsLocalBranch(findname string) bool {
for t := range repo.Tags.IterAll() {
if !strings.HasPrefix(t.Refname, "refs/heads") {
diff --git a/shell.go b/shell.go
index 54b34c9..ace3482 100644
--- a/shell.go
+++ b/shell.go
@@ -170,7 +170,7 @@ func (repo *Repo) ConstructGitDiffLog(branch1, branch2 string) []string {
var cmd []string
cmd = append(cmd, "git")
cmd = append(cmd, "log")
- cmd = append(cmd, "--format=\"%H %ae %as %s\"")
+ cmd = append(cmd, "--format=\"%H%00%ae%00%as%00%s\"")
cmd = append(cmd, branch1)
cmd = append(cmd, "--not")
cmd = append(cmd, branch2)
@@ -188,6 +188,31 @@ func (repo *Repo) CountDiffObjects(branch1, branch2 string) (int, error) {
return len(r.Stdout), err
}
+// count all objects only in branch1
+func (repo *Repo) CountDiffObjectsNew(branch1, branch2 string) ([]string, error) {
+ cmd := repo.ConstructGitDiffLog(branch1, branch2)
+ r, err := repo.RunVerboseOnError(cmd)
+ if err != nil {
+ return nil, err
+ }
+ // log.Info("countDiffObjects()", cmd, len(r.Stdout), strings.Join(r.Stdout, " "))
+ return r.Stdout, err
+}
+
+func (repo *Repo) CountDiffObjectsVerbose(branch1, branch2 string) ([]string, error) {
+ cmd := repo.ConstructGitDiffLog(branch1, branch2)
+ r, err := repo.RunVerboseOnError(cmd)
+ if err != nil {
+ return nil, err
+ }
+ log.Info("Run:", cmd)
+ for _, line := range r.Stdout {
+ log.Info(line, branch1, branch2, repo.FullPath)
+ }
+ // log.Info("countDiffObjects()", cmd, len(r.Stdout), strings.Join(r.Stdout, " "))
+ return r.Stdout, err
+}
+
func (repo *Repo) RunPipe(cmd1 []string, cmd2 []string) cmd.Status {
var s cmd.Status
var arg0 string