Skip to content

Commit 730b2e9

Browse files
committed
fix(ospackage): improve RPM package name extraction for complex names
- Fix extractBasePackageNameFromFile to correctly handle packages with digits in names - Work backwards from dist/arch markers (.azl3, x86_64, etc.) to find version boundary - Prevents false positives where architecture strings matched digits in package names - Add multiple fallback strategies for different RPM naming patterns Examples: - libpcre2-8-0-10.42-3.azl3.x86_64.rpm → libpcre2-8-0 (was incorrectly libpcre2-8-0-10.42) - curl-8.8.0-2.azl3.x86_64.rpm → curl (unchanged) - curl-devel-8.8.0-1.azl3.x86_64.rpm → curl-devel (unchanged) Makes RPM package name trimming consistent with Debian/Ubuntu behavior where package names like libpcre2-8-0, libpcre2-16-0, libpcre2-32-0 are distinct packages (different SONAME versions) rather than version variants.
1 parent 7580d87 commit 730b2e9

1 file changed

Lines changed: 32 additions & 14 deletions

File tree

internal/ospackage/rpmutils/helper.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ func compareVersions(v1, v2 string) int {
210210
// extractBasePackageNameFromFile extracts the base package name from a full package filename
211211
// e.g., "curl-8.8.0-2.azl3.x86_64.rpm" -> "curl"
212212
// e.g., "curl-devel-8.8.0-1.azl3.x86_64.rpm" -> "curl-devel"
213+
// e.g., "libpcre2-8-0-10.42-3.azl3.x86_64.rpm" -> "libpcre2-8-0"
213214
func extractBasePackageNameFromFile(fullName string) string {
214215
// Remove .rpm suffix if present
215216
name := strings.TrimSuffix(fullName, ".rpm")
@@ -220,22 +221,39 @@ func extractBasePackageNameFromFile(fullName string) string {
220221
return name
221222
}
222223

223-
// Find the first part that looks like a version (starts with digit)
224-
for i := 1; i < len(parts); i++ {
225-
if len(parts[i]) > 0 && (parts[i][0] >= '0' && parts[i][0] <= '9') {
226-
// get the name
227-
maybe_name := strings.Join(parts[:i], "-")
228-
// check if version is part of the name
229-
// full name contains version, if package name has version,
230-
// it will be repeated in the full name
231-
for j := i + 1; j < len(parts); j++ {
232-
if len(parts[j]) > 0 && strings.Contains(parts[j], parts[i]) {
233-
maybe_name = strings.Join(parts[:j], "-")
234-
break
224+
// First, try to find dist/arch markers (.azl3, .el9, .fc39, etc.) and work backwards
225+
// This handles the <name>-<version>-<release>.<dist>.<arch> format
226+
for i := len(parts) - 1; i >= 0; i-- {
227+
part := parts[i]
228+
// Check if this part contains a dist marker (azl3, el9, fc39, etc.) or arch (x86_64, aarch64, noarch)
229+
if strings.Contains(part, ".azl3") || strings.Contains(part, ".el") ||
230+
strings.Contains(part, ".fc") || strings.Contains(part, "x86_64") ||
231+
strings.Contains(part, "aarch64") || strings.Contains(part, "noarch") ||
232+
strings.Contains(part, "i686") {
233+
// This is the release part. Name ends before the previous numeric part (version)
234+
// Work backwards to find where version starts
235+
for j := i - 1; j >= 1; j-- {
236+
if len(parts[j]) > 0 && (parts[j][0] >= '0' && parts[j][0] <= '9') {
237+
// This is likely the start of the version
238+
return strings.Join(parts[:j], "-")
235239
}
236240
}
237-
// return name or name-version
238-
return maybe_name
241+
}
242+
}
243+
244+
// Fallback: Find the first part that looks like a version (starts with digit and contains a dot)
245+
// This handles simple cases where there's no clear dist marker
246+
for i := 1; i < len(parts); i++ {
247+
if len(parts[i]) > 0 && (parts[i][0] >= '0' && parts[i][0] <= '9') && strings.Contains(parts[i], ".") {
248+
// This looks like a version (e.g., "8.8.0")
249+
return strings.Join(parts[:i], "-")
250+
}
251+
}
252+
253+
// Second fallback: Find first numeric part
254+
for i := 1; i < len(parts); i++ {
255+
if len(parts[i]) > 0 && (parts[i][0] >= '0' && parts[i][0] <= '9') {
256+
return strings.Join(parts[:i], "-")
239257
}
240258
}
241259

0 commit comments

Comments
 (0)