summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-12-13 02:21:25 -0600
committerJeff Carr <[email protected]>2024-12-13 02:21:25 -0600
commit214865f134363f1bd0465e91c143fc3c2ffdc001 (patch)
treea2423e45d02b3190617d468647782c0c2c81d720 /main.go
parentc3e3dfd20952acfdf58d018cf16b3c2cd8bc2ae3 (diff)
save and restore from git notes
Diffstat (limited to 'main.go')
-rw-r--r--main.go123
1 files changed, 90 insertions, 33 deletions
diff --git a/main.go b/main.go
index 72a0fef..8d6cdf4 100644
--- a/main.go
+++ b/main.go
@@ -1,8 +1,10 @@
package main
import (
+ "errors"
"fmt"
"os"
+ "path/filepath"
"strings"
"go.wit.com/dev/alexflint/arg"
@@ -26,8 +28,6 @@ func main() {
// load the ~/.config/forge/ config
// this lets you configure repos you have read/write access too
forge = forgepb.Init()
- // rescan just in case (?) todo: decide what forge should default too
- forge.ScanGoSrc()
// figure out what directory we are running in
check = findPwdRepo()
@@ -35,51 +35,50 @@ func main() {
log.Info("this directory isn't in a golang project (not in ~/go/src nor a go.work file)")
os.Exit(-1)
}
- log.Info("starting go-mod-clean for", check.GoPath)
- log.Info("go src dir is set to", forge.GetGoSrc())
- if argv.Recursive {
- 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)
+ // skip restore if --force
+ if !argv.Force {
+ // try to restore from the git metadata
+ if restoreFromGit(check) {
+ okExit("go.mod was restored from the git notes")
}
- 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()
- } else {
- simpleStdin(true, []string{"go-mod-clean will recreate go.mod and go.sum"})
+ }
+
+ if check.GetMasterBranchName() != check.GetCurrentBranchName() {
+ log.Info("")
+ log.Info("You can only run go-mod-clean on a git master branch.")
+ log.Info("Publishing go.mod & go.sum files must come from from git version tag")
+ log.Info("Anything else doesn't make sense.")
+ log.Info("")
+ badExit(errors.New("not git master branch"))
}
// re-create go.sum and go.mod
- if _, err := check.RedoGoMod(); err != nil {
+ if _, err := redoGoMod(check); err != nil {
badExit(err)
}
- // try to trim junk
- if err := forge.TrimGoSum(check); err != nil {
- badExit(err)
+ if argv.Trim {
+ // try to trim junk
+ if err := trimGoSum(check); err != nil {
+ badExit(err)
+ }
}
// check go.sum file
- if err := forge.CleanGoDepsCheckOk(check); err != nil {
+ if err := cleanGoDepsCheckOk(check); err != nil {
log.Info("forge.FinalGoDepsCheck() failed. boo. :", check.GoPath)
badExit(err)
}
+ // put the files in the notes section in git
+ // this way, git commits are not messed up
+ // with this autogenerated code
+ if err := saveAsMetadata(check); err != nil {
+ log.Info("save go.mod as git metadata failed", check.GoPath, err)
+ badExit(err)
+ }
+
log.Info("forge.FinalGoDepsCheck() worked :", check.GoPath)
okExit(check.GoPath + " go.sum seems clean")
}
@@ -103,12 +102,70 @@ func findPwdRepo() *gitpb.Repo {
}
func okExit(thing string) {
+ log.DaemonMode(true)
log.Info(thing, "ok")
- log.Info("Finished go-mod-clean on", check.GetGoPath(), "ok")
+ // log.Info("Finished go-mod-clean on", check.GetGoPath(), "ok")
os.Exit(0)
}
func badExit(err error) {
+ log.DaemonMode(true)
log.Info("go-mod-clean failed: ", err, forge.GetGoSrc())
os.Exit(-1)
}
+
+// todo: do this the right way in git
+func saveAsMetadata(repo *gitpb.Repo) error {
+ cname := check.GetCurrentBranchName()
+ cmd := []string{"git", "notes", "remove", cname}
+ if err := check.StrictRun(cmd); err != nil {
+ return err
+ }
+ if check.GoPrimitive {
+ cmd = []string{"git", "notes", "add", "-F", "go.mod", cname}
+ if err := check.StrictRun(cmd); err != nil {
+ return err
+ }
+ } else {
+ cmd = []string{"git", "notes", "add", "-F", "go.mod", cname}
+ if err := check.StrictRun(cmd); err != nil {
+ return err
+ }
+ cmd = []string{"git", "notes", "append", "-m", "GOSUM:", cname}
+ if err := check.StrictRun(cmd); err != nil {
+ return err
+ }
+ cmd = []string{"git", "notes", "append", "-F", "go.sum", cname}
+ if err := check.StrictRun(cmd); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func restoreFromGit(repo *gitpb.Repo) bool {
+ result := repo.Run([]string{"git", "notes", "show"})
+ if result.Exit != 0 {
+ return false
+ }
+ if result.Error != nil {
+ return false
+ }
+ if len(result.Stdout) == 0 {
+ return false
+ }
+
+ all := strings.Join(result.Stdout, "\n")
+ parts := strings.Split(all, "GOSUM:")
+
+ gomod := filepath.Join(filepath.Join(check.FullPath, "go.mod"))
+ newf, _ := os.OpenFile(gomod, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
+ fmt.Fprint(newf, strings.TrimSpace(parts[0]))
+
+ if len(parts) == 2 {
+ gosum := filepath.Join(filepath.Join(check.FullPath, "go.sum"))
+ newf, _ := os.OpenFile(gosum, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
+ fmt.Fprint(newf, strings.TrimSpace(parts[1]))
+ }
+ return true
+}