From dbbc35dd247443d578777e6377f263d07c155705 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 13 Dec 2024 10:44:11 -0600 Subject: [PATCH] scheme to store files in git notes --- main.go | 56 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 8d6cdf4..3dcc969 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,10 @@ func main() { os.Exit(-1) } + if err := check.ValidGoSum(); err == nil { + okExit("go.mod and go.sum are already valid") + } + // skip restore if --force if !argv.Force { // try to restore from the git metadata @@ -101,10 +105,10 @@ func findPwdRepo() *gitpb.Repo { return nil } -func okExit(thing string) { +func okExit(msg string) { + log.Info("exit() go-mod-clean on", check.GetGoPath(), "ok") log.DaemonMode(true) - log.Info(thing, "ok") - // log.Info("Finished go-mod-clean on", check.GetGoPath(), "ok") + log.Info(msg) os.Exit(0) } @@ -121,17 +125,21 @@ func saveAsMetadata(repo *gitpb.Repo) error { if err := check.StrictRun(cmd); err != nil { return err } + cmd = []string{"git", "notes", "add", "-m", "// `autogen:go.mod`", cname} + if err := check.StrictRun(cmd); err != nil { + return err + } if check.GoPrimitive { - cmd = []string{"git", "notes", "add", "-F", "go.mod", cname} + cmd = []string{"git", "notes", "append", "-F", "go.mod", cname} if err := check.StrictRun(cmd); err != nil { return err } } else { - cmd = []string{"git", "notes", "add", "-F", "go.mod", cname} + cmd = []string{"git", "notes", "append", "-F", "go.mod", cname} if err := check.StrictRun(cmd); err != nil { return err } - cmd = []string{"git", "notes", "append", "-m", "GOSUM:", cname} + cmd = []string{"git", "notes", "append", "-m", "// `autogen:go.sum`", cname} if err := check.StrictRun(cmd); err != nil { return err } @@ -155,17 +163,31 @@ func restoreFromGit(repo *gitpb.Repo) bool { 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])) + var newf *os.File + var body string + for _, line := range result.Stdout { + if strings.HasPrefix(line, "// `autogen:") { + if newf != nil { + fmt.Fprintln(newf, strings.TrimSpace(body)) + newf.Close() + newf = nil + body = "" + } + fbase := strings.TrimPrefix(line, "// `autogen:") + fbase = strings.TrimSpace(fbase) + fbase = strings.TrimSuffix(fbase, "`") + // if line == // `autogen:` , then the filename is blank + if fbase != "" { + fname := filepath.Join(filepath.Join(check.FullPath, fbase)) + newf, _ = os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + } + continue + } + body += line + "\n" + } + if newf != nil { + fmt.Fprintln(newf, strings.TrimSpace(body)) + newf.Close() } return true }