summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-12-13 12:34:47 -0600
committerJeff Carr <[email protected]>2024-12-13 12:34:47 -0600
commit86f06c84199c1145712c19f7d432fe9e128f0fdb (patch)
treed14dd7fe06c15bc54d84c71aac0bcfec3e512902
parentdbbc35dd247443d578777e6377f263d07c155705 (diff)
store go.mod and go.sum in git. thank you most excellent git devs
-rw-r--r--README.md1
-rw-r--r--main.go73
2 files changed, 8 insertions, 66 deletions
diff --git a/README.md b/README.md
index 722171a..3cc45ff 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,7 @@ needs to be done sometimes.
* go-mod-clean tries to remove uneeded entries from the go.sum file.
* You must run go-mod-clean in ~/go/src for this to work.
+* stores go.mod and go.sum with `autogen:go.mod` tags in 'git notes'
# Install go-mod-clean
diff --git a/main.go b/main.go
index 3dcc969..95a2bbb 100644
--- a/main.go
+++ b/main.go
@@ -2,9 +2,7 @@ package main
import (
"errors"
- "fmt"
"os"
- "path/filepath"
"strings"
"go.wit.com/dev/alexflint/arg"
@@ -43,8 +41,11 @@ func main() {
// 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")
+ if err := check.AutogenRestore(); err != nil {
+ badExit(err)
+ }
+ if err := check.ValidGoSum(); err == nil {
+ okExit("go.mod and go.sum were restored ok")
}
}
@@ -118,76 +119,16 @@ func badExit(err error) {
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
- }
- 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", "append", "-F", "go.mod", cname}
- if err := check.StrictRun(cmd); err != nil {
+ if err := check.AutogenSave([]string{"go.mod"}, cname, true); err != nil {
return err
}
} else {
- cmd = []string{"git", "notes", "append", "-F", "go.mod", cname}
- if err := check.StrictRun(cmd); err != nil {
- return err
- }
- cmd = []string{"git", "notes", "append", "-m", "// `autogen:go.sum`", 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 {
+ if err := check.AutogenSave([]string{"go.mod", "go.sum"}, cname, true); 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
- }
-
- 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
-}