Skip to content

Commit

Permalink
enhancement: make hashes available as a value and print out for cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
mstg committed Sep 4, 2021
1 parent 825228e commit fff8896
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
10 changes: 9 additions & 1 deletion cmd/srpmproc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package main

import (
"encoding/json"
"github.com/rocky-linux/srpmproc/pkg/srpmproc"
"log"
"os"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -84,10 +86,16 @@ func mn(_ *cobra.Command, _ []string) {
log.Fatal(err)
}

err = srpmproc.ProcessRPM(pd)
latestHashForBranch, err := srpmproc.ProcessRPM(pd)
if err != nil {
log.Fatal(err)
}

err = json.NewEncoder(os.Stdout).Encode(latestHashForBranch)
if err != nil {
log.Fatal(err)
}

}

func main() {
Expand Down
61 changes: 27 additions & 34 deletions pkg/srpmproc/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package srpmproc

import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
Expand All @@ -33,7 +32,6 @@ import (
"github.com/rocky-linux/srpmproc/pkg/modes"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -240,10 +238,10 @@ func NewProcessData(req *ProcessDataRequest) (*data.ProcessData, error) {
// source files goes into -> SOURCES
// all files that are remote goes into .gitignore
// all ignored files' hash goes into .{Name}.metadata
func ProcessRPM(pd *data.ProcessData) error {
func ProcessRPM(pd *data.ProcessData) (map[string]string, error) {
md, err := pd.Importer.RetrieveSource(pd)
if err != nil {
return err
return nil, err
}
md.BlobCache = map[string][]byte{}

Expand All @@ -262,7 +260,7 @@ func ProcessRPM(pd *data.ProcessData) error {
if pd.NoDupMode {
repo, err := git.Init(memory.NewStorage(), memfs.New())
if err != nil {
return fmt.Errorf("could not init git repo: %v", err)
return nil, fmt.Errorf("could not init git repo: %v", err)
}
remoteUrl := fmt.Sprintf("%s/%s/%s.git", pd.UpstreamPrefix, remotePrefix, gitlabify(md.Name))
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
Expand All @@ -273,7 +271,7 @@ func ProcessRPM(pd *data.ProcessData) error {
Fetch: []config.RefSpec{refspec},
})
if err != nil {
return fmt.Errorf("could not create remote: %v", err)
return nil, fmt.Errorf("could not create remote: %v", err)
}

list, err := remote.List(&git.ListOptions{
Expand Down Expand Up @@ -348,17 +346,17 @@ func ProcessRPM(pd *data.ProcessData) error {

createdFs, err := pd.FsCreator(md.PushBranch)
if err != nil {
return err
return nil, err
}

// create new Repo for final dist
repo, err := git.Init(memory.NewStorage(), createdFs)
if err != nil {
return fmt.Errorf("could not create new dist Repo: %v", err)
return nil, fmt.Errorf("could not create new dist Repo: %v", err)
}
w, err := repo.Worktree()
if err != nil {
return fmt.Errorf("could not get dist Worktree: %v", err)
return nil, fmt.Errorf("could not get dist Worktree: %v", err)
}

shouldContinue := true
Expand All @@ -384,7 +382,7 @@ func ProcessRPM(pd *data.ProcessData) error {
Fetch: []config.RefSpec{refspec},
})
if err != nil {
return fmt.Errorf("could not create remote: %v", err)
return nil, fmt.Errorf("could not create remote: %v", err)
}

err = repo.Fetch(&git.FetchOptions{
Expand All @@ -404,7 +402,7 @@ func ProcessRPM(pd *data.ProcessData) error {
if err != nil {
h := plumbing.NewSymbolicReference(plumbing.HEAD, refName)
if err := repo.Storer.CheckAndSetReference(h, nil); err != nil {
return fmt.Errorf("could not set reference: %v", err)
return nil, fmt.Errorf("could not set reference: %v", err)
}
} else {
err = w.Checkout(&git.CheckoutOptions{
Expand All @@ -413,13 +411,13 @@ func ProcessRPM(pd *data.ProcessData) error {
Force: true,
})
if err != nil {
return fmt.Errorf("could not checkout: %v", err)
return nil, fmt.Errorf("could not checkout: %v", err)
}
}

err = pd.Importer.WriteSource(pd, md)
if err != nil {
return err
return nil, err
}

data.CopyFromFs(md.Worktree.Filesystem, w.Filesystem, ".")
Expand All @@ -429,20 +427,20 @@ func ProcessRPM(pd *data.ProcessData) error {
if pd.ModuleMode {
err := patchModuleYaml(pd, md)
if err != nil {
return err
return nil, err
}
} else {
err := executePatchesRpm(pd, md)
if err != nil {
return err
return nil, err
}
}

// get ignored files hash and add to .{Name}.metadata
metadataFile := fmt.Sprintf(".%s.metadata", md.Name)
metadata, err := w.Filesystem.Create(metadataFile)
if err != nil {
return fmt.Errorf("could not create metadata file: %v", err)
return nil, fmt.Errorf("could not create metadata file: %v", err)
}
for _, source := range md.SourcesToIgnore {
sourcePath := source.Name
Expand All @@ -454,23 +452,23 @@ func ProcessRPM(pd *data.ProcessData) error {

sourceFile, err := w.Filesystem.Open(sourcePath)
if err != nil {
return fmt.Errorf("could not open ignored source file %s: %v", sourcePath, err)
return nil, fmt.Errorf("could not open ignored source file %s: %v", sourcePath, err)
}
sourceFileBts, err := ioutil.ReadAll(sourceFile)
if err != nil {
return fmt.Errorf("could not read the whole of ignored source file: %v", err)
return nil, fmt.Errorf("could not read the whole of ignored source file: %v", err)
}

source.HashFunction.Reset()
_, err = source.HashFunction.Write(sourceFileBts)
if err != nil {
return fmt.Errorf("could not write bytes to hash function: %v", err)
return nil, fmt.Errorf("could not write bytes to hash function: %v", err)
}
checksum := hex.EncodeToString(source.HashFunction.Sum(nil))
checksumLine := fmt.Sprintf("%s %s\n", checksum, sourcePath)
_, err = metadata.Write([]byte(checksumLine))
if err != nil {
return fmt.Errorf("could not write to metadata file: %v", err)
return nil, fmt.Errorf("could not write to metadata file: %v", err)
}

if data.StrContains(alreadyUploadedBlobs, checksum) {
Expand All @@ -485,7 +483,7 @@ func ProcessRPM(pd *data.ProcessData) error {

_, err = w.Add(metadataFile)
if err != nil {
return fmt.Errorf("could not add metadata file: %v", err)
return nil, fmt.Errorf("could not add metadata file: %v", err)
}

lastFilesToAdd := []string{".gitignore", "SPECS"}
Expand All @@ -494,7 +492,7 @@ func ProcessRPM(pd *data.ProcessData) error {
if err == nil {
_, err := w.Add(f)
if err != nil {
return fmt.Errorf("could not add %s: %v", f, err)
return nil, fmt.Errorf("could not add %s: %v", f, err)
}
}
}
Expand All @@ -505,7 +503,7 @@ func ProcessRPM(pd *data.ProcessData) error {

err = pd.Importer.PostProcess(md)
if err != nil {
return err
return nil, err
}

// show status
Expand All @@ -519,7 +517,7 @@ func ProcessRPM(pd *data.ProcessData) error {
path := strings.TrimPrefix(trimmed, "D ")
_, err := w.Remove(path)
if err != nil {
return fmt.Errorf("could not delete extra file %s: %v", path, err)
return nil, fmt.Errorf("could not delete extra file %s: %v", path, err)
}
}
}
Expand Down Expand Up @@ -549,12 +547,12 @@ func ProcessRPM(pd *data.ProcessData) error {
Parents: hashes,
})
if err != nil {
return fmt.Errorf("could not commit object: %v", err)
return nil, fmt.Errorf("could not commit object: %v", err)
}

obj, err := repo.CommitObject(commit)
if err != nil {
return fmt.Errorf("could not get commit object: %v", err)
return nil, fmt.Errorf("could not get commit object: %v", err)
}

log.Printf("committed:\n%s", obj.String())
Expand All @@ -569,7 +567,7 @@ func ProcessRPM(pd *data.ProcessData) error {
SignKey: nil,
})
if err != nil {
return fmt.Errorf("could not create tag: %v", err)
return nil, fmt.Errorf("could not create tag: %v", err)
}

pushRefspecs = append(pushRefspecs, config.RefSpec("HEAD:"+plumbing.NewTagReferenceName(newTag)))
Expand All @@ -581,17 +579,12 @@ func ProcessRPM(pd *data.ProcessData) error {
Force: true,
})
if err != nil {
return fmt.Errorf("could not push to remote: %v", err)
return nil, fmt.Errorf("could not push to remote: %v", err)
}

hashString := obj.Hash.String()
latestHashForBranch[md.PushBranch] = hashString
}

err = json.NewEncoder(os.Stdout).Encode(latestHashForBranch)
if err != nil {
return fmt.Errorf("could not print hashes")
}

return nil
return latestHashForBranch, nil
}

0 comments on commit fff8896

Please sign in to comment.