srpmproc/internal/patch.go

261 lines
7.5 KiB
Go
Raw Normal View History

package internal
import (
2020-12-20 10:40:27 +00:00
"fmt"
"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
"git.rockylinux.org/release-engineering/public/srpmproc/internal/directives"
"git.rockylinux.org/release-engineering/public/srpmproc/modulemd"
srpmprocpb "git.rockylinux.org/release-engineering/public/srpmproc/pb"
2020-12-20 10:40:27 +00:00
"github.com/go-git/go-billy/v5/memfs"
"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"
"google.golang.org/protobuf/encoding/prototext"
"io/ioutil"
"log"
"path/filepath"
"strings"
)
func cfgPatches(pd *data.ProcessData, md *data.ModeData, patchTree *git.Worktree, pushTree *git.Worktree) {
2020-12-20 10:40:27 +00:00
// 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, pd, md, patchTree, pushTree)
2020-12-20 10:40:27 +00:00
}
}
}
func applyPatches(pd *data.ProcessData, md *data.ModeData, patchTree *git.Worktree, pushTree *git.Worktree) {
// check if patches exist
2020-12-20 10:40:27 +00:00
_, err := patchTree.Filesystem.Stat("ROCKY")
if err == nil {
cfgPatches(pd, md, patchTree, pushTree)
2020-12-20 10:40:27 +00:00
}
}
func executePatchesRpm(pd *data.ProcessData, md *data.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)
2020-12-20 10:40:27 +00:00
}
w, err := repo.Worktree()
if err != nil {
log.Fatalf("could not get dist Worktree: %v", err)
2020-12-20 10:40:27 +00:00
}
2021-03-17 14:54:16 +00:00
remoteUrl := fmt.Sprintf("%s/patch/%s.git", pd.UpstreamPrefix, gitlabify(md.RpmFile.Name()))
2020-12-20 10:40:27 +00:00
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)
}
2021-03-18 17:44:02 +00:00
fetchOptions := &git.FetchOptions{
2020-12-20 10:40:27 +00:00
RemoteName: "origin",
RefSpecs: []config.RefSpec{refspec},
2021-03-18 17:44:02 +00:00
}
if !strings.HasPrefix(pd.UpstreamPrefix, "http") {
fetchOptions.Auth = pd.Authenticator
}
err = repo.Fetch(fetchOptions)
2020-12-20 10:40:27 +00:00
refName := plumbing.NewBranchReferenceName(md.PushBranch)
2020-12-20 10:40:27 +00:00
log.Printf("set reference to ref: %s", refName)
if err != nil {
// no patches active
2021-03-18 17:44:02 +00:00
log.Println("info: patch repo not found")
2020-12-20 10:40:27 +00:00
return
} else {
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewRemoteReferenceName("origin", "main"),
2020-12-20 10:40:27 +00:00
Force: true,
})
// common patches found, apply them
if err == nil {
applyPatches(pd, md, w, md.Worktree)
2020-12-20 10:40:27 +00:00
} else {
log.Println("info: no common patches found")
}
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewRemoteReferenceName("origin", md.PushBranch),
2020-12-20 10:40:27 +00:00
Force: true,
})
// branch specific patches found, apply them
if err == nil {
applyPatches(pd, md, w, md.Worktree)
2020-12-20 10:40:27 +00:00
} else {
log.Println("info: no branch specific patches found")
}
}
}
2020-12-22 16:28:04 +00:00
2021-03-17 14:54:16 +00:00
func getTipStream(pd *data.ProcessData, module string, pushBranch string, origPushBranch string) string {
2020-12-22 16:28:04 +00:00
repo, err := git.Init(memory.NewStorage(), memfs.New())
if err != nil {
log.Fatalf("could not init git Repo: %v", err)
2020-12-22 16:28:04 +00:00
}
2021-03-17 14:54:16 +00:00
remoteUrl := fmt.Sprintf("%s/rpms/%s.git", pd.UpstreamPrefix, gitlabify(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)
2021-03-17 14:54:16 +00:00
branchVersion := strings.Split(pushBranch, "-")
if len(branchVersion) == 3 {
version := branchVersion[2]
// incompatible pattern: v1,v2 etc.
// example can be found in refs/tags/imports/c8-stream-1.1/subversion-1.10-8030020200519083055.9ce6d490
if strings.HasPrefix(version, "v") {
prefix = strings.Replace(prefix, version, strings.TrimPrefix(version, "v"), 1)
}
}
log.Println(prefix, ref.Name().String())
if strings.HasPrefix(ref.Name().String(), prefix) {
tipHash = ref.Hash().String()
}
}
for _, ref := range list {
prefix := fmt.Sprintf("refs/heads/%s", origPushBranch)
2020-12-22 16:28:04 +00:00
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 *data.ProcessData, md *data.ModeData) {
2020-12-22 16:28:04 +00:00
// special case for platform.yaml
_, err := md.Worktree.Filesystem.Open("platform.yaml")
2020-12-22 16:28:04 +00:00
if err == nil {
return
}
mdTxtPath := "SOURCES/modulemd.src.txt"
f, err := md.Worktree.Filesystem.Open(mdTxtPath)
2020-12-22 16:28:04 +00:00
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
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-") {
repString := fmt.Sprintf("%s%ss-", pd.BranchPrefix, string(split[4][0]))
newString := fmt.Sprintf("%s%s-", pd.BranchPrefix, string(split[4][0]))
pushBranch = strings.Replace(md.PushBranch, repString, newString, 1)
} else if strings.HasPrefix(rpm.Ref, "stream-") && len(split) == 2 {
pushBranch = md.PushBranch
} 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])
} else if strings.HasPrefix(rpm.Ref, "stream-") {
pushBranch = fmt.Sprintf("%s%s-stream-%s", pd.BranchPrefix, string(split[3][0]), split[1])
} else if strings.HasPrefix(rpm.Ref, "rhel-") {
pushBranch = md.PushBranch
2021-01-01 15:20:39 +00:00
} else {
log.Fatal("could not recognize modulemd ref")
2021-01-01 15:20:39 +00:00
}
2020-12-22 16:28:04 +00:00
rpm.Ref = pushBranch
2021-03-17 14:54:16 +00:00
tipHash = getTipStream(pd, name, pushBranch, md.PushBranch)
err = module.Marshal(md.Worktree.Filesystem, mdTxtPath)
2021-01-01 15:20:39 +00:00
if err != nil {
log.Fatalf("could not marshal modulemd: %v", err)
}
rpm.Ref = tipHash
2020-12-22 16:28:04 +00:00
}
rootModule := fmt.Sprintf("%s.yaml", md.RpmFile.Name())
err = module.Marshal(md.Worktree.Filesystem, rootModule)
2020-12-22 16:28:04 +00:00
if err != nil {
log.Fatalf("could not marshal root modulemd: %v", err)
}
_, err = md.Worktree.Add(rootModule)
2020-12-22 16:28:04 +00:00
if err != nil {
log.Fatalf("could not add root modulemd: %v", err)
}
}