2020-12-20 09:36:13 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-02-24 07:27:51 +00:00
|
|
|
"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
|
2020-12-20 09:36:13 +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"
|
2021-01-01 19:09:23 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/object"
|
2020-12-20 09:36:13 +00:00
|
|
|
"github.com/go-git/go-git/v5/storage/memory"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2021-01-01 19:09:23 +00:00
|
|
|
"time"
|
2020-12-20 09:36:13 +00:00
|
|
|
)
|
|
|
|
|
2021-01-01 19:09:23 +00:00
|
|
|
type remoteTarget struct {
|
|
|
|
remote string
|
|
|
|
when time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type remoteTargetSlice []remoteTarget
|
|
|
|
|
|
|
|
func (p remoteTargetSlice) Len() int {
|
|
|
|
return len(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p remoteTargetSlice) Less(i, j int) bool {
|
|
|
|
return p[i].when.Before(p[j].when)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p remoteTargetSlice) Swap(i, j int) {
|
|
|
|
p[i], p[j] = p[j], p[i]
|
|
|
|
}
|
|
|
|
|
2020-12-20 09:36:13 +00:00
|
|
|
type GitMode struct{}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
func (g *GitMode) RetrieveSource(pd *data.ProcessData) *data.ModeData {
|
2020-12-20 09:36:13 +00:00
|
|
|
repo, err := git.Init(memory.NewStorage(), memfs.New())
|
|
|
|
if err != nil {
|
2021-02-24 07:27:51 +00:00
|
|
|
log.Fatalf("could not init git Repo: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w, err := repo.Worktree()
|
|
|
|
if err != nil {
|
2021-02-24 07:27:51 +00:00
|
|
|
log.Fatalf("could not get Worktree: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refspec := config.RefSpec("+refs/heads/*:refs/remotes/*")
|
|
|
|
remote, err := repo.CreateRemote(&config.RemoteConfig{
|
|
|
|
Name: "upstream",
|
|
|
|
URLs: []string{pd.RpmLocation},
|
|
|
|
Fetch: []config.RefSpec{refspec},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create remote: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-01-01 19:09:23 +00:00
|
|
|
err = remote.Fetch(&git.FetchOptions{
|
2021-02-21 13:51:30 +00:00
|
|
|
RefSpecs: []config.RefSpec{refspec},
|
|
|
|
Tags: git.AllTags,
|
|
|
|
Force: true,
|
2021-01-01 19:09:23 +00:00
|
|
|
})
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
2021-01-01 19:09:23 +00:00
|
|
|
log.Fatalf("could not fetch upstream: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 19:09:23 +00:00
|
|
|
var branches remoteTargetSlice
|
2020-12-20 09:36:13 +00:00
|
|
|
|
2021-03-08 22:06:52 +00:00
|
|
|
latestTags := map[string]*remoteTarget{}
|
|
|
|
|
2021-02-21 13:51:30 +00:00
|
|
|
tagAdd := func(tag *object.Tag) error {
|
|
|
|
if strings.HasPrefix(tag.Name, fmt.Sprintf("imports/%s%d", pd.ImportBranchPrefix, pd.Version)) {
|
2021-03-08 22:06:52 +00:00
|
|
|
refSpec := fmt.Sprintf("refs/tags/%s", tag.Name)
|
|
|
|
if tagImportRegex.MatchString(refSpec) {
|
|
|
|
match := tagImportRegex.FindStringSubmatch(refSpec)
|
|
|
|
|
|
|
|
exists := latestTags[match[2]]
|
|
|
|
if exists != nil && exists.when.After(tag.Tagger.When) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
latestTags[match[2]] = &remoteTarget{
|
|
|
|
remote: refSpec,
|
|
|
|
when: tag.Tagger.When,
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
2021-01-01 19:09:23 +00:00
|
|
|
return nil
|
2021-02-21 13:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tagIter, err := repo.TagObjects()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get tag objects: %v", err)
|
|
|
|
}
|
|
|
|
_ = tagIter.ForEach(tagAdd)
|
|
|
|
|
2021-03-08 22:06:52 +00:00
|
|
|
if len(latestTags) == 0 {
|
2021-02-21 13:51:30 +00:00
|
|
|
list, err := remote.List(&git.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not list upstream: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ref := range list {
|
|
|
|
if ref.Hash().IsZero() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := repo.CommitObject(ref.Hash())
|
|
|
|
if err != nil {
|
2021-04-06 19:35:46 +00:00
|
|
|
log.Printf("could not get commit object for ref %s: %v", ref.Name().String(), err)
|
|
|
|
continue
|
2021-02-21 13:51:30 +00:00
|
|
|
}
|
|
|
|
_ = tagAdd(&object.Tag{
|
|
|
|
Name: strings.TrimPrefix(string(ref.Name()), "refs/tags/"),
|
|
|
|
Tagger: commit.Committer,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:06:52 +00:00
|
|
|
for _, branch := range latestTags {
|
|
|
|
log.Printf("tag: %s", strings.TrimPrefix(branch.remote, "refs/tags/"))
|
|
|
|
branches = append(branches, *branch)
|
|
|
|
}
|
|
|
|
|
2021-01-01 19:09:23 +00:00
|
|
|
sort.Sort(branches)
|
|
|
|
|
|
|
|
var sortedBranches []string
|
|
|
|
for _, branch := range branches {
|
|
|
|
sortedBranches = append(sortedBranches, branch.remote)
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
2020-12-20 09:36:13 +00:00
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
return &data.ModeData{
|
|
|
|
Repo: repo,
|
|
|
|
Worktree: w,
|
|
|
|
RpmFile: createPackageFile(filepath.Base(pd.RpmLocation)),
|
|
|
|
FileWrites: nil,
|
|
|
|
Branches: sortedBranches,
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
func (g *GitMode) WriteSource(pd *data.ProcessData, md *data.ModeData) {
|
|
|
|
remote, err := md.Repo.Remote("upstream")
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not get upstream remote: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-12-22 16:28:04 +00:00
|
|
|
var refspec config.RefSpec
|
|
|
|
var branchName string
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
if strings.HasPrefix(md.TagBranch, "refs/heads") {
|
|
|
|
refspec = config.RefSpec(fmt.Sprintf("+%s:%s", md.TagBranch, md.TagBranch))
|
|
|
|
branchName = strings.TrimPrefix(md.TagBranch, "refs/heads/")
|
2020-12-22 16:28:04 +00:00
|
|
|
} else {
|
2021-02-24 07:27:51 +00:00
|
|
|
match := tagImportRegex.FindStringSubmatch(md.TagBranch)
|
2020-12-22 16:28:04 +00:00
|
|
|
branchName = match[2]
|
2021-02-24 07:27:51 +00:00
|
|
|
refspec = config.RefSpec(fmt.Sprintf("+refs/heads/%s:%s", branchName, md.TagBranch))
|
2020-12-22 16:28:04 +00:00
|
|
|
}
|
2020-12-22 05:13:27 +00:00
|
|
|
log.Printf("checking out upstream refspec %s", refspec)
|
2020-12-20 09:36:13 +00:00
|
|
|
err = remote.Fetch(&git.FetchOptions{
|
|
|
|
RemoteName: "upstream",
|
|
|
|
RefSpecs: []config.RefSpec{refspec},
|
|
|
|
Tags: git.AllTags,
|
|
|
|
Force: true,
|
|
|
|
})
|
2021-02-21 13:51:30 +00:00
|
|
|
if err != nil && err != git.NoErrAlreadyUpToDate {
|
2020-12-20 09:36:13 +00:00
|
|
|
log.Fatalf("could not fetch upstream: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
err = md.Worktree.Checkout(&git.CheckoutOptions{
|
|
|
|
Branch: plumbing.ReferenceName(md.TagBranch),
|
2020-12-20 09:36:13 +00:00
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not checkout source from git: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
_, err = md.Worktree.Add(".")
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
2021-02-24 07:27:51 +00:00
|
|
|
log.Fatalf("could not add Worktree: %v", err)
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
metadataFile, err := md.Worktree.Filesystem.Open(fmt.Sprintf(".%s.metadata", md.RpmFile.Name()))
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
2020-12-22 16:28:04 +00:00
|
|
|
log.Printf("warn: could not open metadata file, so skipping: %v", err)
|
|
|
|
return
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fileBytes, err := ioutil.ReadAll(metadataFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not read metadata file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DisableCompression: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fileContent := strings.Split(string(fileBytes), "\n")
|
|
|
|
for _, line := range fileContent {
|
|
|
|
if strings.TrimSpace(line) == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-22 05:13:27 +00:00
|
|
|
lineInfo := strings.SplitN(line, " ", 2)
|
|
|
|
hash := strings.TrimSpace(lineInfo[0])
|
|
|
|
path := strings.TrimSpace(lineInfo[1])
|
2020-12-20 09:36:13 +00:00
|
|
|
|
2021-02-19 15:22:36 +00:00
|
|
|
var body []byte
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
if md.BlobCache[hash] != nil {
|
|
|
|
body = md.BlobCache[hash]
|
2021-02-19 15:22:36 +00:00
|
|
|
log.Printf("retrieving %s from cache", hash)
|
|
|
|
} else {
|
|
|
|
fromBlobStorage := pd.BlobStorage.Read(hash)
|
2021-02-24 07:27:51 +00:00
|
|
|
if fromBlobStorage != nil && !pd.NoStorageDownload {
|
2021-02-19 15:22:36 +00:00
|
|
|
body = fromBlobStorage
|
|
|
|
log.Printf("downloading %s from blob storage", hash)
|
|
|
|
} else {
|
2021-02-24 07:27:51 +00:00
|
|
|
url := fmt.Sprintf("https://git.centos.org/sources/%s/%s/%s", md.RpmFile.Name(), branchName, hash)
|
2021-02-19 15:22:36 +00:00
|
|
|
log.Printf("downloading %s", url)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not create new http request: %v", err)
|
|
|
|
}
|
|
|
|
req.Header.Set("Accept-Encoding", "*")
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not download dist-git file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err = ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not read the whole dist-git file: %v", err)
|
|
|
|
}
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not close body handle: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
md.BlobCache[hash] = body
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
f, err := md.Worktree.Filesystem.Create(path)
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not open file pointer: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:38:25 +00:00
|
|
|
hasher := CompareHash(body, hash)
|
2020-12-20 09:36:13 +00:00
|
|
|
if hasher == nil {
|
|
|
|
log.Fatal("checksum in metadata does not match dist-git file")
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
md.SourcesToIgnore = append(md.SourcesToIgnore, &data.IgnoredSource{
|
|
|
|
Name: path,
|
|
|
|
HashFunction: hasher,
|
2020-12-20 09:36:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
_, err = f.Write(body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not copy dist-git file to in-tree: %v", err)
|
|
|
|
}
|
|
|
|
_ = f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
func (g *GitMode) PostProcess(md *data.ModeData) {
|
|
|
|
for _, source := range md.SourcesToIgnore {
|
|
|
|
_, err := md.Worktree.Filesystem.Stat(source.Name)
|
2021-01-01 15:55:21 +00:00
|
|
|
if err == nil {
|
2021-02-24 07:27:51 +00:00
|
|
|
err := md.Worktree.Filesystem.Remove(source.Name)
|
2021-01-01 15:55:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not remove dist-git file: %v", err)
|
|
|
|
}
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
_, err := md.Worktree.Add(".")
|
2020-12-20 09:36:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("could not add git sources: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
func (g *GitMode) ImportName(_ *data.ProcessData, md *data.ModeData) string {
|
|
|
|
if tagImportRegex.MatchString(md.TagBranch) {
|
|
|
|
match := tagImportRegex.FindStringSubmatch(md.TagBranch)
|
2020-12-22 16:28:04 +00:00
|
|
|
return match[3]
|
|
|
|
}
|
|
|
|
|
2021-02-24 07:27:51 +00:00
|
|
|
return strings.TrimPrefix(md.TagBranch, "refs/heads/")
|
2020-12-20 09:36:13 +00:00
|
|
|
}
|