From 5c156fa55e2ee7980a6bbfc9a0a7df5b8984aef0 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 18 Dec 2024 01:29:07 -0600 Subject: [PATCH] add --purge to get rid of all notes --- all.go | 32 ------------------- argv.go | 8 ++--- main.go | 91 +++++++++++++++++------------------------------------- notes.go | 19 ++++++++++++ restore.go | 45 +++++++++++++++++++++++++++ stdin.go | 5 +-- 6 files changed, 96 insertions(+), 104 deletions(-) delete mode 100644 all.go create mode 100644 notes.go create mode 100644 restore.go diff --git a/all.go b/all.go deleted file mode 100644 index fbdafc7..0000000 --- a/all.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "fmt" -) - -// rethink this. do not run on non-master git branches -func doAll() { - if argv.All { - if forge.IsGoWork() { - var warning []string - warning = append(warning, "go-mod-clean --recursive may not work unless you are in ~/go/src") - warning = append(warning, "you can continue anyway, but it hasn't been tested as much.") - simpleStdin(true, warning) - } - var warning []string - warning = append(warning, "go-mod-clean will recreate go.mod and go.sum") - warning = append(warning, "because you have selected --recursive") - warning = append(warning, "this will redo _every_ repo. This is probably fine.") - warning = append(warning, fmt.Sprintf("You have %d total repositories in %s", forge.Repos.Len(), forge.GetGoSrc())) - warning = append(warning, "") - warning = append(warning, "However, this will also, at times, do:") - warning = append(warning, "") - warning = append(warning, "rm -rf ~/go/pkg/") - warning = append(warning, "rm -rf ~/.config/go-build/") - warning = append(warning, "") - warning = append(warning, "Which is also probably fine, but will clear all your build cache and go mod cache") - warning = append(warning, "") - simpleStdin(false, warning) - // purgeGoCaches() - } -} diff --git a/argv.go b/argv.go index 4da63f1..67eed18 100644 --- a/argv.go +++ b/argv.go @@ -7,14 +7,12 @@ package main var argv args type args struct { - All bool `arg:"--all" default:"false" help:"redo every repo found in go/src or go.work"` - Auto bool `arg:"--auto" help:"don't approve via STDIN"` Trim bool `arg:"--trim" default:"true" help:"trim entries from go.sum"` Verbose bool `arg:"--verbose" help:"show more"` - Notes bool `arg:"--metadata" help:"save as git metadata (notes)"` - Restore bool `arg:"--restore" default:"true" help:"restore from git metadata"` + Restore bool `arg:"--restore" help:"only restore from go/pkg/mod/"` Force bool `arg:"--force" help:"remove things and redo them no matter what"` - Strict bool `arg:"--strict" default:"false" help:"never make go.* files unless everything is perfect"` + Strict bool `arg:"--strict" help:"never make go.* files unless everything is perfect"` + Purge bool `arg:"--purge" help:"purge all the git notes. this might be bad for you."` } func (args) Version() string { diff --git a/main.go b/main.go index 6bed396..213bdc7 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,7 @@ package main import ( - "errors" "os" - "path/filepath" "strings" "go.wit.com/dev/alexflint/arg" @@ -33,35 +31,37 @@ func main() { // this lets you configure repos you have read/write access too forge = forgepb.Init() - if argv.All { - // run this on every single repo - // do this before publishing new golang versions - all := forge.Repos.SortByFullPath() - for all.Scan() { - check = all.Next() - if err := doMain(check); err != nil { - badExit(check, err) - } - } - } else { - // figure out what directory we are running in - check = findPwdRepo() - if check == nil { - log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)") - badExit(nil, nil) - } + // figure out what directory we are running in + check = findPwdRepo() + if check == nil { + log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)") + badExit(nil, nil) + } - if err := doMain(check); err != nil { + // deletes all the git notes + if argv.Purge { + purgeNotes(check) + okExit(check, "notes gone") + } + + if argv.Restore { + // attempt to restore from ~/go/pkg/mod/ + if err := restoreFromGoPkg(check); err != nil { badExit(check, err) } - if argv.Force { - if err := doForce(check); err != nil { - badExit(check, err) - } - } else { - if err := doSmart(check); err != nil { - badExit(check, err) - } + okExit(check, "go.mod and go.sum restored from ~/go/pkg/mod/") + } + + if err := doMain(check); err != nil { + badExit(check, err) + } + if argv.Force { + if err := doForce(check); err != nil { + badExit(check, err) + } + } else { + if err := doSmart(check); err != nil { + badExit(check, err) } } @@ -105,41 +105,6 @@ func saveAsMetadata(repo *gitpb.Repo) error { return nil } -func restoreFromGoPkg(repo *gitpb.Repo) error { - homedir, err := os.UserHomeDir() - if err != nil { - badExit(nil, err) - } - rver := repo.GetMasterVersion() - if rver == "" { - return errors.New("could not get master version") - } - modfile := filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver, "go.mod") - log.Info("mod path should be", modfile) - data, err := os.ReadFile(modfile) - if err != nil { - return err - } - modf, err := os.OpenFile("go.mod", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - return err - } - defer modf.Close() - modf.Write(data) - - modfile = filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver, "go.sum") - log.Info("mod path should be", modfile) - data, err = os.ReadFile(modfile) - if err == nil { - sumf, _ := os.OpenFile("go.sum", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - defer sumf.Close() - sumf.Write(data) - } - - // try go.sum, but no error checking since it might not be there - return nil -} - func doMain(repo *gitpb.Repo) error { if argv.Strict { return doStrict(repo) diff --git a/notes.go b/notes.go new file mode 100644 index 0000000..59543c9 --- /dev/null +++ b/notes.go @@ -0,0 +1,19 @@ +package main + +import ( + "strings" + + "go.wit.com/lib/protobuf/gitpb" + "go.wit.com/log" +) + +func purgeNotes(repo *gitpb.Repo) error { + result := repo.Run([]string{"git", "notes", "list"}) + for _, line := range result.Stdout { + parts := strings.Fields(line) + log.Info("line:", line, "part", parts[1]) + blah := repo.Run([]string{"git", "notes", "remove", parts[1]}) + log.Info(strings.Join(blah.Stdout, "\n")) + } + return nil +} diff --git a/restore.go b/restore.go new file mode 100644 index 0000000..1acb9b2 --- /dev/null +++ b/restore.go @@ -0,0 +1,45 @@ +package main + +import ( + "errors" + "os" + "path/filepath" + + "go.wit.com/lib/protobuf/gitpb" + "go.wit.com/log" +) + +func restoreFromGoPkg(repo *gitpb.Repo) error { + homedir, err := os.UserHomeDir() + if err != nil { + badExit(nil, err) + } + rver := repo.GetMasterVersion() + if rver == "" { + return errors.New("could not get master version") + } + modfile := filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver, "go.mod") + log.Info("mod path should be", modfile) + data, err := os.ReadFile(modfile) + if err != nil { + return err + } + modf, err := os.OpenFile("go.mod", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + defer modf.Close() + modf.Write(data) + + modfile = filepath.Join(homedir, "go/pkg/mod", repo.GetGoPath()+"@"+rver, "go.sum") + log.Info("mod path should be", modfile) + data, err = os.ReadFile(modfile) + if err == nil { + sumf, _ := os.OpenFile("go.sum", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + defer sumf.Close() + sumf.Write(data) + } + + // try go.sum, but no error checking since it might not be there + return nil +} diff --git a/stdin.go b/stdin.go index a28e3fa..51c0bcd 100644 --- a/stdin.go +++ b/stdin.go @@ -24,10 +24,7 @@ func showOptions(b bool, s []string) { } // if b == true, default is to continue with 'Y' -func simpleStdin(b bool, s []string) { - if argv.Auto { - return - } +func simpleStdinOld(b bool, s []string) { err := errors.New("user cancelled via stdin") showOptions(b, s) scanner := bufio.NewScanner(os.Stdin)