store autogenerated files in git notes
This commit is contained in:
parent
c5f7570834
commit
adef980837
|
@ -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
|
||||
}
|
|
@ -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
|
||||
|
|
8
shell.go
8
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue