2021-04-06 19:39:02 +00:00
|
|
|
// Copyright (c) 2021 The Srpmproc Authors
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
|
|
|
|
2021-08-19 10:09:53 +00:00
|
|
|
package srpmproc
|
2020-12-14 01:29:02 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-14 03:51:03 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2021-09-03 21:07:02 +00:00
|
|
|
"github.com/go-git/go-billy/v5"
|
2021-09-10 18:14:33 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/http"
|
2021-09-03 21:07:02 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
2021-09-08 15:00:56 +00:00
|
|
|
srpmprocpb "github.com/rocky-linux/srpmproc/pb"
|
2021-09-03 21:07:02 +00:00
|
|
|
"github.com/rocky-linux/srpmproc/pkg/blob"
|
|
|
|
"github.com/rocky-linux/srpmproc/pkg/blob/file"
|
|
|
|
"github.com/rocky-linux/srpmproc/pkg/blob/gcs"
|
|
|
|
"github.com/rocky-linux/srpmproc/pkg/blob/s3"
|
2021-09-08 15:00:56 +00:00
|
|
|
"github.com/rocky-linux/srpmproc/pkg/misc"
|
2021-09-03 21:07:02 +00:00
|
|
|
"github.com/rocky-linux/srpmproc/pkg/modes"
|
2021-09-08 15:00:56 +00:00
|
|
|
"github.com/rocky-linux/srpmproc/pkg/rpmutils"
|
2020-12-14 03:51:03 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2021-09-03 21:07:02 +00:00
|
|
|
"os/user"
|
2020-12-22 16:28:04 +00:00
|
|
|
"path/filepath"
|
2020-12-14 03:51:03 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2021-04-15 04:41:12 +00:00
|
|
|
|
|
|
|
"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/storage/memory"
|
2021-08-19 10:09:53 +00:00
|
|
|
"github.com/rocky-linux/srpmproc/pkg/data"
|
2020-12-14 01:29:02 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 21:07:02 +00:00
|
|
|
const (
|
|
|
|
RpmPrefixCentOS = "https://git.centos.org/rpms"
|
|
|
|
ModulePrefixCentOS = "https://git.centos.org/modules"
|
|
|
|
RpmPrefixRocky = "https://git.rockylinux.org/staging/rpms"
|
|
|
|
ModulePrefixRocky = "https://git.rockylinux.org/staging/modules"
|
|
|
|
UpstreamPrefixRocky = "https://git.rockylinux.org/staging"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProcessDataRequest struct {
|
|
|
|
// Required
|
|
|
|
Version int
|
|
|
|
StorageAddr string
|
|
|
|
Package string
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
ModuleMode bool
|
|
|
|
TmpFsMode string
|
|
|
|
ModulePrefix string
|
|
|
|
RpmPrefix string
|
|
|
|
SshKeyLocation string
|
|
|
|
SshUser string
|
2021-09-10 18:14:33 +00:00
|
|
|
HttpUsername string
|
|
|
|
HttpPassword string
|
2021-09-03 21:07:02 +00:00
|
|
|
ManualCommits string
|
|
|
|
UpstreamPrefix string
|
|
|
|
GitCommitterName string
|
|
|
|
GitCommitterEmail string
|
|
|
|
ImportBranchPrefix string
|
|
|
|
BranchPrefix string
|
|
|
|
FsCreator data.FsCreatorFunc
|
|
|
|
NoDupMode bool
|
|
|
|
AllowStreamBranches bool
|
|
|
|
ModuleFallbackStream string
|
|
|
|
NoStorageUpload bool
|
|
|
|
NoStorageDownload bool
|
|
|
|
SingleTag string
|
2021-09-05 00:12:43 +00:00
|
|
|
CdnUrl string
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 14:54:16 +00:00
|
|
|
func gitlabify(str string) string {
|
2021-04-11 22:08:42 +00:00
|
|
|
if str == "tree" {
|
|
|
|
return "treepkg"
|
|
|
|
}
|
|
|
|
|
2021-03-17 14:54:16 +00:00
|
|
|
return strings.Replace(str, "+", "plus", -1)
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:07:02 +00:00
|
|
|
func NewProcessData(req *ProcessDataRequest) (*data.ProcessData, error) {
|
|
|
|
switch req.Version {
|
|
|
|
case 8:
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported upstream version %d", req.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set defaults
|
|
|
|
if req.ModulePrefix == "" {
|
|
|
|
req.ModulePrefix = ModulePrefixCentOS
|
|
|
|
}
|
|
|
|
if req.RpmPrefix == "" {
|
|
|
|
req.RpmPrefix = RpmPrefixCentOS
|
|
|
|
}
|
|
|
|
if req.SshUser == "" {
|
|
|
|
req.SshUser = "git"
|
|
|
|
}
|
|
|
|
if req.UpstreamPrefix == "" {
|
|
|
|
req.UpstreamPrefix = UpstreamPrefixRocky
|
|
|
|
}
|
|
|
|
if req.GitCommitterName == "" {
|
|
|
|
req.GitCommitterName = "rockyautomation"
|
|
|
|
}
|
|
|
|
if req.GitCommitterEmail == "" {
|
|
|
|
req.GitCommitterEmail = "rockyautomation@rockylinux.org"
|
|
|
|
}
|
|
|
|
if req.ImportBranchPrefix == "" {
|
|
|
|
req.ImportBranchPrefix = "c"
|
|
|
|
}
|
|
|
|
if req.BranchPrefix == "" {
|
|
|
|
req.BranchPrefix = "r"
|
|
|
|
}
|
2021-09-05 00:12:43 +00:00
|
|
|
if req.CdnUrl == "" {
|
|
|
|
req.CdnUrl = "https://git.centos.org/sources"
|
|
|
|
}
|
2021-09-03 21:07:02 +00:00
|
|
|
|
|
|
|
// Validate required
|
|
|
|
if req.Package == "" {
|
|
|
|
return nil, fmt.Errorf("package cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
var importer data.ImportMode
|
|
|
|
var blobStorage blob.Storage
|
|
|
|
|
|
|
|
if strings.HasPrefix(req.StorageAddr, "gs://") {
|
|
|
|
blobStorage = gcs.New(strings.Replace(req.StorageAddr, "gs://", "", 1))
|
|
|
|
} else if strings.HasPrefix(req.StorageAddr, "s3://") {
|
|
|
|
blobStorage = s3.New(strings.Replace(req.StorageAddr, "s3://", "", 1))
|
|
|
|
} else if strings.HasPrefix(req.StorageAddr, "file://") {
|
|
|
|
blobStorage = file.New(strings.Replace(req.StorageAddr, "file://", "", 1))
|
|
|
|
} else {
|
|
|
|
log.Fatalf("invalid blob storage")
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceRpmLocation := ""
|
|
|
|
if req.ModuleMode {
|
|
|
|
sourceRpmLocation = fmt.Sprintf("%s/%s", req.ModulePrefix, req.Package)
|
|
|
|
} else {
|
|
|
|
sourceRpmLocation = fmt.Sprintf("%s/%s", req.RpmPrefix, req.Package)
|
|
|
|
}
|
|
|
|
importer = &modes.GitMode{}
|
|
|
|
|
|
|
|
lastKeyLocation := req.SshKeyLocation
|
|
|
|
if lastKeyLocation == "" {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get user: %v", err)
|
|
|
|
}
|
|
|
|
lastKeyLocation = filepath.Join(usr.HomeDir, ".ssh/id_rsa")
|
|
|
|
}
|
|
|
|
|
2021-09-10 18:14:33 +00:00
|
|
|
var authenticator transport.AuthMethod
|
2021-09-03 21:07:02 +00:00
|
|
|
|
|
|
|
var err error
|
2021-09-10 18:14:33 +00:00
|
|
|
if req.HttpUsername != "" {
|
|
|
|
authenticator = &http.BasicAuth{
|
|
|
|
Username: req.HttpUsername,
|
|
|
|
Password: req.HttpPassword,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// create ssh key authenticator
|
|
|
|
authenticator, err = ssh.NewPublicKeysFromFile(req.SshUser, lastKeyLocation, "")
|
|
|
|
}
|
2021-09-03 21:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get git authenticator: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fsCreator := func(branch string) (billy.Filesystem, error) {
|
|
|
|
return memfs.New(), nil
|
|
|
|
}
|
2021-09-08 15:00:56 +00:00
|
|
|
reqFsCreator := fsCreator
|
2021-09-03 21:07:02 +00:00
|
|
|
if req.FsCreator != nil {
|
2021-09-08 15:00:56 +00:00
|
|
|
reqFsCreator = req.FsCreator
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if req.TmpFsMode != "" {
|
|
|
|
log.Printf("using tmpfs dir: %s", req.TmpFsMode)
|
|
|
|
fsCreator = func(branch string) (billy.Filesystem, error) {
|
2021-09-08 15:00:56 +00:00
|
|
|
fs, err := reqFsCreator(branch)
|
2021-09-03 21:07:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tmpDir := filepath.Join(req.TmpFsMode, branch)
|
|
|
|
err = fs.MkdirAll(tmpDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create tmpfs dir: %v", err)
|
|
|
|
}
|
|
|
|
nFs, err := fs.Chroot(tmpDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nFs, nil
|
|
|
|
}
|
2021-09-08 15:00:56 +00:00
|
|
|
} else {
|
|
|
|
fsCreator = reqFsCreator
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var manualCs []string
|
|
|
|
if strings.TrimSpace(req.ManualCommits) != "" {
|
|
|
|
manualCs = strings.Split(req.ManualCommits, ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &data.ProcessData{
|
|
|
|
Importer: importer,
|
|
|
|
RpmLocation: sourceRpmLocation,
|
|
|
|
UpstreamPrefix: req.UpstreamPrefix,
|
|
|
|
SshKeyLocation: lastKeyLocation,
|
|
|
|
SshUser: req.SshUser,
|
|
|
|
Version: req.Version,
|
|
|
|
BlobStorage: blobStorage,
|
|
|
|
GitCommitterName: req.GitCommitterName,
|
|
|
|
GitCommitterEmail: req.GitCommitterEmail,
|
|
|
|
ModulePrefix: req.ModulePrefix,
|
|
|
|
ImportBranchPrefix: req.ImportBranchPrefix,
|
|
|
|
BranchPrefix: req.BranchPrefix,
|
|
|
|
SingleTag: req.SingleTag,
|
|
|
|
Authenticator: authenticator,
|
|
|
|
NoDupMode: req.NoDupMode,
|
|
|
|
ModuleMode: req.ModuleMode,
|
|
|
|
TmpFsMode: req.TmpFsMode,
|
|
|
|
NoStorageDownload: req.NoStorageDownload,
|
|
|
|
NoStorageUpload: req.NoStorageUpload,
|
|
|
|
ManualCommits: manualCs,
|
|
|
|
ModuleFallbackStream: req.ModuleFallbackStream,
|
|
|
|
AllowStreamBranches: req.AllowStreamBranches,
|
|
|
|
FsCreator: fsCreator,
|
2021-09-08 15:00:56 +00:00
|
|
|
CdnUrl: req.CdnUrl,
|
2021-09-03 21:07:02 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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
|
2021-02-24 07:27:51 +00:00
|
|
|
// all ignored files' hash goes into .{Name}.metadata
|
2021-09-08 15:00:56 +00:00
|
|
|
func ProcessRPM(pd *data.ProcessData) (*srpmprocpb.ProcessResponse, error) {
|
2021-09-03 21:07:02 +00:00
|
|
|
md, err := pd.Importer.RetrieveSource(pd)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, err
|
2021-04-27 17:14:01 +00:00
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
md.BlobCache = map[string][]byte{}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-02-14 20:44:44 +00:00
|
|
|
remotePrefix := "rpms"
|
2020-12-22 16:28:04 +00:00
|
|
|
if pd.ModuleMode {
|
|
|
|
remotePrefix = "modules"
|
|
|
|
}
|
|
|
|
|
2021-02-20 04:12:16 +00:00
|
|
|
latestHashForBranch := map[string]string{}
|
2021-09-08 15:00:56 +00:00
|
|
|
versionForBranch := map[string]*srpmprocpb.VersionRelease{}
|
2021-02-19 15:22:36 +00:00
|
|
|
|
|
|
|
// already uploaded blobs are skipped
|
|
|
|
var alreadyUploadedBlobs []string
|
|
|
|
|
2020-12-22 05:13:27 +00:00
|
|
|
// if no-dup-mode is enabled then skip already imported versions
|
|
|
|
var tagIgnoreList []string
|
|
|
|
if pd.NoDupMode {
|
|
|
|
repo, err := git.Init(memory.NewStorage(), memfs.New())
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not init git repo: %v", err)
|
2020-12-22 05:13:27 +00:00
|
|
|
}
|
2021-09-03 21:07:02 +00:00
|
|
|
remoteUrl := fmt.Sprintf("%s/%s/%s.git", pd.UpstreamPrefix, remotePrefix, gitlabify(md.Name))
|
2020-12-22 05:13:27 +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 {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not create remote: %v", err)
|
2020-12-22 05:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list, err := remote.List(&git.ListOptions{
|
|
|
|
Auth: pd.Authenticator,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-12-22 16:28:04 +00:00
|
|
|
log.Println("ignoring no-dup-mode")
|
2020-12-22 05:19:53 +00:00
|
|
|
} else {
|
|
|
|
for _, ref := range list {
|
|
|
|
if !strings.HasPrefix(string(ref.Name()), "refs/tags/imports") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tagIgnoreList = append(tagIgnoreList, string(ref.Name()))
|
2020-12-22 05:13:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
sourceRepo := *md.Repo
|
|
|
|
sourceWorktree := *md.Worktree
|
|
|
|
|
2021-04-06 19:35:46 +00:00
|
|
|
commitPin := map[string]string{}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
if pd.SingleTag != "" {
|
|
|
|
md.Branches = []string{fmt.Sprintf("refs/tags/%s", pd.SingleTag)}
|
2021-04-06 19:35:46 +00:00
|
|
|
} else if len(pd.ManualCommits) > 0 {
|
|
|
|
md.Branches = []string{}
|
|
|
|
for _, commit := range pd.ManualCommits {
|
|
|
|
branchCommit := strings.Split(commit, ":")
|
|
|
|
if len(branchCommit) != 2 {
|
|
|
|
log.Fatalln("invalid manual commit list")
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:07:02 +00:00
|
|
|
head := fmt.Sprintf("refs/tags/imports/%s/%s-%s", branchCommit[0], md.Name, branchCommit[1])
|
2021-04-06 19:35:46 +00:00
|
|
|
md.Branches = append(md.Branches, head)
|
|
|
|
commitPin[head] = branchCommit[1]
|
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
for _, branch := range md.Branches {
|
|
|
|
md.Repo = &sourceRepo
|
|
|
|
md.Worktree = &sourceWorktree
|
|
|
|
md.TagBranch = branch
|
|
|
|
for _, source := range md.SourcesToIgnore {
|
|
|
|
source.Expired = true
|
2021-01-01 15:55:21 +00:00
|
|
|
}
|
2020-12-20 09:36:13 +00:00
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
if strings.Contains(md.TagBranch, "-beta") {
|
2021-02-20 04:12:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-22 16:28:04 +00:00
|
|
|
var matchString string
|
2021-09-08 15:00:56 +00:00
|
|
|
if !misc.GetTagImportRegex(pd.ImportBranchPrefix, pd.AllowStreamBranches).MatchString(md.TagBranch) {
|
2020-12-22 16:28:04 +00:00
|
|
|
if pd.ModuleMode {
|
2021-02-15 04:20:42 +00:00
|
|
|
prefix := fmt.Sprintf("refs/heads/%s%d", pd.ImportBranchPrefix, pd.Version)
|
2021-02-24 07:27:51 +00:00
|
|
|
if strings.HasPrefix(md.TagBranch, prefix) {
|
|
|
|
replace := strings.Replace(md.TagBranch, "refs/heads/", "", 1)
|
2020-12-22 16:28:04 +00:00
|
|
|
matchString = fmt.Sprintf("refs/tags/imports/%s/%s", replace, filepath.Base(pd.RpmLocation))
|
|
|
|
log.Printf("using match string: %s", matchString)
|
|
|
|
}
|
|
|
|
}
|
2021-09-08 15:00:56 +00:00
|
|
|
if !misc.GetTagImportRegex(pd.ImportBranchPrefix, pd.AllowStreamBranches).MatchString(matchString) {
|
2021-02-15 04:20:42 +00:00
|
|
|
continue
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-24 07:27:51 +00:00
|
|
|
matchString = md.TagBranch
|
2020-12-16 10:46:33 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-09-08 15:00:56 +00:00
|
|
|
match := misc.GetTagImportRegex(pd.ImportBranchPrefix, pd.AllowStreamBranches).FindStringSubmatch(matchString)
|
2021-02-24 07:27:51 +00:00
|
|
|
md.PushBranch = pd.BranchPrefix + strings.TrimPrefix(match[2], pd.ImportBranchPrefix)
|
2021-02-21 13:51:30 +00:00
|
|
|
newTag := "imports/" + pd.BranchPrefix + strings.TrimPrefix(match[1], "imports/"+pd.ImportBranchPrefix)
|
2021-04-26 02:00:00 +00:00
|
|
|
newTag = strings.Replace(newTag, "%", "_", -1)
|
2020-12-22 05:13:27 +00:00
|
|
|
|
2021-09-03 21:07:02 +00:00
|
|
|
createdFs, err := pd.FsCreator(md.PushBranch)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, err
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 17:44:02 +00:00
|
|
|
// create new Repo for final dist
|
2021-09-03 21:07:02 +00:00
|
|
|
repo, err := git.Init(memory.NewStorage(), createdFs)
|
2021-03-18 17:44:02 +00:00
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not create new dist Repo: %v", err)
|
2021-03-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
w, err := repo.Worktree()
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not get dist Worktree: %v", err)
|
2021-03-18 17:44:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 05:13:27 +00:00
|
|
|
shouldContinue := true
|
|
|
|
for _, ignoredTag := range tagIgnoreList {
|
|
|
|
if ignoredTag == "refs/tags/"+newTag {
|
|
|
|
log.Printf("skipping %s", ignoredTag)
|
|
|
|
shouldContinue = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !shouldContinue {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
// create a new remote
|
2021-09-03 21:07:02 +00:00
|
|
|
remoteUrl := fmt.Sprintf("%s/%s/%s.git", pd.UpstreamPrefix, remotePrefix, gitlabify(md.Name))
|
2020-12-20 09:36:13 +00:00
|
|
|
log.Printf("using remote: %s", remoteUrl)
|
2021-02-24 07:27:51 +00:00
|
|
|
refspec := config.RefSpec(fmt.Sprintf("+refs/heads/%s:refs/remotes/origin/%s", md.PushBranch, md.PushBranch))
|
2020-12-20 09:36:13 +00:00
|
|
|
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 {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not create remote: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
err = repo.Fetch(&git.FetchOptions{
|
|
|
|
RemoteName: "origin",
|
|
|
|
RefSpecs: []config.RefSpec{refspec},
|
|
|
|
Auth: pd.Authenticator,
|
|
|
|
})
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
refName := plumbing.NewBranchReferenceName(md.PushBranch)
|
2020-12-20 09:36:13 +00:00
|
|
|
log.Printf("set reference to ref: %s", refName)
|
2020-12-16 10:46:33 +00:00
|
|
|
|
2021-04-06 19:35:46 +00:00
|
|
|
var hash plumbing.Hash
|
|
|
|
if commitPin[md.PushBranch] != "" {
|
|
|
|
hash = plumbing.NewHash(commitPin[md.PushBranch])
|
|
|
|
}
|
|
|
|
|
2020-12-14 03:51:03 +00:00
|
|
|
if err != nil {
|
2020-12-20 09:36:13 +00:00
|
|
|
h := plumbing.NewSymbolicReference(plumbing.HEAD, refName)
|
|
|
|
if err := repo.Storer.CheckAndSetReference(h, nil); err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not set reference: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = w.Checkout(&git.CheckoutOptions{
|
2021-02-24 07:27:51 +00:00
|
|
|
Branch: plumbing.NewRemoteReferenceName("origin", md.PushBranch),
|
2021-04-06 19:35:46 +00:00
|
|
|
Hash: hash,
|
2020-12-20 09:36:13 +00:00
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not checkout: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 21:07:02 +00:00
|
|
|
err = pd.Importer.WriteSource(pd, md)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, err
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-09-07 11:30:50 +00:00
|
|
|
err = data.CopyFromFs(md.Worktree.Filesystem, w.Filesystem, ".")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
md.Repo = repo
|
|
|
|
md.Worktree = w
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2020-12-22 16:28:04 +00:00
|
|
|
if pd.ModuleMode {
|
2021-09-03 21:07:02 +00:00
|
|
|
err := patchModuleYaml(pd, md)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, err
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
2020-12-22 16:28:04 +00:00
|
|
|
} else {
|
2021-09-03 21:07:02 +00:00
|
|
|
err := executePatchesRpm(pd, md)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, err
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
// get ignored files hash and add to .{Name}.metadata
|
2021-09-03 21:07:02 +00:00
|
|
|
metadataFile := fmt.Sprintf(".%s.metadata", md.Name)
|
2020-12-20 09:36:13 +00:00
|
|
|
metadata, err := w.Filesystem.Create(metadataFile)
|
2020-12-14 03:51:03 +00:00
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not create metadata file: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
for _, source := range md.SourcesToIgnore {
|
|
|
|
sourcePath := source.Name
|
2021-02-14 17:14:21 +00:00
|
|
|
|
|
|
|
_, err := w.Filesystem.Stat(sourcePath)
|
2021-02-24 07:27:51 +00:00
|
|
|
if source.Expired || err != nil {
|
2021-01-01 15:55:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
sourceFile, err := w.Filesystem.Open(sourcePath)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not open ignored source file %s: %v", sourcePath, err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
sourceFileBts, err := ioutil.ReadAll(sourceFile)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not read the whole of ignored source file: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
source.HashFunction.Reset()
|
|
|
|
_, err = source.HashFunction.Write(sourceFileBts)
|
2020-12-14 03:51:03 +00:00
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not write bytes to hash function: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
2021-02-24 07:27:51 +00:00
|
|
|
checksum := hex.EncodeToString(source.HashFunction.Sum(nil))
|
2020-12-20 17:38:25 +00:00
|
|
|
checksumLine := fmt.Sprintf("%s %s\n", checksum, sourcePath)
|
2020-12-20 09:36:13 +00:00
|
|
|
_, err = metadata.Write([]byte(checksumLine))
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not write to metadata file: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
2020-12-20 17:38:25 +00:00
|
|
|
|
2021-04-08 02:54:44 +00:00
|
|
|
if data.StrContains(alreadyUploadedBlobs, checksum) {
|
2020-12-23 13:38:46 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-18 17:56:47 +00:00
|
|
|
if !pd.BlobStorage.Exists(checksum) && !pd.NoStorageUpload {
|
|
|
|
pd.BlobStorage.Write(checksum, sourceFileBts)
|
|
|
|
log.Printf("wrote %s to blob storage", checksum)
|
2021-02-19 15:22:36 +00:00
|
|
|
}
|
2021-03-18 17:56:47 +00:00
|
|
|
alreadyUploadedBlobs = append(alreadyUploadedBlobs, checksum)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
_, err = w.Add(metadataFile)
|
2020-12-14 03:51:03 +00:00
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not add metadata file: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
lastFilesToAdd := []string{".gitignore", "SPECS"}
|
|
|
|
for _, f := range lastFilesToAdd {
|
2020-12-22 16:28:04 +00:00
|
|
|
_, err := w.Filesystem.Stat(f)
|
|
|
|
if err == nil {
|
|
|
|
_, err := w.Add(f)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not add %s: %v", f, err)
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
2020-12-15 08:56:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:00:56 +00:00
|
|
|
nvrMatch := rpmutils.Nvr.FindStringSubmatch(match[3])
|
|
|
|
if len(nvrMatch) >= 4 {
|
|
|
|
versionForBranch[md.PushBranch] = &srpmprocpb.VersionRelease{
|
|
|
|
Version: nvrMatch[2],
|
|
|
|
Release: nvrMatch[3],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 17:56:47 +00:00
|
|
|
if pd.TmpFsMode != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-03 21:07:02 +00:00
|
|
|
err = pd.Importer.PostProcess(md)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, err
|
2021-09-03 21:07:02 +00:00
|
|
|
}
|
2020-12-20 09:36:13 +00:00
|
|
|
|
|
|
|
// show status
|
|
|
|
status, _ := w.Status()
|
|
|
|
log.Printf("successfully processed:\n%s", status)
|
|
|
|
|
2021-02-20 04:12:16 +00:00
|
|
|
statusLines := strings.Split(status.String(), "\n")
|
|
|
|
for _, line := range statusLines {
|
|
|
|
trimmed := strings.TrimSpace(line)
|
|
|
|
if strings.HasPrefix(trimmed, "D") {
|
|
|
|
path := strings.TrimPrefix(trimmed, "D ")
|
|
|
|
_, err := w.Remove(path)
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not delete extra file %s: %v", path, err)
|
2021-02-20 04:12:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
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())
|
2021-02-24 07:27:51 +00:00
|
|
|
refOrigin := "refs/heads/" + md.PushBranch
|
2020-12-20 09:36:13 +00:00
|
|
|
pushRefspecs = append(pushRefspecs, config.RefSpec(fmt.Sprintf("HEAD:%s", refOrigin)))
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
// we are now finished with the tree and are going to push it to the src Repo
|
2020-12-20 09:36:13 +00:00
|
|
|
// 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 {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not commit object: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
obj, err := repo.CommitObject(commit)
|
2020-12-14 03:51:03 +00:00
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not get commit object: %v", err)
|
2020-12-14 03:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
log.Printf("committed:\n%s", obj.String())
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
_, err = repo.CreateTag(newTag, commit, &git.CreateTagOptions{
|
|
|
|
Tagger: &object.Signature{
|
|
|
|
Name: pd.GitCommitterName,
|
|
|
|
Email: pd.GitCommitterEmail,
|
|
|
|
When: time.Now(),
|
|
|
|
},
|
2021-02-24 07:27:51 +00:00
|
|
|
Message: "import " + md.TagBranch + " from " + pd.RpmLocation,
|
2020-12-20 09:36:13 +00:00
|
|
|
SignKey: nil,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not create tag: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2021-04-13 04:36:44 +00:00
|
|
|
pushRefspecs = append(pushRefspecs, config.RefSpec("HEAD:"+plumbing.NewTagReferenceName(newTag)))
|
2020-12-14 03:51:03 +00:00
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
err = repo.Push(&git.PushOptions{
|
|
|
|
RemoteName: "origin",
|
|
|
|
Auth: pd.Authenticator,
|
|
|
|
RefSpecs: pushRefspecs,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-09-04 21:33:08 +00:00
|
|
|
return nil, fmt.Errorf("could not push to remote: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
2021-02-19 15:22:36 +00:00
|
|
|
|
|
|
|
hashString := obj.Hash.String()
|
2021-02-24 07:27:51 +00:00
|
|
|
latestHashForBranch[md.PushBranch] = hashString
|
2021-02-19 15:22:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 15:00:56 +00:00
|
|
|
return &srpmprocpb.ProcessResponse{
|
|
|
|
BranchCommits: latestHashForBranch,
|
|
|
|
BranchVersions: versionForBranch,
|
|
|
|
}, nil
|
2020-12-14 01:29:02 +00:00
|
|
|
}
|