177 lines
4.2 KiB
Go
177 lines
4.2 KiB
Go
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
|
|
// Use of this source code is governed by the GPL 3.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.wit.com/lib/protobuf/forgepb"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
func doPatch() error {
|
|
if argv.Patch.Submit != "" {
|
|
_, err := me.forge.SubmitDevelPatchSet(argv.Patch.Submit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if argv.Patch.Get != nil {
|
|
return doPatchGet()
|
|
}
|
|
|
|
if argv.Patch.List != nil {
|
|
return doPatchList()
|
|
}
|
|
|
|
// if no option is given to patch, list out the
|
|
// repos that have patches ready in them
|
|
findReposWithPatches()
|
|
if me.found.Len() == 0 {
|
|
log.Info("you currently have no patches in your user branches")
|
|
return nil
|
|
}
|
|
me.forge.PrintHumanTable(me.found)
|
|
return nil
|
|
}
|
|
|
|
func doPatchList() error {
|
|
psets, err := openPatchsets()
|
|
if err != nil {
|
|
log.Info("Open Patchsets failed", err)
|
|
return err
|
|
}
|
|
log.Info("got psets len", len(psets.Patchsets))
|
|
all := psets.SortByName()
|
|
for all.Scan() {
|
|
pset := all.Next()
|
|
// log.Info("pset name =", pset.Name)
|
|
dumpPatchset(pset)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func savePatchsets(psets *forgepb.Patchsets) error {
|
|
data, err := psets.Marshal()
|
|
if err != nil {
|
|
log.Info("protobuf.Marshal() failed:", err)
|
|
return err
|
|
}
|
|
fullpath := filepath.Join(me.forge.GetConfigDir(), "patchsets.pb")
|
|
var pfile *os.File
|
|
pfile, err = os.OpenFile(fullpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
if err != nil {
|
|
log.Info("Patchsets save failed:", err, fullpath)
|
|
return err
|
|
}
|
|
pfile.Write(data)
|
|
pfile.Close()
|
|
return nil
|
|
}
|
|
|
|
func openPatchsets() (*forgepb.Patchsets, error) {
|
|
fullpath := filepath.Join(me.forge.GetConfigDir(), "patchsets.pb")
|
|
data, err := os.ReadFile(fullpath)
|
|
if err != nil {
|
|
log.Info("Patchsets open failed:", err, fullpath)
|
|
return nil, err
|
|
}
|
|
|
|
psets := new(forgepb.Patchsets)
|
|
err = psets.Unmarshal(data)
|
|
if err != nil {
|
|
log.Info("Unmarshal patchsets failed", err)
|
|
return nil, err
|
|
}
|
|
return psets, nil
|
|
}
|
|
|
|
// returns bad if patches can not be applied
|
|
// logic is not great here but it was a first pass
|
|
func dumpPatchset(pset *forgepb.Patchset) bool {
|
|
|
|
// don't even bother to continue if we already know it's broken
|
|
if pset.State == "BROKEN" {
|
|
log.Printf("Patchset Name: %-24s Author: %s <%s> IS BAD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
|
|
return false
|
|
} else {
|
|
log.Printf("Patchset Name: %-24s Author: %s <%s> IS GOOD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
|
|
}
|
|
|
|
/*
|
|
log.Info("applyPatches() State", pset.State)
|
|
log.Info("applyPatches() COMMENT", pset.Comment)
|
|
log.Info("applyPatches() Branch Name", pset.GetStartBranchName())
|
|
log.Info("applyPatches() Start Hash", pset.GetStartBranchHash())
|
|
*/
|
|
|
|
var count int
|
|
var bad int
|
|
all := pset.Patches.SortByFilename()
|
|
for all.Scan() {
|
|
p := all.Next()
|
|
if IsValidPatch(p) {
|
|
// ok
|
|
} else {
|
|
pset.State = "BROKEN"
|
|
bad += 1
|
|
}
|
|
count += 1
|
|
}
|
|
log.Info("pset has", count, "total patches, ", bad, "bad patches")
|
|
if bad == 0 {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsValidPatch(p *forgepb.Patch) bool {
|
|
basepath, filename := filepath.Split(p.Filename)
|
|
repo := me.forge.FindByGoPath(basepath)
|
|
if argv.Verbose {
|
|
log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
|
|
}
|
|
if repo == nil {
|
|
log.Info("can not apply patch! repo not found", basepath, filename)
|
|
return false
|
|
}
|
|
if repo.DevelHash() != p.StartHash {
|
|
log.Info("can not apply patch! devel hash mismatch", basepath, filename)
|
|
return false
|
|
}
|
|
if repo.DevelHash() == p.StartHash {
|
|
log.Info("local devel hash:", repo.DevelHash(), "matches patch hash", p.StartHash, "and can be applied")
|
|
}
|
|
log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
|
|
for _, line := range p.Files {
|
|
log.Info("\t", line)
|
|
}
|
|
return true
|
|
}
|
|
|
|
func doPatchGet() error {
|
|
psets, err := me.forge.GetPatchesets()
|
|
if err != nil {
|
|
log.Info("Get Patchsets failed", err)
|
|
return err
|
|
}
|
|
log.Info("Got Patchsets ok", psets.Uuid)
|
|
|
|
log.Info("got psets len", len(psets.Patchsets))
|
|
all := psets.SortByName()
|
|
for all.Scan() {
|
|
pset := all.Next()
|
|
// log.Info("pset name =", pset.Name)
|
|
dumpPatchset(pset)
|
|
}
|
|
|
|
if err := savePatchsets(psets); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|