From 71138fb15b014200cc9c18e9c903700794947678 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 13 Oct 2025 03:43:41 -0500 Subject: patch process getting better --- doPatch.findByPatchId.go | 117 ----------------------------------------------- doPatch.go | 85 ++++++++++++++++++++-------------- findByPatchId.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+), 151 deletions(-) delete mode 100644 doPatch.findByPatchId.go create mode 100644 findByPatchId.go diff --git a/doPatch.findByPatchId.go b/doPatch.findByPatchId.go deleted file mode 100644 index 6882055..0000000 --- a/doPatch.findByPatchId.go +++ /dev/null @@ -1,117 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "errors" - "fmt" - "io" - "os" - "os/exec" - "strings" -) - -func searchAllCommits(targetPatchID string) (string, error) { - revListArgs := []string{"--all"} - cmdRevList := exec.Command("git", append([]string{"rev-list"}, revListArgs...)...) - - // We'll read the commit hashes from the command's standard output. - revListStdout, err := cmdRevList.StdoutPipe() - if err != nil { - fmt.Fprintf(os.Stderr, "Error creating rev-list stdout pipe: %v\n", err) - return "", err - } - - // Start the command but don't wait for it to finish yet. - if err := cmdRevList.Start(); err != nil { - fmt.Fprintf(os.Stderr, "Error starting git rev-list: %v\n", err) - return "", err - } - - var commitHash string - // 3. Process the stream of commit hashes line by line. - scanner := bufio.NewScanner(revListStdout) - found := false - for scanner.Scan() { - commitHash = scanner.Text() - - // 4. For each commit, calculate its patch ID. - calculatedIDs, err := getPatchIDForCommit(commitHash) - if err != nil { - // Log the error but continue trying other commits. - fmt.Fprintf(os.Stderr, "Warning: could not get patch-id for %s: %v\n", commitHash, err) - continue - } - - // 5. Compare the calculated IDs with our target. - for _, id := range calculatedIDs { - if id == targetPatchID { - fmt.Printf("Found matching commit: %s\n", commitHash) - found = true - return commitHash, nil - // We could exit here, but continuing will find all duplicates (e.g., from cherry-picks). - } - } - } - - // Wait for the rev-list command to finish and check for errors. - if err := cmdRevList.Wait(); err != nil { - fmt.Fprintf(os.Stderr, "Error running git rev-list: %v\n", err) - return "", err - } - if err := scanner.Err(); err != nil { - fmt.Fprintf(os.Stderr, "Error reading commit hashes: %v\n", err) - return "", err - } - - if !found { - fmt.Fprintf(os.Stderr, "No commit found with patch ID: %s\n", targetPatchID) - return "not found", errors.New("patchId not found") - } - return commitHash, nil -} - -// getPatchIDForCommit runs `git show | git patch-id --stable` -// and returns the stable and unstable patch IDs. -func getPatchIDForCommit(commitHash string) ([]string, error) { - // Command to get the commit's diff - cmdShow := exec.Command("git", "show", commitHash) - - // Command to calculate the patch-id from stdin - cmdPatchID := exec.Command("git", "patch-id", "--stable") - - // Create a pipe to connect the output of cmdShow to the input of cmdPatchID. - r, w := io.Pipe() - cmdShow.Stdout = w - cmdPatchID.Stdin = r - - var patchIDOutput bytes.Buffer - cmdPatchID.Stdout = &patchIDOutput - - // Start both commands. - if err := cmdShow.Start(); err != nil { - return nil, fmt.Errorf("failed to start git-show: %w", err) - } - if err := cmdPatchID.Start(); err != nil { - return nil, fmt.Errorf("failed to start git-patch-id: %w", err) - } - - // Wait for 'show' to finish writing to the pipe, then close the writer. - // This will signal EOF to the 'patch-id' command's stdin. - errShow := cmdShow.Wait() - w.Close() - if errShow != nil { - return nil, fmt.Errorf("git-show failed: %w", errShow) - } - - // Now wait for 'patch-id' to finish reading and processing. - errPatchID := cmdPatchID.Wait() - if errPatchID != nil { - return nil, fmt.Errorf("git-patch-id failed: %w", errPatchID) - } - - // The output is typically " ". - // We split the string by whitespace to get both. - ids := strings.Fields(patchIDOutput.String()) - return ids, nil -} diff --git a/doPatch.go b/doPatch.go index ef43095..c895c8a 100644 --- a/doPatch.go +++ b/doPatch.go @@ -12,7 +12,6 @@ import ( "path/filepath" "regexp" "strings" - "time" "go.wit.com/lib/fhelp" "go.wit.com/lib/protobuf/forgepb" @@ -98,6 +97,7 @@ func doPatchSubmit() (string, error) { func doPatchProcess() (string, error) { var needfix int + needapply := forgepb.NewPatches() for patch := range me.curpatches.IterAll() { repo := me.forge.Repos.FindByNamespace(patch.Namespace) if repo == nil { @@ -149,41 +149,61 @@ func doPatchProcess() (string, error) { } // log.Info(patch.PatchId, newId, repo.Namespace, "new patch", patch.Comment) patch.State = "new patch" + needapply.Clone(patch) + } + var applycounter int + for patch := range needapply.IterAll() { + repo := me.forge.Repos.FindByNamespace(patch.Namespace) + if repo == nil { + if argv.Verbose { + // dump the whole patch + log.Info("skipping patch for unknown namespace", patch.Namespace) + } + continue + } if !argv.Patch.Apply { + // --apply isn't set. don't go any further needfix += 1 - } else { + continue + } + // show some of the patch details + if argv.Verbose { + // dump the whole patch log.Info(string(patch.Data)) - log.Info(repo.FullPath, "todo: show last commit to repo here") - log.Info(repo.FullPath, patch.Comment) - for _, fname := range patch.Files { - log.Info(repo.FullPath, fname) + } + log.Info(repo.FullPath, "") + log.Info(repo.FullPath, patch.Comment) + for _, fname := range patch.Files { + log.Info(repo.FullPath, fname) + } + for _, line := range strings.Split(string(patch.Data), "\n") { + // log.Info(repo.FullPath, line) + if strings.HasPrefix(line, "From: ") { + log.Info(repo.FullPath, line) + continue } - for _, line := range strings.Split(string(patch.Data), "\n") { - // log.Info(repo.FullPath, line) - if strings.HasPrefix(line, "From: ") { - log.Info(repo.FullPath, line) - continue - } - if strings.HasPrefix(line, "Date: ") { - log.Info(repo.FullPath, line) - continue - } - if strings.HasPrefix(line, "---") { - break - } - if line == "-- " { - break - } + if strings.HasPrefix(line, "Date: ") { + log.Info(repo.FullPath, line) + continue } - log.Info(repo.FullPath, patch.Gan, patch.Gae, patch.GaI) - if fhelp.QuestionUser("apply this patch? (--force to autoapply)") { - newhash, err := applyPatch(repo, patch) - if err != nil { - log.Info("apply results:", newhash, err) - } - if err != nil { - return "git am problem. manually investigate or purge everything and start over", err - } + if strings.HasPrefix(line, "---") { + break + } + if line == "-- " { + break + } + } + log.Info(repo.FullPath, patch.Gan, patch.Gae, patch.GaI) + // prompt user to apply patch + applycounter += 1 + s := log.Sprintf("apply this patch (%d/%d)? (--force to autoapply)", applycounter, needapply.Len()) + if fhelp.QuestionUser(s) { + newhash, err := applyPatch(repo, patch) + if err != nil { + log.Info("apply results:", newhash, err) + } + if err != nil { + return "git am problem. manually investigate or purge everything and start over", err } } } @@ -289,9 +309,6 @@ func applyPatch(repo *gitpb.Repo, p *forgepb.Patch) (string, error) { } log.Info(cmd) log.Info("everything worked ok") - log.Info("sleep 5") - time.Sleep(5 * time.Second) - return "patch applied with git am", nil } diff --git a/findByPatchId.go b/findByPatchId.go new file mode 100644 index 0000000..6882055 --- /dev/null +++ b/findByPatchId.go @@ -0,0 +1,117 @@ +package main + +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io" + "os" + "os/exec" + "strings" +) + +func searchAllCommits(targetPatchID string) (string, error) { + revListArgs := []string{"--all"} + cmdRevList := exec.Command("git", append([]string{"rev-list"}, revListArgs...)...) + + // We'll read the commit hashes from the command's standard output. + revListStdout, err := cmdRevList.StdoutPipe() + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating rev-list stdout pipe: %v\n", err) + return "", err + } + + // Start the command but don't wait for it to finish yet. + if err := cmdRevList.Start(); err != nil { + fmt.Fprintf(os.Stderr, "Error starting git rev-list: %v\n", err) + return "", err + } + + var commitHash string + // 3. Process the stream of commit hashes line by line. + scanner := bufio.NewScanner(revListStdout) + found := false + for scanner.Scan() { + commitHash = scanner.Text() + + // 4. For each commit, calculate its patch ID. + calculatedIDs, err := getPatchIDForCommit(commitHash) + if err != nil { + // Log the error but continue trying other commits. + fmt.Fprintf(os.Stderr, "Warning: could not get patch-id for %s: %v\n", commitHash, err) + continue + } + + // 5. Compare the calculated IDs with our target. + for _, id := range calculatedIDs { + if id == targetPatchID { + fmt.Printf("Found matching commit: %s\n", commitHash) + found = true + return commitHash, nil + // We could exit here, but continuing will find all duplicates (e.g., from cherry-picks). + } + } + } + + // Wait for the rev-list command to finish and check for errors. + if err := cmdRevList.Wait(); err != nil { + fmt.Fprintf(os.Stderr, "Error running git rev-list: %v\n", err) + return "", err + } + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "Error reading commit hashes: %v\n", err) + return "", err + } + + if !found { + fmt.Fprintf(os.Stderr, "No commit found with patch ID: %s\n", targetPatchID) + return "not found", errors.New("patchId not found") + } + return commitHash, nil +} + +// getPatchIDForCommit runs `git show | git patch-id --stable` +// and returns the stable and unstable patch IDs. +func getPatchIDForCommit(commitHash string) ([]string, error) { + // Command to get the commit's diff + cmdShow := exec.Command("git", "show", commitHash) + + // Command to calculate the patch-id from stdin + cmdPatchID := exec.Command("git", "patch-id", "--stable") + + // Create a pipe to connect the output of cmdShow to the input of cmdPatchID. + r, w := io.Pipe() + cmdShow.Stdout = w + cmdPatchID.Stdin = r + + var patchIDOutput bytes.Buffer + cmdPatchID.Stdout = &patchIDOutput + + // Start both commands. + if err := cmdShow.Start(); err != nil { + return nil, fmt.Errorf("failed to start git-show: %w", err) + } + if err := cmdPatchID.Start(); err != nil { + return nil, fmt.Errorf("failed to start git-patch-id: %w", err) + } + + // Wait for 'show' to finish writing to the pipe, then close the writer. + // This will signal EOF to the 'patch-id' command's stdin. + errShow := cmdShow.Wait() + w.Close() + if errShow != nil { + return nil, fmt.Errorf("git-show failed: %w", errShow) + } + + // Now wait for 'patch-id' to finish reading and processing. + errPatchID := cmdPatchID.Wait() + if errPatchID != nil { + return nil, fmt.Errorf("git-patch-id failed: %w", errPatchID) + } + + // The output is typically " ". + // We split the string by whitespace to get both. + ids := strings.Fields(patchIDOutput.String()) + return ids, nil +} -- cgit v1.2.3