add --purge to get rid of all notes

This commit is contained in:
Jeff Carr 2024-12-18 01:29:07 -06:00
parent 4879befeb3
commit 5c156fa55e
6 changed files with 96 additions and 104 deletions

32
all.go
View File

@ -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()
}
}

View File

@ -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 {

91
main.go
View File

@ -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)

19
notes.go Normal file
View File

@ -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
}

45
restore.go Normal file
View File

@ -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
}

View File

@ -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)