delete applied patches from PB file

This commit is contained in:
Jeff Carr 2025-09-25 16:24:08 -05:00
parent 92be907bce
commit d331af80e0
3 changed files with 130 additions and 83 deletions

11
argv.go
View File

@ -18,7 +18,7 @@ var argv args
type args struct { type args struct {
Checkout *CheckoutCmd `arg:"subcommand:checkout" help:"switch branches using 'git checkout'"` Checkout *CheckoutCmd `arg:"subcommand:checkout" help:"switch branches using 'git checkout'"`
Clean *CleanCmd `arg:"subcommand:clean" help:"start over at the beginning"` Clean *CleanCmd `arg:"subcommand:reset" help:"reset all git repos to the original state"`
Commit *CommitCmd `arg:"subcommand:commit" help:"'git commit' but errors out if on wrong branch"` Commit *CommitCmd `arg:"subcommand:commit" help:"'git commit' but errors out if on wrong branch"`
Config *ConfigCmd `arg:"subcommand:config" help:"show your .config/forge/ settings"` Config *ConfigCmd `arg:"subcommand:config" help:"show your .config/forge/ settings"`
Gui *EmptyCmd `arg:"subcommand:gui" help:"open the gui"` Gui *EmptyCmd `arg:"subcommand:gui" help:"open the gui"`
@ -35,9 +35,10 @@ type args struct {
func (args) Examples() string { func (args) Examples() string {
var out string var out string
out += "forge merge --all # merge all patches to devel & master\n"
out += "forge pull --force # 'git pull' on all repos\n" out += "forge pull --force # 'git pull' on all repos\n"
out += "forge normal # the defaults for 'normal' forge distributed development\n" out += "forge merge --all # merge all patches to devel & master\n"
out += "forge normal fix # the defaults for 'normal' forge distributed development (makes local user branches)\n"
out += "forge reset fix # smartly restore all git repos to the upstream state (removes changes forge might have made)\n"
return out return out
} }
@ -86,7 +87,7 @@ type DevCmd struct {
type CleanCmd struct { type CleanCmd struct {
Fix *EmptyCmd `arg:"subcommand:fix" help:"try to fix problems"` Fix *EmptyCmd `arg:"subcommand:fix" help:"try to fix problems"`
Repo string `arg:"--repo" help:"which repo to look at"` Repo string `arg:"--repo" help:"work on one specific git repository"`
} }
// matches // matches
@ -205,7 +206,7 @@ func (args) Appname() string {
func (a args) DoAutoComplete(pb *prep.Auto) { func (a args) DoAutoComplete(pb *prep.Auto) {
if pb.Cmd == "" { if pb.Cmd == "" {
pb.Autocomplete3([]string{"checkout", "clean", "commit", "config", "gui", "merge", "normal", "patch", "pull", "show"}) pb.Autocomplete3([]string{"checkout", "reset", "commit", "config", "gui", "merge", "normal", "patch", "pull", "show"})
} else { } else {
pb.SubCommand(pb.Argv...) pb.SubCommand(pb.Argv...)
} }

View File

@ -100,86 +100,106 @@ func doPatch() error {
} }
if argv.Patch.List != nil { if argv.Patch.List != nil {
var changed bool return doPatchList()
newpatches := new(forgepb.Set)
newpatches.Patches = forgepb.NewPatches()
for pset := range me.forge.Patchsets.IterAll() {
pset.PrintTable()
for patch := range pset.Patches.IterAll() {
changed = true
if patch.NewHash == "" || patch.NewHash == "na" {
if newpatches.Patches.AppendByPatchId(patch) {
log.Info("patchId added here", patch.PatchId)
} else {
log.Info("patchId already here", patch.PatchId)
}
} else {
if err := setNewCommitHash(patch); err != nil {
log.Infof("%s bad check on patch failure %v\n", patch.Filename, err)
return err
}
log.Info("newhash set already here", patch.PatchId, patch.NewHash)
}
}
}
if changed {
if err := me.forge.SavePatchsets(); err != nil {
log.Warn("savePatchsets() failed", err)
}
}
log.Info("NEW PATCHES TABLE")
newpatches.PrintTable()
for patch := range newpatches.Patches.IterAll() {
if err := setNewCommitHash(patch); err == nil {
log.Info("newhash set already here", patch.PatchId, patch.NewHash)
continue
}
log.Infof("%s is new\n", patch.Filename)
repo := me.forge.FindByGoPath(patch.Namespace)
if repo == nil {
log.Info("\tCould not find namespace:", patch.Namespace)
continue
}
if fhelp.QuestionUser("apply this patch?") {
newhash, err := applyAndTrackPatch(repo, patch)
log.Info("apply results:", newhash, err)
}
}
return nil
/*
if newpatches.Len() != 0 {
for patch := range newpatches.IterAll() {
log.Info("new patch:", patch.CommitHash, patch.NewHash, patch.Filename)
repo := me.forge.FindByGoPath(patch.Namespace)
if repo == nil {
log.Info("\tCould not find namespace:", patch.Namespace)
continue
}
}
return log.Errorf("patches need to be applied")
}
// return doPatchList()
applied := findApplied()
if applied == nil || applied.Len() == 0 {
log.Info("no patches have been appled to the devel branch yet")
return nil
}
// for patch := range applied.IterAll() {
// log.Info("SEND APPLIED: newhash:", patch.NewHash, "commithash:", patch.CommitHash, "patch", patch.Namespace)
// }
newpb, _, err := applied.HttpPostVerbose(myServer(), "applied")
if err != nil {
return err
}
newpb.PrintTable()
return nil
*/
} }
// if nothing, show patches & dirty repos // list patches by default
me.forge.Patchsets.PrintTable() return doPatchList()
showWorkRepos() }
func doPatchList() error {
// if nothing selected, list the patches
var changed bool
newpatches := new(forgepb.Set)
newpatches.Patches = forgepb.NewPatches()
for pset := range me.forge.Patchsets.IterAll() {
pset.PrintTable()
if pset.Patches.Len() == 0 {
log.Info("pset is empty")
me.forge.Patchsets.Delete(pset)
changed = true
continue
}
for patch := range pset.Patches.IterAll() {
changed = true
if err := isPatchIdApplied(patch); err == nil {
log.Info("patchId already applied", patch.PatchId, patch.Filename)
pset.Patches.Delete(patch)
changed = true
continue
}
if patch.NewHash == "" || patch.NewHash == "na" {
if newpatches.Patches.AppendByPatchId(patch) {
log.Info("patchId added here", patch.PatchId)
} else {
log.Info("patchId already here", patch.PatchId)
pset.Patches.Delete(patch)
changed = true
}
} else {
if err := setNewCommitHash(patch); err != nil {
log.Infof("%s bad check on patch failure %v\n", patch.Filename, err)
return err
}
log.Info("newhash set already here", patch.PatchId, patch.NewHash)
}
}
}
log.Info("NEW PATCHES TABLE")
newpatches.PrintTable()
for patch := range newpatches.Patches.IterAll() {
if err := setNewCommitHash(patch); err == nil {
log.Info("newhash set already here", patch.PatchId, patch.NewHash)
changed = true
continue
}
log.Infof("%s is new\n", patch.Filename)
repo := me.forge.FindByGoPath(patch.Namespace)
if repo == nil {
log.Info("\tCould not find namespace:", patch.Namespace)
continue
}
if fhelp.QuestionUser("apply this patch?") {
newhash, err := applyAndTrackPatch(repo, patch)
log.Info("apply results:", newhash, err)
}
}
if changed {
if err := me.forge.SavePatchsets(); err != nil {
log.Warn("savePatchsets() failed", err)
}
}
return nil
/*
if newpatches.Len() != 0 {
for patch := range newpatches.IterAll() {
log.Info("new patch:", patch.CommitHash, patch.NewHash, patch.Filename)
repo := me.forge.FindByGoPath(patch.Namespace)
if repo == nil {
log.Info("\tCould not find namespace:", patch.Namespace)
continue
}
}
return log.Errorf("patches need to be applied")
}
// return doPatchList()
applied := findApplied()
if applied == nil || applied.Len() == 0 {
log.Info("no patches have been appled to the devel branch yet")
return nil
}
// for patch := range applied.IterAll() {
// log.Info("SEND APPLIED: newhash:", patch.NewHash, "commithash:", patch.CommitHash, "patch", patch.Namespace)
// }
newpb, _, err := applied.HttpPostVerbose(myServer(), "applied")
if err != nil {
return err
}
newpb.PrintTable()
return nil
*/
return nil return nil
} }

View File

@ -172,6 +172,32 @@ func findCommitBySubject(subject string) (string, error) {
return "", fmt.Errorf("no commit found for subject: %s", subject) return "", fmt.Errorf("no commit found for subject: %s", subject)
} }
func isPatchIdApplied(patch *forgepb.Patch) error {
repo := me.forge.FindByGoPath(patch.Namespace)
if repo == nil {
return log.Errorf("could not find repo %s", patch.Namespace)
}
comment := cleanSubject(patch.Comment)
os.Chdir(repo.GetFullPath())
newhash, err := findCommitBySubject(comment)
if err != nil {
return log.Errorf("patch: not found hash: %s %s %s %s %v", patch.CommitHash, patch.Namespace, comment, newhash, err)
}
patchId, err := repo.FindPatchId(newhash)
if err != nil {
return err
}
patch.PatchId = patchId
patch.NewHash = newhash
log.Info("patch: found hash:", patch.CommitHash, newhash, patch.Namespace, comment)
return nil
}
// returns true if PB changed // returns true if PB changed
func setNewCommitHash(patch *forgepb.Patch) error { func setNewCommitHash(patch *forgepb.Patch) error {
repo := me.forge.FindByGoPath(patch.Namespace) repo := me.forge.FindByGoPath(patch.Namespace)