From dbc948a453f8cbdf11c8c595299b114ea4b4a814 Mon Sep 17 00:00:00 2001 From: Mustafa Gezen Date: Tue, 22 Dec 2020 06:13:27 +0100 Subject: [PATCH] add no-dup-mode to skip already imported tags --- cmd/srpmproc/main.go | 3 +++ internal/git.go | 7 ++++--- internal/process.go | 48 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/cmd/srpmproc/main.go b/cmd/srpmproc/main.go index 114a1b2..ef1f603 100644 --- a/cmd/srpmproc/main.go +++ b/cmd/srpmproc/main.go @@ -26,6 +26,7 @@ var ( gitCommitterEmail string modulePrefix string rpmPrefix string + noDupMode bool ) var root = &cobra.Command{ @@ -87,6 +88,7 @@ func mn(_ *cobra.Command, _ []string) { GitCommitterEmail: gitCommitterEmail, ModulePrefix: modulePrefix, Authenticator: authenticator, + NoDupMode: noDupMode, }) } @@ -106,6 +108,7 @@ func main() { root.Flags().StringVar(&gitCommitterEmail, "git-committer-email", "mustafa+distrobuild@bycrates.com", "Email of committer") root.Flags().StringVar(&modulePrefix, "module-prefix", "https://git.centos.org/modules", "Where to retrieve modules if exists. Only used when source-rpm is a git repo") root.Flags().StringVar(&rpmPrefix, "rpm-prefix", "https://git.centos.org/rpms", "Where to retrieve SRPM content. Only used when source-rpm is not a local file") + root.Flags().BoolVar(&noDupMode, "no-dup-mode", false, "If enabled, skips already imported tags") if err := root.Execute(); err != nil { log.Fatal(err) diff --git a/internal/git.go b/internal/git.go index 8f2739a..e1c4c7e 100644 --- a/internal/git.go +++ b/internal/git.go @@ -71,6 +71,7 @@ func (g *GitMode) WriteSource(md *modeData) { match := tagImportRegex.FindStringSubmatch(md.tagBranch) refspec := config.RefSpec(fmt.Sprintf("+refs/heads/%s:%s", match[2], md.tagBranch)) + log.Printf("checking out upstream refspec %s", refspec) err = remote.Fetch(&git.FetchOptions{ RemoteName: "upstream", RefSpecs: []config.RefSpec{refspec}, @@ -115,9 +116,9 @@ func (g *GitMode) WriteSource(md *modeData) { continue } - lineInfo := strings.Split(line, " ") - hash := lineInfo[0] - path := lineInfo[1] + lineInfo := strings.SplitN(line, " ", 2) + hash := strings.TrimSpace(lineInfo[0]) + path := strings.TrimSpace(lineInfo[1]) url := fmt.Sprintf("https://git.centos.org/sources/%s/%s/%s", md.rpmFile.Name(), match[2], hash) log.Printf("downloading %s", url) diff --git a/internal/process.go b/internal/process.go index 9f45af2..1bf071b 100644 --- a/internal/process.go +++ b/internal/process.go @@ -35,6 +35,7 @@ type ProcessData struct { Authenticator *ssh.PublicKeys Importer ImportMode BlobStorage blob.Storage + NoDupMode bool } type ignoredSource struct { @@ -63,6 +64,40 @@ type modeData struct { func ProcessRPM(pd *ProcessData) { md := pd.Importer.RetrieveSource(pd) + // 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 { + log.Fatalf("could not init git repo: %v", err) + } + remoteUrl := fmt.Sprintf("%s/dist/%s.git", pd.UpstreamPrefix, md.rpmFile.Name()) + 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 list remote: %v", err) + } + + for _, ref := range list { + if !strings.HasPrefix(string(ref.Name()), "refs/tags/imports") { + continue + } + tagIgnoreList = append(tagIgnoreList, string(ref.Name())) + } + } + sourceRepo := *md.repo sourceWorktree := *md.worktree @@ -89,6 +124,18 @@ func ProcessRPM(pd *ProcessData) { match := tagImportRegex.FindStringSubmatch(md.tagBranch) md.pushBranch = "rocky" + strings.TrimPrefix(match[2], "c") + newTag := "imports/rocky" + strings.TrimPrefix(match[1], "imports/c") + + shouldContinue := true + for _, ignoredTag := range tagIgnoreList { + if ignoredTag == "refs/tags/"+newTag { + log.Printf("skipping %s", ignoredTag) + shouldContinue = false + } + } + if !shouldContinue { + continue + } // create a new remote remoteUrl := fmt.Sprintf("%s/dist/%s.git", pd.UpstreamPrefix, rpmFile.Name()) @@ -225,7 +272,6 @@ func ProcessRPM(pd *ProcessData) { log.Printf("committed:\n%s", obj.String()) - newTag := "imports/rocky" + strings.TrimPrefix(match[1], "imports/c") _, err = repo.CreateTag(newTag, commit, &git.CreateTagOptions{ Tagger: &object.Signature{ Name: pd.GitCommitterName,