summaryrefslogtreecommitdiff
path: root/doClean.go
diff options
context:
space:
mode:
Diffstat (limited to 'doClean.go')
-rw-r--r--doClean.go77
1 files changed, 77 insertions, 0 deletions
diff --git a/doClean.go b/doClean.go
index 2729750..2cd0642 100644
--- a/doClean.go
+++ b/doClean.go
@@ -7,9 +7,11 @@ import (
"errors"
"fmt"
"path/filepath"
+ "strings"
"go.wit.com/lib/env"
"go.wit.com/lib/fhelp"
+ "go.wit.com/lib/protobuf/argvpb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
@@ -323,3 +325,78 @@ func checkPatchIds(repo *gitpb.Repo, b1 string, b2 string) error {
}
return baderr
}
+
+func findPatchIdInStats(pb *gitpb.Stats, patchId string) *gitpb.Stat {
+ for stat := range pb.IterAll() {
+ if stat.PatchId == patchId {
+ return stat
+ }
+ }
+ // log.Info("findPatchId searched in", pb.Len(), "stats")
+ return nil
+}
+
+// delete localRef if it's completely contained in the masterRef
+func safeDelete(r *gitpb.Repo, deleteHash string, keepHash string) error {
+ // compare the branches
+ hashok, hashbad, cmd1, cmd2, err := r.CompareHashes(keepHash, deleteHash)
+
+ if err != nil {
+ // things are really really messed up. might be 'branchless' at this point (?)
+ log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", r.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1)
+ log.Printf("%-13.13s %-55.55s err='%v' %s %v\n", "CMD ERR", r.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2)
+ return err
+ }
+
+ // things only in the master branch (safe to ignore)
+ for _, line := range hashok {
+ parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL
+ patchId, err := r.FindPatchIdByHash(parts[0])
+ _ = err
+ log.Printf("%-13.13s %-55.55s hId %10.10s pId %10.10s %v\n", "OK delete", r.FullPath, parts[0], patchId, parts[2:])
+ }
+
+ if len(hashbad) > 0 {
+ log.Printf("%-13.13s %v\n", "BAD cmd", cmd1)
+ }
+ var ACTUALLYOK bool = true
+ // things still only in the local branch (bad to delete)
+ for _, line := range hashbad {
+ parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL
+ patchId, err := r.FindPatchIdByHash(parts[0])
+ _ = err
+ searchResult := log.Sprintf("NOPE(%d)", r.Stats().Len())
+ stat := findPatchIdInStats(r.Stats(), patchId)
+ if stat != nil {
+ searchResult = log.Sprintf("FOUND %10.10s %s", stat.PatchId, "todo: []slice")
+ } else {
+ ACTUALLYOK = false
+ }
+
+ log.Printf("%-13.13s %-55.55s hId %10.10s pId %10.10s %s %v\n", "BAD keep", r.FullPath, parts[0], patchId, searchResult, parts[2:])
+ }
+
+ if ACTUALLYOK {
+ // todo: force checkout to local master branch
+ // before doing this
+ cmd := []string{"git", "update-ref", "-d", deleteHash}
+ // log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", r.FullPath, cmd1, "")
+ // log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", r.FullPath, cmd2, "")
+ if r.GetCurrentBranchName() == r.GetMasterBranchName() {
+ log.Printf("%-55.55s %v %s\n", r.FullPath, cmd, "SAFE TO DELETE add --fix")
+ } else {
+ log.Printf("%-55.55s %v %s\n", r.FullPath, cmd, "SAFE TO DELETE BUT NOT ON MASTER BRANCH add --fix")
+ }
+ if argv.Fix {
+ err := r.RunVerbose(cmd)
+ if err != nil {
+ log.Info(deleteHash, r.FullPath)
+ s := "local user branch could not be deleted"
+ argvpb.BadExit(s, err)
+ }
+ }
+ return ErrorNeedArgvFix
+ }
+
+ return log.Errorf("NOT SAFE")
+}