srpmproc/internal/process.go

255 lines
7.0 KiB
Go
Raw Normal View History

2020-12-14 01:29:02 +00:00
package internal
import (
2020-12-14 03:51:03 +00:00
"encoding/hex"
"fmt"
"github.com/cavaliercoder/go-rpm"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/mstg/srpmproc/internal/blob"
"hash"
2020-12-14 03:51:03 +00:00
"io/ioutil"
"log"
"regexp"
2020-12-14 03:51:03 +00:00
"strings"
"time"
2020-12-14 01:29:02 +00:00
)
var tagImportRegex = regexp.MustCompile("refs/tags/(imports/(.*)/(.*))")
2020-12-14 01:29:02 +00:00
type ProcessData struct {
2020-12-17 11:12:10 +00:00
RpmLocation string
UpstreamPrefix string
SshKeyLocation string
SshUser string
Version int
2020-12-17 11:12:10 +00:00
GitCommitterName string
GitCommitterEmail string
Mode int
ModulePrefix string
Authenticator *ssh.PublicKeys
Importer ImportMode
BlobStorage blob.Storage
2020-12-14 01:29:02 +00:00
}
type ignoredSource struct {
name string
hashFunction hash.Hash
}
2020-12-14 01:29:02 +00:00
type modeData struct {
repo *git.Repository
worktree *git.Worktree
rpmFile *rpm.PackageFile
fileWrites map[string][]byte
tagBranch string
pushBranch string
branches []string
sourcesToIgnore []*ignoredSource
2020-12-14 01:29:02 +00:00
}
// ProcessRPM checks the RPM specs and discards any remote files
// This functions also sorts files into directories
// .spec files goes into -> SPECS
// metadata files goes to root
// source files goes into -> SOURCES
// all files that are remote goes into .gitignore
// all ignored files' hash goes into .{name}.metadata
func ProcessRPM(pd *ProcessData) {
md := pd.Importer.RetrieveSource(pd)
2020-12-14 03:51:03 +00:00
sourceRepo := *md.repo
sourceWorktree := *md.worktree
2020-12-14 03:51:03 +00:00
for _, branch := range md.branches {
md.repo = &sourceRepo
md.worktree = &sourceWorktree
md.tagBranch = branch
md.sourcesToIgnore = []*ignoredSource{}
rpmFile := md.rpmFile
// create new repo for final dist
repo, err := git.Init(memory.NewStorage(), memfs.New())
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not create new dist repo: %v", err)
2020-12-14 03:51:03 +00:00
}
w, err := repo.Worktree()
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not get dist worktree: %v", err)
2020-12-14 03:51:03 +00:00
}
if !tagImportRegex.MatchString(md.tagBranch) {
log.Fatal("import tag invalid")
}
2020-12-14 03:51:03 +00:00
match := tagImportRegex.FindStringSubmatch(md.tagBranch)
md.pushBranch = "rocky" + strings.TrimPrefix(match[2], "c")
2020-12-14 03:51:03 +00:00
// create a new remote
remoteUrl := fmt.Sprintf("%s/dist/%s.git", pd.UpstreamPrefix, rpmFile.Name())
log.Printf("using remote: %s", remoteUrl)
refspec := config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/remotes/origin/%s", md.pushBranch, md.pushBranch))
log.Printf("using refspec: %s", refspec)
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{remoteUrl},
Fetch: []config.RefSpec{refspec},
2020-12-14 03:51:03 +00:00
})
if err != nil {
log.Fatalf("could not create remote: %v", err)
2020-12-14 03:51:03 +00:00
}
err = repo.Fetch(&git.FetchOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{refspec},
Auth: pd.Authenticator,
})
2020-12-14 03:51:03 +00:00
refName := plumbing.NewBranchReferenceName(md.pushBranch)
log.Printf("set reference to ref: %s", refName)
2020-12-14 03:51:03 +00:00
if err != nil {
h := plumbing.NewSymbolicReference(plumbing.HEAD, refName)
if err := repo.Storer.CheckAndSetReference(h, nil); err != nil {
log.Fatalf("could not set reference: %v", err)
}
} else {
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewRemoteReferenceName("origin", md.pushBranch),
Force: true,
})
if err != nil {
log.Fatalf("could not checkout: %v", err)
}
2020-12-14 03:51:03 +00:00
}
pd.Importer.WriteSource(md)
2020-12-14 03:51:03 +00:00
copyFromFs(md.worktree.Filesystem, w.Filesystem, ".")
md.repo = repo
md.worktree = w
2020-12-14 03:51:03 +00:00
executePatches(w)
2020-12-14 03:51:03 +00:00
// get ignored files hash and add to .{name}.metadata
metadataFile := fmt.Sprintf(".%s.metadata", rpmFile.Name())
metadata, err := w.Filesystem.Create(metadataFile)
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not create metadata file: %v", err)
2020-12-14 03:51:03 +00:00
}
for _, source := range md.sourcesToIgnore {
sourcePath := "SOURCES/" + source.name
sourceFile, err := w.Filesystem.Open(sourcePath)
if err != nil {
log.Fatalf("could not open ignored source file %s: %v", sourcePath, err)
}
sourceFileBts, err := ioutil.ReadAll(sourceFile)
if err != nil {
log.Fatalf("could not read the whole of ignored source file: %v", err)
}
2020-12-14 03:51:03 +00:00
path := fmt.Sprintf("%s-%s/%s", rpmFile.Name(), md.pushBranch, source.name)
pd.BlobStorage.Write(path, sourceFileBts)
log.Printf("wrote %s to blob storage", path)
2020-12-14 03:51:03 +00:00
source.hashFunction.Reset()
_, err = source.hashFunction.Write(sourceFileBts)
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not write bytes to hash function: %v", err)
2020-12-14 03:51:03 +00:00
}
checksum := source.hashFunction.Sum(nil)
checksumLine := fmt.Sprintf("%s %s\n", hex.EncodeToString(checksum), sourcePath)
_, err = metadata.Write([]byte(checksumLine))
if err != nil {
log.Fatalf("could not write to metadata file: %v", err)
2020-12-14 03:51:03 +00:00
}
}
_, err = w.Add(metadataFile)
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not add metadata file: %v", err)
2020-12-14 03:51:03 +00:00
}
lastFilesToAdd := []string{".gitignore", "SPECS"}
for _, f := range lastFilesToAdd {
_, err := w.Add(f)
if err != nil {
log.Fatalf("could not add metadata file: %v", err)
}
}
pd.Importer.PostProcess(md)
// show status
status, _ := w.Status()
log.Printf("successfully processed:\n%s", status)
var hashes []plumbing.Hash
var pushRefspecs []config.RefSpec
head, err := repo.Head()
if err != nil {
hashes = nil
pushRefspecs = append(pushRefspecs, "*:*")
} else {
log.Printf("tip %s", head.String())
hashes = append(hashes, head.Hash())
refOrigin := "refs/heads/" + md.pushBranch
pushRefspecs = append(pushRefspecs, config.RefSpec(fmt.Sprintf("HEAD:%s", refOrigin)))
}
// we are now finished with the tree and are going to push it to the src repo
// create import commit
commit, err := w.Commit("import "+pd.Importer.ImportName(pd, md), &git.CommitOptions{
Author: &object.Signature{
Name: pd.GitCommitterName,
Email: pd.GitCommitterEmail,
When: time.Now(),
},
Parents: hashes,
})
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not commit object: %v", err)
2020-12-14 03:51:03 +00:00
}
obj, err := repo.CommitObject(commit)
2020-12-14 03:51:03 +00:00
if err != nil {
log.Fatalf("could not get commit object: %v", err)
2020-12-14 03:51:03 +00:00
}
log.Printf("committed:\n%s", obj.String())
2020-12-14 03:51:03 +00:00
newTag := "imports/rocky" + strings.TrimPrefix(match[1], "imports/c")
_, err = repo.CreateTag(newTag, commit, &git.CreateTagOptions{
Tagger: &object.Signature{
Name: pd.GitCommitterName,
Email: pd.GitCommitterEmail,
When: time.Now(),
},
Message: "import " + md.tagBranch + " from " + pd.RpmLocation,
SignKey: nil,
})
if err != nil {
log.Fatalf("could not create tag: %v", err)
}
2020-12-14 03:51:03 +00:00
pushRefspecs = append(pushRefspecs, config.RefSpec(fmt.Sprintf("HEAD:%s", plumbing.NewTagReferenceName(newTag))))
2020-12-14 03:51:03 +00:00
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
Auth: pd.Authenticator,
RefSpecs: pushRefspecs,
Force: true,
})
if err != nil {
log.Fatalf("could not push to remote: %v", err)
}
2020-12-14 03:51:03 +00:00
}
2020-12-14 01:29:02 +00:00
}