summaryrefslogtreecommitdiff
path: root/repo.new.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-23 14:51:23 -0500
committerJeff Carr <[email protected]>2025-09-23 14:51:23 -0500
commit843d9fbb8d31b2735621d02631924155615d97f6 (patch)
tree1577b07d981924584110de7ae39f82e75795b115 /repo.new.go
parent9c1ba367f6b90e58bbbb57468b88205ca1e69dbb (diff)
lookup git PatchIdv0.0.133
Diffstat (limited to 'repo.new.go')
-rw-r--r--repo.new.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/repo.new.go b/repo.new.go
index 619ff05..cb79bf2 100644
--- a/repo.new.go
+++ b/repo.new.go
@@ -1,9 +1,13 @@
package gitpb
import (
+ "bytes"
"errors"
+ "fmt"
"net/url"
+ "os/exec"
"path/filepath"
+ "strings"
"go.wit.com/log"
)
@@ -131,3 +135,56 @@ func NewRepo(fullpath string) (*Repo, error) {
}
return &repo, nil
}
+
+func (repo *Repo) FindPatchId(hash string) (string, error) {
+ if hash == "" {
+ return "", log.Errorf("commit hash blank")
+ }
+
+ // 1. Create the command to get the diff for the commit.
+ // "git show" is the perfect tool for this.
+ cmdShow := exec.Command("git", "show", hash)
+ cmdShow.Dir = repo.GetFullPath()
+
+ // 2. Create the command to calculate the patch-id from stdin.
+ cmdPipeID := exec.Command("git", "patch-id", "--stable")
+ cmdPipeID.Dir = repo.GetFullPath()
+
+ // 3. Connect the output of "git show" to the input of "git patch-id".
+ // This is the Go equivalent of the shell pipe `|`.
+ pipe, err := cmdShow.StdoutPipe()
+ if err != nil {
+ return "", fmt.Errorf("failed to create pipe: %w", err)
+ }
+ cmdPipeID.Stdin = pipe
+
+ // 4. We need a buffer to capture the final output from git patch-id.
+ var output bytes.Buffer
+ cmdPipeID.Stdout = &output
+
+ // 5. Start the reading command (patch-id) first.
+ if err := cmdPipeID.Start(); err != nil {
+ return "", fmt.Errorf("failed to start git-patch-id: %w", err)
+ }
+
+ // 6. Run the writing command (show). This will block until it's done.
+ if err := cmdShow.Run(); err != nil {
+ return "", fmt.Errorf("failed to run git-show: %w", err)
+ }
+
+ // 7. Wait for the reading command to finish.
+ if err := cmdPipeID.Wait(); err != nil {
+ return "", fmt.Errorf("failed to wait for git-patch-id: %w", err)
+ }
+
+ fields := strings.Fields(output.String())
+ if len(fields) != 2 {
+ return "", fmt.Errorf("git-patch-id produced empty output")
+ }
+
+ if fields[1] != hash {
+ return "", fmt.Errorf("patchid did not match %s != %v", hash, fields)
+ }
+
+ return fields[0], nil
+}