forgepb/patch.Make.go

147 lines
3.1 KiB
Go

package forgepb
import (
"fmt"
"os"
"path/filepath"
"go.wit.com/log"
)
func (f *Forge) MakeDevelPatchSet() (*Patchs, error) {
pset := new(Patchs)
dir, err := os.MkdirTemp("", "forge")
if err != nil {
return nil, err
}
defer os.RemoveAll(dir) // clean up
loop := f.Repos.SortByGoPath()
for loop.Scan() {
repo := loop.Next()
userb := repo.GetUserBranchName()
develb := repo.GetDevelBranchName()
if develb == "" {
continue
}
if userb == "" {
continue
}
return f.makePatchSetNew(develb, userb)
}
return pset, nil
}
func (f *Forge) makePatchSetNew(fromBranch string, toBranch string) (*Patchs, error) {
return nil, nil
}
var pset *Patchs
func (f *Forge) MakePatchSet() (*Patchs, error) {
pset = new(Patchs)
dir, err := os.MkdirTemp("", "forge")
if err != nil {
return nil, err
}
defer os.RemoveAll(dir) // clean up
loop := f.Repos.SortByGoPath()
for loop.Scan() {
repo := loop.Next()
userb := repo.GetUserBranchName()
develb := repo.GetDevelBranchName()
if develb == "" {
continue
}
if userb == "" {
continue
}
repoDir := filepath.Join(dir, repo.GoPath)
err := os.MkdirAll(repoDir, 0755)
if err != nil {
return nil, err
}
// git format-patch branch1..branch2
cmd := []string{"git", "format-patch", "-o", repoDir, develb + ".." + userb}
r := repo.Run(cmd)
if r.Error != nil {
log.Info("git format-patch", repo.FullPath)
log.Info("git format-patch", cmd)
log.Info("git format-patch error", r.Error)
return nil, r.Error
}
if r.Exit != 0 {
log.Info("git format-patch", repo.FullPath)
log.Info("git format-patch", cmd)
log.Info("git format-patch exit", r.Exit)
return nil, r.Error
}
if len(r.Stdout) == 0 {
continue
}
addPatchFiles(repoDir)
}
return pset, nil
}
// process each file in pDir/
func addPatchFiles(pDir string) error {
// log.Info("ADD PATCH FILES ADDED DIR", pDir)
var baderr error
filepath.Walk(pDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
// Handle possible errors, like permission issues
fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
baderr = err
return err
}
if info.IsDir() {
return nil
}
// log.Info("TESTING FILE", path)
data, err := os.ReadFile(path)
if err != nil {
log.Info("addPatchFile() failed", path)
baderr = err
return err
}
patch := new(Patch)
patch.Filename = path
patch.Data = data
pset.Patchs = append(pset.Patchs, patch)
// log.Info("ADDED PATCH FILE", path)
return nil
})
return baderr
}
// just an example of how to walk only directories
func onlyWalkDirs(pDir string) error {
log.Info("DIR", pDir)
// var all []string
var baderr error
filepath.WalkDir(pDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
// Handle possible errors, like permission issues
fmt.Fprintf(os.Stderr, "error accessing path %q: %v\n", path, err)
baderr = err
return err
}
log.Info("TESTING DIR", path)
if d.IsDir() {
return filepath.SkipDir
}
log.Info("NEVER GETS HERE? WHAT IS THIS?", path)
return nil
})
return baderr
}