summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-06 19:27:40 -0500
committerJeff Carr <[email protected]>2025-10-06 19:27:40 -0500
commitb047426232e5c7737a5d669dcb7fe482be2a3cc5 (patch)
tree84bd39b5796a42930579ae9bc419bb684fbecff3
parent02a2d26694787cf2e59683bdd38274b40a9670ad (diff)
isolate patchid code
-rw-r--r--patchid.go72
-rw-r--r--repo.new.go76
2 files changed, 72 insertions, 76 deletions
diff --git a/patchid.go b/patchid.go
new file mode 100644
index 0000000..28c8313
--- /dev/null
+++ b/patchid.go
@@ -0,0 +1,72 @@
+package gitpb
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+
+ "go.wit.com/log"
+)
+
+
+// git show 5b277e7686974d2195586d5f5b82838ee9ddb036 |git patch-id --stable
+// bf86be06af03b1a89ee155b214358362ec76f7b6 5b277e7686974d2195586d5f5b82838ee9ddb036
+// forged patch: "working on ping pong"
+// The --stable flag is an important detail. When you use it, git patch-id outputs two hashes:
+// <stable_patch_id> <unstable_patch_id>
+func (repo *Repo) FindPatchIdByHash(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
+}
diff --git a/repo.new.go b/repo.new.go
index 65aa251..cd338f0 100644
--- a/repo.new.go
+++ b/repo.new.go
@@ -56,29 +56,6 @@ func (all *Repos) NewGoRepo(fullpath string, gopath string) (*Repo, error) {
return nil, errors.New("repo gitpb.NewGoPath() should never have gotten here " + gopath)
}
-// enforces GoPath is unique
-// TODO: deprecate this (?)
-// mutex's should finally work in the autogenpb protobuf code
-/*
-func (all *Repos) AppendByGoPathOld(newr *Repo) bool {
- // all.RLock()
- repoMu.RLock()
-
- for _, r := range all.Repos {
- if r.GoInfo.GoPath == newr.GoInfo.GoPath {
- // all.RUnlock()
- repoMu.RUnlock()
- return false
- }
- }
- // all.RUnlock()
- repoMu.RUnlock()
-
- all.Append(newr)
- return true
-}
-*/
-
func (all *Repos) NewRepo(fullpath string, namespace string) (*Repo, error) {
if r := all.FindByFullPath(fullpath); r != nil {
log.Info("gitpb.NewRepo() might already have namespace", r.GetNamespace())
@@ -137,56 +114,3 @@ 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
-}