diff options
Diffstat (limited to 'doPatch.go')
| -rw-r--r-- | doPatch.go | 172 |
1 files changed, 113 insertions, 59 deletions
@@ -7,6 +7,7 @@ import ( "os" "path/filepath" + "go.wit.com/lib/fhelp" "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" @@ -95,6 +96,40 @@ func doPatch() error { if argv.Patch.List != nil { log.Info("todo: fix this") + curpatches := forgepb.NewPatches() + curpatches.Filename = "/tmp/curpatches.pb" + if err := curpatches.Load(); err != nil { + return err + } + curpatches.PrintTable() + for patch := range curpatches.IterAll() { + repo := me.forge.Repos.FindByNamespace(patch.Namespace) + if repo == nil { + log.Info("no namespace", patch.PatchId, patch.Namespace, patch.Comment) + continue + } + newId, newHash, err := isPatchIdApplied(repo, patch) + if err != nil { + log.Info("err", patch.PatchId, patch.Namespace, patch.Comment, err) + return patch.Error(err) + } + if (newId == patch.PatchId) && (newHash == patch.CommitHash) { + log.Info(patch.PatchId, "patch made here", patch.Comment) + continue + } + if newId == patch.PatchId { + log.Info(patch.PatchId, "patch already applied", patch.Comment) + continue + } + if newId != patch.PatchId { + log.Info(patch.PatchId, "probably duplicate subject", patch.Comment) + } + log.Info("new patch", patch.PatchId, patch.Comment) + if fhelp.QuestionUser("apply this patch?") { + newhash, err := applyPatch(repo, patch) + log.Info("apply results:", newhash, err) + } + } // return doPatchList() return nil } @@ -105,6 +140,29 @@ func doPatch() error { return nil } +func applyPatch(repo *gitpb.Repo, p *forgepb.Patch) (string, error) { + _, filen := filepath.Split(p.Filename) + tmpname := filepath.Join("/tmp", filen) + log.Info("saving as", tmpname, p.Filename) + raw, err := os.OpenFile(tmpname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return "", err + } + raw.Write(p.Data) + raw.Close() + + cmd := []string{"git", "am", tmpname} + err = repo.RunVerbose(cmd) + if err != nil { + log.Info("git am failed. run 'git am --abort' here") + return "", log.Errorf("git am failed") + } + + log.Info("Try to find hash value now") + + return p.NewHash, log.Errorf("did not lookup new hash") +} + func doPatchGet(newpb *forgepb.Sets) { // var changed bool curpatches := forgepb.NewPatches() @@ -159,32 +217,25 @@ func doPatchGet(newpb *forgepb.Sets) { // me.forge.Patchsets.Save() } -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) - } - +func isPatchIdApplied(repo *gitpb.Repo, patch *forgepb.Patch) (string, string, error) { 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) + return "", "", log.Errorf("comment not found") } patchId, err := repo.FindPatchIdByHash(newhash) if err != nil { - return err + return "", "", err } - patch.PatchId = patchId - patch.NewHash = newhash - - log.Infof("%s %s patch: found hash: %s %s\n", patch.CommitHash, newhash, patch.Namespace, comment) - return nil + // log.Infof("%s %s found hash by comment %s \n", patchId, newhash, patch.Comment) + return patchId, newhash, nil } +/* func doPatchList() error { // if nothing selected, list the patches var changed bool @@ -239,63 +290,66 @@ func doPatchList() error { } } } - /* - 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) - } +*/ +/* + 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 - } +*/ +/* + 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 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 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 +*/ +// } // Shows repos that are: // - git dirty repos |
