diff options
Diffstat (limited to 'doStats.go')
| -rw-r--r-- | doStats.go | 307 |
1 files changed, 129 insertions, 178 deletions
@@ -5,240 +5,191 @@ package main import ( "errors" - "strings" - "go.wit.com/lib/cobol" "go.wit.com/lib/env" - "go.wit.com/lib/protobuf/argvpb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" - "google.golang.org/protobuf/types/known/timestamppb" ) +// /tmp/go-nsupdate$ git ls-remote +// From [email protected]:SpComb/go-nsupdate.git +// d65f28d10991354b3af0168001a4beea6b8242f9 HEAD +// d65f28d10991354b3af0168001a4beea6b8242f9 refs/heads/master +// fb04ebe13a16c01e1a7eb3892a0aca8c6db96fa0 refs/pull/3/head +// d774220311b2d44c770e7431ec663c8875488a1e refs/pull/6/head +// fefb9ea80745893a2203576bdc2872d33e539064 refs/pull/7/head +// c09c90d0a931888862262a6ed3753eed0af4ddca refs/pull/8/head + func doStats(cmd *StatsCmd) (string, error) { - var allerr error - if argv.All { - for r := range me.forge.Repos.IterByFullPath() { - if r.Stats().Len() == 0 { - _, err := doStatsRepo(r) - allerr = errors.Join(allerr, err) - } + if cmd.All != nil { + fixed := me.forge.RunOnRepos(me.forge.Repos, doReload) + if fixed.Len() == 0 { + return "no git config changes", nil } - return "verify ran everywhere", nil + + // show the changed repos & save cache .pb file + fixed = fixed.SortActual() + footer := me.forge.PrintErrorsTB(fixed) + me.forge.Repos.SaveVerbose() + return ".git/config problems: " + footer, nil } repo := workingDirToRepo() if repo == nil { return "no repo", errors.New("working dir isn't a repo I know about") } - - if cmd.List { - s := repo.Stats() - s.SortPatchId() - // s.SortCtime() - footer := s.PrintTableLimit(-1) - return "stats table: " + footer, nil + if cmd.Repo != "" { + r := me.forge.Repos.FindByNamespace(cmd.Repo) + if r == nil { + return "no repo " + cmd.Repo, errors.New("no repo " + cmd.Repo) + } + for _, rmote := range r.Config.Remotes { + stats, err := r.LoadRemoteRefs(rmote.Name) + if err != nil { + // return err + } + if env.True("resort") { + stats.SaveByHash() + log.Info("stats should have been resorted and saved") + } else { + footer := stats.PrintTable() + log.Info("full remote refs footer:", footer) + } + } + return "remote refs table", nil } - s, err := doStatsRepo(repo) - allerr = errors.Join(allerr, err) - - user, err := refHash(repo, "heads/"+repo.GetUserBranchName()) - allerr = errors.Join(allerr, err) - master, err := refHash(repo, "remotes/origin/master") - allerr = errors.Join(allerr, err) - HEAD, err := refHash(repo, "remotes/origin/HEAD") - allerr = errors.Join(allerr, err) - log.Printf("user=%10.10s master=%10.10s HEAD=%10.10s\n", user, master, HEAD) - - safeDelete(repo, "refs/heads/"+repo.GetUserBranchName(), "refs/remotes/origin/HEAD") // delete user if safely contained in HEAD - safeDelete(repo, user, HEAD) // checkes by hash ID // delete user if safely contained in HEAD - - return s, allerr -} - -func doStatsRepo(r *gitpb.Repo) (string, error) { - var allerr error - pb, err := r.LoadStats() - if err == nil { - log.Info("LoadStats() ok", pb.Filename) - } else { - log.Info("LoadStats() err", err) + if cmd.Reload { + err := doReload(repo) + return "doReload()", err } - allerr = errors.Join(allerr, err) - if hasOrigin(r) { - log.Info("todo: detect origin") + if cmd.CheckRefs { + err := doCheckRefs(repo) + return "checked refs", err } - // collect the stats - counter, err := last100(r, pb) - allerr = errors.Join(allerr, err) - s := log.Sprintf("found %d new hashes", counter) + if cmd.CheckRemote { + err := doCheckRemote(repo) + return "checked remote refs", err + } - for stat := range pb.IterAll() { - if stat.PatchId == "" { - stat.PatchId, err = r.FindPatchIdByHash(stat.Hash) - allerr = errors.Join(allerr, err) - log.Info("patchid for hash", stat.Hash, "is", stat.PatchId) - counter += 1 - } + if cmd.CheckRemoteRefs { + err := doCheckRemoteRefs(repo) + return "checked remote refs", err } - if counter > 0 { - pb.SaveVerbose() + if cmd.UpdateRefs { + err := doUpdateRefs(repo) + return "update refs", err } - return s, nil -} -func collectStats(r *gitpb.Repo, pb *gitpb.Stats) error { - return nil -} + if cmd.UpdateRemote { + err := doUpdateRemote(repo) + return "update remote", err + } -// git show-ref refs/heads/devel -func refHash(r *gitpb.Repo, name string) (string, error) { - var hash string - refname := "refs/" + name - cmd := []string{"git", "show-ref", refname} - cmdout := r.Run(cmd) - for i, line := range cmdout.Stdout { - parts := strings.Fields(line) - if env.If("stats") { - log.Info(parts[0], "LINE:", i, line) - } - hash = parts[0] + if cmd.UpdateRemoteRefs { + err := doUpdateRemoteRefs(repo) + return "update remote refs", err } - if len(cmdout.Stdout) != 1 { - return "", errors.New("no refname:" + name) + + if cmd.List { } - return hash, nil -} -// git show-ref --verify refs/heads/devel -func hasOrigin(r *gitpb.Repo) bool { - // git show-ref refs/heads/devel - return true + return "do what?", nil } -var standardFmts []string = []string{"H", "T", "at", "ct", "f"} -var standardSeperator string = "___FORGE___" +func doCheckRefs(r *gitpb.Repo) error { + stats, err := r.LoadRefs() + if err != nil { + return err + } -func makeFmts() string { - // fmts := strings.Fields(config.GetPanic("standardFmts")) - // fmts := strings.Fields(config.GetPanic("standardSeperator")) - var all []string - for _, fmtvar := range standardFmts { - all = append(all, "%"+fmtvar) + if env.True("resort") { + stats.SaveByHash() + log.Info("stats should have been resorted and saved") + return nil } - return "--format=" + strings.Join(all, standardSeperator) + footer := stats.PrintTable() + log.Info("stats footer:", footer) + return nil } -// the correct syntax for -// git log -n 8 --format=%H%00%ae%00%as%00%s origin/HEAD -func last100(r *gitpb.Repo, pb *gitpb.Stats) (int, error) { - var allerr error - var counter int - cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/" + r.GetMasterBranchName()} // must use 'master' as queried from the git server - // cmd := []string{"git", "log", "-n", "100", makeFmts(), "origin/HEAD"} // HEAD is _NOT_ always set - if env.If("stats") { - log.Info("Run:", cmd) - } - cmdout := r.Run(cmd) - for i, line := range cmdout.Stdout { - parts := strings.Split(line, standardSeperator) - hash := parts[0] - if env.If("stats") { - log.Printf("LINE:%8.8s %2d %v\n", hash, i, parts[1:]) +func doCheckRemote(r *gitpb.Repo) error { + for _, rmote := range r.Config.Remotes { + morestats, err := r.MakeRemote(rmote.Name) + if err != nil { + // return err } - found := pb.FindByHash(hash) - if found != nil { - // already have this hash - continue + if env.True("resort") { + morestats.SaveByHash() + return errors.New("stats should have been resorted and saved") } - counter += 1 - astat := new(gitpb.Stat) - astat.Hash = hash - ctime, err := cobol.GetTime(parts[2]) - allerr = errors.Join(allerr, err) - astat.Ctime = timestamppb.New(*ctime) - astat.Subject = parts[4] - astat.Type = gitpb.Stat_REMOTE - pb.Append(astat) + footer := morestats.PrintTable() + log.Printf("full %s remote refs footer: %s\n", rmote.Name, footer) } - return counter, allerr + + return nil } -func findPatchIdInStats(pb *gitpb.Stats, patchId string) *gitpb.Stat { - for stat := range pb.IterAll() { - if stat.PatchId == patchId { - return stat +func doCheckRemoteRefs(r *gitpb.Repo) error { + for _, rmote := range r.Config.Remotes { + morestats, err := r.MakeRemoteRefs(rmote.Name) + if err != nil { + // return err } + if env.True("resort") { + morestats.SaveByHash() + return errors.New("stats should have been resorted and saved") + } + footer := morestats.PrintTable() + log.Printf("full %s remote refs footer: %s\n", rmote.Name, footer) } - // 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) - +func doUpdateRefs(r *gitpb.Repo) error { + stats, err := r.LoadRefs() 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:]) - } + footer := stats.PrintTableLimit(2) + log.Info("stats footer:", footer) - 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, stat.Subject) - } else { - ACTUALLYOK = false - } + err = r.UpdateRefs(stats) + return err +} - 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:]) +func doUpdateRemote(r *gitpb.Repo) error { + for _, rmote := range r.Config.Remotes { + err := r.UpdateRemote(rmote.Name) + if err != nil { + return err + } + // footer := morestats.PrintTableLimit(20) + log.Printf("full %s remote update remote footer\n", rmote.Name) } - 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) + return nil +} + +func doUpdateRemoteRefs(r *gitpb.Repo) error { + for _, rmote := range r.Config.Remotes { + /* + morestats, err := r.LoadRemoteRefs(rmote.Name) if err != nil { - log.Info(deleteHash, r.FullPath) - s := "local user branch could not be deleted" - argvpb.BadExit(s, err) + return err } + */ + + err := r.UpdateRemoteRefs(rmote.Name) + if err != nil { + return err } - return ErrorNeedArgvFix } - return log.Errorf("NOT SAFE") + return nil } |
