2020-12-20 09:36:13 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-12-20 10:40:27 +00:00
|
|
|
"fmt"
|
2020-12-20 09:36:13 +00:00
|
|
|
"github.com/bluekeyes/go-gitdiff/gitdiff"
|
2020-12-20 10:40:27 +00:00
|
|
|
"github.com/go-git/go-billy/v5/memfs"
|
2020-12-20 09:36:13 +00:00
|
|
|
"github.com/go-git/go-git/v5"
|
2020-12-20 10:40:27 +00:00
|
|
|
"github.com/go-git/go-git/v5/config"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/go-git/go-git/v5/storage/memory"
|
|
|
|
"github.com/mstg/srpmproc/internal/directives"
|
2020-12-22 16:28:04 +00:00
|
|
|
"github.com/mstg/srpmproc/modulemd"
|
2020-12-20 10:40:27 +00:00
|
|
|
srpmprocpb "github.com/mstg/srpmproc/pb"
|
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
|
|
|
"io/ioutil"
|
2020-12-20 09:36:13 +00:00
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-20 10:40:27 +00:00
|
|
|
func srpmPatches(patchTree *git.Worktree, pushTree *git.Worktree) {
|
2020-12-20 09:36:13 +00:00
|
|
|
// check SRPM patches
|
2020-12-20 10:40:27 +00:00
|
|
|
_, err := patchTree.Filesystem.Stat("ROCKY/SRPM")
|
2020-12-20 09:36:13 +00:00
|
|
|
if err == nil {
|
|
|
|
// iterate through patches
|
2020-12-20 10:40:27 +00:00
|
|
|
infos, err := patchTree.Filesystem.ReadDir("ROCKY/SRPM")
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not walk patches: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
// can only process .patch files
|
|
|
|
if !strings.HasSuffix(info.Name(), ".patch") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("applying %s", info.Name())
|
|
|
|
filePath := filepath.Join("ROCKY/SRPM", info.Name())
|
|
|
|
|
2020-12-20 10:40:27 +00:00
|
|
|
patch, err := patchTree.Filesystem.Open(filePath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not open patch file %s: %v", info.Name(), err)
|
|
|
|
}
|
|
|
|
files, _, err := gitdiff.Parse(patch)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not parse patch file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, patchedFile := range files {
|
|
|
|
srcPath := patchedFile.NewName
|
|
|
|
if !strings.HasPrefix(srcPath, "SPECS") {
|
|
|
|
srcPath = filepath.Join("SOURCES", patchedFile.NewName)
|
|
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
|
|
if !patchedFile.IsDelete && !patchedFile.IsNew {
|
2020-12-20 10:40:27 +00:00
|
|
|
patchSubjectFile, err := pushTree.Filesystem.Open(srcPath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not open patch subject: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gitdiff.NewApplier(patchSubjectFile).ApplyFile(&output, patchedFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not apply patch: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
oldName := filepath.Join("SOURCES", patchedFile.OldName)
|
2020-12-20 10:40:27 +00:00
|
|
|
_ = pushTree.Filesystem.Remove(oldName)
|
|
|
|
_ = pushTree.Filesystem.Remove(srcPath)
|
2020-12-20 09:36:13 +00:00
|
|
|
|
|
|
|
if patchedFile.IsNew {
|
2020-12-20 10:40:27 +00:00
|
|
|
newFile, err := pushTree.Filesystem.Create(srcPath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create new file: %v", err)
|
|
|
|
}
|
|
|
|
err = gitdiff.NewApplier(newFile).ApplyFile(&output, patchedFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not apply patch: %v", err)
|
|
|
|
}
|
|
|
|
_, err = newFile.Write(output.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not write post-patch file: %v", err)
|
|
|
|
}
|
2020-12-20 10:40:27 +00:00
|
|
|
_, err = pushTree.Add(srcPath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not add file %s to git: %v", srcPath, err)
|
|
|
|
}
|
|
|
|
log.Printf("git add %s", srcPath)
|
|
|
|
} else if !patchedFile.IsDelete {
|
2020-12-20 10:40:27 +00:00
|
|
|
newFile, err := pushTree.Filesystem.Create(srcPath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create post-patch file: %v", err)
|
|
|
|
}
|
|
|
|
_, err = newFile.Write(output.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not write post-patch file: %v", err)
|
|
|
|
}
|
2020-12-20 10:40:27 +00:00
|
|
|
_, err = pushTree.Add(srcPath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not add file %s to git: %v", srcPath, err)
|
|
|
|
}
|
|
|
|
log.Printf("git add %s", srcPath)
|
|
|
|
} else {
|
2020-12-20 10:40:27 +00:00
|
|
|
_, err = pushTree.Remove(oldName)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not remove file %s to git: %v", oldName, err)
|
|
|
|
}
|
|
|
|
log.Printf("git rm %s", oldName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 10:40:27 +00:00
|
|
|
_, err = pushTree.Add(filePath)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not add file %s to git: %v", filePath, err)
|
|
|
|
}
|
|
|
|
log.Printf("git add %s", filePath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 10:40:27 +00:00
|
|
|
func cfgPatches(patchTree *git.Worktree, pushTree *git.Worktree) {
|
|
|
|
// check CFG patches
|
|
|
|
_, err := patchTree.Filesystem.Stat("ROCKY/CFG")
|
|
|
|
if err == nil {
|
|
|
|
// iterate through patches
|
|
|
|
infos, err := patchTree.Filesystem.ReadDir("ROCKY/CFG")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not walk patches: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, info := range infos {
|
|
|
|
// can only process .cfg files
|
|
|
|
if !strings.HasSuffix(info.Name(), ".cfg") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("applying directive %s", info.Name())
|
|
|
|
filePath := filepath.Join("ROCKY/CFG", info.Name())
|
|
|
|
directive, err := patchTree.Filesystem.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not open directive file %s: %v", info.Name(), err)
|
|
|
|
}
|
|
|
|
directiveBytes, err := ioutil.ReadAll(directive)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not read directive file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfg srpmprocpb.Cfg
|
|
|
|
err = prototext.Unmarshal(directiveBytes, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not unmarshal cfg file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
directives.Apply(&cfg, patchTree, pushTree)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyPatches(patchTree *git.Worktree, pushTree *git.Worktree) {
|
2020-12-20 09:36:13 +00:00
|
|
|
// check if patches exist
|
2020-12-20 10:40:27 +00:00
|
|
|
_, err := patchTree.Filesystem.Stat("ROCKY")
|
2020-12-20 09:36:13 +00:00
|
|
|
if err == nil {
|
2020-12-20 10:40:27 +00:00
|
|
|
srpmPatches(patchTree, pushTree)
|
|
|
|
cfgPatches(patchTree, pushTree)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 16:28:04 +00:00
|
|
|
func executePatchesRpm(pd *ProcessData, md *modeData) {
|
2020-12-20 10:40:27 +00:00
|
|
|
// fetch patch repository
|
|
|
|
repo, err := git.Init(memory.NewStorage(), memfs.New())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create new dist repo: %v", err)
|
|
|
|
}
|
|
|
|
w, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get dist worktree: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteUrl := fmt.Sprintf("%s/patch/%s.git", pd.UpstreamPrefix, md.rpmFile.Name())
|
|
|
|
refspec := config.RefSpec(fmt.Sprintf("+refs/heads/*:refs/remotes/origin/*"))
|
|
|
|
|
|
|
|
_, err = repo.CreateRemote(&config.RemoteConfig{
|
|
|
|
Name: "origin",
|
|
|
|
URLs: []string{remoteUrl},
|
|
|
|
Fetch: []config.RefSpec{refspec},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create remote: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.Fetch(&git.FetchOptions{
|
|
|
|
RemoteName: "origin",
|
|
|
|
RefSpecs: []config.RefSpec{refspec},
|
|
|
|
Auth: pd.Authenticator,
|
|
|
|
})
|
|
|
|
|
|
|
|
refName := plumbing.NewBranchReferenceName(md.pushBranch)
|
|
|
|
log.Printf("set reference to ref: %s", refName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// no patches active
|
|
|
|
log.Println("info: patch repo not found")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
2021-02-20 04:12:16 +00:00
|
|
|
Branch: plumbing.NewRemoteReferenceName("origin", "main"),
|
2020-12-20 10:40:27 +00:00
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
// common patches found, apply them
|
|
|
|
if err == nil {
|
|
|
|
applyPatches(w, md.worktree)
|
|
|
|
} else {
|
|
|
|
log.Println("info: no common patches found")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
|
|
|
Branch: plumbing.NewRemoteReferenceName("origin", md.pushBranch),
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
// branch specific patches found, apply them
|
|
|
|
if err == nil {
|
|
|
|
applyPatches(w, md.worktree)
|
|
|
|
} else {
|
|
|
|
log.Println("info: no branch specific patches found")
|
|
|
|
}
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-22 16:28:04 +00:00
|
|
|
|
|
|
|
func getTipStream(pd *ProcessData, module string, pushBranch string) string {
|
|
|
|
repo, err := git.Init(memory.NewStorage(), memfs.New())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not init git repo: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:44:44 +00:00
|
|
|
remoteUrl := fmt.Sprintf("%s/rpms/%s.git", pd.UpstreamPrefix, module)
|
2020-12-22 16:28:04 +00:00
|
|
|
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
|
|
|
|
remote, err := repo.CreateRemote(&config.RemoteConfig{
|
|
|
|
Name: "origin",
|
|
|
|
URLs: []string{remoteUrl},
|
|
|
|
Fetch: []config.RefSpec{refspec},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create remote: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
list, err := remote.List(&git.ListOptions{
|
|
|
|
Auth: pd.Authenticator,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get rpm refs. import the rpm before the module: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tipHash string
|
|
|
|
|
|
|
|
for _, ref := range list {
|
|
|
|
prefix := fmt.Sprintf("refs/heads/%s", pushBranch)
|
|
|
|
if strings.HasPrefix(ref.Name().String(), prefix) {
|
|
|
|
tipHash = ref.Hash().String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tipHash == "" {
|
|
|
|
log.Fatal("could not find tip hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tipHash
|
|
|
|
}
|
|
|
|
|
|
|
|
func patchModuleYaml(pd *ProcessData, md *modeData) {
|
|
|
|
// special case for platform.yaml
|
|
|
|
_, err := md.worktree.Filesystem.Open("platform.yaml")
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mdTxtPath := "SOURCES/modulemd.src.txt"
|
|
|
|
f, err := md.worktree.Filesystem.Open(mdTxtPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not open modulemd file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not read modulemd file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
module, err := modulemd.Parse(content)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not parse modulemd file: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-01 15:20:39 +00:00
|
|
|
for name, rpm := range module.Data.Components.Rpms {
|
|
|
|
var tipHash string
|
2021-01-01 16:29:32 +00:00
|
|
|
var pushBranch string
|
|
|
|
split := strings.Split(rpm.Ref, "-")
|
|
|
|
// TODO: maybe point to correct release tag? but refer to latest for now,
|
|
|
|
// we're bootstrapping a new distro for latest RHEL8 anyways. So earlier
|
|
|
|
// versions are not that important
|
|
|
|
if strings.HasPrefix(rpm.Ref, "stream-rhel-") {
|
2021-02-14 17:25:00 +00:00
|
|
|
repString := fmt.Sprintf("%s%ss-", pd.BranchPrefix, string(split[4][0]))
|
|
|
|
newString := fmt.Sprintf("%s%s-", pd.BranchPrefix, string(split[4][0]))
|
2021-01-01 16:29:32 +00:00
|
|
|
pushBranch = strings.Replace(md.pushBranch, repString, newString, 1)
|
|
|
|
} else if strings.HasPrefix(rpm.Ref, "stream-") && len(split) == 2 {
|
|
|
|
pushBranch = md.pushBranch
|
2021-02-20 04:12:16 +00:00
|
|
|
} else if strings.HasPrefix(rpm.Ref, "stream-") && len(split) == 3 {
|
|
|
|
// example: ant
|
|
|
|
pushBranch = fmt.Sprintf("%s%d-stream-%s", pd.BranchPrefix, pd.Version, split[2])
|
2021-01-01 16:29:32 +00:00
|
|
|
} else if strings.HasPrefix(rpm.Ref, "stream-") {
|
2021-02-14 17:25:00 +00:00
|
|
|
pushBranch = fmt.Sprintf("%s%s-stream-%s", pd.BranchPrefix, string(split[3][0]), split[1])
|
2021-01-01 16:29:32 +00:00
|
|
|
} else if strings.HasPrefix(rpm.Ref, "rhel-") {
|
|
|
|
pushBranch = md.pushBranch
|
2021-01-01 15:20:39 +00:00
|
|
|
} else {
|
2021-01-01 16:29:32 +00:00
|
|
|
log.Fatal("could not recognize modulemd ref")
|
2021-01-01 15:20:39 +00:00
|
|
|
}
|
2020-12-22 16:28:04 +00:00
|
|
|
|
2021-01-01 16:29:32 +00:00
|
|
|
rpm.Ref = pushBranch
|
|
|
|
tipHash = getTipStream(pd, name, pushBranch)
|
|
|
|
|
2021-01-01 15:20:39 +00:00
|
|
|
err = module.Marshal(md.worktree.Filesystem, mdTxtPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not marshal modulemd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rpm.Ref = tipHash
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 15:20:39 +00:00
|
|
|
rootModule := fmt.Sprintf("%s.yaml", md.rpmFile.Name())
|
2020-12-22 16:28:04 +00:00
|
|
|
err = module.Marshal(md.worktree.Filesystem, rootModule)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not marshal root modulemd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = md.worktree.Add(rootModule)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not add root modulemd: %v", err)
|
|
|
|
}
|
|
|
|
}
|