attempt to find PatchId
This commit is contained in:
parent
a1a524e933
commit
698e404051
108
patchset.Make.go
108
patchset.Make.go
|
@ -1,9 +1,11 @@
|
|||
package forgepb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -83,40 +85,6 @@ func (f *Forge) MakeDevelPatchSet(name string) (*Patchset, error) {
|
|||
return pset, nil
|
||||
}
|
||||
|
||||
/*
|
||||
func (f *Forge) MakeMasterPatchSet() (*Patchset, error) {
|
||||
pset := newPatchset("masterBranchPS")
|
||||
dir, err := os.MkdirTemp("", "forge")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(dir) // clean up
|
||||
pset.TmpDir = dir
|
||||
|
||||
all := f.Repos.SortByFullPath()
|
||||
for all.Scan() {
|
||||
repo := all.Next()
|
||||
startb := repo.GetMasterBranchName()
|
||||
endb := repo.GetUserBranchName()
|
||||
|
||||
if startb == "" {
|
||||
continue
|
||||
}
|
||||
if endb == "" {
|
||||
continue
|
||||
}
|
||||
// log.Info("repo", repo.GetGoPath(), startb, "..", endb)
|
||||
pset.StartBranchName = startb
|
||||
pset.EndBranchName = endb
|
||||
err := pset.makePatchSetNew(repo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return pset, nil
|
||||
}
|
||||
*/
|
||||
|
||||
func (pset *Patchset) makePatchSetNew(repo *gitpb.Repo) error {
|
||||
startBranch := pset.StartBranchName
|
||||
endBranch := pset.EndBranchName
|
||||
|
@ -163,6 +131,8 @@ func (pset *Patchset) makePatchSetNew(repo *gitpb.Repo) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// git show <original_commit_hash> | git patch-id
|
||||
// git cat-file -p <commit_hash> | grep tree
|
||||
// process each file in pDir/
|
||||
func (p *Patchset) addPatchFiles(repo *gitpb.Repo) error {
|
||||
psetDir := repo.GetGoPath()
|
||||
|
@ -181,10 +151,6 @@ func (p *Patchset) addPatchFiles(repo *gitpb.Repo) error {
|
|||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
// log.Info("IS THIS A FULL PATH ?", path)
|
||||
// log.Info("trim this from path ?", fullDir)
|
||||
// log.Info("trim this from path ?", psetDir)
|
||||
// log.Info("trim this from path ?", tmpDir)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Info("addPatchFile() failed", path)
|
||||
|
@ -194,7 +160,12 @@ func (p *Patchset) addPatchFiles(repo *gitpb.Repo) error {
|
|||
patch := new(Patch)
|
||||
patch.Filename, _ = filepath.Rel(p.TmpDir, path)
|
||||
patch.Data = data
|
||||
patch.parseData()
|
||||
if err := patch.parseData(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := findPatchId(repo, patch); err != nil {
|
||||
return err
|
||||
}
|
||||
patch.StartHash = repo.ActualDevelHash()
|
||||
patch.NewHash = "na"
|
||||
patch.Namespace = repo.GetGoPath()
|
||||
|
@ -212,7 +183,7 @@ func (p *Patchset) addPatchFiles(repo *gitpb.Repo) error {
|
|||
// looks at the git format-patch output
|
||||
// saves the commit Hash
|
||||
// saves the diff lines
|
||||
func (p *Patch) parseData() string {
|
||||
func (p *Patch) parseData() error {
|
||||
lines := strings.Split(string(p.Data), "\n")
|
||||
for _, line := range lines {
|
||||
fields := strings.Fields(line)
|
||||
|
@ -228,7 +199,7 @@ func (p *Patch) parseData() string {
|
|||
p.Files = append(p.Files, line)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
// just an example of how to walk only directories
|
||||
|
@ -253,3 +224,58 @@ func onlyWalkDirs(pDir string) error {
|
|||
})
|
||||
return baderr
|
||||
}
|
||||
|
||||
// func runPipe() error {
|
||||
func findPatchId(repo *gitpb.Repo, p *Patch) error {
|
||||
if p.CommitHash == "" {
|
||||
return log.Errorf("%s commit hash not found", p.Filename)
|
||||
}
|
||||
|
||||
// 1. Create the command to get the diff for the commit.
|
||||
// "git show" is the perfect tool for this.
|
||||
cmdShow := exec.Command("git", "show", p.CommitHash)
|
||||
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[0] != p.CommitHash {
|
||||
return fmt.Errorf("patchid did not match %s != %v", p.CommitHash, fields)
|
||||
}
|
||||
|
||||
p.PatchId = fields[1]
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue