From adef980837a2d75f7fd0bb03a600f98f5081f89d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 13 Dec 2024 12:57:02 -0600 Subject: [PATCH] store autogenerated files in git notes --- autogen.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.go | 8 ++--- shell.go | 8 +++-- 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 autogen.go diff --git a/autogen.go b/autogen.go new file mode 100644 index 0000000..b157d03 --- /dev/null +++ b/autogen.go @@ -0,0 +1,85 @@ +package gitpb + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +// files : a list of files to save ["go.mod", "go.sum"] +// refname : could be "master" or "v0.1.5" or "a605119c2cc41" +// del : true means empty out existing notes, otherwise append +func (repo *Repo) AutogenSave(files []string, refname string, del bool) error { + if del { + cmd := []string{"git", "notes", "remove", refname} + if err := repo.StrictRun(cmd); err != nil { + return err + } + } + for _, fname := range files { + autotag := "// `autogen:" + fname + "`" + cmd := []string{"git", "notes", "append", "-m", autotag, refname} + if err := repo.StrictRun(cmd); err != nil { + return err + } + cmd = []string{"git", "notes", "append", "-F", fname, refname} + if err := repo.StrictRun(cmd); err != nil { + return err + } + } + // a tag with a blank name indicates the end of the autogen file or files + autotag := "// `autogen:`" + cmd := []string{"git", "notes", "append", "-m", autotag, refname} + if err := repo.StrictRun(cmd); err != nil { + return err + } + return nil +} + +// restores files from git metadata (notes) +func (repo *Repo) AutogenRestore() error { + result := repo.Run([]string{"git", "notes", "show"}) + if result.Exit != 0 { + return errors.New(fmt.Sprint("git notes show returned ", result.Exit)) + } + if result.Error != nil { + return result.Error + } + if len(result.Stdout) == 0 { + return nil + } + + var newf *os.File + var err error + 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(repo.FullPath, fbase)) + newf, err = os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + } + continue + } + body += line + "\n" + } + if newf != nil { + fmt.Fprintln(newf, strings.TrimSpace(body)) + newf.Close() + } + return nil +} diff --git a/config.go b/config.go index 4d86b81..2fc02d0 100644 --- a/config.go +++ b/config.go @@ -43,7 +43,8 @@ func (all *Repos) ConfigLoad() error { var data []byte var err error - if data, err = loadFile("repos.pb"); err != nil { + cfgname := filepath.Join(os.Getenv("FORGE_GOSRC"), "repos.pb") + if data, err = loadFile(cfgname); err != nil { // something went wrong loading the file return err } @@ -71,14 +72,13 @@ func (all *Repos) ConfigLoad() error { } return err } - log.Info("gitpb.Init() ", len(all.Repos), "repos in ~/.config/forge/repos.pb") + log.Info("gitpb.Init() ", len(all.Repos), "repos in", cfgname) return nil } return nil } -func loadFile(filename string) ([]byte, error) { - fullname := filepath.Join(os.Getenv("FORGE_GOSRC"), filename) +func loadFile(fullname string) ([]byte, error) { data, err := os.ReadFile(fullname) if errors.Is(err, os.ErrNotExist) { // if file does not exist, just return nil. this diff --git a/shell.go b/shell.go index 3a5fc0d..8215d46 100644 --- a/shell.go +++ b/shell.go @@ -48,7 +48,11 @@ func (repo *Repo) RunRealtime(cmd []string) cmd.Status { return shell.PathRunRealtime(repo.GetFullPath(), cmd) } -// for now, even check cmd.Exit +// error if result.Error or if result.Exit != 0 +func (repo *Repo) RunStrict(cmd []string) error { + return repo.StrictRun(cmd) +} + func (repo *Repo) StrictRun(cmd []string) error { result := repo.RunQuiet(cmd) if result.Error != nil { @@ -95,6 +99,6 @@ func (repo *Repo) mtime(filename string) (time.Time, error) { if err == nil { return statf.ModTime(), nil } - log.Log(GITPBWARN, "mtime() error", pathf, err) + log.Log(GITPB, "mtime() os.Stat() error", pathf, err) return time.Now(), err }