diff options
| author | Jeff Carr <[email protected]> | 2025-10-06 04:26:44 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-06 04:26:44 -0500 |
| commit | a7d418da30889a3b85257baf65d44f0d32c5df71 (patch) | |
| tree | 5e56f53cf6cd0117679689f239a466bfe4e55e34 /doDev.go | |
| parent | 50fea58cf96abaa3f8c28c6ffb67630d6dd7829e (diff) | |
some decent code to delete branches with checking
Diffstat (limited to 'doDev.go')
| -rw-r--r-- | doDev.go | 167 |
1 files changed, 133 insertions, 34 deletions
@@ -4,7 +4,10 @@ package main import ( + "strings" + "go.wit.com/lib/protobuf/forgepb" + "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -12,49 +15,145 @@ import ( // so don't delete them func doDev() (string, error) { if argv.Dev.DeleteUser { + found := gitpb.NewRepos() setForgeMode(forgepb.ForgeMode_MASTER) for repo := range me.forge.Repos.IterByNamespace() { - if !repo.IsBranchRemote(repo.GetUserBranchName()) { + uremoteref := repo.GetRemoteTag(repo.GetUserBranchName()) + if uremoteref == nil { continue } - // log.Info("delete remote user", repo.FullPath) - localtag := repo.IsBranchLocal(repo.GetUserBranchName()) - if localtag == nil { - log.Info("no local user", repo.FullPath) + found.Repos = append(found.Repos, repo) + if !repo.IsMasterBranch() { + log.Info("you just be in the master branch") + repo.State = "needs master branch" continue } - // log.Info("local user", repo.FullPath) - hashes, err := repo.DeleteLocalBranch(repo.GetUserBranchName()) + doFixDeleteUserBranches(repo, uremoteref) + } + me.forge.PrintHumanTable(found) + } + return "", nil +} + +func doFixDeleteUserBranches(repo *gitpb.Repo, remoteRef *gitpb.GitTag) { + // log.Info("delete remote user", repo.FullPath) + localTag := repo.IsBranchLocal(repo.GetUserBranchName()) + if localTag != nil { + // log.Info("THERE IS local user", repo.FullPath, localTag.Refname) + log.Printf("%-13.13s %-55.55s %v\n", "LOCAL USER", repo.FullPath, localTag.Refname+" local branch exists") + doFixDeleteUserLocalBranch(repo, remoteRef, localTag) + return + } + log.Printf("%-13.13s %-55.55s %v\n", "NO LOCAL USER", repo.FullPath, "only has remote user branch") +} + +// git push --delete origin jcarr +// git push origin :refs/remotes/origin/jcarr +func doFixDeleteRemoteUserBranch(repo *gitpb.Repo, remoteRef *gitpb.GitTag, localRef *gitpb.GitTag) (error, *notesList) { + return nil, nil +} + +// git branch -D refs/heads/jcarr +// git update-ref -d refs/heads/jcarr +func doFixDeleteUserLocalBranch(repo *gitpb.Repo, remoteRef *gitpb.GitTag, localRef *gitpb.GitTag) (error, *notesList) { + notes := new(notesList) + + // get local user, remote user & local devel branches + dref := repo.GetLocalDevelRef() + localUser := repo.NewCompareRef(localRef) + remoteUser := repo.NewCompareRef(remoteRef) + localDevel := repo.NewCompareRef(dref) + + // Is the localUser completely inside the remoteUser branch? + hashok, hashbad, cmd1, cmd2, err := remoteUser.DeleteBranch(localUser) + for _, line := range hashok { + notes.addNote("OK", repo, remoteUser.GetRef(), cmd1, line) + } + for _, line := range hashbad { + notes.addNote("BAD", repo, localUser.GetRef(), cmd2, line) + } + if err == nil { + // git update-ref -d refs/heads/jcarr + cmd := []string{"git", "update-ref", "-d", localUser.GetRefname()} + log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "") + log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "") + log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix") + if argv.Fix { + err := repo.RunVerbose(cmd) if err != nil { - // log.Info("LOCAL BRANCH NOT SAFE. COMPARE WITH DEVEL", err, repo.FullPath) - } else if hashes == nil { - log.Info("LOCAL SAFE TO DELETE", repo.FullPath) - continue + log.Info(localUser.GetRefname(), repo.FullPath) + s := "local user branch could not be deleted" + me.sh.BadExit(s, err) } - uref := repo.GetLocalUserRef() - dref := repo.GetLocalDevelRef() - // try compare against devel - uver := repo.NewCompareRef(uref) - dver := repo.NewCompareRef(dref) - hashok, hashbad, err := dver.DeleteBranch(uver) - if err == nil { - for _, line := range hashok { - log.Info("SAFE hashok", line) - } - for _, line := range hashbad { - log.Info("NOT SAFE hashbad", line) - } - log.Info("SAFE TO DELETE uver from dver", uref.Refname, dref.Refname, repo.FullPath) - continue - } - for _, line := range hashok { - log.Info("SAFE hashok", line) - } - for _, line := range hashbad { - log.Info("NOT SAFE hashbad", line) + return nil, notes + } + return log.Errorf("argv needs --fix"), notes + } + + // Is the localUser completely inside the localDevel branch? + hashok, hashbad, cmd1, cmd2, err = localDevel.DeleteBranch(localUser) + for _, line := range hashok { + notes.addNote("OK", repo, localDevel.GetRef(), cmd1, line) + } + for _, line := range hashbad { + notes.addNote("BAD", repo, localUser.GetRef(), cmd2, line) + } + // git update-ref -d refs/heads/jcarr + if err == nil { + cmd := []string{"git", "update-ref", "-d", localUser.GetRefname()} + log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd1, "") + log.Printf("%-13.13s %-55.55s %v %s\n", "CMD OK", repo.FullPath, cmd2, "") + log.Printf("%-13.13s %-55.55s %v %s\n", "SAFE TO DELETE", repo.FullPath, cmd, "add --fix") + if argv.Fix { + err := repo.RunVerbose(cmd) + if err != nil { + log.Info(localUser.GetRefname(), repo.FullPath) + s := "local user branch could not be deleted" + me.sh.BadExit(s, err) } - log.Info("NOT SAFE TO DELETE uver from dver", uref.Refname, dref.Refname, repo.FullPath, err) } + return nil, notes + } else { + log.Printf("%-13.13s %-55.55s %v %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd1) + log.Printf("%-13.13s %-55.55s %v %s %v\n", "CMD ERR", repo.FullPath, err, "NOT SAFE TO DELETE. Reload()?", cmd2) } - return "", nil + // log.Printf("%-13.13s %-55.55s %v %s\n", "NOT SAFE", repo.FullPath, localUser.GetRefname(), "NOT SAFE TO DELETE") + return log.Errorf("NOT SAFE"), notes +} + +// notes.addNote("SAFE", repo, remoteUser, line) +func (notes *notesList) addNote(note string, repo *gitpb.Repo, ref *gitpb.GitTag, cmd []string, line string) { + newnote := new(DeleteBranchNotes) + newnote.Note = note + newnote.Refname = ref.Refname + newnote.Fullpath = repo.FullPath + newnote.Cmd = cmd + parts := strings.Split(line, "%00") // git log doesn't actually convert %00 to NULL + if len(parts) != 4 { + log.Info("len", len(parts)) + panic("nope") + } + newnote.H = parts[0] + newnote.Ae = parts[1] + newnote.As = parts[2] + newnote.S = parts[3] + // newnote.Hash = parts[0] + log.Printf("%-5.5s %7.7s %-55.55s %v\n", note, newnote.H, newnote.Fullpath, cmd) + notes.all = append(notes.all, newnote) +} + +type notesList struct { + all []*DeleteBranchNotes +} + +type DeleteBranchNotes struct { + Note string + Refname string + Fullpath string + Cmd []string + // git log variable names + H string + S string + Ae string + As string } |
