scheme to store files in git notes
This commit is contained in:
parent
214865f134
commit
dbbc35dd24
56
main.go
56
main.go
|
@ -36,6 +36,10 @@ func main() {
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := check.ValidGoSum(); err == nil {
|
||||||
|
okExit("go.mod and go.sum are already valid")
|
||||||
|
}
|
||||||
|
|
||||||
// skip restore if --force
|
// skip restore if --force
|
||||||
if !argv.Force {
|
if !argv.Force {
|
||||||
// try to restore from the git metadata
|
// try to restore from the git metadata
|
||||||
|
@ -101,10 +105,10 @@ func findPwdRepo() *gitpb.Repo {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func okExit(thing string) {
|
func okExit(msg string) {
|
||||||
|
log.Info("exit() go-mod-clean on", check.GetGoPath(), "ok")
|
||||||
log.DaemonMode(true)
|
log.DaemonMode(true)
|
||||||
log.Info(thing, "ok")
|
log.Info(msg)
|
||||||
// log.Info("Finished go-mod-clean on", check.GetGoPath(), "ok")
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,17 +125,21 @@ func saveAsMetadata(repo *gitpb.Repo) error {
|
||||||
if err := check.StrictRun(cmd); err != nil {
|
if err := check.StrictRun(cmd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
cmd = []string{"git", "notes", "add", "-m", "// `autogen:go.mod`", cname}
|
||||||
|
if err := check.StrictRun(cmd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if check.GoPrimitive {
|
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 {
|
if err := check.StrictRun(cmd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} 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 {
|
if err := check.StrictRun(cmd); err != nil {
|
||||||
return err
|
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 {
|
if err := check.StrictRun(cmd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -155,17 +163,31 @@ func restoreFromGit(repo *gitpb.Repo) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
all := strings.Join(result.Stdout, "\n")
|
var newf *os.File
|
||||||
parts := strings.Split(all, "GOSUM:")
|
var body string
|
||||||
|
for _, line := range result.Stdout {
|
||||||
gomod := filepath.Join(filepath.Join(check.FullPath, "go.mod"))
|
if strings.HasPrefix(line, "// `autogen:") {
|
||||||
newf, _ := os.OpenFile(gomod, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
if newf != nil {
|
||||||
fmt.Fprint(newf, strings.TrimSpace(parts[0]))
|
fmt.Fprintln(newf, strings.TrimSpace(body))
|
||||||
|
newf.Close()
|
||||||
if len(parts) == 2 {
|
newf = nil
|
||||||
gosum := filepath.Join(filepath.Join(check.FullPath, "go.sum"))
|
body = ""
|
||||||
newf, _ := os.OpenFile(gosum, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
|
}
|
||||||
fmt.Fprint(newf, strings.TrimSpace(parts[1]))
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue