2024-08-16 00:41:38 -06:00
|
|
|
package objdiff
|
|
|
|
|
|
|
|
|
|
import (
|
2024-08-29 22:37:30 -06:00
|
|
|
"github.com/encounter/decompal/common"
|
2024-08-16 00:41:38 -06:00
|
|
|
"github.com/encounter/decompal/config"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
"os/exec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func GenerateChanges(
|
|
|
|
|
config *config.AppConfig,
|
|
|
|
|
logger zerolog.Logger,
|
2024-08-29 22:37:30 -06:00
|
|
|
prev *common.ReportFile,
|
|
|
|
|
curr *common.ReportFile,
|
|
|
|
|
) (*common.Changes, error) {
|
2024-08-16 00:41:38 -06:00
|
|
|
if config.ObjdiffPath == "" {
|
|
|
|
|
return nil, errors.New("objdiff_path not set")
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 22:37:30 -06:00
|
|
|
data, err := proto.Marshal(&common.ChangesInput{
|
2024-08-16 00:41:38 -06:00
|
|
|
From: prev.Report,
|
|
|
|
|
To: curr.Report,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to encode changes input")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run objdiff with proto input and output
|
|
|
|
|
// The `--` is to delimit the end of flags and the start of positional arguments
|
|
|
|
|
// Otherwise the argument parser gets confused
|
|
|
|
|
cmd := exec.Command(
|
|
|
|
|
config.ObjdiffPath,
|
|
|
|
|
"report",
|
|
|
|
|
"changes",
|
|
|
|
|
"-f",
|
|
|
|
|
"proto",
|
|
|
|
|
"--",
|
|
|
|
|
"-",
|
|
|
|
|
"-",
|
|
|
|
|
)
|
|
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to open stdin pipe")
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
defer stdin.Close()
|
|
|
|
|
if _, err := stdin.Write(data); err != nil {
|
|
|
|
|
logger.Err(err).Msg("failed to write data to objdiff")
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
// It shouldn't have any stderr output when successful, but we want to capture errors
|
|
|
|
|
// May need to change this if objdiff changes
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrapf(err, "failed to generate changes: %s", string(output))
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 22:37:30 -06:00
|
|
|
changes := &common.Changes{}
|
2024-08-16 00:41:38 -06:00
|
|
|
err = proto.Unmarshal(output, changes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "failed to decode changes file")
|
|
|
|
|
}
|
|
|
|
|
return changes, nil
|
|
|
|
|
}
|