scheme to store files in git notes

This commit is contained in:
Jeff Carr 2024-12-13 10:44:11 -06:00
parent 214865f134
commit dbbc35dd24
1 changed files with 39 additions and 17 deletions

56
main.go
View File

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