Skip to content
This repository was archived by the owner on Jun 27, 2025. It is now read-only.

Commit 40180a3

Browse files
authored
Merge pull request #11 from skip77/manual-commits-fix
Manual commits fix
2 parents 6e2ed2e + 7f14563 commit 40180a3

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

pkg/misc/regex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func GetTagImportRegex(pd *data.ProcessData) *regexp.Regexp {
3838
// if we are ok with importing that reference. We are looking for the traditional <prefix><version><suffix> pattern, like "c9s", and also the
3939
// modular "stream-<NAME>-<VERSION>-rhel-<VERSION> branch pattern as well
4040
func TaglessRefOk(tag string, pd *data.ProcessData) bool {
41-
// First case is very easy: if we are "refs/heads/<prefix><version><suffix>" , then this is def. a branch we should import
42-
if strings.HasPrefix(tag, fmt.Sprintf("refs/heads/%s%d%s", pd.ImportBranchPrefix, pd.Version, pd.BranchSuffix)) {
41+
// First case is very easy: if we are exactly "refs/heads/<prefix><version><suffix>" , then this is def. a branch we should import
42+
if tag == fmt.Sprintf("refs/heads/%s%d%s", pd.ImportBranchPrefix, pd.Version, pd.BranchSuffix) {
4343
return true
4444
}
4545

pkg/srpmproc/process.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,9 @@ func ProcessRPM(pd *data.ProcessData) (*srpmprocpb.ProcessResponse, error) {
413413
if pd.SingleTag != "" {
414414
md.Branches = []string{fmt.Sprintf("refs/tags/%s", pd.SingleTag)}
415415
} else if len(pd.ManualCommits) > 0 {
416-
md.Branches = []string{}
417-
for _, commit := range pd.ManualCommits {
418-
branchCommit := strings.Split(commit, ":")
419-
if len(branchCommit) != 2 {
420-
return nil, fmt.Errorf("invalid manual commit list")
421-
}
422-
423-
head := fmt.Sprintf("refs/tags/imports/%s/%s-%s", branchCommit[0], md.Name, branchCommit[1])
424-
md.Branches = append(md.Branches, head)
425-
commitPin[head] = branchCommit[1]
426-
}
416+
log.Println("Manual commits were listed for import. Switching to perform a tagless import of these commit(s).")
417+
pd.TaglessMode = true
418+
return processRPMTagless(pd)
427419
}
428420

429421
// If we have no valid branches to consider, then we'll automatically switch to attempt a tagless import:
@@ -813,6 +805,20 @@ func processRPMTagless(pd *data.ProcessData) (*srpmprocpb.ProcessResponse, error
813805
sourceWorktree := *md.Worktree
814806
localPath := ""
815807

808+
// if a manual commit list is provided, we want to create our md.Branches[] array in a special format:
809+
if len(pd.ManualCommits) > 0 {
810+
md.Branches = []string{}
811+
for _, commit := range pd.ManualCommits {
812+
branchCommit := strings.Split(commit, ":")
813+
if len(branchCommit) != 2 {
814+
return nil, fmt.Errorf("invalid manual commit list")
815+
}
816+
817+
head := fmt.Sprintf("COMMIT:%s:%s", branchCommit[0], branchCommit[1])
818+
md.Branches = append(md.Branches, head)
819+
}
820+
}
821+
816822
for _, branch := range md.Branches {
817823
md.Repo = &sourceRepo
818824
md.Worktree = &sourceWorktree
@@ -832,13 +838,35 @@ func processRPMTagless(pd *data.ProcessData) (*srpmprocpb.ProcessResponse, error
832838
return nil, fmt.Errorf("Could not create temporary directory: %s", localPath)
833839
}
834840

841+
// we'll make our branch we're processing more presentable if it's in the COMMIT:<branch>:<hash> format:
842+
if strings.HasPrefix(branch, "COMMIT:") {
843+
branch = fmt.Sprintf("refs/heads/%s", strings.Split(branch, ":")[1])
844+
}
845+
835846
// Clone repo into the temporary path, but only the tag we're interested in:
836847
// (TODO: will probably need to assign this a variable or use the md struct gitrepo object to perform a successful tag+push later)
837-
_, _ = git.PlainClone(localPath, false, &git.CloneOptions{
848+
rTmp, err := git.PlainClone(localPath, false, &git.CloneOptions{
838849
URL: pd.RpmLocation,
839850
SingleBranch: true,
840851
ReferenceName: plumbing.ReferenceName(branch),
841852
})
853+
if err != nil {
854+
return nil, err
855+
}
856+
857+
// If we're dealing with a special manual commit to import ("COMMIT:<branch>:<githash>"), then we need to check out that
858+
// specific hash from the repo we are importing from:
859+
if strings.HasPrefix(md.TagBranch, "COMMIT:") {
860+
commitList := strings.Split(md.TagBranch, ":")
861+
862+
wTmp, _ := rTmp.Worktree()
863+
err = wTmp.Checkout(&git.CheckoutOptions{
864+
Hash: plumbing.NewHash(commitList[2]),
865+
})
866+
if err != nil {
867+
return nil, fmt.Errorf("Could not find manual commit %s in the repository. Must be a valid commit hash.", commitList[2])
868+
}
869+
}
842870

843871
// Now that we're cloned into localPath, we need to "covert" the import into the old format
844872
// We want sources to become .PKGNAME.metadata, we want SOURCES and SPECS folders, etc.
@@ -919,6 +947,12 @@ func processRPMTagless(pd *data.ProcessData) (*srpmprocpb.ProcessResponse, error
919947
Force: true,
920948
})
921949

950+
// These os commands actually move the data from our cloned import repo to our cloned "push" repo (upstream -> downstream)
951+
// First we clobber the target push repo's data, and then move the new data into it from the upstream repo we cloned earlier
952+
os.RemoveAll(fmt.Sprintf("%s_gitpush/SPECS", localPath))
953+
os.RemoveAll(fmt.Sprintf("%s_gitpush/SOURCES", localPath))
954+
os.RemoveAll(fmt.Sprintf("%s_gitpush/.gitignore", localPath))
955+
os.RemoveAll(fmt.Sprintf("%s_gitpush/%s.metadata", localPath, md.Name))
922956
os.Rename(fmt.Sprintf("%s/SPECS", localPath), fmt.Sprintf("%s_gitpush/SPECS", localPath))
923957
os.Rename(fmt.Sprintf("%s/SOURCES", localPath), fmt.Sprintf("%s_gitpush/SOURCES", localPath))
924958
os.Rename(fmt.Sprintf("%s/.gitignore", localPath), fmt.Sprintf("%s_gitpush/.gitignore", localPath))
@@ -1052,7 +1086,6 @@ func processRPMTagless(pd *data.ProcessData) (*srpmprocpb.ProcessResponse, error
10521086
Version: pd.PackageVersion,
10531087
Release: pd.PackageRelease,
10541088
}
1055-
10561089
}
10571090

10581091
// return struct with all our branch:commit and branch:version+release mappings
@@ -1205,7 +1238,7 @@ func getVersionFromSpec(localRepo string, majorVersion int) (string, error) {
12051238
fmt.Sprintf("%s/SPECS/%s", localRepo, specFile),
12061239
}
12071240
cmd := exec.Command("rpmspec", cmdArgs...)
1208-
nvrTmp, err := cmd.CombinedOutput()
1241+
nvrTmp, err := cmd.Output()
12091242
if err != nil {
12101243
return "", fmt.Errorf("Error running rpmspec command to determine RPM name-version-release identifier. \nCommand attempted: %s \nCommand output: %s", cmd.String(), string(nvrTmp))
12111244
}

0 commit comments

Comments
 (0)