forge/doPatch.go

124 lines
3.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 (
"path/filepath"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
func doPatch() error {
if err := me.forge.LoadPatchsets(); err != nil {
log.Info("patches failed to open", err)
if err := me.forge.SavePatchsets(); err != nil {
log.Warn("savePatchsets() failed", err)
}
}
if argv.Patch.Submit != nil {
_, err := me.forge.SubmitDevelPatchSet(argv.Patch.Submit.Match)
if err != nil {
return err
}
return nil
}
if argv.Patch.Get != nil {
if err := me.forge.GetPatches(); err != nil {
log.Info("Get Patchsets Failed", err)
return err
}
me.forge.Patchsets.PrintTable()
return nil
}
if argv.Patch.Check != nil {
for pset := range me.forge.Patchsets.IterAll() {
log.Info(pset.Uuid)
for patch := range pset.Patches.IterAll() {
if repo, ok := me.forge.IsPatchApplied(patch); ok {
log.Info("found patch in repo", repo.Namespace, patch.Filename)
} else {
// log.Info("did not find patch", patch.CommitHash, patch.NewHash, patch.Filename)
}
}
}
// me.forge.Patchsets.PrintTable()
return nil
}
if argv.Patch.List != nil {
me.forge.Patchsets.PrintTable()
// return doPatchList()
return nil
}
// if no option is given to patch, list out the
// repos that have patches ready in them
found := findReposWithPatches()
if found.Len() == 0 {
log.Info("you currently have no patches in your user branches")
return nil
}
me.forge.PrintHumanTable(found)
return 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())
}
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
}