forge/applyPatch.go

110 lines
3.3 KiB
Go
Raw Normal View History

2024-12-27 22:27:19 -06:00
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
package main
import (
"os"
2024-12-28 03:00:42 -06:00
"path/filepath"
2024-12-27 22:27:19 -06:00
2024-12-30 02:03:56 -06:00
"go.wit.com/lib/gui/shell"
2024-12-27 22:27:19 -06:00
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
2025-01-05 02:01:15 -06:00
func dumpPatchset(pset *forgepb.Patchs) error {
log.Info("applyPatches() NAME", pset.Name)
log.Info("applyPatches() COMMENT", pset.Comment)
log.Info("applyPatches() GIT_AUTHOR_NAME", pset.GetGitAuthorName())
log.Info("applyPatches() GIT_AUTHOR_EMAIL", pset.GetGitAuthorEmail())
2025-01-05 03:21:26 -06:00
log.Info("applyPatches() Branch Name", pset.GetStartBranchName())
log.Info("applyPatches() Start Hash", pset.GetStartBranchHash())
var count int
2025-01-05 02:01:15 -06:00
all := pset.SortByFilename()
for all.Scan() {
p := all.Next()
// log.Info("pset filename FILENAME IS REAL?", p.Filename, pset.Name, pset.Comment)
2025-01-05 03:21:26 -06:00
basepath, filename := filepath.Split(p.Filename)
repo := me.forge.FindByGoPath(basepath)
if repo == nil {
log.Info("can not apply patch! repo not found", basepath, filename)
continue
}
if repo.DevelHash() != p.StartHash {
log.Info("can not apply patch! devel hash mismatch", basepath, filename)
continue
}
log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
count += 1
2025-01-05 02:01:15 -06:00
}
2025-01-05 03:21:26 -06:00
log.Info("pset has", count, "patches")
2025-01-05 02:01:15 -06:00
return nil
}
2024-12-27 22:27:19 -06:00
func applyPatches(pset *forgepb.Patchs) error {
2024-12-30 02:03:56 -06:00
var everythingworked bool = true
tmpdir, err := os.MkdirTemp("", "forge")
if err != nil {
return err
}
2024-12-28 03:00:42 -06:00
// log.Info("got to applyPatches() pset", pset)
log.Info("applyPatches() NAME", pset.Name)
log.Info("applyPatches() COMMENT", pset.Comment)
log.Info("applyPatches() GIT_AUTHOR_NAME", pset.GetGitAuthorName())
log.Info("applyPatches() GIT_AUTHOR_EMAIL", pset.GetGitAuthorEmail())
2024-12-27 22:27:19 -06:00
all := pset.SortByFilename()
for all.Scan() {
p := all.Next()
2024-12-28 03:00:42 -06:00
// log.Info("pset filename FILENAME IS REAL?", p.Filename, pset.Name, pset.Comment)
basepath, filename := filepath.Split(p.Filename)
fullpath := filepath.Join(me.forge.GetGoSrc(), basepath)
log.Info("pset filename FILENAME IS REAL? fullpath", fullpath)
2024-12-30 02:03:56 -06:00
fullTmpdir := filepath.Join(tmpdir, basepath)
err := os.MkdirAll(fullTmpdir, os.ModePerm)
if err != nil {
log.Info("applyPathces() MkdirAll failed for", fullTmpdir)
log.Info("applyPathces() MkdirAll failed err", err)
everythingworked = false
continue
}
log.Info("pset filename FILENAME IS REAL? tmp fullTmpdir", fullTmpdir)
tmpname := filepath.Join(fullTmpdir, filename)
raw, _ := os.OpenFile(tmpname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
2024-12-28 03:00:42 -06:00
raw.Write(p.Data)
raw.Close()
2024-12-30 02:03:56 -06:00
cmd := []string{"git", "am", tmpname}
result := shell.PathRun(fullpath, cmd)
if result.Exit != 0 {
log.Info("cmd failed", cmd, result.Exit)
everythingworked = false
}
// until 'git am' works
everythingworked = false
}
if everythingworked {
os.RemoveAll(tmpdir) // clean up
2024-12-27 22:27:19 -06:00
}
2024-12-28 03:00:42 -06:00
log.Info("THIS IS THE END MY FRIEND")
2024-12-27 22:27:19 -06:00
return nil
}
func readPatchFile(pbfile string) (*forgepb.Patchs, error) {
bytes, err := os.ReadFile(pbfile)
if err != nil {
log.Info("readfile error", pbfile, err)
return nil, err
}
2024-12-28 03:00:42 -06:00
return handleBytes(bytes)
}
func handleBytes(bytes []byte) (*forgepb.Patchs, error) {
2024-12-27 22:27:19 -06:00
var pset *forgepb.Patchs
pset = new(forgepb.Patchs)
2024-12-28 03:00:42 -06:00
err := pset.Unmarshal(bytes)
2024-12-27 22:27:19 -06:00
if err != nil {
2024-12-28 03:00:42 -06:00
log.Info("Unmarshal failed", err)
2024-12-27 22:27:19 -06:00
return nil, err
}
return pset, nil
}